diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-10-20 20:31:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-20 20:31:26 (GMT) |
commit | 124259f9b30f6cbab4dd2bcfb0d32f1cddcc35db (patch) | |
tree | 39b669ac077b565790ce9e26fe39268fcb842e91 | |
parent | 37e4e20eaa8f27ada926d49e5971fecf0477ad26 (diff) | |
download | cpython-124259f9b30f6cbab4dd2bcfb0d32f1cddcc35db.zip cpython-124259f9b30f6cbab4dd2bcfb0d32f1cddcc35db.tar.gz cpython-124259f9b30f6cbab4dd2bcfb0d32f1cddcc35db.tar.bz2 |
gh-111132: Fix crash on interactive_filename in `run_mod` (#111136)
-rw-r--r-- | Lib/test/test_cmd_line_script.py | 11 | ||||
-rw-r--r-- | Python/pythonrun.c | 4 |
2 files changed, 14 insertions, 1 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index 614c6b3..e9405ff 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -684,6 +684,17 @@ class CmdLineTest(unittest.TestCase): ] ) + def test_syntaxerror_does_not_crash(self): + script = "nonlocal x\n" + with os_helper.temp_dir() as script_dir: + script_name = _make_test_script(script_dir, 'script', script) + exitcode, stdout, stderr = assert_python_failure(script_name) + text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() + # It used to crash in https://github.com/python/cpython/issues/111132 + self.assertTrue(text.endswith( + 'SyntaxError: nonlocal declaration not allowed at module level\n', + ), text) + def test_consistent_sys_path_for_direct_execution(self): # This test case ensures that the following all give the same # sys.path configuration: diff --git a/Python/pythonrun.c b/Python/pythonrun.c index b915c06..db49916 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1277,7 +1277,9 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals, PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1, arena); if (co == NULL) { - Py_DECREF(interactive_filename); + if (interactive_src) { + Py_DECREF(interactive_filename); + } return NULL; } |