mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #1545463: Global variables caught in reference cycles are now garbage-collected at shutdown.
This commit is contained in:
parent
1df37c657d
commit
5f454a07a0
6 changed files with 56 additions and 5 deletions
|
@ -1,6 +1,8 @@
|
|||
import unittest
|
||||
from test.support import (verbose, refcount_test, run_unittest,
|
||||
strip_python_stderr)
|
||||
from test.script_helper import assert_python_ok, make_script, temp_dir
|
||||
|
||||
import sys
|
||||
import time
|
||||
import gc
|
||||
|
@ -610,6 +612,40 @@ class GCTests(unittest.TestCase):
|
|||
stderr = run_command(code % "gc.DEBUG_SAVEALL")
|
||||
self.assertNotIn(b"uncollectable objects at shutdown", stderr)
|
||||
|
||||
def test_gc_main_module_at_shutdown(self):
|
||||
# Create a reference cycle through the __main__ module and check
|
||||
# it gets collected at interpreter shutdown.
|
||||
code = """if 1:
|
||||
import weakref
|
||||
class C:
|
||||
def __del__(self):
|
||||
print('__del__ called')
|
||||
l = [C()]
|
||||
l.append(l)
|
||||
"""
|
||||
rc, out, err = assert_python_ok('-c', code)
|
||||
self.assertEqual(out.strip(), b'__del__ called')
|
||||
|
||||
def test_gc_ordinary_module_at_shutdown(self):
|
||||
# Same as above, but with a non-__main__ module.
|
||||
with temp_dir() as script_dir:
|
||||
module = """if 1:
|
||||
import weakref
|
||||
class C:
|
||||
def __del__(self):
|
||||
print('__del__ called')
|
||||
l = [C()]
|
||||
l.append(l)
|
||||
"""
|
||||
code = """if 1:
|
||||
import sys
|
||||
sys.path.insert(0, %r)
|
||||
import gctest
|
||||
""" % (script_dir,)
|
||||
make_script(script_dir, 'gctest', module)
|
||||
rc, out, err = assert_python_ok('-c', code)
|
||||
self.assertEqual(out.strip(), b'__del__ called')
|
||||
|
||||
def test_get_stats(self):
|
||||
stats = gc.get_stats()
|
||||
self.assertEqual(len(stats), 3)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue