Commit d8cba5d1 authored by Zackery Spytz's avatar Zackery Spytz Committed by Antoine Pitrou

bpo-24596: Decref module in PyRun_SimpleFileExFlags() on SystemExit (GH-7918)

PyErr_Print() will not return when the exception is a SystemExit, so
decref the __main__ module object in that case.
parent 831c2972
import unittest import unittest
from test.support import (verbose, refcount_test, run_unittest, from test.support import (verbose, refcount_test, run_unittest,
strip_python_stderr, cpython_only, start_threads, 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 from test.support.script_helper import assert_python_ok, make_script
import sys import sys
...@@ -708,6 +708,21 @@ class GCTests(unittest.TestCase): ...@@ -708,6 +708,21 @@ class GCTests(unittest.TestCase):
rc, out, err = assert_python_ok('-c', code) rc, out, err = assert_python_ok('-c', code)
self.assertEqual(out.strip(), b'__del__ called') 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): def test_get_stats(self):
stats = gc.get_stats() stats = gc.get_stats()
self.assertEqual(len(stats), 3) self.assertEqual(len(stats), 3)
......
Decref the module object in :c:func:`PyRun_SimpleFileExFlags` before calling
:c:func:`PyErr_Print()`. Patch by Zackery Spytz.
...@@ -431,6 +431,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ...@@ -431,6 +431,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
} }
flush_io(); flush_io();
if (v == NULL) { if (v == NULL) {
Py_CLEAR(m);
PyErr_Print(); PyErr_Print();
goto done; goto done;
} }
...@@ -439,7 +440,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ...@@ -439,7 +440,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
done: done:
if (set_file_name && PyDict_DelItemString(d, "__file__")) if (set_file_name && PyDict_DelItemString(d, "__file__"))
PyErr_Clear(); PyErr_Clear();
Py_DECREF(m); Py_XDECREF(m);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment