From 2f48243d854f8da2a52f3170d652b2a9dedfb391 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Feb 2017 12:09:30 +0100 Subject: Issue #29300: Convert _struct module to Argument Clinic * The struct module now requires contiguous buffers. * Convert most functions and methods of the _struct module to Argument Clinic * Use "Py_buffer" type for the "buffer" argument. Argument Clinic is responsible to create and release the Py_buffer object. * Use "PyStructObject *" type for self to avoid explicit conversions. * Add an unit test on the _struct.Struct.unpack_from() method to test passing arguments as keywords. * Rephrase docstrings. * Rename "fmt" argument to "format" in docstrings and the documentation. As a side effect, functions and methods which used METH_VARARGS calling convention like struct.pack() now use the METH_FASTCALL calling convention which avoids the creation of temporary tuple to pass positional arguments and so is faster. For example, struct.pack("i", 1) becomes 1.56x faster (-36%):: $ ./python -m perf timeit \ -s 'import struct; pack=struct.pack' 'pack("i", 1)' \ --compare-to=../default-ref/python Median +- std dev: 119 ns +- 1 ns -> 76.8 ns +- 0.4 ns: 1.56x faster (-36%) Significant (t=295.91) Patch co-written with Serhiy Storchaka. --- Lib/test/test_struct.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Lib/test/test_struct.py') diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 4d9d601ef4..be0047589c 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -412,6 +412,10 @@ class StructTest(unittest.TestCase): for i in range(6, len(test_string) + 1): self.assertRaises(struct.error, struct.unpack_from, fmt, data, i) + # keyword arguments + self.assertEqual(s.unpack_from(buffer=test_string, offset=2), + (b'cd01',)) + def test_pack_into(self): test_string = b'Reykjavik rocks, eow!' writable_buf = array.array('b', b' '*100) -- cgit v1.2.1 From a96910ecad21b69100cc9ee045839fecdbfcfa72 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 2 Feb 2017 14:24:16 +0100 Subject: Rename struct.unpack() 2nd parameter to "buffer" Issue #29300: Rename struct.unpack() second parameter from "inputstr" to "buffer", and use the Py_buffer type. Fix also unit tests on struct.unpack() which passed a Unicode string instead of a bytes string as struct.unpack() second parameter. The purpose of test_trailing_counter() is to test invalid format strings, not to test the buffer parameter. --- Lib/test/test_struct.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Lib/test/test_struct.py') diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index be0047589c..cf1d567966 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -530,13 +530,13 @@ class StructTest(unittest.TestCase): # format lists containing only count spec should result in an error self.assertRaises(struct.error, struct.pack, '12345') - self.assertRaises(struct.error, struct.unpack, '12345', '') + self.assertRaises(struct.error, struct.unpack, '12345', b'') self.assertRaises(struct.error, struct.pack_into, '12345', store, 0) self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0) # Format lists with trailing count spec should result in an error self.assertRaises(struct.error, struct.pack, 'c12345', 'x') - self.assertRaises(struct.error, struct.unpack, 'c12345', 'x') + self.assertRaises(struct.error, struct.unpack, 'c12345', b'x') self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0, 'x') self.assertRaises(struct.error, struct.unpack_from, 'c12345', store, @@ -545,7 +545,7 @@ class StructTest(unittest.TestCase): # Mixed format tests self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs') self.assertRaises(struct.error, struct.unpack, '14s42', - 'spam and eggs') + b'spam and eggs') self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0, 'spam and eggs') self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0) -- cgit v1.2.1