mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
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
This commit is contained in:
parent
2dece90808
commit
5442561c1a
3 changed files with 23 additions and 5 deletions
|
@ -1539,6 +1539,7 @@ class SizeofTest(unittest.TestCase):
|
||||||
samples = ['1'*100, '\xff'*50,
|
samples = ['1'*100, '\xff'*50,
|
||||||
'\u0100'*40, '\uffff'*100,
|
'\u0100'*40, '\uffff'*100,
|
||||||
'\U00010000'*30, '\U0010ffff'*100]
|
'\U00010000'*30, '\U0010ffff'*100]
|
||||||
|
# also update field definitions in test_unicode.test_raiseMemError
|
||||||
asciifields = "nnb"
|
asciifields = "nnb"
|
||||||
compactfields = asciifields + "nP"
|
compactfields = asciifields + "nP"
|
||||||
unicodefields = compactfields + "P"
|
unicodefields = compactfields + "P"
|
||||||
|
|
|
@ -2370,15 +2370,19 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertIs(s.expandtabs(), s)
|
self.assertIs(s.expandtabs(), s)
|
||||||
|
|
||||||
def test_raiseMemError(self):
|
def test_raiseMemError(self):
|
||||||
null_byte = 1
|
asciifields = "nnb"
|
||||||
ascii_struct_size = sys.getsizeof("a") - len("a") - null_byte
|
compactfields = asciifields + "nP"
|
||||||
compact_struct_size = sys.getsizeof("\xff") - len("\xff") - null_byte
|
ascii_struct_size = support.calcobjsize(asciifields)
|
||||||
|
compact_struct_size = support.calcobjsize(compactfields)
|
||||||
|
|
||||||
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
|
for char in ('a', '\xe9', '\u20ac', '\U0010ffff'):
|
||||||
code = ord(char)
|
code = ord(char)
|
||||||
if code < 0x100:
|
if code < 0x80:
|
||||||
char_size = 1 # sizeof(Py_UCS1)
|
char_size = 1 # sizeof(Py_UCS1)
|
||||||
struct_size = ascii_struct_size
|
struct_size = ascii_struct_size
|
||||||
|
elif code < 0x100:
|
||||||
|
char_size = 1 # sizeof(Py_UCS1)
|
||||||
|
struct_size = compact_struct_size
|
||||||
elif code < 0x10000:
|
elif code < 0x10000:
|
||||||
char_size = 2 # sizeof(Py_UCS2)
|
char_size = 2 # sizeof(Py_UCS2)
|
||||||
struct_size = compact_struct_size
|
struct_size = compact_struct_size
|
||||||
|
@ -2390,7 +2394,16 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
# be allocatable, given enough memory.
|
# be allocatable, given enough memory.
|
||||||
maxlen = ((sys.maxsize - struct_size) // char_size)
|
maxlen = ((sys.maxsize - struct_size) // char_size)
|
||||||
alloc = lambda: char * maxlen
|
alloc = lambda: char * maxlen
|
||||||
with self.subTest(char=char):
|
with self.subTest(
|
||||||
|
char=char,
|
||||||
|
struct_size=struct_size,
|
||||||
|
char_size=char_size
|
||||||
|
):
|
||||||
|
# self-check
|
||||||
|
self.assertEqual(
|
||||||
|
sys.getsizeof(char * 42),
|
||||||
|
struct_size + (char_size * (42 + 1))
|
||||||
|
)
|
||||||
self.assertRaises(MemoryError, alloc)
|
self.assertRaises(MemoryError, alloc)
|
||||||
self.assertRaises(MemoryError, alloc)
|
self.assertRaises(MemoryError, alloc)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Fix issue with test_unicode test_raiseMemError. The test case now use
|
||||||
|
``test.support.calcobjsize`` to calculate size of PyUnicode structs.
|
||||||
|
:func:`sys.getsizeof` may return different size when string has UTF-8
|
||||||
|
memory.
|
Loading…
Add table
Add a link
Reference in a new issue