Skip to content

Commit 5442561

Browse files
authored
gh-93575: Use correct way to calculate PyUnicode struct sizes (GH-93602)
* gh-93575: Use correct way to calculate PyUnicode struct sizes * Add comment to keep test_sys and test_unicode in sync * Fix case code < 256
1 parent 2dece90 commit 5442561

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Lib/test/test_sys.py

+1
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ class newstyleclass(object): pass
15391539
samples = ['1'*100, '\xff'*50,
15401540
'\u0100'*40, '\uffff'*100,
15411541
'\U00010000'*30, '\U0010ffff'*100]
1542+
# also update field definitions in test_unicode.test_raiseMemError
15421543
asciifields = "nnb"
15431544
compactfields = asciifields + "nP"
15441545
unicodefields = compactfields + "P"

Lib/test/test_unicode.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -2370,15 +2370,19 @@ 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+
asciifields = "nnb"
2374+
compactfields = asciifields + "nP"
2375+
ascii_struct_size = support.calcobjsize(asciifields)
2376+
compact_struct_size = support.calcobjsize(compactfields)
23762377

23772378
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
23782379
code = ord(char)
2379-
if code < 0x100:
2380+
if code < 0x80:
23802381
char_size = 1 # sizeof(Py_UCS1)
23812382
struct_size = ascii_struct_size
2383+
elif code < 0x100:
2384+
char_size = 1 # sizeof(Py_UCS1)
2385+
struct_size = compact_struct_size
23822386
elif code < 0x10000:
23832387
char_size = 2 # sizeof(Py_UCS2)
23842388
struct_size = compact_struct_size
@@ -2390,7 +2394,16 @@ def test_raiseMemError(self):
23902394
# be allocatable, given enough memory.
23912395
maxlen = ((sys.maxsize - struct_size) // char_size)
23922396
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+
)
23942407
self.assertRaises(MemoryError, alloc)
23952408
self.assertRaises(MemoryError, alloc)
23962409

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix issue with test_unicode test_raiseMemError. The test case now use
2+
``test.support.calcobjsize`` to calculate size of PyUnicode structs.
3+
:func:`sys.getsizeof` may return different size when string has UTF-8
4+
memory.

0 commit comments

Comments
 (0)