Skip to content

Commit ce0eaa6

Browse files
[3.13] gh-124722: Fix leak in test_detach_materialized_dict_no_memory (GH-124769) (#124777)
gh-124722: Fix leak in `test_detach_materialized_dict_no_memory` (GH-124769) (cherry picked from commit 6f4d64b) Co-authored-by: sobolevn <[email protected]>
1 parent a1a4cfc commit ce0eaa6

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

Lib/test/test_class.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"Test the functionality of Python classes implementing operators."
22

33
import unittest
4-
import test.support
4+
from test.support import cpython_only, import_helper, script_helper
55

66
testmeths = [
77

@@ -920,20 +920,36 @@ class C:
920920
C.a = X()
921921
C.a = X()
922922

923+
@cpython_only
923924
def test_detach_materialized_dict_no_memory(self):
924-
import _testcapi
925-
class A:
926-
def __init__(self):
927-
self.a = 1
928-
self.b = 2
929-
a = A()
930-
d = a.__dict__
931-
with test.support.catch_unraisable_exception() as ex:
932-
_testcapi.set_nomemory(0, 1)
933-
del a
934-
self.assertEqual(ex.unraisable.exc_type, MemoryError)
935-
with self.assertRaises(KeyError):
936-
d["a"]
925+
# Skip test if _testcapi is not available:
926+
import_helper.import_module('_testcapi')
927+
928+
code = """if 1:
929+
import test.support
930+
import _testcapi
931+
932+
class A:
933+
def __init__(self):
934+
self.a = 1
935+
self.b = 2
936+
a = A()
937+
d = a.__dict__
938+
with test.support.catch_unraisable_exception() as ex:
939+
_testcapi.set_nomemory(0, 1)
940+
del a
941+
assert ex.unraisable.exc_type is MemoryError
942+
try:
943+
d["a"]
944+
except KeyError:
945+
pass
946+
else:
947+
assert False, "KeyError not raised"
948+
"""
949+
rc, out, err = script_helper.assert_python_ok("-c", code)
950+
self.assertEqual(rc, 0)
951+
self.assertFalse(out, msg=out.decode('utf-8'))
952+
self.assertFalse(err, msg=err.decode('utf-8'))
937953

938954
if __name__ == '__main__':
939955
unittest.main()

0 commit comments

Comments
 (0)