|
25 | 25 |
|
26 | 26 | from __future__ import unicode_literals
|
27 | 27 | import io
|
| 28 | +import linecache |
28 | 29 | import os
|
29 | 30 | import stat
|
30 | 31 | from itertools import islice
|
@@ -232,3 +233,74 @@ def append_reload_and_write(self, s, filename, encoding):
|
232 | 233 | # Make sure that entries contains at least one element. If the
|
233 | 234 | # file and s are empty, this can occur.
|
234 | 235 | self.entries = ['']
|
| 236 | + |
| 237 | +class BPythonLinecache(dict): |
| 238 | + """Replaces the cache dict in the standard-library linecache module, |
| 239 | + to also remember (in an unerasable way) bpython console input.""" |
| 240 | + |
| 241 | + def __init__(self, *args, **kwargs): |
| 242 | + dict.__init__(self, *args, **kwargs) |
| 243 | + self.bpython_history = [] |
| 244 | + |
| 245 | + def is_bpython_filename(self, fname): |
| 246 | + try: |
| 247 | + return fname.startswith('<bpython-input-') |
| 248 | + except AttributeError: |
| 249 | + # In case the key isn't a string |
| 250 | + return False |
| 251 | + |
| 252 | + def get_bpython_history(self, key): |
| 253 | + """Given a filename provided by remember_bpython_input, |
| 254 | + returns the associated source string.""" |
| 255 | + try: |
| 256 | + idx = int(key.split('-')[2][:-1]) |
| 257 | + return self.bpython_history[idx] |
| 258 | + except (IndexError, ValueError): |
| 259 | + raise KeyError |
| 260 | + |
| 261 | + def remember_bpython_input(self, source): |
| 262 | + """Remembers a string of source code, and returns |
| 263 | + a fake filename to use to retrieve it later.""" |
| 264 | + filename = '<bpython-input-%s>' % len(self.bpython_history) |
| 265 | + self.bpython_history.append((len(source), None, |
| 266 | + source.splitlines(True), filename)) |
| 267 | + return filename |
| 268 | + |
| 269 | + def __getitem__(self, key): |
| 270 | + if self.is_bpython_filename(key): |
| 271 | + return self.get_bpython_history(key) |
| 272 | + return dict.__getitem__(self, key) |
| 273 | + |
| 274 | + def __contains__(self, key): |
| 275 | + if self.is_bpython_filename(key): |
| 276 | + try: |
| 277 | + self.get_bpython_history(key) |
| 278 | + return True |
| 279 | + except KeyError: |
| 280 | + return False |
| 281 | + return dict.__contains__(self, key) |
| 282 | + |
| 283 | + def __delitem__(self, key): |
| 284 | + if not self.is_bpython_filename(key): |
| 285 | + return dict.__delitem__(self, key) |
| 286 | + |
| 287 | +def _bpython_clear_linecache(): |
| 288 | + try: |
| 289 | + bpython_history = linecache.cache.bpython_history |
| 290 | + except AttributeError: |
| 291 | + bpython_history = [] |
| 292 | + linecache.cache = BPythonLinecache() |
| 293 | + linecache.cache.bpython_history = bpython_history |
| 294 | + |
| 295 | +# Monkey-patch the linecache module so that we're able |
| 296 | +# to hold our command history there and have it persist |
| 297 | +linecache.cache = BPythonLinecache(linecache.cache) |
| 298 | +linecache.clearcache = _bpython_clear_linecache |
| 299 | + |
| 300 | +def filename_for_console_input(code_string): |
| 301 | + """Remembers a string of source code, and returns |
| 302 | + a fake filename to use to retrieve it later.""" |
| 303 | + try: |
| 304 | + return linecache.cache.remember_bpython_input(code_string) |
| 305 | + except AttributeError: |
| 306 | + return '<input>' |
0 commit comments