|
1 | 1 | import unittest |
| 2 | +import sys |
| 3 | +from unittest.mock import Mock |
2 | 4 |
|
3 | 5 | from curtsies.fmtfuncs import bold, green, magenta, cyan, red, plain |
4 | 6 |
|
@@ -34,6 +36,57 @@ def test_syntaxerror(self): |
34 | 36 | self.assertMultiLineEqual(str(plain("").join(a)), str(expected)) |
35 | 37 | self.assertEqual(plain("").join(a), expected) |
36 | 38 |
|
| 39 | + def test_semantic_syntaxerrors_leave_interpreter_usable(self): |
| 40 | + for source in ("nonlocal a", "return", "break", "continue"): |
| 41 | + with self.subTest(source=source): |
| 42 | + i = Interpreter() |
| 43 | + callback = Mock() |
| 44 | + i.syntaxerror_callback = callback |
| 45 | + self.assertFalse(i.runsource(source)) |
| 46 | + callback.assert_called_once_with() |
| 47 | + output = str(plain("").join(i.a)) |
| 48 | + self.assertIn("SyntaxError", output) |
| 49 | + self.assertIn('"<input>"', output) |
| 50 | + self.assertNotIn("<bpython-input-", output) |
| 51 | + self.assertIsInstance(sys.last_value, SyntaxError) |
| 52 | + self.assertEqual(sys.last_value.filename, "<input>") |
| 53 | + self.assertFalse(i.runsource("answer = 6 * 7")) |
| 54 | + self.assertEqual(i.locals["answer"], 42) |
| 55 | + |
| 56 | + def test_syntaxerror_keeps_original_exception_and_details(self): |
| 57 | + i = Interpreter() |
| 58 | + error = SyntaxError("message") |
| 59 | + error.filename = "<bpython-input-123>" |
| 60 | + error.lineno = 3 |
| 61 | + error.offset = 2 |
| 62 | + error.end_lineno = 3 |
| 63 | + error.end_offset = 5 |
| 64 | + error.text = "bad source" |
| 65 | + error.add_note("additional information") |
| 66 | + try: |
| 67 | + raise error |
| 68 | + except SyntaxError: |
| 69 | + i.showsyntaxerror(error.filename) |
| 70 | + self.assertIs(sys.last_value, error) |
| 71 | + self.assertEqual(error.filename, "<input>") |
| 72 | + self.assertEqual((error.lineno, error.offset), (3, 2)) |
| 73 | + self.assertEqual((error.end_lineno, error.end_offset), (3, 5)) |
| 74 | + self.assertIn("additional information", str(plain("").join(i.a))) |
| 75 | + |
| 76 | + def test_explicit_syntaxerror_filename(self): |
| 77 | + i = Interpreter() |
| 78 | + i.runsource("1.1.1.1", filename="example.py") |
| 79 | + self.assertIn('"example.py"', str(plain("").join(i.a))) |
| 80 | + self.assertEqual(sys.last_value.filename, "example.py") |
| 81 | + |
| 82 | + def test_indentationerror_filename(self): |
| 83 | + i = Interpreter() |
| 84 | + i.runsource(" answer = 42") |
| 85 | + output = str(plain("").join(i.a)) |
| 86 | + self.assertIn("IndentationError", output) |
| 87 | + self.assertIn('"<input>"', output) |
| 88 | + self.assertIsInstance(sys.last_value, IndentationError) |
| 89 | + |
37 | 90 | def test_traceback(self): |
38 | 91 | i = Interpreter() |
39 | 92 |
|
|
0 commit comments