Skip to content

Commit 47a7855

Browse files
authored
[3.11] pythongh-93575: Use correct way to calculate PyUnicode struct sizes (pythonGH-93602) (pythonGH-93613)
* pythongh-93575: Use correct way to calculate PyUnicode struct sizes * Add comment to keep test_sys and test_unicode in sync * Fix case code < 256. (cherry picked from commit 5442561) Co-authored-by: Christian Heimes <[email protected]>
1 parent 1b7942a commit 47a7855

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Lib/test/test_sys.py

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

Lib/test/test_unicode.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -2370,20 +2370,19 @@ 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+
asciifields = "nnbP"
2374+
compactfields = asciifields + "nPn"
2375+
ascii_struct_size = support.calcobjsize(asciifields)
2376+
compact_struct_size = support.calcobjsize(compactfields)
23812377

23822378
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
23832379
code = ord(char)
2384-
if code < 0x100:
2380+
if code < 0x80:
23852381
char_size = 1 # sizeof(Py_UCS1)
23862382
struct_size = ascii_struct_size
2383+
elif code < 0x100:
2384+
char_size = 1 # sizeof(Py_UCS1)
2385+
struct_size = compact_struct_size
23872386
elif code < 0x10000:
23882387
char_size = 2 # sizeof(Py_UCS2)
23892388
struct_size = compact_struct_size
@@ -2395,8 +2394,18 @@ def test_raiseMemError(self):
23952394
# be allocatable, given enough memory.
23962395
maxlen = ((sys.maxsize - struct_size) // char_size)
23972396
alloc = lambda: char * maxlen
2398-
self.assertRaises(MemoryError, alloc)
2399-
self.assertRaises(MemoryError, alloc)
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+
)
2407+
self.assertRaises(MemoryError, alloc)
2408+
self.assertRaises(MemoryError, alloc)
24002409

24012410
def test_format_subclass(self):
24022411
class S(str):
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)