Skip to content

Commit 8e89228

Browse files
committed
pythongh-93575: Correctly calculate NULL bytes in test_raiseMemError
1 parent f012df7 commit 8e89228

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Lib/test/test_unicode.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2370,9 +2370,10 @@ def test_expandtabs_optimization(self):
23702370
self.assertIs(s.expandtabs(), s)
23712371

23722372
def test_raiseMemError(self):
2373-
null_byte = 1
2374-
ascii_struct_size = sys.getsizeof("a") - len("a") - null_byte
2375-
compact_struct_size = sys.getsizeof("\xff") - len("\xff") - null_byte
2373+
# Note: null byte subtracted later once we know char size
2374+
# (PyUnicode_GET_LENGTH(self) + 1) * PyUnicode_KIND(self)
2375+
ascii_struct_size = sys.getsizeof("a") - len("a")
2376+
compact_struct_size = sys.getsizeof("\xff") - len("\xff")
23762377

23772378
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
23782379
code = ord(char)
@@ -2385,12 +2386,18 @@ def test_raiseMemError(self):
23852386
else:
23862387
char_size = 4 # sizeof(Py_UCS4)
23872388
struct_size = compact_struct_size
2389+
struct_size -= char_size # space for NULL byte(s)
23882390
# Note: sys.maxsize is half of the actual max allocation because of
23892391
# the signedness of Py_ssize_t. Strings of maxlen-1 should in principle
23902392
# be allocatable, given enough memory.
23912393
maxlen = ((sys.maxsize - struct_size) // char_size)
23922394
alloc = lambda: char * maxlen
2393-
with self.subTest(char=char):
2395+
with self.subTest(
2396+
char=char,
2397+
maxlen=maxlen,
2398+
struct_size=struct_size,
2399+
char_size=char_size
2400+
):
23942401
self.assertRaises(MemoryError, alloc)
23952402
self.assertRaises(MemoryError, alloc)
23962403

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_unicode test_raiseMemError now correctly calculates size of NULL byte(s).

0 commit comments

Comments
 (0)