diff options
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r-- | Lib/test/test_struct.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 0678761..b62d74c 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -614,53 +614,61 @@ def test_pack_into_fn(): assertRaises(struct.error, pack_into, small_buf, 0, test_string) assertRaises(struct.error, pack_into, small_buf, 2, test_string) +def test_unpack_with_buffer(): + # SF bug 1563759: struct.unpack doens't support buffer protocol objects + data1 = array.array('B', '\x12\x34\x56\x78') + data2 = buffer('......\x12\x34\x56\x78......', 6, 4) + for data in [data1, data2]: + value, = struct.unpack('>I', data) + vereq(value, 0x12345678) # Test methods to pack and unpack from buffers rather than strings. test_unpack_from() test_pack_into() test_pack_into_fn() +test_unpack_with_buffer() def test_bool(): for prefix in tuple("<>!=")+('',): false = (), [], [], '', 0 true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2 - + falseFormat = prefix + 't' * len(false) if verbose: print('trying bool pack/unpack on', false, 'using format', falseFormat) packedFalse = struct.pack(falseFormat, *false) unpackedFalse = struct.unpack(falseFormat, packedFalse) - + trueFormat = prefix + 't' * len(true) if verbose: print('trying bool pack/unpack on', true, 'using format', trueFormat) packedTrue = struct.pack(trueFormat, *true) unpackedTrue = struct.unpack(trueFormat, packedTrue) - + if len(true) != len(unpackedTrue): raise TestFailed('unpacked true array is not of same size as input') if len(false) != len(unpackedFalse): raise TestFailed('unpacked false array is not of same size as input') - + for t in unpackedFalse: if t is not False: raise TestFailed('%r did not unpack as False' % t) for t in unpackedTrue: if t is not True: raise TestFailed('%r did not unpack as false' % t) - + if prefix and verbose: print('trying size of bool with format %r' % (prefix+'t')) packed = struct.pack(prefix+'t', 1) - + if len(packed) != struct.calcsize(prefix+'t'): raise TestFailed('packed length is not equal to calculated size') - + if len(packed) != 1 and prefix: raise TestFailed('encoded bool is not one byte: %r' % packed) elif not prefix and verbose: print('size of bool in native format is %i' % (len(packed))) - + for c in '\x01\x7f\xff\x0f\xf0': if struct.unpack('>t', c)[0] is not True: raise TestFailed('%c did not unpack as True' % c) |