MCPcopy Index your code
hub / github.com/python/cpython / test_no_memory_when_clearing

Method test_no_memory_when_clearing

Lib/test/test_weakref.py:1023–1045  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1021
1022 @support.cpython_only
1023 def test_no_memory_when_clearing(self):
1024 # gh-118331: Make sure we do not raise an exception from the destructor
1025 # when clearing weakrefs if allocating the intermediate tuple fails.
1026 code = textwrap.dedent("""
1027 import _testcapi
1028 import weakref
1029
1030 class TestObj:
1031 pass
1032
1033 def callback(obj):
1034 pass
1035
1036 obj = TestObj()
1037 # The choice of 50 is arbitrary, but must be large enough to ensure
1038 # the allocation won't be serviced by the free list.
1039 wrs = [weakref.ref(obj, callback) for _ in range(50)]
1040 _testcapi.set_nomemory(0)
1041 del obj
1042 """).strip()
1043 res, _ = script_helper.run_python_until_end("-c", code)
1044 stderr = res.err.decode("ascii", "backslashreplace")
1045 self.assertNotRegex(stderr, "_Py_Dealloc: Deallocator of type 'TestObj'")
1046
1047 def test_clearing_weakrefs_in_gc(self):
1048 # This test checks that when finalizers are called:

Callers

nothing calls this directly

Calls 4

assertNotRegexMethod · 0.80
stripMethod · 0.45
dedentMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected