diff options
author | CF Bolz-Tereick <cfbolz@gmx.de> | 2024-07-31 12:33:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-31 10:33:29 +0000 |
commit | bd3d31f380cd451a4ab6da5fbfde463fed95b5b5 (patch) | |
tree | eef67c37d5ff5ef20ac7b8a972bd9fe73d910b00 /Lib/test/test_code_module.py | |
parent | e60ee11cb51b87deeb22ad125717bd0d0dc10fa8 (diff) | |
download | cpython-bd3d31f380cd451a4ab6da5fbfde463fed95b5b5.tar.gz cpython-bd3d31f380cd451a4ab6da5fbfde463fed95b5b5.zip |
gh-87320: In the code module, handle exceptions raised in sys.excepthook (GH-122456)
Before, the exception caused by calling non-default sys.excepthook
in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
Diffstat (limited to 'Lib/test/test_code_module.py')
-rw-r--r-- | Lib/test/test_code_module.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py index 259778a5cad..5dc89108f0a 100644 --- a/Lib/test/test_code_module.py +++ b/Lib/test/test_code_module.py @@ -77,6 +77,39 @@ class TestInteractiveConsole(unittest.TestCase, MockSys): self.console.interact() self.assertTrue(hook.called) + def test_sysexcepthook_crashing_doesnt_close_repl(self): + self.infunc.side_effect = ["1/0", "a = 123", "print(a)", EOFError('Finished')] + self.sysmod.excepthook = 1 + self.console.interact() + self.assertEqual(['write', ('123', ), {}], self.stdout.method_calls[0]) + error = "".join(call.args[0] for call in self.stderr.method_calls if call[0] == 'write') + self.assertIn("Error in sys.excepthook:", error) + self.assertEqual(error.count("'int' object is not callable"), 1) + self.assertIn("Original exception was:", error) + self.assertIn("division by zero", error) + + def test_sysexcepthook_raising_BaseException(self): + self.infunc.side_effect = ["1/0", "a = 123", "print(a)", EOFError('Finished')] + s = "not so fast" + def raise_base(*args, **kwargs): + raise BaseException(s) + self.sysmod.excepthook = raise_base + self.console.interact() + self.assertEqual(['write', ('123', ), {}], self.stdout.method_calls[0]) + error = "".join(call.args[0] for call in self.stderr.method_calls if call[0] == 'write') + self.assertIn("Error in sys.excepthook:", error) + self.assertEqual(error.count("not so fast"), 1) + self.assertIn("Original exception was:", error) + self.assertIn("division by zero", error) + + def test_sysexcepthook_raising_SystemExit_gets_through(self): + self.infunc.side_effect = ["1/0"] + def raise_base(*args, **kwargs): + raise SystemExit + self.sysmod.excepthook = raise_base + with self.assertRaises(SystemExit): + self.console.interact() + def test_banner(self): # with banner self.infunc.side_effect = EOFError('Finished') |