diff options
author | Andrew Nester <andrew.nester.dev@gmail.com> | 2017-04-04 10:46:25 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-04-04 10:46:25 (GMT) |
commit | f78b119364b521307237a1484ba5f43f42300898 (patch) | |
tree | 240db0104a9eba34ef4d5bd89ad9cb13472f97bc /Lib/test/test_struct.py | |
parent | 5de85a17029356084b96db63e04d9eb150efd9c0 (diff) | |
download | cpython-f78b119364b521307237a1484ba5f43f42300898.zip cpython-f78b119364b521307237a1484ba5f43f42300898.tar.gz cpython-f78b119364b521307237a1484ba5f43f42300898.tar.bz2 |
bpo-29649: Improve struct.pack_into() boundary error messages (#424)
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r-- | Lib/test/test_struct.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index cf1d567..932ef47 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -578,6 +578,26 @@ class StructTest(unittest.TestCase): self.check_sizeof('0s', 1) self.check_sizeof('0c', 0) + def test_boundary_error_message(self): + regex = ( + r'pack_into requires a buffer of at least 6 ' + r'bytes for packing 1 bytes at offset 5 ' + r'\(actual buffer size is 1\)' + ) + with self.assertRaisesRegex(struct.error, regex): + struct.pack_into('b', bytearray(1), 5, 1) + + def test_boundary_error_message_with_negative_offset(self): + byte_list = bytearray(10) + with self.assertRaisesRegex( + struct.error, + r'no space to pack 4 bytes at offset -2'): + struct.pack_into('<I', byte_list, -2, 123) + + with self.assertRaisesRegex( + struct.error, + 'offset -11 out of range for 10-byte buffer'): + struct.pack_into('<B', byte_list, -11, 123) class UnpackIteratorTest(unittest.TestCase): """ |