]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (GH-7918) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 3 Jul 2018 20:17:46 +0000 (13:17 -0700)
committerAntoine Pitrou <pitrou@free.fr>
Tue, 3 Jul 2018 20:17:46 +0000 (22:17 +0200)
PyErr_Print() will not return when the exception is a SystemExit, so
decref the __main__ module object in that case.
(cherry picked from commit d8cba5d16f1333fd625726fc72e66afbd45b8d00)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/test/test_gc.py
Misc/NEWS.d/next/Core and Builtins/2018-06-25-16-54-05.bpo-24596.Rkwova.rst [new file with mode: 0644]
Python/pythonrun.c

index e7274998d52656dff92be34de687b3e92cf6666d..7e82b242dab5da59af6073f579bc9244b97281e8 100644 (file)
@@ -1,7 +1,7 @@
 import unittest
 from test.support import (verbose, refcount_test, run_unittest,
                           strip_python_stderr, cpython_only, start_threads,
-                          temp_dir, requires_type_collecting)
+                          temp_dir, requires_type_collecting, TESTFN, unlink)
 from test.support.script_helper import assert_python_ok, make_script
 
 import sys
@@ -713,6 +713,21 @@ class GCTests(unittest.TestCase):
             rc, out, err = assert_python_ok('-c', code)
             self.assertEqual(out.strip(), b'__del__ called')
 
+    @requires_type_collecting
+    def test_global_del_SystemExit(self):
+        code = """if 1:
+            class ClassWithDel:
+                def __del__(self):
+                    print('__del__ called')
+            a = ClassWithDel()
+            a.link = a
+            raise SystemExit(0)"""
+        self.addCleanup(unlink, TESTFN)
+        with open(TESTFN, 'w') as script:
+            script.write(code)
+        rc, out, err = assert_python_ok(TESTFN)
+        self.assertEqual(out.strip(), b'__del__ called')
+
     def test_get_stats(self):
         stats = gc.get_stats()
         self.assertEqual(len(stats), 3)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-25-16-54-05.bpo-24596.Rkwova.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-25-16-54-05.bpo-24596.Rkwova.rst
new file mode 100644 (file)
index 0000000..1b33fd4
--- /dev/null
@@ -0,0 +1,2 @@
+Decref the module object in :c:func:`PyRun_SimpleFileExFlags` before calling
+:c:func:`PyErr_Print()`.  Patch by Zackery Spytz.
index b23cfdafd60e6d0782dc068246f3fc443675f6dc..7db7052e35e77889d4f37199a067fa3c598c5cbb 100644 (file)
@@ -421,6 +421,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
     }
     flush_io();
     if (v == NULL) {
+        Py_CLEAR(m);
         PyErr_Print();
         goto done;
     }
@@ -429,7 +430,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
   done:
     if (set_file_name && PyDict_DelItemString(d, "__file__"))
         PyErr_Clear();
-    Py_DECREF(m);
+    Py_XDECREF(m);
     return ret;
 }