diff options
author | Xiang Zhang <angwerzx@126.com> | 2018-03-10 18:58:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-10 18:58:52 (GMT) |
commit | c10b288f345aaef66d2c844924b9a576f9ea4f8b (patch) | |
tree | 30d64745d62c991b5d18bfc1c381f31b165c7c4d /Lib | |
parent | 67ee07795bcd84b679c000780212d4d81a1490a3 (diff) | |
download | cpython-c10b288f345aaef66d2c844924b9a576f9ea4f8b.zip cpython-c10b288f345aaef66d2c844924b9a576f9ea4f8b.tar.gz cpython-c10b288f345aaef66d2c844924b9a576f9ea4f8b.tar.bz2 |
bpo-30249: Improve struct.unpack_from() error messages (GH-6059)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_struct.py | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 8fd56c9..454082e 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -579,14 +579,22 @@ class StructTest(unittest.TestCase): self.check_sizeof('0c', 0) def test_boundary_error_message(self): - regex = ( + regex1 = ( 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): + with self.assertRaisesRegex(struct.error, regex1): struct.pack_into('b', bytearray(1), 5, 1) + regex2 = ( + r'unpack_from requires a buffer of at least 6 ' + r'bytes for unpacking 1 bytes at offset 5 ' + r'\(actual buffer size is 1\)' + ) + with self.assertRaisesRegex(struct.error, regex2): + struct.unpack_from('b', bytearray(1), 5) + def test_boundary_error_message_with_negative_offset(self): byte_list = bytearray(10) with self.assertRaisesRegex( @@ -599,16 +607,34 @@ class StructTest(unittest.TestCase): 'offset -11 out of range for 10-byte buffer'): struct.pack_into('<B', byte_list, -11, 123) + with self.assertRaisesRegex( + struct.error, + r'not enough data to unpack 4 bytes at offset -2'): + struct.unpack_from('<I', byte_list, -2) + + with self.assertRaisesRegex( + struct.error, + "offset -11 out of range for 10-byte buffer"): + struct.unpack_from('<B', byte_list, -11) + def test_boundary_error_message_with_large_offset(self): # Test overflows cause by large offset and value size (issue 30245) - regex = ( + regex1 = ( r'pack_into requires a buffer of at least ' + str(sys.maxsize + 4) + r' bytes for packing 4 bytes at offset ' + str(sys.maxsize) + r' \(actual buffer size is 10\)' ) - with self.assertRaisesRegex(struct.error, regex): + with self.assertRaisesRegex(struct.error, regex1): struct.pack_into('<I', bytearray(10), sys.maxsize, 1) + regex2 = ( + r'unpack_from requires a buffer of at least ' + str(sys.maxsize + 4) + + r' bytes for unpacking 4 bytes at offset ' + str(sys.maxsize) + + r' \(actual buffer size is 10\)' + ) + with self.assertRaisesRegex(struct.error, regex2): + struct.unpack_from('<I', bytearray(10), sys.maxsize) + def test_issue29802(self): # When the second argument of struct.unpack() was of wrong type # the Struct object was decrefed twice and the reference to |