@@ -2370,15 +2370,19 @@ 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
+ asciifields = "nnb"
2374
+ compactfields = asciifields + "nP"
2375
+ ascii_struct_size = support .calcobjsize (asciifields )
2376
+ compact_struct_size = support .calcobjsize (compactfields )
2376
2377
2377
2378
for char in ('a' , '\xe9 ' , '\u20ac ' , '\U0010ffff ' ):
2378
2379
code = ord (char )
2379
- if code < 0x100 :
2380
+ if code < 0x80 :
2380
2381
char_size = 1 # sizeof(Py_UCS1)
2381
2382
struct_size = ascii_struct_size
2383
+ elif code < 0x100 :
2384
+ char_size = 1 # sizeof(Py_UCS1)
2385
+ struct_size = compact_struct_size
2382
2386
elif code < 0x10000 :
2383
2387
char_size = 2 # sizeof(Py_UCS2)
2384
2388
struct_size = compact_struct_size
@@ -2390,7 +2394,16 @@ def test_raiseMemError(self):
2390
2394
# be allocatable, given enough memory.
2391
2395
maxlen = ((sys .maxsize - struct_size ) // char_size )
2392
2396
alloc = lambda : char * maxlen
2393
- with self .subTest (char = char ):
2397
+ with self .subTest (
2398
+ char = char ,
2399
+ struct_size = struct_size ,
2400
+ char_size = char_size
2401
+ ):
2402
+ # self-check
2403
+ self .assertEqual (
2404
+ sys .getsizeof (char * 42 ),
2405
+ struct_size + (char_size * (42 + 1 ))
2406
+ )
2394
2407
self .assertRaises (MemoryError , alloc )
2395
2408
self .assertRaises (MemoryError , alloc )
2396
2409
0 commit comments