Skip to content

Commit 7ca380d

Browse files
committed
Move linecache code to new file, use super()
1 parent 38f1ed6 commit 7ca380d

File tree

3 files changed

+77
-73
lines changed

3 files changed

+77
-73
lines changed

bpython/history.py

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
from __future__ import unicode_literals
2727
import io
28-
import linecache
2928
import os
3029
import stat
3130
from itertools import islice
@@ -233,74 +232,3 @@ def append_reload_and_write(self, s, filename, encoding):
233232
# Make sure that entries contains at least one element. If the
234233
# file and s are empty, this can occur.
235234
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>'

bpython/patch_linecache.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import linecache
2+
3+
class BPythonLinecache(dict):
4+
"""Replaces the cache dict in the standard-library linecache module,
5+
to also remember (in an unerasable way) bpython console input."""
6+
7+
def __init__(self, *args, **kwargs):
8+
super(BPythonLinecache, self).__init__(*args, **kwargs)
9+
self.bpython_history = []
10+
11+
def is_bpython_filename(self, fname):
12+
try:
13+
return fname.startswith('<bpython-input-')
14+
except AttributeError:
15+
# In case the key isn't a string
16+
return False
17+
18+
def get_bpython_history(self, key):
19+
"""Given a filename provided by remember_bpython_input,
20+
returns the associated source string."""
21+
try:
22+
idx = int(key.split('-')[2][:-1])
23+
return self.bpython_history[idx]
24+
except (IndexError, ValueError):
25+
raise KeyError
26+
27+
def remember_bpython_input(self, source):
28+
"""Remembers a string of source code, and returns
29+
a fake filename to use to retrieve it later."""
30+
filename = '<bpython-input-%s>' % len(self.bpython_history)
31+
self.bpython_history.append((len(source), None,
32+
source.splitlines(True), filename))
33+
return filename
34+
35+
def __getitem__(self, key):
36+
if self.is_bpython_filename(key):
37+
return self.get_bpython_history(key)
38+
return super(BPythonLinecache, self).__getitem__(key)
39+
40+
def __contains__(self, key):
41+
if self.is_bpython_filename(key):
42+
try:
43+
self.get_bpython_history(key)
44+
return True
45+
except KeyError:
46+
return False
47+
return super(BPythonLinecache, self).__contains__(key)
48+
49+
def __delitem__(self, key):
50+
if not self.is_bpython_filename(key):
51+
return super(BPythonLinecache, self).__delitem__(key)
52+
53+
def _bpython_clear_linecache():
54+
try:
55+
bpython_history = linecache.cache.bpython_history
56+
except AttributeError:
57+
bpython_history = []
58+
linecache.cache = BPythonLinecache()
59+
linecache.cache.bpython_history = bpython_history
60+
61+
# Monkey-patch the linecache module so that we're able
62+
# to hold our command history there and have it persist
63+
linecache.cache = BPythonLinecache(linecache.cache)
64+
linecache.clearcache = _bpython_clear_linecache
65+
66+
def filename_for_console_input(code_string):
67+
"""Remembers a string of source code, and returns
68+
a fake filename to use to retrieve it later."""
69+
try:
70+
return linecache.cache.remember_bpython_input(code_string)
71+
except AttributeError:
72+
# If someone else has patched linecache.cache, better for code to
73+
# simply be unavailable to inspect.getsource() than to raise
74+
# an exception.
75+
return '<input>'

bpython/repl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@
4747
from bpython.clipboard import get_clipboard, CopyFailed
4848
from bpython.config import getpreferredencoding
4949
from bpython.formatter import Parenthesis
50-
from bpython.history import History, filename_for_console_input
50+
from bpython.history import History
5151
from bpython.paste import PasteHelper, PastePinnwand, PasteFailed
52+
from bpython.patch_linecache import filename_for_console_input
5253
from bpython.translations import _, ngettext
5354

5455

0 commit comments

Comments
 (0)