Make built-in methods picklable through the reduce protocol.

This commit is contained in:
Alexandre Vassalotti 2013-11-24 02:41:05 -08:00
parent d606ba7f55
commit 4c05d3bc56
3 changed files with 22 additions and 41 deletions

View file

@ -3513,34 +3513,6 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
return 0;
}
static int
save_method(PicklerObject *self, PyObject *obj)
{
PyObject *method_self = PyCFunction_GET_SELF(obj);
if (method_self == NULL || PyModule_Check(method_self)) {
return save_global(self, obj, NULL);
}
else {
PyObject *builtins;
PyObject *getattr;
PyObject *reduce_value;
int status = -1;
_Py_IDENTIFIER(getattr);
builtins = PyEval_GetBuiltins();
getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
reduce_value = \
Py_BuildValue("O(Os)", getattr, method_self,
((PyCFunctionObject *)obj)->m_ml->ml_name);
if (reduce_value != NULL) {
status = save_reduce(self, reduce_value, obj);
Py_DECREF(reduce_value);
}
return status;
}
}
static int
save(PicklerObject *self, PyObject *obj, int pers_save)
{
@ -3652,10 +3624,6 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
goto done;
}
}
else if (type == &PyCFunction_Type) {
status = save_method(self, obj);
goto done;
}
/* XXX: This part needs some unit tests. */