Index: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 62508) +++ Modules/arraymodule.c (working copy) @@ -1137,36 +1137,6 @@ 4, or 8 bytes in size, RuntimeError is raised."); static PyObject * -array_reduce(arrayobject *array) -{ - PyObject *dict, *result; - - dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); - if (dict == NULL) { - PyErr_Clear(); - dict = Py_None; - Py_INCREF(dict); - } - if (Py_SIZE(array)> 0) { - result = Py_BuildValue("O(cs#)O", - Py_TYPE(array), - array->ob_descr->typecode, - array->ob_item, - Py_SIZE(array) * array->ob_descr->itemsize, - dict); - } else { - result = Py_BuildValue("O(c)O", - Py_TYPE(array), - array->ob_descr->typecode, - dict); - } - Py_DECREF(dict); - return result; -} - -PyDoc_STRVAR(array_doc, "Return state information for pickling."); - -static PyObject * array_reverse(arrayobject *self, PyObject *unused) { register Py_ssize_t itemsize = self->ob_descr->itemsize; @@ -1353,7 +1323,7 @@ static PyObject * -array_tolist(arrayobject *self, PyObject *unused) +array_tolist(arrayobject *self) { PyObject *list = PyList_New(Py_SIZE(self)); Py_ssize_t i; @@ -1416,7 +1386,7 @@ static PyObject * -array_tostring(arrayobject *self, PyObject *unused) +array_tostring(arrayobject *self) { return PyString_FromStringAndSize(self->ob_item, Py_SIZE(self) * self->ob_descr->itemsize); @@ -1473,7 +1443,7 @@ static PyObject * -array_tounicode(arrayobject *self, PyObject *unused) +array_tounicode(arrayobject *self) { if (self->ob_descr->typecode != 'u') { PyErr_SetString(PyExc_ValueError, @@ -1493,7 +1463,50 @@ #endif /* Py_USING_UNICODE */ +static PyObject * +array_reduce(arrayobject *array) +{ + PyObject *dict, *result; + /* Array subclasses may include extra attributes which need to be + pickled as well. Therefore, fetch __dict__ and store it in the + reduce value. */ + dict = PyObject_GetAttrString((PyObject *)array, "__dict__"); + if (dict == NULL) { + PyErr_Clear(); + dict = Py_None; + Py_INCREF(dict); + } + if (Py_SIZE(array)> 0) { + PyObject *list; + /* Convert the array to a list to store the array in a + platform-independent manner. This used to return a memory + string (for efficiency reasons), but this made the + representation dependent of the platform's word size and + byte-order (see issue #2389 for more info). */ + list = array_tolist(array); + if (list == NULL) + return NULL; + result = Py_BuildValue("O(cO)O", + Py_TYPE(array), + array->ob_descr->typecode, + list, + dict); + Py_DECREF(list); + } + else { /* empty array */ + result = Py_BuildValue("O(c)O", + Py_TYPE(array), + array->ob_descr->typecode, + dict); + } + Py_DECREF(dict); + return result; +} + +PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); + + static PyObject * array_get_typecode(arrayobject *a, void *closure) { @@ -1549,7 +1562,7 @@ {"read", (PyCFunction)array_fromfile_as_read, METH_VARARGS, fromfile_doc}, {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, - array_doc}, + reduce_doc}, {"remove", (PyCFunction)array_remove, METH_O, remove_doc}, {"reverse", (PyCFunction)array_reverse, METH_NOARGS, @@ -1586,13 +1599,13 @@ } if (typecode == 'c') - v = array_tostring(a, NULL); + v = array_tostring(a); #ifdef Py_USING_UNICODE else if (typecode == 'u') - v = array_tounicode(a, NULL); + v = array_tounicode(a); #endif else - v = array_tolist(a, NULL); + v = array_tolist(a); t = PyObject_Repr(v); Py_XDECREF(v);