Skip to content

gh-124722: Fix leak in test_detach_materialized_dict_no_memory #124769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions Lib/test/test_class.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"Test the functionality of Python classes implementing operators."

import unittest
import test.support
from test.support import cpython_only, import_helper, script_helper

testmeths = [

Expand Down Expand Up @@ -933,20 +933,36 @@ class C:
C.a = X()
C.a = X()

@cpython_only
def test_detach_materialized_dict_no_memory(self):
import _testcapi
class A:
def __init__(self):
self.a = 1
self.b = 2
a = A()
d = a.__dict__
with test.support.catch_unraisable_exception() as ex:
_testcapi.set_nomemory(0, 1)
del a
self.assertEqual(ex.unraisable.exc_type, MemoryError)
with self.assertRaises(KeyError):
d["a"]
# Skip test if _testcapi is not available:
import_helper.import_module('_testcapi')

code = """if 1:
import test.support
import _testcapi

class A:
def __init__(self):
self.a = 1
self.b = 2
a = A()
d = a.__dict__
with test.support.catch_unraisable_exception() as ex:
_testcapi.set_nomemory(0, 1)
del a
assert ex.unraisable.exc_type is MemoryError
try:
d["a"]
except KeyError:
pass
else:
assert False, "KeyError not raised"
"""
rc, out, err = script_helper.assert_python_ok("-c", code)
self.assertEqual(rc, 0)
self.assertFalse(out, msg=out.decode('utf-8'))
self.assertFalse(err, msg=err.decode('utf-8'))

if __name__ == '__main__':
unittest.main()
Loading