diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2013-11-24 10:41:05 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2013-11-24 10:41:05 (GMT) |
commit | 4c05d3bc561dcdd70dd0d268fa769197334349a8 (patch) | |
tree | cb64c1535b23aa2904108d62f67a769ec415b974 /Objects/methodobject.c | |
parent | d606ba7f5579a152d8826e10cdc528af2a5c0e10 (diff) | |
download | cpython-4c05d3bc561dcdd70dd0d268fa769197334349a8.zip cpython-4c05d3bc561dcdd70dd0d268fa769197334349a8.tar.gz cpython-4c05d3bc561dcdd70dd0d268fa769197334349a8.tar.bz2 |
Make built-in methods picklable through the reduce protocol.
Diffstat (limited to 'Objects/methodobject.c')
-rw-r--r-- | Objects/methodobject.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/Objects/methodobject.c b/Objects/methodobject.c index ca21a68..55a7d6a 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -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 */ |