Skip to content

Commit 6f4d64b

Browse files
authored
gh-124722: Fix leak in test_detach_materialized_dict_no_memory (GH-124769)
1 parent b577460 commit 6f4d64b

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

Lib/test/test_class.py

+30-14
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

@@ -933,20 +933,36 @@ class C:
933933
C.a = X()
934934
C.a = X()
935935

936+
@cpython_only
936937
def test_detach_materialized_dict_no_memory(self):
937-
import _testcapi
938-
class A:
939-
def __init__(self):
940-
self.a = 1
941-
self.b = 2
942-
a = A()
943-
d = a.__dict__
944-
with test.support.catch_unraisable_exception() as ex:
945-
_testcapi.set_nomemory(0, 1)
946-
del a
947-
self.assertEqual(ex.unraisable.exc_type, MemoryError)
948-
with self.assertRaises(KeyError):
949-
d["a"]
938+
# Skip test if _testcapi is not available:
939+
import_helper.import_module('_testcapi')
940+
941+
code = """if 1:
942+
import test.support
943+
import _testcapi
944+
945+
class A:
946+
def __init__(self):
947+
self.a = 1
948+
self.b = 2
949+
a = A()
950+
d = a.__dict__
951+
with test.support.catch_unraisable_exception() as ex:
952+
_testcapi.set_nomemory(0, 1)
953+
del a
954+
assert ex.unraisable.exc_type is MemoryError
955+
try:
956+
d["a"]
957+
except KeyError:
958+
pass
959+
else:
960+
assert False, "KeyError not raised"
961+
"""
962+
rc, out, err = script_helper.assert_python_ok("-c", code)
963+
self.assertEqual(rc, 0)
964+
self.assertFalse(out, msg=out.decode('utf-8'))
965+
self.assertFalse(err, msg=err.decode('utf-8'))
950966

951967
if __name__ == '__main__':
952968
unittest.main()

0 commit comments

Comments
 (0)