[Python-checkins] r62059 - in python/trunk: Lib/test/test_ast.py Parser/asdl_c.py Python/Python-ast.c
georg.brandl
python-checkins at python.org
Sun Mar 30 22:20:40 CEST 2008
Author: georg.brandl
Date: Sun Mar 30 22:20:39 2008
New Revision: 62059
Modified:
python/trunk/Lib/test/test_ast.py
python/trunk/Parser/asdl_c.py
python/trunk/Python/Python-ast.c
Log:
Make AST nodes pickleable.
Modified: python/trunk/Lib/test/test_ast.py
==============================================================================
--- python/trunk/Lib/test/test_ast.py (original)
+++ python/trunk/Lib/test/test_ast.py Sun Mar 30 22:20:39 2008
@@ -166,6 +166,20 @@
# this used to fail because Sub._fields was None
x = _ast.Sub()
+ def test_pickling(self):
+ import pickle
+ mods = [pickle]
+ try:
+ import cPickle
+ mods.append(cPickle)
+ except ImportError:
+ pass
+ protocols = [0, 1, 2]
+ for mod in mods:
+ for protocol in protocols:
+ for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
+ ast2 = mod.loads(mod.dumps(ast, protocol))
+ self.assertEquals(to_tuple(ast2), to_tuple(ast))
def test_main():
test_support.run_unittest(AST_Tests)
Modified: python/trunk/Parser/asdl_c.py
==============================================================================
--- python/trunk/Parser/asdl_c.py (original)
+++ python/trunk/Parser/asdl_c.py Sun Mar 30 22:20:39 2008
@@ -629,9 +629,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 */
@@ -657,7 +682,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 */
Modified: python/trunk/Python/Python-ast.c
==============================================================================
--- python/trunk/Python/Python-ast.c (original)
+++ python/trunk/Python/Python-ast.c Sun Mar 30 22:20:39 2008
@@ -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 */
More information about the Python-checkins
mailing list