[3.13] gh-87320: In the code module, handle exceptions raised in sys.excepthook (GH-122456) (GH-122514)

Before, the exception caused by calling non-default sys.excepthook
in code.InteractiveInterpreter bubbled up to the caller, ending the REPL.
(cherry picked from commit bd3d31f380)

Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
This commit is contained in:
Miss Islington (bot) 2024-08-08 09:26:52 +02:00 committed by GitHub
parent 1fd1c6c738
commit 84c8cd0f3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 76 additions and 3 deletions

View file

@ -1049,6 +1049,30 @@ class TestMain(TestCase):
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)
@force_not_colorized
def test_bad_sys_excepthook_doesnt_crash_pyrepl(self):
env = os.environ.copy()
commands = ("import sys\n"
"sys.excepthook = 1\n"
"1/0\n"
"exit()\n")
def check(output, exitcode):
self.assertIn("Error in sys.excepthook:", output)
self.assertEqual(output.count("'int' object is not callable"), 1)
self.assertIn("Original exception was:", output)
self.assertIn("division by zero", output)
self.assertEqual(exitcode, 0)
env.pop("PYTHON_BASIC_REPL", None)
output, exit_code = self.run_repl(commands, env=env)
if "can\'t use pyrepl" in output:
self.skipTest("pyrepl not available")
check(output, exit_code)
env["PYTHON_BASIC_REPL"] = "1"
output, exit_code = self.run_repl(commands, env=env)
check(output, exit_code)
def test_not_wiping_history_file(self):
# skip, if readline module is not available
import_module('readline')