Skip to content

gh-132413: Clear weakref to _datetime after modules are finalized #136152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
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
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy