Skip to content

Commit 3ea0524

Browse files
authored
[3.15] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) (#159292)
gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) On Py_TRACE_REFS build, use malloc() and free() functions of the C library for the "reference chain" hash table. So it becomes possible to use _testcapi.set_nomemory() with Py_TRACE_REFS. Mark new tests using set_nomemory() with @support.nomemtest. (cherry picked from commit 051b168)
1 parent 3dfe214 commit 3ea0524

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,14 +1305,14 @@ def wrapper(self):
13051305
return wrapper
13061306
return decorator
13071307

1308-
def nomemtest(f):
1308+
def nomemtest(test):
13091309
"""Check that we can use this test with `_testcapi.set_nomemory`."""
13101310
from .import_helper import import_module
13111311

1312-
@functools.wraps(f)
1312+
@functools.wraps(test)
13131313
def internal(*args, **kwargs):
13141314
import_module('_testcapi')
1315-
return f(*args, **kwargs)
1315+
return test(*args, **kwargs)
13161316

13171317
return cpython_only(internal)
13181318

‎Lib/test/test_bytes.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ def test_resize(self):
15791579
self.assertRaises(MemoryError, bytearray().resize, sys.maxsize)
15801580
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize)
15811581

1582+
@support.nomemtest
15821583
def test_resize_error(self):
15831584
# gh-157242: If bytearray.resize() fails (MemoryError),
15841585
# the bytearray must be left unchanged.
@@ -1672,6 +1673,7 @@ def test_take_bytes(self):
16721673
self.assertEqual(ba, bytearray(b'A'))
16731674
self.assertEqual(ord(b'c'), ord('c'))
16741675

1676+
@support.nomemtest
16751677
def test_take_bytes_error(self):
16761678
# gh-157242: If bytearray.take_bytes() fails (MemoryError),
16771679
# the bytearray must be left unchanged.

‎Lib/test/test_capi/test_bytes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import unittest
3-
from test.support import import_helper
43
from test import support
4+
from test.support import import_helper
55

66
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
77
_testcapi = import_helper.import_module('_testcapi')
@@ -396,6 +396,7 @@ def test_resize(self):
396396
writer.resize(len(b'number=123456'), b'456')
397397
self.bytes_equal(writer.finish(), b'number=123456')
398398

399+
@support.nomemtest
399400
def test_resize_error(self):
400401
small_buffer = _testcapi.PyBytesWriter_small_buffer
401402
init = b'x' * (small_buffer * 2)

‎Objects/object.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ refchain_init(PyInterpreterState *interp)
198198
return 0;
199199
}
200200
_Py_hashtable_allocator_t alloc = {
201-
// Don't use default PyMem_Malloc() and PyMem_Free() which
202-
// require the caller to hold the GIL.
203-
.malloc = PyMem_RawMalloc,
204-
.free = PyMem_RawFree,
201+
// Use directly malloc() and free() of the C library. Using
202+
// PyMem_RawMalloc() and PyMem_RawFree() prevents testing
203+
// _testcapi.set_nomemory().
204+
.malloc = malloc,
205+
.free = free,
205206
};
206207
REFCHAIN(interp) = _Py_hashtable_new_full(
207208
_Py_hashtable_hash_ptr, _Py_hashtable_compare_direct,

0 commit comments

Comments
 (0)