Skip to content

Commit f615af2

Browse files
committed
Add tests to make sure hook runs before exception
1 parent e1186d5 commit f615af2

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎Lib/test/test_pyrepl/test_interact.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ def f():
303303

304304

305305
class TestStatementSubmittedHook(unittest.TestCase):
306-
307306
def _run_interactive(self, statements, hook):
308307
console = InteractiveColoredConsole()
309308
statement_iter = iter(statements)
@@ -352,8 +351,27 @@ def test_hook_exception_is_displayed(self):
352351
self.assertIn("RuntimeError: hook error", output)
353352
self.assertEqual(namespace["x"], 1)
354353

354+
def test_hook_return_value_is_ignored(self):
355+
hook = MagicMock(return_value=True)
356+
357+
_, namespace = self._run_interactive(["x = 1"], hook)
358+
359+
self.assertEqual(namespace["x"], 1)
360+
355361
def test_hook_not_called_for_repl_commands(self):
356362
hook = MagicMock()
357363
self._run_interactive(["clear"], hook)
358364

359365
hook.assert_not_called()
366+
367+
@force_not_colorized
368+
def test_hook_called_before_execution_exception(self):
369+
statement = "1 / 0"
370+
marker = "hook ran first"
371+
hook = MagicMock(side_effect=lambda statement: print(marker))
372+
output, _ = self._run_interactive([statement], hook)
373+
374+
hook.assert_called_once_with(statement)
375+
marker_index = output.index(marker)
376+
traceback_index = output.index("ZeroDivisionError")
377+
self.assertLess(marker_index, traceback_index)

0 commit comments

Comments
 (0)