Skip to content

Commit 7f7aee1

Browse files
Merge branch 'fix-broken-history'
2 parents 2c5ada7 + 4cb4e77 commit 7f7aee1

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

bpython/repl.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import os
3030
import pkgutil
3131
import pydoc
32+
import re
3233
import shlex
3334
import subprocess
3435
import sys
@@ -133,6 +134,9 @@ def showsyntaxerror(self, filename=None):
133134
# Stuff in the right filename and right lineno
134135
if not py3:
135136
lineno -= 2
137+
# strip linecache line number
138+
if re.match(r'<bpython-input-\d+>', filename):
139+
filename = '<input>'
136140
value = SyntaxError(msg, (filename, lineno, offset, line))
137141
sys.last_value = value
138142
list = traceback.format_exception_only(type, value)
@@ -149,9 +153,14 @@ def showtraceback(self):
149153
sys.last_traceback = tb
150154
tblist = traceback.extract_tb(tb)
151155
del tblist[:1]
152-
# Set the right lineno (encoding header adds an extra line)
153-
if not py3:
154-
for i, (fname, lineno, module, something) in enumerate(tblist):
156+
157+
for i, (fname, lineno, module, something) in enumerate(tblist):
158+
# strip linecache line number
159+
if re.match(r'<bpython-input-\d+>', fname):
160+
fname = '<input>'
161+
tblist[i] = (fname, lineno, module, something)
162+
# Set the right lineno (encoding header adds an extra line)
163+
if not py3:
155164
if fname == '<input>':
156165
tblist[i] = (fname, lineno - 2, module, something)
157166

bpython/test/test_interpreter.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import unicode_literals
44

5-
import linecache
65
import sys
76

87
from curtsies.fmtfuncs import bold, green, magenta, cyan, red, plain
@@ -14,12 +13,6 @@
1413
pypy = 'PyPy' in sys.version
1514

1615

17-
def _last_console_filename():
18-
"""Returns the last 'filename' used for console input
19-
(as will be displayed in a traceback)."""
20-
return '<bpython-input-%s>' % (len(linecache.cache.bpython_history) - 1)
21-
22-
2316
class TestInterpreter(unittest.TestCase):
2417
def test_syntaxerror(self):
2518
i = interpreter.Interp()
@@ -33,13 +26,13 @@ def append_to_a(message):
3326

3427
if pypy:
3528
expected = (
36-
' File ' + green('"%s"' % _last_console_filename()) +
29+
' File ' + green('"<input>"') +
3730
', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' +
3831
bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
3932
'\n')
4033
else:
4134
expected = (
42-
' File ' + green('"%s"' % _last_console_filename()) +
35+
' File ' + green('"<input>"') +
4336
', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' +
4437
bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') +
4538
'\n')
@@ -62,7 +55,7 @@ def f():
6255
def g():
6356
return f()
6457

65-
i.runsource('g()', encode=False)
58+
i.runsource('g()')
6659

6760
if pypy:
6861
global_not_found = "global name 'g' is not defined"
@@ -71,7 +64,7 @@ def g():
7164

7265
expected = (
7366
'Traceback (most recent call last):\n File ' +
74-
green('"%s"' % _last_console_filename()) + ', line ' +
67+
green('"<input>"') + ', line ' +
7568
bold(magenta('1')) + ', in ' + cyan('<module>') + '\n g()\n' +
7669
bold(red('NameError')) + ': ' + cyan(global_not_found) + '\n')
7770

0 commit comments

Comments
 (0)