Skip to content

Commit bb29ea1

Browse files
committed
Add unit test for GH-132413.
1 parent 84bd123 commit bb29ea1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Lib/test/test_gc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,26 @@ def func():
309309
self.assertRegex(stdout, rb"""\A\s*func=None""")
310310
self.assertFalse(stderr)
311311

312+
def test_datetime_weakref_cycle(self):
313+
# https://github.com/python/cpython/issues/132413
314+
# If the weakref used by the datetime extension gets cleared by the GC (due to being
315+
# in an unreachable cycle) then datetime functions would crash (get_module_state()
316+
# was returning a NULL pointer). This bug is fixed by clearing weakrefs without
317+
# callbacks *after* running finalizers.
318+
code = """if 1:
319+
import _datetime
320+
class C:
321+
def __del__(self):
322+
print('__del__ called')
323+
_datetime.timedelta(days=1) # crash?
324+
325+
l = [C()]
326+
l.append(l)
327+
"""
328+
rc, stdout, stderr = assert_python_ok("-c", code)
329+
self.assertEqual(rc, 0)
330+
self.assertEqual(stdout.strip(), b'__del__ called')
331+
312332
@refcount_test
313333
def test_frame(self):
314334
def f():

0 commit comments

Comments
 (0)