Mercurial > cpython-lru_cache-weakref
diff Modules/arraymodule.c @ 76091:4ff234337e24
Issue #14288: Serialization support for builtin iterators.
| author | Kristján Valur Jónsson <kristjan@ccpgames.com> |
|---|---|
| date | Tue, 03 Apr 2012 10:49:41 +0000 |
| parents | b7c5e30582d1 |
| children | fb907ee4fdfa |
line wrap: on
line diff
--- a/Modules/arraymodule.c Tue Apr 03 09:16:46 2012 +0200 +++ b/Modules/arraymodule.c Tue Apr 03 10:49:41 2012 +0000 @@ -2753,6 +2753,34 @@ return 0; } +static PyObject * +arrayiter_reduce(arrayiterobject *it) +{ + return Py_BuildValue("N(O)n", _PyIter_GetBuiltin("iter"), + it->ao, it->index); +} + +static PyObject * +arrayiter_setstate(arrayiterobject *it, PyObject *state) +{ + Py_ssize_t index = PyLong_AsSsize_t(state); + if (index == -1 && PyErr_Occurred()) + return NULL; + if (index < 0) + index = 0; + it->index = index; + Py_RETURN_NONE; +} + +PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); +static PyMethodDef arrayiter_methods[] = { + {"__reduce__", (PyCFunction)arrayiter_reduce, METH_NOARGS, + reduce_doc}, + {"__setstate__", (PyCFunction)arrayiter_setstate, METH_O, + setstate_doc}, + {NULL, NULL} /* sentinel */ +}; + static PyTypeObject PyArrayIter_Type = { PyVarObject_HEAD_INIT(NULL, 0) "arrayiterator", /* tp_name */ @@ -2782,7 +2810,7 @@ 0, /* tp_weaklistoffset */ PyObject_SelfIter, /* tp_iter */ (iternextfunc)arrayiter_next, /* tp_iternext */ - 0, /* tp_methods */ + arrayiter_methods, /* tp_methods */ };
