summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_struct.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-02-21 17:51:17 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-02-21 17:51:17 (GMT)
commitb8285d96f40a635157b0394f7cf9e69d0e6492cf (patch)
tree488dc04feb71eb05a252e37b48eabc8d9bf4e673 /Lib/test/test_struct.py
parent4809d1fccd81bd3e1e4b08152545cfd88b69231c (diff)
downloadcpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.zip
cpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.tar.gz
cpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.tar.bz2
Issue #22113: struct.pack_into() now supports new buffer protocol (in
particular accepts writable memoryview).
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r--Lib/test/test_struct.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 4811974..fe56dcb 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -433,24 +433,24 @@ class StructTest(unittest.TestCase):
self.assertRaises(struct.error, s.unpack_from, data, i)
self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)
- def test_pack_into(self):
+ def test_pack_into(self, cls=bytearray, tobytes=str):
test_string = 'Reykjavik rocks, eow!'
- writable_buf = array.array('c', ' '*100)
+ writable_buf = cls(' '*100)
fmt = '21s'
s = struct.Struct(fmt)
# Test without offset
s.pack_into(writable_buf, 0, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)]
+ from_buf = tobytes(writable_buf)[:len(test_string)]
self.assertEqual(from_buf, test_string)
# Test with offset.
s.pack_into(writable_buf, 10, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)+10]
+ from_buf = tobytes(writable_buf)[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)
# Go beyond boundaries.
- small_buf = array.array('c', ' '*10)
+ small_buf = cls(' '*10)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
test_string)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
@@ -461,6 +461,15 @@ class StructTest(unittest.TestCase):
self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
None)
+ def test_pack_into_array(self):
+ self.test_pack_into(cls=lambda b: array.array('c', b),
+ tobytes=array.array.tostring)
+
+ def test_pack_into_memoryview(self):
+ # Issue #22113
+ self.test_pack_into(cls=lambda b: memoryview(bytearray(b)),
+ tobytes=memoryview.tobytes)
+
def test_pack_into_fn(self):
test_string = 'Reykjavik rocks, eow!'
writable_buf = array.array('c', ' '*100)