Commits

Amaury Forgeot d'Arc committed 4604f1b

Add pickle support to ast objects

Comments (0)

Files changed (3)

pypy/interpreter/astcompiler/ast.py

             self.w_dict = space.newdict(instance=True)
         return self.w_dict
 
+    def reduce_w(self, space):
+        w_dict = self.w_dict
+        if w_dict is None:
+            w_dict = space.newdict()
+        w_type = space.type(self)
+        w_fields = w_type.getdictvalue(space, "_fields")
+        for w_name in space.fixedview(w_fields):
+            space.setitem(w_dict, w_name,
+                          space.getattr(self, w_name))
+        w_attrs = space.findattr(w_type, space.wrap("_attributes"))
+        if w_attrs:
+            for w_name in space.fixedview(w_attrs):
+                space.setitem(w_dict, w_name,
+                              space.getattr(self, w_name))
+        return space.newtuple([space.type(self),
+                               space.newtuple([]),
+                               w_dict])
+
+    def setstate_w(self, space, w_state):
+        for w_name in space.unpackiterable(w_state):
+            space.setattr(self, w_name,
+                          space.getitem(w_state, w_name))
+
 
 class NodeVisitorNotImplemented(Exception):
     pass
 AST.typedef = typedef.TypeDef("AST",
     _fields=_FieldsWrapper([]),
     _attributes=_FieldsWrapper([]),
+    __module__='_ast',
+    __reduce__=interp2app(AST.reduce_w),
+    __setstate__=interp2app(AST.setstate_w),
+    __dict__ = typedef.GetSetProperty(typedef.descr_get_dict,
+                                      typedef.descr_set_dict, cls=AST),
 )
 AST.typedef.acceptable_as_base_class = False
 
 
 mod.typedef = typedef.TypeDef("mod",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 mod.typedef.acceptable_as_base_class = False
 
 Module.typedef = typedef.TypeDef("Module",
     mod.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body']),
     body=typedef.GetSetProperty(Module_get_body, Module_set_body, cls=Module),
     __new__=interp2app(get_AST_new(Module)),
 
 Interactive.typedef = typedef.TypeDef("Interactive",
     mod.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body']),
     body=typedef.GetSetProperty(Interactive_get_body, Interactive_set_body, cls=Interactive),
     __new__=interp2app(get_AST_new(Interactive)),
 
 Expression.typedef = typedef.TypeDef("Expression",
     mod.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body']),
     body=typedef.GetSetProperty(Expression_get_body, Expression_set_body, cls=Expression),
     __new__=interp2app(get_AST_new(Expression)),
 
 Suite.typedef = typedef.TypeDef("Suite",
     mod.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body']),
     body=typedef.GetSetProperty(Suite_get_body, Suite_set_body, cls=Suite),
     __new__=interp2app(get_AST_new(Suite)),
 
 stmt.typedef = typedef.TypeDef("stmt",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper(['lineno', 'col_offset']),
     lineno=typedef.GetSetProperty(stmt_get_lineno, stmt_set_lineno, cls=stmt),
     col_offset=typedef.GetSetProperty(stmt_get_col_offset, stmt_set_col_offset, cls=stmt),
 
 FunctionDef.typedef = typedef.TypeDef("FunctionDef",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['name', 'args', 'body', 'decorator_list']),
     name=typedef.GetSetProperty(FunctionDef_get_name, FunctionDef_set_name, cls=FunctionDef),
     args=typedef.GetSetProperty(FunctionDef_get_args, FunctionDef_set_args, cls=FunctionDef),
 
 ClassDef.typedef = typedef.TypeDef("ClassDef",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['name', 'bases', 'body', 'decorator_list']),
     name=typedef.GetSetProperty(ClassDef_get_name, ClassDef_set_name, cls=ClassDef),
     bases=typedef.GetSetProperty(ClassDef_get_bases, ClassDef_set_bases, cls=ClassDef),
 
 Return.typedef = typedef.TypeDef("Return",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Return_get_value, Return_set_value, cls=Return),
     __new__=interp2app(get_AST_new(Return)),
 
 Delete.typedef = typedef.TypeDef("Delete",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['targets']),
     targets=typedef.GetSetProperty(Delete_get_targets, Delete_set_targets, cls=Delete),
     __new__=interp2app(get_AST_new(Delete)),
 
 Assign.typedef = typedef.TypeDef("Assign",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['targets', 'value']),
     targets=typedef.GetSetProperty(Assign_get_targets, Assign_set_targets, cls=Assign),
     value=typedef.GetSetProperty(Assign_get_value, Assign_set_value, cls=Assign),
 
 AugAssign.typedef = typedef.TypeDef("AugAssign",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['target', 'op', 'value']),
     target=typedef.GetSetProperty(AugAssign_get_target, AugAssign_set_target, cls=AugAssign),
     op=typedef.GetSetProperty(AugAssign_get_op, AugAssign_set_op, cls=AugAssign),
 
 Print.typedef = typedef.TypeDef("Print",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['dest', 'values', 'nl']),
     dest=typedef.GetSetProperty(Print_get_dest, Print_set_dest, cls=Print),
     values=typedef.GetSetProperty(Print_get_values, Print_set_values, cls=Print),
 
 For.typedef = typedef.TypeDef("For",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['target', 'iter', 'body', 'orelse']),
     target=typedef.GetSetProperty(For_get_target, For_set_target, cls=For),
     iter=typedef.GetSetProperty(For_get_iter, For_set_iter, cls=For),
 
 While.typedef = typedef.TypeDef("While",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['test', 'body', 'orelse']),
     test=typedef.GetSetProperty(While_get_test, While_set_test, cls=While),
     body=typedef.GetSetProperty(While_get_body, While_set_body, cls=While),
 
 If.typedef = typedef.TypeDef("If",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['test', 'body', 'orelse']),
     test=typedef.GetSetProperty(If_get_test, If_set_test, cls=If),
     body=typedef.GetSetProperty(If_get_body, If_set_body, cls=If),
 
 With.typedef = typedef.TypeDef("With",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['context_expr', 'optional_vars', 'body']),
     context_expr=typedef.GetSetProperty(With_get_context_expr, With_set_context_expr, cls=With),
     optional_vars=typedef.GetSetProperty(With_get_optional_vars, With_set_optional_vars, cls=With),
 
 Raise.typedef = typedef.TypeDef("Raise",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['type', 'inst', 'tback']),
     type=typedef.GetSetProperty(Raise_get_type, Raise_set_type, cls=Raise),
     inst=typedef.GetSetProperty(Raise_get_inst, Raise_set_inst, cls=Raise),
 
 TryExcept.typedef = typedef.TypeDef("TryExcept",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body', 'handlers', 'orelse']),
     body=typedef.GetSetProperty(TryExcept_get_body, TryExcept_set_body, cls=TryExcept),
     handlers=typedef.GetSetProperty(TryExcept_get_handlers, TryExcept_set_handlers, cls=TryExcept),
 
 TryFinally.typedef = typedef.TypeDef("TryFinally",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body', 'finalbody']),
     body=typedef.GetSetProperty(TryFinally_get_body, TryFinally_set_body, cls=TryFinally),
     finalbody=typedef.GetSetProperty(TryFinally_get_finalbody, TryFinally_set_finalbody, cls=TryFinally),
 
 Assert.typedef = typedef.TypeDef("Assert",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['test', 'msg']),
     test=typedef.GetSetProperty(Assert_get_test, Assert_set_test, cls=Assert),
     msg=typedef.GetSetProperty(Assert_get_msg, Assert_set_msg, cls=Assert),
 
 Import.typedef = typedef.TypeDef("Import",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['names']),
     names=typedef.GetSetProperty(Import_get_names, Import_set_names, cls=Import),
     __new__=interp2app(get_AST_new(Import)),
 
 ImportFrom.typedef = typedef.TypeDef("ImportFrom",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['module', 'names', 'level']),
     module=typedef.GetSetProperty(ImportFrom_get_module, ImportFrom_set_module, cls=ImportFrom),
     names=typedef.GetSetProperty(ImportFrom_get_names, ImportFrom_set_names, cls=ImportFrom),
 
 Exec.typedef = typedef.TypeDef("Exec",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['body', 'globals', 'locals']),
     body=typedef.GetSetProperty(Exec_get_body, Exec_set_body, cls=Exec),
     globals=typedef.GetSetProperty(Exec_get_globals, Exec_set_globals, cls=Exec),
 
 Global.typedef = typedef.TypeDef("Global",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['names']),
     names=typedef.GetSetProperty(Global_get_names, Global_set_names, cls=Global),
     __new__=interp2app(get_AST_new(Global)),
 
 Expr.typedef = typedef.TypeDef("Expr",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Expr_get_value, Expr_set_value, cls=Expr),
     __new__=interp2app(get_AST_new(Expr)),
 
 Pass.typedef = typedef.TypeDef("Pass",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(Pass)),
     __init__=interp2app(Pass_init),
 
 Break.typedef = typedef.TypeDef("Break",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(Break)),
     __init__=interp2app(Break_init),
 
 Continue.typedef = typedef.TypeDef("Continue",
     stmt.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(Continue)),
     __init__=interp2app(Continue_init),
 
 expr.typedef = typedef.TypeDef("expr",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper(['lineno', 'col_offset']),
     lineno=typedef.GetSetProperty(expr_get_lineno, expr_set_lineno, cls=expr),
     col_offset=typedef.GetSetProperty(expr_get_col_offset, expr_set_col_offset, cls=expr),
 
 BoolOp.typedef = typedef.TypeDef("BoolOp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['op', 'values']),
     op=typedef.GetSetProperty(BoolOp_get_op, BoolOp_set_op, cls=BoolOp),
     values=typedef.GetSetProperty(BoolOp_get_values, BoolOp_set_values, cls=BoolOp),
 
 BinOp.typedef = typedef.TypeDef("BinOp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['left', 'op', 'right']),
     left=typedef.GetSetProperty(BinOp_get_left, BinOp_set_left, cls=BinOp),
     op=typedef.GetSetProperty(BinOp_get_op, BinOp_set_op, cls=BinOp),
 
 UnaryOp.typedef = typedef.TypeDef("UnaryOp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['op', 'operand']),
     op=typedef.GetSetProperty(UnaryOp_get_op, UnaryOp_set_op, cls=UnaryOp),
     operand=typedef.GetSetProperty(UnaryOp_get_operand, UnaryOp_set_operand, cls=UnaryOp),
 
 Lambda.typedef = typedef.TypeDef("Lambda",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['args', 'body']),
     args=typedef.GetSetProperty(Lambda_get_args, Lambda_set_args, cls=Lambda),
     body=typedef.GetSetProperty(Lambda_get_body, Lambda_set_body, cls=Lambda),
 
 IfExp.typedef = typedef.TypeDef("IfExp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['test', 'body', 'orelse']),
     test=typedef.GetSetProperty(IfExp_get_test, IfExp_set_test, cls=IfExp),
     body=typedef.GetSetProperty(IfExp_get_body, IfExp_set_body, cls=IfExp),
 
 Dict.typedef = typedef.TypeDef("Dict",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['keys', 'values']),
     keys=typedef.GetSetProperty(Dict_get_keys, Dict_set_keys, cls=Dict),
     values=typedef.GetSetProperty(Dict_get_values, Dict_set_values, cls=Dict),
 
 Set.typedef = typedef.TypeDef("Set",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elts']),
     elts=typedef.GetSetProperty(Set_get_elts, Set_set_elts, cls=Set),
     __new__=interp2app(get_AST_new(Set)),
 
 ListComp.typedef = typedef.TypeDef("ListComp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elt', 'generators']),
     elt=typedef.GetSetProperty(ListComp_get_elt, ListComp_set_elt, cls=ListComp),
     generators=typedef.GetSetProperty(ListComp_get_generators, ListComp_set_generators, cls=ListComp),
 
 SetComp.typedef = typedef.TypeDef("SetComp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elt', 'generators']),
     elt=typedef.GetSetProperty(SetComp_get_elt, SetComp_set_elt, cls=SetComp),
     generators=typedef.GetSetProperty(SetComp_get_generators, SetComp_set_generators, cls=SetComp),
 
 DictComp.typedef = typedef.TypeDef("DictComp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['key', 'value', 'generators']),
     key=typedef.GetSetProperty(DictComp_get_key, DictComp_set_key, cls=DictComp),
     value=typedef.GetSetProperty(DictComp_get_value, DictComp_set_value, cls=DictComp),
 
 GeneratorExp.typedef = typedef.TypeDef("GeneratorExp",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elt', 'generators']),
     elt=typedef.GetSetProperty(GeneratorExp_get_elt, GeneratorExp_set_elt, cls=GeneratorExp),
     generators=typedef.GetSetProperty(GeneratorExp_get_generators, GeneratorExp_set_generators, cls=GeneratorExp),
 
 Yield.typedef = typedef.TypeDef("Yield",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Yield_get_value, Yield_set_value, cls=Yield),
     __new__=interp2app(get_AST_new(Yield)),
 
 Compare.typedef = typedef.TypeDef("Compare",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['left', 'ops', 'comparators']),
     left=typedef.GetSetProperty(Compare_get_left, Compare_set_left, cls=Compare),
     ops=typedef.GetSetProperty(Compare_get_ops, Compare_set_ops, cls=Compare),
 
 Call.typedef = typedef.TypeDef("Call",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['func', 'args', 'keywords', 'starargs', 'kwargs']),
     func=typedef.GetSetProperty(Call_get_func, Call_set_func, cls=Call),
     args=typedef.GetSetProperty(Call_get_args, Call_set_args, cls=Call),
 
 Repr.typedef = typedef.TypeDef("Repr",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Repr_get_value, Repr_set_value, cls=Repr),
     __new__=interp2app(get_AST_new(Repr)),
 
 Num.typedef = typedef.TypeDef("Num",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['n']),
     n=typedef.GetSetProperty(Num_get_n, Num_set_n, cls=Num),
     __new__=interp2app(get_AST_new(Num)),
 
 Str.typedef = typedef.TypeDef("Str",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['s']),
     s=typedef.GetSetProperty(Str_get_s, Str_set_s, cls=Str),
     __new__=interp2app(get_AST_new(Str)),
 
 Attribute.typedef = typedef.TypeDef("Attribute",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value', 'attr', 'ctx']),
     value=typedef.GetSetProperty(Attribute_get_value, Attribute_set_value, cls=Attribute),
     attr=typedef.GetSetProperty(Attribute_get_attr, Attribute_set_attr, cls=Attribute),
 
 Subscript.typedef = typedef.TypeDef("Subscript",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value', 'slice', 'ctx']),
     value=typedef.GetSetProperty(Subscript_get_value, Subscript_set_value, cls=Subscript),
     slice=typedef.GetSetProperty(Subscript_get_slice, Subscript_set_slice, cls=Subscript),
 
 Name.typedef = typedef.TypeDef("Name",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['id', 'ctx']),
     id=typedef.GetSetProperty(Name_get_id, Name_set_id, cls=Name),
     ctx=typedef.GetSetProperty(Name_get_ctx, Name_set_ctx, cls=Name),
 
 List.typedef = typedef.TypeDef("List",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elts', 'ctx']),
     elts=typedef.GetSetProperty(List_get_elts, List_set_elts, cls=List),
     ctx=typedef.GetSetProperty(List_get_ctx, List_set_ctx, cls=List),
 
 Tuple.typedef = typedef.TypeDef("Tuple",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['elts', 'ctx']),
     elts=typedef.GetSetProperty(Tuple_get_elts, Tuple_set_elts, cls=Tuple),
     ctx=typedef.GetSetProperty(Tuple_get_ctx, Tuple_set_ctx, cls=Tuple),
 
 Const.typedef = typedef.TypeDef("Const",
     expr.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Const_get_value, Const_set_value, cls=Const),
     __new__=interp2app(get_AST_new(Const)),
 
 expr_context.typedef = typedef.TypeDef("expr_context",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 expr_context.typedef.acceptable_as_base_class = False
 
 _Load.typedef = typedef.TypeDef("Load",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Load)),
 )
 
 _Store.typedef = typedef.TypeDef("Store",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Store)),
 )
 
 _Del.typedef = typedef.TypeDef("Del",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Del)),
 )
 
 _AugLoad.typedef = typedef.TypeDef("AugLoad",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_AugLoad)),
 )
 
 _AugStore.typedef = typedef.TypeDef("AugStore",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_AugStore)),
 )
 
 _Param.typedef = typedef.TypeDef("Param",
     expr_context.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Param)),
 )
 
 slice.typedef = typedef.TypeDef("slice",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 slice.typedef.acceptable_as_base_class = False
 
 Ellipsis.typedef = typedef.TypeDef("Ellipsis",
     slice.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(Ellipsis)),
     __init__=interp2app(Ellipsis_init),
 
 Slice.typedef = typedef.TypeDef("Slice",
     slice.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['lower', 'upper', 'step']),
     lower=typedef.GetSetProperty(Slice_get_lower, Slice_set_lower, cls=Slice),
     upper=typedef.GetSetProperty(Slice_get_upper, Slice_set_upper, cls=Slice),
 
 ExtSlice.typedef = typedef.TypeDef("ExtSlice",
     slice.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['dims']),
     dims=typedef.GetSetProperty(ExtSlice_get_dims, ExtSlice_set_dims, cls=ExtSlice),
     __new__=interp2app(get_AST_new(ExtSlice)),
 
 Index.typedef = typedef.TypeDef("Index",
     slice.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['value']),
     value=typedef.GetSetProperty(Index_get_value, Index_set_value, cls=Index),
     __new__=interp2app(get_AST_new(Index)),
 
 boolop.typedef = typedef.TypeDef("boolop",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 boolop.typedef.acceptable_as_base_class = False
 
 _And.typedef = typedef.TypeDef("And",
     boolop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_And)),
 )
 
 _Or.typedef = typedef.TypeDef("Or",
     boolop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Or)),
 )
 
 operator.typedef = typedef.TypeDef("operator",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 operator.typedef.acceptable_as_base_class = False
 
 _Add.typedef = typedef.TypeDef("Add",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Add)),
 )
 
 _Sub.typedef = typedef.TypeDef("Sub",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Sub)),
 )
 
 _Mult.typedef = typedef.TypeDef("Mult",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Mult)),
 )
 
 _Div.typedef = typedef.TypeDef("Div",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Div)),
 )
 
 _Mod.typedef = typedef.TypeDef("Mod",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Mod)),
 )
 
 _Pow.typedef = typedef.TypeDef("Pow",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Pow)),
 )
 
 _LShift.typedef = typedef.TypeDef("LShift",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_LShift)),
 )
 
 _RShift.typedef = typedef.TypeDef("RShift",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_RShift)),
 )
 
 _BitOr.typedef = typedef.TypeDef("BitOr",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_BitOr)),
 )
 
 _BitXor.typedef = typedef.TypeDef("BitXor",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_BitXor)),
 )
 
 _BitAnd.typedef = typedef.TypeDef("BitAnd",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_BitAnd)),
 )
 
 _FloorDiv.typedef = typedef.TypeDef("FloorDiv",
     operator.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_FloorDiv)),
 )
 
 unaryop.typedef = typedef.TypeDef("unaryop",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 unaryop.typedef.acceptable_as_base_class = False
 
 _Invert.typedef = typedef.TypeDef("Invert",
     unaryop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Invert)),
 )
 
 _Not.typedef = typedef.TypeDef("Not",
     unaryop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Not)),
 )
 
 _UAdd.typedef = typedef.TypeDef("UAdd",
     unaryop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_UAdd)),
 )
 
 _USub.typedef = typedef.TypeDef("USub",
     unaryop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_USub)),
 )
 
 cmpop.typedef = typedef.TypeDef("cmpop",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper([]),
 )
 cmpop.typedef.acceptable_as_base_class = False
 
 _Eq.typedef = typedef.TypeDef("Eq",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Eq)),
 )
 
 _NotEq.typedef = typedef.TypeDef("NotEq",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_NotEq)),
 )
 
 _Lt.typedef = typedef.TypeDef("Lt",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Lt)),
 )
 
 _LtE.typedef = typedef.TypeDef("LtE",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_LtE)),
 )
 
 _Gt.typedef = typedef.TypeDef("Gt",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Gt)),
 )
 
 _GtE.typedef = typedef.TypeDef("GtE",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_GtE)),
 )
 
 _Is.typedef = typedef.TypeDef("Is",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_Is)),
 )
 
 _IsNot.typedef = typedef.TypeDef("IsNot",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_IsNot)),
 )
 
 _In.typedef = typedef.TypeDef("In",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_In)),
 )
 
 _NotIn.typedef = typedef.TypeDef("NotIn",
     cmpop.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper([]),
     __new__=interp2app(get_AST_new(_NotIn)),
 )
 
 comprehension.typedef = typedef.TypeDef("comprehension",
     AST.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['target', 'iter', 'ifs']),
     target=typedef.GetSetProperty(comprehension_get_target, comprehension_set_target, cls=comprehension),
     iter=typedef.GetSetProperty(comprehension_get_iter, comprehension_set_iter, cls=comprehension),
 
 excepthandler.typedef = typedef.TypeDef("excepthandler",
     AST.typedef,
+    __module__='_ast',
     _attributes=_FieldsWrapper(['lineno', 'col_offset']),
     lineno=typedef.GetSetProperty(excepthandler_get_lineno, excepthandler_set_lineno, cls=excepthandler),
     col_offset=typedef.GetSetProperty(excepthandler_get_col_offset, excepthandler_set_col_offset, cls=excepthandler),
 
 ExceptHandler.typedef = typedef.TypeDef("ExceptHandler",
     excepthandler.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['type', 'name', 'body']),
     type=typedef.GetSetProperty(ExceptHandler_get_type, ExceptHandler_set_type, cls=ExceptHandler),
     name=typedef.GetSetProperty(ExceptHandler_get_name, ExceptHandler_set_name, cls=ExceptHandler),
 
 arguments.typedef = typedef.TypeDef("arguments",
     AST.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['args', 'vararg', 'kwarg', 'defaults']),
     args=typedef.GetSetProperty(arguments_get_args, arguments_set_args, cls=arguments),
     vararg=typedef.GetSetProperty(arguments_get_vararg, arguments_set_vararg, cls=arguments),
 
 keyword.typedef = typedef.TypeDef("keyword",
     AST.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['arg', 'value']),
     arg=typedef.GetSetProperty(keyword_get_arg, keyword_set_arg, cls=keyword),
     value=typedef.GetSetProperty(keyword_get_value, keyword_set_value, cls=keyword),
 
 alias.typedef = typedef.TypeDef("alias",
     AST.typedef,
+    __module__='_ast',
     _fields=_FieldsWrapper(['name', 'asname']),
     name=typedef.GetSetProperty(alias_get_name, alias_set_name, cls=alias),
     asname=typedef.GetSetProperty(alias_get_asname, alias_set_asname, cls=alias),

pypy/interpreter/astcompiler/tools/asdl_py.py

             display_name = name
         self.emit("%s.typedef = typedef.TypeDef(\"%s\"," % (name, display_name))
         self.emit("%s.typedef," % (base,), 1)
+        self.emit("__module__='_ast',", 1)
         comma_fields = ", ".join(repr(field.name.value) for field in fields)
         self.emit("%s=_FieldsWrapper([%s])," % (fields_name, comma_fields), 1)
         for field in fields:
             self.w_dict = space.newdict(instance=True)
         return self.w_dict
 
+    def reduce_w(self, space):
+        w_dict = self.w_dict
+        if w_dict is None:
+            w_dict = space.newdict()
+        w_type = space.type(self)
+        w_fields = w_type.getdictvalue(space, "_fields")
+        for w_name in space.fixedview(w_fields):
+            space.setitem(w_dict, w_name,
+                          space.getattr(self, w_name))
+        w_attrs = space.findattr(w_type, space.wrap("_attributes"))
+        if w_attrs:
+            for w_name in space.fixedview(w_attrs):
+                space.setitem(w_dict, w_name,
+                              space.getattr(self, w_name))
+        return space.newtuple([space.type(self),
+                               space.newtuple([]),
+                               w_dict])
+
+    def setstate_w(self, space, w_state):
+        for w_name in space.unpackiterable(w_state):
+            space.setattr(self, w_name,
+                          space.getitem(w_state, w_name))
+
 
 class NodeVisitorNotImplemented(Exception):
     pass
 AST.typedef = typedef.TypeDef("AST",
     _fields=_FieldsWrapper([]),
     _attributes=_FieldsWrapper([]),
+    __module__='_ast',
+    __reduce__=interp2app(AST.reduce_w),
+    __setstate__=interp2app(AST.setstate_w),
+    __dict__ = typedef.GetSetProperty(typedef.descr_get_dict,
+                                      typedef.descr_set_dict, cls=AST),
 )
 AST.typedef.acceptable_as_base_class = False
 

pypy/module/_ast/test/test_ast.py

         assert x._fields == 666
 
     def test_pickle(self):
-        skip("XXX implement me")
         import pickle
         mod = self.get_ast("if y: x = 4")
         co = compile(mod, "<example>", "exec")