bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (… · python/cpython@d8cba5d (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
1 1 import unittest
2 2 from test.support import (verbose, refcount_test, run_unittest,
3 3 strip_python_stderr, cpython_only, start_threads,
4 -temp_dir, requires_type_collecting)
4 +temp_dir, requires_type_collecting, TESTFN, unlink)
5 5 from test.support.script_helper import assert_python_ok, make_script
6 6
7 7 import sys
@@ -708,6 +708,21 @@ def __del__(self):
708 708 rc, out, err = assert_python_ok('-c', code)
709 709 self.assertEqual(out.strip(), b'__del__ called')
710 710
711 +@requires_type_collecting
712 +def test_global_del_SystemExit(self):
713 +code = """if 1:
714 + class ClassWithDel:
715 + def __del__(self):
716 + print('__del__ called')
717 + a = ClassWithDel()
718 + a.link = a
719 + raise SystemExit(0)"""
720 +self.addCleanup(unlink, TESTFN)
721 +with open(TESTFN, 'w') as script:
722 +script.write(code)
723 +rc, out, err = assert_python_ok(TESTFN)
724 +self.assertEqual(out.strip(), b'__del__ called')
725 +
711 726 def test_get_stats(self):
712 727 stats = gc.get_stats()
713 728 self.assertEqual(len(stats), 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Decref the module object in :c:func:`PyRun_SimpleFileExFlags` before calling
2 +:c:func:`PyErr_Print()`. Patch by Zackery Spytz.
Original file line number Diff line number Diff line change
@@ -431,6 +431,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
431 431 }
432 432 flush_io();
433 433 if (v == NULL) {
434 +Py_CLEAR(m);
434 435 PyErr_Print();
435 436 goto done;
436 437 }
@@ -439,7 +440,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
439 440 done:
440 441 if (set_file_name && PyDict_DelItemString(d, "__file__"))
441 442 PyErr_Clear();
442 -Py_DECREF(m);
443 +Py_XDECREF(m);
443 444 return ret;
444 445 }
445 446