Skip to content

Commit 3cbe24d

Browse files
delete repeated code in interpreters
1 parent b6a2a5f commit 3cbe24d

File tree

3 files changed

+21
-60
lines changed

3 files changed

+21
-60
lines changed

bpython/curtsiesfrontend/interpreter.py

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from bpython.curtsiesfrontend.parse import parse
1414
from bpython.repl import Interpreter as ReplInterpreter
15+
from bpython.config import getpreferredencoding
16+
from bpython._py3compat import py3
1517

1618
default_colors = {
1719
Generic.Error: 'R',
@@ -75,66 +77,30 @@ def __init__(self, locals=None):
7577
"""
7678
if locals is None:
7779
locals = {"__name__": "__console__", "__doc__": None}
78-
ReplInterpreter.__init__(self, locals)
80+
ReplInterpreter.__init__(self, locals, getpreferredencoding())
81+
7982
self.locals = locals
8083
self.compile = CommandCompiler()
8184

8285
# typically changed after being instantiated
8386
self.write = lambda stuff: sys.stderr.write(stuff)
8487
self.outfile = self
8588

86-
def showsyntaxerror(self, filename=None):
87-
"""Display the syntax error that just occurred.
88-
89-
This doesn't display a stack trace because there isn't one.
90-
91-
If a filename is given, it is stuffed in the exception instead
92-
of what was there before (because Python's parser always uses
93-
"<string>" when reading from a string).
94-
95-
The output is written by self.write(), below.
96-
97-
"""
98-
type, value, sys.last_traceback = sys.exc_info()
99-
sys.last_type = type
100-
sys.last_value = value
101-
if filename and type is SyntaxError:
102-
# Work hard to stuff the correct filename in the exception
103-
try:
104-
msg, (dummy_filename, lineno, offset, line) = value
105-
except:
106-
# Not the format we expect; leave it alone
107-
pass
108-
else:
109-
# Stuff in the right filename
110-
value = SyntaxError(msg, (filename, lineno, offset, line))
111-
sys.last_value = value
112-
l = traceback.format_exception_only(type, value)
113-
tbtext = ''.join(l)
89+
def writetb(self, lines):
90+
tbtext = ''.join(lines)
11491
lexer = get_lexer_by_name("pytb")
11592
self.format(tbtext, lexer)
116-
117-
def showtraceback(self):
118-
"""Display the exception that just occurred.
119-
120-
We remove the first stack item because it is our own code.
121-
122-
123-
"""
124-
type, value, tb = sys.exc_info()
125-
sys.last_type = type
126-
sys.last_value = value
127-
sys.last_traceback = tb
128-
tblist = traceback.extract_tb(tb)
129-
del tblist[:1]
130-
l = traceback.format_list(tblist)
131-
if l:
132-
l.insert(0, "Traceback (most recent call last):\n")
133-
l[len(l):] = traceback.format_exception_only(type, value)
134-
tbtext = ''.join(l)
135-
lexer = get_lexer_by_name("pytb", stripall=True)
136-
137-
self.format(tbtext, lexer)
93+
# TODO for tracebacks get_lexer_by_name("pytb", stripall=True)
94+
95+
if not py3:
96+
def runsource(self, source, filename="<input>", symbol="single",
97+
encode=True):
98+
# TODO: write a test for and implement encoding
99+
with self.timer:
100+
if encode:
101+
source = '#\n%s' % (source,) # dummy line so linenos match
102+
return code.InteractiveInterpreter.runsource(self, source,
103+
filename, symbol)
138104

139105
def format(self, tbtext, lexer):
140106
traceback_informative_formatter = BPythonFormatter(default_colors)
@@ -160,12 +126,6 @@ def format(self, tbtext, lexer):
160126
cur_line.append((token, text))
161127
assert cur_line == [], cur_line
162128

163-
def runsource(self, source, filename="<input>", symbol="single",
164-
encode=None):
165-
# TODO: encode does nothing
166-
with self.timer:
167-
return code.InteractiveInterpreter.runsource(
168-
self, source, filename, symbol)
169129

170130
def code_finished_will_parse(s, compiler):
171131
"""Returns a tuple of whether the buffer could be complete and whether it

bpython/curtsiesfrontend/repl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def __init__(self,
266266

267267
if interp is None:
268268
interp = Interp(locals=locals_)
269-
interp.writetb = self.send_to_stderr
269+
interp.write = self.send_to_stderr
270270
if banner is None:
271271
if config.help_key:
272272
banner = ' '.join((_('Welcome to bpython!'),
@@ -1347,7 +1347,7 @@ def reevaluate(self, insert_into_history=False):
13471347

13481348
if not self.weak_rewind:
13491349
self.interp = self.interp.__class__()
1350-
self.interp.writetb = self.send_to_stderr
1350+
self.interp.write = self.send_to_stderr
13511351
self.coderunner.interp = self.interp
13521352

13531353
self.buffer = []

bpython/test/test_interpreter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def test_runsource_bytes_over_128_syntax_error(self):
7777
i.runsource(u"a = b'\xfe'")
7878
i.showsyntaxerror.assert_called_with()
7979

80-
@unittest.skipIf(py3, "only ASCII allowed in bytestrings in Python 3")
80+
@unittest.skip("Is the goal of encoding to get this to work?")
81+
@unittest.skipIf(py3, "bytes 128-255 only permitted in Py 2")
8182
def test_runsource_bytes_over_128_syntax_error(self):
8283
i = interpreter.Interp()
8384
i.encoding = 'latin-1'

0 commit comments

Comments
 (0)