File tree Expand file tree Collapse file tree 2 files changed +44
-23
lines changed
Expand file tree Collapse file tree 2 files changed +44
-23
lines changed Original file line number Diff line number Diff line change @@ -1517,29 +1517,6 @@ def test_ast_fini(self):
15171517 """ )
15181518 assert_python_ok ("-c" , code )
15191519
1520- def test_weakref_to_module_at_shutdown (self ):
1521- # gh-132413: Weakref gets cleared before modules are finalized
1522- code = textwrap .dedent ("""
1523- import os # any module other than sys
1524-
1525- def gen():
1526- import weakref
1527- wr = weakref.ref(os)
1528- try:
1529- yield
1530- finally:
1531- print(
1532- os is not None,
1533- os is wr()
1534- )
1535-
1536- it = gen()
1537- next(it)
1538- # quirk: Shutdown starts, then the finally block above follows
1539- """ )
1540- res = assert_python_ok ('-c' , code )
1541- self .assertEqual (res .out .rstrip (), b'True True' )
1542-
15431520
15441521def setUpModule ():
15451522 global enabled , debug
Original file line number Diff line number Diff line change @@ -1044,6 +1044,50 @@ def callback(obj):
10441044 stderr = res .err .decode ("ascii" , "backslashreplace" )
10451045 self .assertNotRegex (stderr , "_Py_Dealloc: Deallocator of type 'TestObj'" )
10461046
1047+ def test_module_at_shutdown (self ):
1048+ # gh-132413: Weakref gets cleared by gc before modules are finalized
1049+ code = textwrap .dedent ("""
1050+ import os # any module other than sys
1051+ import weakref
1052+ wr = weakref.ref(os)
1053+
1054+ def gen():
1055+ try:
1056+ yield
1057+ finally:
1058+ print(
1059+ os is not None,
1060+ os is wr()
1061+ )
1062+
1063+ it = gen()
1064+ next(it)
1065+ print('fini', end=' ')
1066+ """ )
1067+ with self .subTest ("gen" ):
1068+ res = script_helper .assert_python_ok ('-c' , code )
1069+ self .assertEqual (res .out .rstrip (), b'fini True True' )
1070+
1071+ code = textwrap .dedent ("""
1072+ import os
1073+ import weakref
1074+ wr = weakref.ref(os)
1075+
1076+ class C:
1077+ def __del__(self):
1078+ print(
1079+ os is not None,
1080+ os is wr()
1081+ )
1082+
1083+ l = [C()]
1084+ l.append(l)
1085+ print('fini', end=' ')
1086+ """ )
1087+ with self .subTest ("del" ):
1088+ res = script_helper .assert_python_ok ('-c' , code )
1089+ self .assertEqual (res .out .rstrip (), b'fini True True' )
1090+
10471091
10481092class SubclassableWeakrefTestCase (TestBase ):
10491093
You can’t perform that action at this time.
0 commit comments