Skip to content

Commit d42f6c0

Browse files
committed
gh-124722: Fix leak in test_detach_materialized_dict_no_memory
1 parent 6d0d26e commit d42f6c0

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

Lib/test/test_class.py

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

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

67
testmeths = [
78

@@ -933,20 +934,36 @@ class C:
933934
C.a = X()
934935
C.a = X()
935936

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

951968
if __name__ == '__main__':
952969
unittest.main()

0 commit comments

Comments
 (0)