[Python-checkins] r54690 - in python/branches/release25-maint: Lib/test/test_struct.py Misc/NEWS Modules/_struct.c

raymond.hettinger python-checkins at python.org
Wed Apr 4 22:32:04 CEST 2007


Author: raymond.hettinger
Date: Wed Apr  4 22:32:03 2007
New Revision: 54690

Modified:
   python/branches/release25-maint/Lib/test/test_struct.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_struct.c
Log:
Bug #1563759: struct.unpack doens't support buffer protocol objects



Modified: python/branches/release25-maint/Lib/test/test_struct.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_struct.py	(original)
+++ python/branches/release25-maint/Lib/test/test_struct.py	Wed Apr  4 22:32:03 2007
@@ -612,8 +612,14 @@
     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
+    data = array.array('B', '\x12\x34\x56\x78')
+    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()

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Wed Apr  4 22:32:03 2007
@@ -134,6 +134,8 @@
 Extension Modules
 -----------------
 
+- Bug #1563759: struct.unpack doens't support buffer protocol objects
+
 - Bug #1686475: Support stat'ing open files on Windows again.
 
 - Bug #1647541: Array module's buffer interface can now handle empty arrays.

Modified: python/branches/release25-maint/Modules/_struct.c
==============================================================================
--- python/branches/release25-maint/Modules/_struct.c	(original)
+++ python/branches/release25-maint/Modules/_struct.c	Wed Apr  4 22:32:03 2007
@@ -1485,17 +1485,31 @@
 static PyObject *
 s_unpack(PyObject *self, PyObject *inputstr)
 {
+	char *start;
+	int len;
+	PyObject * args;
 	PyStructObject *soself = (PyStructObject *)self;
 	assert(PyStruct_Check(self));
 	assert(soself->s_codes != NULL);
-	if (inputstr == NULL || !PyString_Check(inputstr) ||
-		PyString_GET_SIZE(inputstr) != soself->s_size) {
+	if (inputstr != NULL && PyString_Check(inputstr) &&
+		PyString_GET_SIZE(inputstr) == soself->s_size) {
+			return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
+	}
+	args = PyTuple_Pack(1, inputstr);
+	if (args == NULL)
+		return NULL;
+	if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) {
+		Py_DECREF(args);
+		return NULL;
+	}
+	Py_DECREF(args);
+	if (soself->s_size != len) {
 		PyErr_Format(StructError,
 			"unpack requires a string argument of length %zd",
 			soself->s_size);
 		return NULL;
 	}
-	return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
+	return s_unpack_internal(soself, start);
 }
 
 PyDoc_STRVAR(s_unpack_from__doc__,


More information about the Python-checkins mailing list