Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions bpython/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,11 @@ def showsyntaxerror(self, filename: str | None = None, **kwargs) -> None:
exc_type, value, sys.last_traceback = sys.exc_info()
sys.last_type = exc_type
sys.last_value = value
if filename and exc_type is SyntaxError and value is not None:
msg = value.args[0]
args = list(value.args[1])
# strip linechache line number
if filename and isinstance(value, SyntaxError):
# Hide the linecache identifier without rebuilding the exception:
# compiler errors can have location attributes but only one arg.
if self.bpython_input_re.match(filename):
args[0] = "<input>"
value = SyntaxError(msg, tuple(args))
sys.last_value = value
value.filename = "<input>"
exc_formatted = traceback.format_exception_only(exc_type, value)
self.writetb(exc_formatted)

Expand Down
53 changes: 53 additions & 0 deletions bpython/test/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import unittest
import sys
from unittest.mock import Mock

from curtsies.fmtfuncs import bold, green, magenta, cyan, red, plain

Expand Down Expand Up @@ -34,6 +36,57 @@ def test_syntaxerror(self):
self.assertMultiLineEqual(str(plain("").join(a)), str(expected))
self.assertEqual(plain("").join(a), expected)

def test_semantic_syntaxerrors_leave_interpreter_usable(self):
for source in ("nonlocal a", "return", "break", "continue"):
with self.subTest(source=source):
i = Interpreter()
callback = Mock()
i.syntaxerror_callback = callback
self.assertFalse(i.runsource(source))
callback.assert_called_once_with()
output = str(plain("").join(i.a))
self.assertIn("SyntaxError", output)
self.assertIn('"<input>"', output)
self.assertNotIn("<bpython-input-", output)
self.assertIsInstance(sys.last_value, SyntaxError)
self.assertEqual(sys.last_value.filename, "<input>")
self.assertFalse(i.runsource("answer = 6 * 7"))
self.assertEqual(i.locals["answer"], 42)

def test_syntaxerror_keeps_original_exception_and_details(self):
i = Interpreter()
error = SyntaxError("message")
error.filename = "<bpython-input-123>"
error.lineno = 3
error.offset = 2
error.end_lineno = 3
error.end_offset = 5
error.text = "bad source"
error.add_note("additional information")
try:
raise error
except SyntaxError:
i.showsyntaxerror(error.filename)
self.assertIs(sys.last_value, error)
self.assertEqual(error.filename, "<input>")
self.assertEqual((error.lineno, error.offset), (3, 2))
self.assertEqual((error.end_lineno, error.end_offset), (3, 5))
self.assertIn("additional information", str(plain("").join(i.a)))

def test_explicit_syntaxerror_filename(self):
i = Interpreter()
i.runsource("1.1.1.1", filename="example.py")
self.assertIn('"example.py"', str(plain("").join(i.a)))
self.assertEqual(sys.last_value.filename, "example.py")

def test_indentationerror_filename(self):
i = Interpreter()
i.runsource(" answer = 42")
output = str(plain("").join(i.a))
self.assertIn("IndentationError", output)
self.assertIn('"<input>"', output)
self.assertIsInstance(sys.last_value, IndentationError)

def test_traceback(self):
i = Interpreter()

Expand Down
Loading