Content-Length: 390429 | pFad | https://github.com/python/cpython/commit/416c3465e247bc5a36f86b832360f65e06a0767a

ad gh-155146: Do not depend on the exact number of allocations in test_c… · python/cpython@416c346 · GitHub
Skip to content

Commit 416c346

Browse files
gh-155146: Do not depend on the exact number of allocations in test_class (GH-155150)
Try to fail every one of the first allocations and accept the first one which fails in the code detaching the instance dictionary from the object, instead of assuming that this is the first allocation after set_nomemory(). Run the test with the test.support.isolation.runInSubprocess() decorator instead of executing it as a source string in a subprocess. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 089b713 commit 416c346

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

Lib/test/test_class.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44
from test import support
5-
from test.support import cpython_only, import_helper, script_helper
5+
from test.support import cpython_only, import_helper, isolation
66

77
testmeths = [
88

@@ -1014,32 +1014,43 @@ class C:
10141014
C.a = X()
10151015

10161016
@support.nomemtest
1017+
@isolation.runInSubprocess()
10171018
def test_detach_materialized_dict_no_memory(self):
1018-
code = """if 1:
1019-
import test.support
1020-
import _testcapi
1021-
1022-
class A:
1023-
def __init__(self):
1024-
self.a = 1
1025-
self.b = 2
1019+
import _testcapi
1020+
1021+
class A:
1022+
def __init__(self):
1023+
self.a = 1
1024+
self.b = 2
1025+
1026+
# The failing allocation should be the one which detaches the
1027+
# dictionary from the object, but other allocations can happen
1028+
# first, so try to fail every one of the first allocations.
1029+
raised = False
1030+
for n in range(20):
10261031
a = A()
10271032
d = a.__dict__
1028-
with test.support.catch_unraisable_exception() as ex:
1029-
_testcapi.set_nomemory(0, 1)
1030-
del a
1031-
assert ex.unraisable.exc_type is MemoryError
10321033
try:
1033-
d["a"]
1034-
except KeyError:
1035-
pass
1036-
else:
1037-
assert False, "KeyError not raised"
1038-
"""
1039-
rc, out, err = script_helper.assert_python_ok("-c", code)
1040-
self.assertEqual(rc, 0)
1041-
self.assertFalse(out, msg=out.decode('utf-8'))
1042-
self.assertFalse(err, msg=err.decode('utf-8'))
1034+
with support.catch_unraisable_exception() as ex:
1035+
_testcapi.set_nomemory(n, n + 1)
1036+
try:
1037+
del a
1038+
finally:
1039+
_testcapi.remove_mem_hooks()
1040+
exc_type = ex.unraisable and ex.unraisable.exc_type
1041+
except MemoryError:
1042+
# The failing allocation was not in the deallocation code.
1043+
continue
1044+
if exc_type is not MemoryError:
1045+
continue
1046+
raised = True
1047+
if "a" not in d:
1048+
# The dictionary was cleared, as expected.
1049+
break
1050+
else:
1051+
if not raised:
1052+
self.fail("MemoryError was not raised during deallocation")
1053+
self.fail("the dictionary was not cleared")
10431054

10441055
if __name__ == '__main__':
10451056
unittest.main()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/python/cpython/commit/416c3465e247bc5a36f86b832360f65e06a0767a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy