Mercurial > cpython-lru_cache-weakref
diff Python/Python-ast.c @ 46212:be18df634405 legacy-trunk
Make AST nodes pickleable.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 30 Mar 2008 20:20:39 +0000 |
| parents | 59d42e6d1885 |
| children | 4fe192d48960 |
line wrap: on
line diff
--- a/Python/Python-ast.c Sun Mar 30 19:43:27 2008 +0000 +++ b/Python/Python-ast.c Sun Mar 30 20:20:39 2008 +0000 @@ -420,9 +420,34 @@ return res; } +/* Pickling support */ +static PyObject * +ast_type_reduce(PyObject *self, PyObject *unused) +{ + PyObject *res; + PyObject *dict = PyObject_GetAttrString(self, "__dict__"); + if (dict == NULL) { + if (PyErr_ExceptionMatches(PyExc_AttributeError)) + PyErr_Clear(); + else + return NULL; + } + if (dict) { + res = Py_BuildValue("O()O", Py_TYPE(self), dict); + Py_DECREF(dict); + return res; + } + return Py_BuildValue("O()", Py_TYPE(self)); +} + +static PyMethodDef ast_type_methods[] = { + {"__reduce__", ast_type_reduce, METH_NOARGS, NULL}, + {NULL} +}; + static PyTypeObject AST_type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "AST", + "_ast.AST", sizeof(PyObject), 0, 0, /* tp_dealloc */ @@ -448,7 +473,7 @@ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + ast_type_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */
