bpo-45034: Fix how upper limit is formatted for struct.pack("H", ...) (GH-28178)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Nikita Sobolev 2021-09-07 15:18:46 +03:00 committed by GitHub
parent 97b754d4b4
commit 8ca6b61e3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 6 deletions

View file

@ -678,6 +678,24 @@ class StructTest(unittest.TestCase):
'embedded null character'):
struct.calcsize(s)
@support.cpython_only
def test_issue45034_unsigned(self):
from _testcapi import USHRT_MAX
error_msg = f'ushort format requires 0 <= number <= {USHRT_MAX}'
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('H', 70000) # too large
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('H', -1) # too small
@support.cpython_only
def test_issue45034_signed(self):
from _testcapi import SHRT_MIN, SHRT_MAX
error_msg = f'short format requires {SHRT_MIN} <= number <= {SHRT_MAX}'
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('h', 70000) # too large
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('h', -70000) # too small
class UnpackIteratorTest(unittest.TestCase):
"""