]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111132: Fix crash on interactive_filename in `run_mod` (#111136)
authorNikita Sobolev <mail@sobolevn.me>
Fri, 20 Oct 2023 20:31:26 +0000 (23:31 +0300)
committerGitHub <noreply@github.com>
Fri, 20 Oct 2023 20:31:26 +0000 (21:31 +0100)
Lib/test/test_cmd_line_script.py
Python/pythonrun.c

index 614c6b3c3b52994cebd6015157d71cbd8c1fde4a..e9405ff8b214145169795001dd7080bccb507de9 100644 (file)
@@ -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:
index b915c063d0b456e862dba84a9ff4e9926cd4fcb7..db4991662b8bb1c4b0bea24de052317720cc4faa 100644 (file)
@@ -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;
     }