@@ -2370,9 +2370,10 @@ def test_expandtabs_optimization(self):
2370
2370
self .assertIs (s .expandtabs (), s )
2371
2371
2372
2372
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 " )
2376
2377
2377
2378
for char in ('a' , '\xe9 ' , '\u20ac ' , '\U0010ffff ' ):
2378
2379
code = ord (char )
@@ -2385,12 +2386,18 @@ def test_raiseMemError(self):
2385
2386
else :
2386
2387
char_size = 4 # sizeof(Py_UCS4)
2387
2388
struct_size = compact_struct_size
2389
+ struct_size -= char_size # space for NULL byte(s)
2388
2390
# Note: sys.maxsize is half of the actual max allocation because of
2389
2391
# the signedness of Py_ssize_t. Strings of maxlen-1 should in principle
2390
2392
# be allocatable, given enough memory.
2391
2393
maxlen = ((sys .maxsize - struct_size ) // char_size )
2392
2394
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
+ ):
2394
2401
self .assertRaises (MemoryError , alloc )
2395
2402
self .assertRaises (MemoryError , alloc )
2396
2403
0 commit comments