Skip to content
Prev Previous commit
add and move test case
  • Loading branch information
neonene committed Jul 8, 2025
commit d7c36b960c675e5772f8cc00f167c71de4fa4f2d
23 changes: 0 additions & 23 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,29 +1517,6 @@ def test_ast_fini(self):
""")
assert_python_ok("-c", code)

def test_weakref_to_module_at_shutdown(self):
# gh-132413: Weakref gets cleared before modules are finalized
code = textwrap.dedent("""
import os # any module other than sys

def gen():
import weakref
wr = weakref.ref(os)
try:
yield
finally:
print(
os is not None,
os is wr()
)

it = gen()
next(it)
# quirk: Shutdown starts, then the finally block above follows
""")
res = assert_python_ok('-c', code)
self.assertEqual(res.out.rstrip(), b'True True')


def setUpModule():
global enabled, debug
Expand Down
44 changes: 44 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,50 @@ def callback(obj):
stderr = res.err.decode("ascii", "backslashreplace")
self.assertNotRegex(stderr, "_Py_Dealloc: Deallocator of type 'TestObj'")

def test_module_at_shutdown(self):
# gh-132413: Weakref gets cleared by gc before modules are finalized
code = textwrap.dedent("""
import os # any module other than sys
import weakref
wr = weakref.ref(os)

def gen():
try:
yield
finally:
print(
os is not None,
os is wr()
)

it = gen()
next(it)
print('fini', end=' ')
""")
with self.subTest("gen"):
res = script_helper.assert_python_ok('-c', code)
self.assertEqual(res.out.rstrip(), b'fini True True')

code = textwrap.dedent("""
import os
import weakref
wr = weakref.ref(os)

class C:
def __del__(self):
print(
os is not None,
os is wr()
)

l = [C()]
l.append(l)
print('fini', end=' ')
""")
with self.subTest("del"):
res = script_helper.assert_python_ok('-c', code)
self.assertEqual(res.out.rstrip(), b'fini True True')


class SubclassableWeakrefTestCase(TestBase):

Expand Down