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

@ -159,6 +159,26 @@ meth_dealloc(PyCFunctionObject *m)
}
}
static PyObject *
meth_reduce(PyCFunctionObject *m)
{
PyObject *builtins;
PyObject *getattr;
_Py_IDENTIFIER(getattr);
if (m->m_self == NULL || PyModule_Check(m->m_self))
return PyUnicode_FromString(m->m_ml->ml_name);
builtins = PyEval_GetBuiltins();
getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
return Py_BuildValue("O(Os)", getattr, m->m_self, m->m_ml->ml_name);
}
static PyMethodDef meth_methods[] = {
{"__reduce__", (PyCFunction)meth_reduce, METH_NOARGS, NULL},
{NULL, NULL}
};
/*
* finds the docstring's introspection signature.
* if present, returns a pointer pointing to the first '('.
@ -394,7 +414,7 @@ PyTypeObject PyCFunction_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
meth_methods, /* tp_methods */
meth_members, /* tp_members */
meth_getsets, /* tp_getset */
0, /* tp_base */