Skip to content

Commit 124259f

Browse files
authored
gh-111132: Fix crash on interactive_filename in run_mod (#111136)
1 parent 37e4e20 commit 124259f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,17 @@ def test_syntaxerror_null_bytes_in_multiline_string(self):
684684
]
685685
)
686686

687+
def test_syntaxerror_does_not_crash(self):
688+
script = "nonlocal x\n"
689+
with os_helper.temp_dir() as script_dir:
690+
script_name = _make_test_script(script_dir, 'script', script)
691+
exitcode, stdout, stderr = assert_python_failure(script_name)
692+
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
693+
# It used to crash in https://github.com/python/cpython/issues/111132
694+
self.assertTrue(text.endswith(
695+
'SyntaxError: nonlocal declaration not allowed at module level\n',
696+
), text)
697+
687698
def test_consistent_sys_path_for_direct_execution(self):
688699
# This test case ensures that the following all give the same
689700
# sys.path configuration:

Python/pythonrun.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,9 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
12771277

12781278
PyCodeObject *co = _PyAST_Compile(mod, interactive_filename, flags, -1, arena);
12791279
if (co == NULL) {
1280-
Py_DECREF(interactive_filename);
1280+
if (interactive_src) {
1281+
Py_DECREF(interactive_filename);
1282+
}
12811283
return NULL;
12821284
}
12831285

0 commit comments

Comments
 (0)