diff options
author | Christian Heimes <christian@python.org> | 2022-06-08 18:18:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-08 18:18:08 (GMT) |
commit | 5442561c1a094b68900198bade616da9ed509ac8 (patch) | |
tree | baa10e0216d4e5ffadc32624b9c7acac23e14807 /Lib/test/test_unicode.py | |
parent | 2dece908085e719d2345ea80ea174fa6a4473851 (diff) | |
download | cpython-5442561c1a094b68900198bade616da9ed509ac8.zip cpython-5442561c1a094b68900198bade616da9ed509ac8.tar.gz cpython-5442561c1a094b68900198bade616da9ed509ac8.tar.bz2 |
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
Diffstat (limited to 'Lib/test/test_unicode.py')
-rw-r--r-- | Lib/test/test_unicode.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 64abc0c..9765ed9 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -2370,15 +2370,19 @@ class UnicodeTest(string_tests.CommonTest, self.assertIs(s.expandtabs(), s) def test_raiseMemError(self): - null_byte = 1 - ascii_struct_size = sys.getsizeof("a") - len("a") - null_byte - compact_struct_size = sys.getsizeof("\xff") - len("\xff") - null_byte + asciifields = "nnb" + compactfields = asciifields + "nP" + ascii_struct_size = support.calcobjsize(asciifields) + compact_struct_size = support.calcobjsize(compactfields) for char in ('a', '\xe9', '\u20ac', '\U0010ffff'): code = ord(char) - if code < 0x100: + if code < 0x80: char_size = 1 # sizeof(Py_UCS1) struct_size = ascii_struct_size + elif code < 0x100: + char_size = 1 # sizeof(Py_UCS1) + struct_size = compact_struct_size elif code < 0x10000: char_size = 2 # sizeof(Py_UCS2) struct_size = compact_struct_size @@ -2390,7 +2394,16 @@ class UnicodeTest(string_tests.CommonTest, # be allocatable, given enough memory. maxlen = ((sys.maxsize - struct_size) // char_size) 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) |