@@ -2598,32 +2598,67 @@ validate_file_input(tree)
25982598} /* validate_file_input() */
25992599
26002600
2601+ static PyObject *
2602+ pickle_constructor = NULL ;
2603+
2604+
2605+ static PyObject *
2606+ parser__pickler (self , args )
2607+ PyObject * self ;
2608+ PyObject * args ;
2609+ {
2610+ PyObject * result = NULL ;
2611+ PyObject * ast = NULL ;
2612+
2613+ if (PyArg_ParseTuple (args , "O!:_pickler" , & PyAST_Type , & ast )) {
2614+ PyObject * newargs ;
2615+ PyObject * tuple ;
2616+
2617+ if ((newargs = Py_BuildValue ("Oi" , ast , 1 )) == NULL )
2618+ goto finally ;
2619+ tuple = parser_ast2tuple (NULL , newargs );
2620+ if (tuple != NULL ) {
2621+ result = Py_BuildValue ("O(O)" , pickle_constructor , tuple );
2622+ Py_DECREF (tuple );
2623+ }
2624+ Py_XDECREF (newargs );
2625+ }
2626+ finally :
2627+ return (result );
2628+
2629+ } /* parser__pickler() */
2630+
2631+
26012632/* Functions exported by this module. Most of this should probably
26022633 * be converted into an AST object with methods, but that is better
26032634 * done directly in Python, allowing subclasses to be created directly.
26042635 * We'd really have to write a wrapper around it all anyway to allow
26052636 * inheritance.
26062637 */
26072638static PyMethodDef parser_functions [] = {
2608- {"ast2tuple" , parser_ast2tuple , 1 ,
2639+ {"ast2tuple" , parser_ast2tuple , METH_VARARGS ,
26092640 "Creates a tuple-tree representation of an AST." },
2610- {"ast2list" , parser_ast2list , 1 ,
2641+ {"ast2list" , parser_ast2list , METH_VARARGS ,
26112642 "Creates a list-tree representation of an AST." },
2612- {"compileast" , parser_compileast , 1 ,
2643+ {"compileast" , parser_compileast , METH_VARARGS ,
26132644 "Compiles an AST object into a code object." },
2614- {"expr" , parser_expr , 1 ,
2645+ {"expr" , parser_expr , METH_VARARGS ,
26152646 "Creates an AST object from an expression." },
2616- {"isexpr" , parser_isexpr , 1 ,
2647+ {"isexpr" , parser_isexpr , METH_VARARGS ,
26172648 "Determines if an AST object was created from an expression." },
2618- {"issuite" , parser_issuite , 1 ,
2649+ {"issuite" , parser_issuite , METH_VARARGS ,
26192650 "Determines if an AST object was created from a suite." },
2620- {"suite" , parser_suite , 1 ,
2651+ {"suite" , parser_suite , METH_VARARGS ,
26212652 "Creates an AST object from a suite." },
2622- {"sequence2ast" , parser_tuple2ast , 1 ,
2653+ {"sequence2ast" , parser_tuple2ast , METH_VARARGS ,
26232654 "Creates an AST object from a tree representation." },
2624- {"tuple2ast" , parser_tuple2ast , 1 ,
2655+ {"tuple2ast" , parser_tuple2ast , METH_VARARGS ,
26252656 "Creates an AST object from a tree representation." },
26262657
2658+ /* private stuff: support pickle module */
2659+ {"_pickler" , parser__pickler , METH_VARARGS ,
2660+ "Returns the pickle magic to allow ast objects to be pickled." },
2661+
26272662 {0 , 0 , 0 }
26282663 };
26292664
@@ -2633,6 +2668,7 @@ initparser()
26332668 {
26342669 PyObject * module ;
26352670 PyObject * dict ;
2671+ PyObject * temp ;
26362672
26372673 PyAST_Type .ob_type = & PyType_Type ;
26382674 module = Py_InitModule ("parser" , parser_functions );
@@ -2660,9 +2696,24 @@ initparser()
26602696 PyDict_SetItemString (dict , "__version__" ,
26612697 PyString_FromString (parser_version_string ));
26622698
2699+ /* register to support pickling */
2700+ module = PyImport_ImportModule ("copy_reg" );
2701+ if (module != NULL ) {
2702+ PyObject * func , * constructor , * pickler ;
2703+
2704+ func = PyObject_GetAttrString (module , "pickle" );
2705+ pickle_constructor = PyDict_GetItemString (dict , "sequence2ast" );
2706+ pickler = PyDict_GetItemString (dict , "_pickler" );
2707+ Py_XINCREF (pickle_constructor );
2708+ if ((func != NULL ) && (pickle_constructor != NULL )
2709+ && (pickler != NULL )) {
2710+ PyObject * res ;
2711+
2712+ res = PyObject_CallFunction (
2713+ func , "OOO" , & PyAST_Type , pickler , pickle_constructor );
2714+ Py_XDECREF (res );
2715+ }
2716+ Py_XDECREF (func );
2717+ Py_DECREF (module );
2718+ }
26632719} /* initparser() */
2664-
2665-
2666- /*
2667- * end of parsermodule.c
2668- */
0 commit comments