Author: Matti Picus <[email protected]>
Branch: cpyext-pickle
Changeset: r84640:7db80cefddb0
Date: 2016-05-23 22:43 +0300
http://bitbucket.org/pypy/pypy/changeset/7db80cefddb0/
Log: replicate numpy failure - use a PyCFunction object in the tuple
returned from __reduce__
diff --git a/pypy/module/cpyext/test/array.c b/pypy/module/cpyext/test/array.c
--- a/pypy/module/cpyext/test/array.c
+++ b/pypy/module/cpyext/test/array.c
@@ -1502,7 +1502,7 @@
static PyObject *
array_reduce(arrayobject *array)
{
- PyObject *dict, *result, *list;
+ PyObject *dict, *result, *list, *mod, *obj;
dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
if (dict == NULL) {
@@ -1512,6 +1512,18 @@
dict = Py_None;
Py_INCREF(dict);
}
+ /* Return a tuple of (callable object, typecode, values, state) */
+ mod = PyImport_ImportModule("array");
+ if (mod == NULL) {
+ Py_DECREF(dict);
+ return NULL;
+ }
+ obj = PyObject_GetAttrString(mod, "_reconstruct");
+ Py_DECREF(mod);
+ if (obj == NULL) {
+ Py_DECREF(dict);
+ return NULL;
+ }
/* Unlike in Python 3.x, we never use the more efficient memory
* representation of an array for pickling. This is unfortunately
* necessary to allow array objects to be unpickled by Python 3.x,
@@ -1524,7 +1536,7 @@
return NULL;
}
result = Py_BuildValue(
- "O(cO)O", Py_TYPE(array), array->ob_descr->typecode, list, dict);
+ "O(cO)O", obj, array->ob_descr->typecode, list, dict);
Py_DECREF(list);
Py_DECREF(dict);
return result;
@@ -1916,6 +1928,11 @@
char c;
PyObject *initial = NULL, *it = NULL;
struct arraydescr *descr;
+ if (type == NULL)
+ {
+ /* when called from _reconstruct */
+ type = &Arraytype;
+ }
if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
return NULL;
@@ -2017,7 +2034,6 @@
return NULL;
}
-
PyDoc_STRVAR(module_doc,
"This module defines an object type which can efficiently represent\n\
an array of basic values: characters, integers, floating point\n\
@@ -2223,6 +2239,7 @@
/* No functions in array module. */
static PyMethodDef a_methods[] = {
+ {"_reconstruct", (PyCFunction)array_new, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL} /* Sentinel */
};
@@ -2244,6 +2261,8 @@
return;
Py_INCREF((PyObject *)&Arraytype);
+ if (PyType_Ready(&Arraytype) < 0)
+ return;
PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
Py_INCREF((PyObject *)&Arraytype);
PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
diff --git a/pypy/module/cpyext/test/test_arraymodule.py
b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -73,5 +73,5 @@
module = self.import_module(name='array')
arr = module.array('i', [1,2,3,4])
s = pickle.dumps(arr)
- rra = pickle.loads(s)
+ rra = pickle.loads(s) # rra is arr backwards
assert arr.tolist() == rra.tolist()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit