From 22872a09781e0e855ac9de682dd261c617459386 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 16 Dec 2004 16:23:40 +0000 Subject: SF #1085304: Make array.array pickle-able --- Modules/arraymodule.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Modules/arraymodule.c') diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 7ed3b73d53..6430333001 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1132,6 +1132,29 @@ PyDoc_STRVAR(byteswap_doc, Byteswap all items of the array. If the items in the array are not 1, 2,\n\ 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); + } + result = Py_BuildValue("O(cs#)O", + array->ob_type, + array->ob_descr->typecode, + array->ob_item, + array->ob_size * array->ob_descr->itemsize, + dict); + Py_DECREF(dict); + return result; +} + +PyDoc_STRVAR(array_doc, "Return state information for pickling."); + static PyObject * array_reverse(arrayobject *self, PyObject *unused) { @@ -1490,6 +1513,8 @@ PyMethodDef array_methods[] = { pop_doc}, {"read", (PyCFunction)array_fromfile, METH_VARARGS, fromfile_doc}, + {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS, + array_doc}, {"remove", (PyCFunction)array_remove, METH_O, remove_doc}, {"reverse", (PyCFunction)array_reverse, METH_NOARGS, -- cgit v1.2.1