Skip to content

Commit 111ece3

Browse files
committed
Update unicode struct size to ensure MemError is raised
1 parent bd30461 commit 111ece3

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

Lib/test/test_unicode.py

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

23722372
def test_raiseMemError(self):
2373-
if struct.calcsize('P') == 8:
2374-
# 64 bits pointers
2375-
ascii_struct_size = 48
2376-
compact_struct_size = 72
2377-
else:
2378-
# 32 bits pointers
2379-
ascii_struct_size = 24
2380-
compact_struct_size = 36
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
23812376

23822377
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
23832378
code = ord(char)
@@ -2395,8 +2390,9 @@ def test_raiseMemError(self):
23952390
# be allocatable, given enough memory.
23962391
maxlen = ((sys.maxsize - struct_size) // char_size)
23972392
alloc = lambda: char * maxlen
2398-
self.assertRaises(MemoryError, alloc)
2399-
self.assertRaises(MemoryError, alloc)
2393+
with self.subTest(char=char):
2394+
self.assertRaises(MemoryError, alloc)
2395+
self.assertRaises(MemoryError, alloc)
24002396

24012397
def test_format_subclass(self):
24022398
class S(str):

0 commit comments

Comments
 (0)