Skip to content

Commit 937626d

Browse files
committed
Improve handling of SyntaxErrors
<bpython-line-%> is now detected and replaced with <input> in more cases.
1 parent 59a8095 commit 937626d

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

bpython/repl.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,13 @@ def showsyntaxerror(self, filename: Optional[str] = None) -> None:
166166
exc_type, value, sys.last_traceback = sys.exc_info()
167167
sys.last_type = exc_type
168168
sys.last_value = value
169-
if (
170-
filename
171-
and exc_type is SyntaxError
172-
and value is not None
173-
and len(value.args) >= 4
174-
):
175-
msg = str(value)
176-
lineno = value.args[1]
177-
offset = value.args[2]
178-
line = value.args[3]
169+
if filename and exc_type is SyntaxError and value is not None:
170+
msg = value.args[0]
171+
args = list(value.args[1])
179172
# strip linechache line number
180173
if self.bpython_input_re.match(filename):
181-
filename = "<input>"
182-
value = SyntaxError(msg, (filename, lineno, offset, line))
174+
args[0] = "<input>"
175+
value = SyntaxError(msg, tuple(args))
183176
sys.last_value = value
184177
exc_formatted = traceback.format_exception_only(exc_type, value)
185178
self.writetb(exc_formatted)

bpython/test/test_interpreter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_syntaxerror(self):
3838
if (3, 10, 1) <= sys.version_info[:3]:
3939
expected = (
4040
" File "
41-
+ green('"<bpython-input-147>"')
41+
+ green('"<input>"')
4242
+ ", line "
4343
+ bold(magenta("1"))
4444
+ "\n 1.1.1.1\n ^^\n"
@@ -50,7 +50,7 @@ def test_syntaxerror(self):
5050
elif (3, 10) <= sys.version_info[:2]:
5151
expected = (
5252
" File "
53-
+ green('"<bpython-input-147>"')
53+
+ green('"<input>"')
5454
+ ", line "
5555
+ bold(magenta("1"))
5656
+ "\n 1.1.1.1\n ^^^^^\n"

0 commit comments

Comments
 (0)