Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r72889:16d07ec7276a Date: 2014-08-18 16:09 +0200 http://bitbucket.org/pypy/pypy/changeset/16d07ec7276a/
Log: merge heads diff too long, truncating to 2000 out of 10269 lines diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst --- a/pypy/doc/whatsnew-head.rst +++ b/pypy/doc/whatsnew-head.rst @@ -54,3 +54,6 @@ .. branch: pytest-25 Update our copies of py.test and pylib to versions 2.5.2 and 1.4.20, respectively. + +.. branch: split-ast-classes +Classes in the ast module are now distinct from structures used by the compiler. diff --git a/pypy/interpreter/astcompiler/ast.py b/pypy/interpreter/astcompiler/ast.py --- a/pypy/interpreter/astcompiler/ast.py +++ b/pypy/interpreter/astcompiler/ast.py @@ -1,5 +1,4 @@ # Generated by tools/asdl_py.py -from rpython.rlib.unroll import unrolling_iterable from rpython.tool.pairtype import extendabletype from rpython.tool.sourcetools import func_with_new_name @@ -21,11 +20,15 @@ 'AST string must be of type str or unicode')) return w_obj - -class AST(W_Root): - - w_dict = None - +def get_field(space, w_node, name, optional): + w_obj = w_node.getdictvalue(space, name) + if w_obj is None and not optional: + raise oefmt(space.w_TypeError, + "required field \"%s\" missing from %T", name, w_node) + return w_obj + + +class AST(object): __metaclass__ = extendabletype def walkabout(self, visitor): @@ -34,8 +37,23 @@ def mutate_over(self, visitor): raise AssertionError("mutate_over() implementation not provided") - def sync_app_attrs(self, space): - raise NotImplementedError + +class NodeVisitorNotImplemented(Exception): + pass + + +class _FieldsWrapper(W_Root): + "Hack around the fact we can't store tuples on a TypeDef." + + def __init__(self, fields): + self.fields = fields + + def __spacebind__(self, space): + return space.newtuple([space.wrap(field) for field in self.fields]) + + +class W_AST(W_Root): + w_dict = None def getdict(self, space): if self.w_dict is None: @@ -47,7 +65,7 @@ if w_dict is None: w_dict = space.newdict() w_type = space.type(self) - w_fields = w_type.getdictvalue(space, "_fields") + w_fields = space.getattr(w_type, space.wrap("_fields")) for w_name in space.fixedview(w_fields): try: space.setitem(w_dict, w_name, @@ -71,79 +89,94 @@ space.setattr(self, w_name, space.getitem(w_state, w_name)) - def missing_field(self, space, required, host): - "Find which required field is missing." - state = self.initialization_state - for i in range(len(required)): - if (state >> i) & 1: - continue # field is present - missing = required[i] - if missing is None: - continue # field is optional - w_obj = self.getdictvalue(space, missing) - if w_obj is None: - raise oefmt(space.w_TypeError, - "required field \"%s\" missing from %s", - missing, host) - else: - raise oefmt(space.w_TypeError, - "incorrect type for field \"%s\" in %s", - missing, host) - raise AssertionError("should not reach here") - - -class NodeVisitorNotImplemented(Exception): - pass - - -class _FieldsWrapper(W_Root): - "Hack around the fact we can't store tuples on a TypeDef." - - def __init__(self, fields): - self.fields = fields - - def __spacebind__(self, space): - return space.newtuple([space.wrap(field) for field in self.fields]) - - -def get_AST_new(node_class): - def generic_AST_new(space, w_type, __args__): - node = space.allocate_instance(node_class, w_type) - node.initialization_state = 0 - return space.wrap(node) - return func_with_new_name(generic_AST_new, "new_%s" % node_class.__name__) - -def AST_init(space, w_self, __args__): +def W_AST_new(space, w_type, __args__): + node = space.allocate_instance(W_AST, w_type) + return space.wrap(node) + +def W_AST_init(space, w_self, __args__): args_w, kwargs_w = __args__.unpack() - if args_w and len(args_w) != 0: - w_err = space.wrap("_ast.AST constructor takes 0 positional arguments") - raise OperationError(space.w_TypeError, w_err) + fields_w = space.fixedview(space.getattr(space.type(w_self), + space.wrap("_fields"))) + num_fields = len(fields_w) if fields_w else 0 + if args_w and len(args_w) != num_fields: + if num_fields == 0: + raise oefmt(space.w_TypeError, + "%T constructor takes 0 positional arguments", w_self) + elif num_fields == 1: + raise oefmt(space.w_TypeError, + "%T constructor takes either 0 or %d positional argument", w_self, num_fields) + else: + raise oefmt(space.w_TypeError, + "%T constructor takes either 0 or %d positional arguments", w_self, num_fields) + if args_w: + for i, w_field in enumerate(fields_w): + space.setattr(w_self, w_field, args_w[i]) for field, w_value in kwargs_w.iteritems(): space.setattr(w_self, space.wrap(field), w_value) -AST.typedef = typedef.TypeDef("_ast.AST", + +W_AST.typedef = typedef.TypeDef("_ast.AST", _fields=_FieldsWrapper([]), _attributes=_FieldsWrapper([]), - __reduce__=interp2app(AST.reduce_w), - __setstate__=interp2app(AST.setstate_w), + __reduce__=interp2app(W_AST.reduce_w), + __setstate__=interp2app(W_AST.setstate_w), __dict__ = typedef.GetSetProperty(typedef.descr_get_dict, - typedef.descr_set_dict, cls=AST), - __new__=interp2app(get_AST_new(AST)), - __init__=interp2app(AST_init), + typedef.descr_set_dict, cls=W_AST), + __new__=interp2app(W_AST_new), + __init__=interp2app(W_AST_init), ) - - +class State: + AST_TYPES = [] + + @classmethod + def ast_type(cls, name, base, fields, attributes=None): + cls.AST_TYPES.append((name, base, fields, attributes)) + + def __init__(self, space): + self.w_AST = space.gettypeobject(W_AST.typedef) + for (name, base, fields, attributes) in self.AST_TYPES: + self.make_new_type(space, name, base, fields, attributes) + + def make_new_type(self, space, name, base, fields, attributes): + w_base = getattr(self, 'w_%s' % base) + w_dict = space.newdict() + space.setitem_str(w_dict, '__module__', space.wrap('_ast')) + if fields is not None: + space.setitem_str(w_dict, "_fields", + space.newtuple([space.wrap(f) for f in fields])) + if attributes is not None: + space.setitem_str(w_dict, "_attributes", + space.newtuple([space.wrap(a) for a in attributes])) + w_type = space.call_function( + space.w_type, + space.wrap(name), space.newtuple([w_base]), w_dict) + setattr(self, 'w_%s' % name, w_type) + +def get(space): + return space.fromcache(State) class mod(AST): - pass + @staticmethod + def from_object(space, w_node): + if space.is_w(w_node, space.w_None): + return None + if space.isinstance_w(w_node, get(space).w_Module): + return Module.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Interactive): + return Interactive.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Expression): + return Expression.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Suite): + return Suite.from_object(space, w_node) + raise oefmt(space.w_TypeError, + "Expected mod node, got %T", w_node) +State.ast_type('mod', 'AST', None, []) class Module(mod): def __init__(self, body): self.body = body - self.w_body = None - self.initialization_state = 1 def walkabout(self, visitor): visitor.visit_Module(self) @@ -153,29 +186,30 @@ visitor._mutate_sequence(self.body) return visitor.visit_Module(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 1: - self.missing_field(space, ['body'], 'Module') + def to_object(self, space): + w_node = space.call_function(get(space).w_Module) + if self.body is None: + body_w = [] else: - pass - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + return Module(_body) + +State.ast_type('Module', 'mod', ['body']) class Interactive(mod): def __init__(self, body): self.body = body - self.w_body = None - self.initialization_state = 1 def walkabout(self, visitor): visitor.visit_Interactive(self) @@ -185,28 +219,30 @@ visitor._mutate_sequence(self.body) return visitor.visit_Interactive(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 1: - self.missing_field(space, ['body'], 'Interactive') + def to_object(self, space): + w_node = space.call_function(get(space).w_Interactive) + if self.body is None: + body_w = [] else: - pass - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + return Interactive(_body) + +State.ast_type('Interactive', 'mod', ['body']) class Expression(mod): def __init__(self, body): self.body = body - self.initialization_state = 1 def walkabout(self, visitor): visitor.visit_Expression(self) @@ -215,20 +251,25 @@ self.body = self.body.mutate_over(visitor) return visitor.visit_Expression(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 1: - self.missing_field(space, ['body'], 'Expression') - else: - pass - self.body.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Expression) + w_body = self.body.to_object(space) # expr + space.setattr(w_node, space.wrap('body'), w_body) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + _body = expr.from_object(space, w_body) + return Expression(_body) + +State.ast_type('Expression', 'mod', ['body']) class Suite(mod): def __init__(self, body): self.body = body - self.w_body = None - self.initialization_state = 1 def walkabout(self, visitor): visitor.visit_Suite(self) @@ -238,21 +279,24 @@ visitor._mutate_sequence(self.body) return visitor.visit_Suite(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 1: - self.missing_field(space, ['body'], 'Suite') + def to_object(self, space): + w_node = space.call_function(get(space).w_Suite) + if self.body is None: + body_w = [] else: - pass - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + return Suite(_body) + +State.ast_type('Suite', 'mod', ['body']) class stmt(AST): @@ -261,17 +305,68 @@ self.lineno = lineno self.col_offset = col_offset + @staticmethod + def from_object(space, w_node): + if space.is_w(w_node, space.w_None): + return None + if space.isinstance_w(w_node, get(space).w_FunctionDef): + return FunctionDef.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_ClassDef): + return ClassDef.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Return): + return Return.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Delete): + return Delete.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Assign): + return Assign.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_AugAssign): + return AugAssign.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Print): + return Print.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_For): + return For.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_While): + return While.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_If): + return If.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_With): + return With.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Raise): + return Raise.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_TryExcept): + return TryExcept.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_TryFinally): + return TryFinally.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Assert): + return Assert.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Import): + return Import.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_ImportFrom): + return ImportFrom.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Exec): + return Exec.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Global): + return Global.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Expr): + return Expr.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Pass): + return Pass.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Break): + return Break.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Continue): + return Continue.from_object(space, w_node) + raise oefmt(space.w_TypeError, + "Expected stmt node, got %T", w_node) +State.ast_type('stmt', 'AST', None, ['lineno', 'col_offset']) + class FunctionDef(stmt): def __init__(self, name, args, body, decorator_list, lineno, col_offset): self.name = name self.args = args self.body = body - self.w_body = None self.decorator_list = decorator_list - self.w_decorator_list = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 63 def walkabout(self, visitor): visitor.visit_FunctionDef(self) @@ -284,32 +379,49 @@ visitor._mutate_sequence(self.decorator_list) return visitor.visit_FunctionDef(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 63: - self.missing_field(space, ['lineno', 'col_offset', 'name', 'args', 'body', 'decorator_list'], 'FunctionDef') + def to_object(self, space): + w_node = space.call_function(get(space).w_FunctionDef) + w_name = space.wrap(self.name) # identifier + space.setattr(w_node, space.wrap('name'), w_name) + w_args = self.args.to_object(space) # arguments + space.setattr(w_node, space.wrap('args'), w_args) + if self.body is None: + body_w = [] else: - pass - self.args.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_decorator_list - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.decorator_list = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.decorator_list = None - if self.decorator_list is not None: - for node in self.decorator_list: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.decorator_list is None: + decorator_list_w = [] + else: + decorator_list_w = [node.to_object(space) for node in self.decorator_list] # expr + w_decorator_list = space.newlist(decorator_list_w) + space.setattr(w_node, space.wrap('decorator_list'), w_decorator_list) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_name = get_field(space, w_node, 'name', False) + w_args = get_field(space, w_node, 'args', False) + w_body = get_field(space, w_node, 'body', False) + w_decorator_list = get_field(space, w_node, 'decorator_list', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _name = space.realstr_w(w_name) + _args = arguments.from_object(space, w_args) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + decorator_list_w = space.unpackiterable(w_decorator_list) + _decorator_list = [expr.from_object(space, w_item) for w_item in decorator_list_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return FunctionDef(_name, _args, _body, _decorator_list, _lineno, _col_offset) + +State.ast_type('FunctionDef', 'stmt', ['name', 'args', 'body', 'decorator_list']) class ClassDef(stmt): @@ -317,13 +429,9 @@ def __init__(self, name, bases, body, decorator_list, lineno, col_offset): self.name = name self.bases = bases - self.w_bases = None self.body = body - self.w_body = None self.decorator_list = decorator_list - self.w_decorator_list = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 63 def walkabout(self, visitor): visitor.visit_ClassDef(self) @@ -337,41 +445,54 @@ visitor._mutate_sequence(self.decorator_list) return visitor.visit_ClassDef(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 63: - self.missing_field(space, ['lineno', 'col_offset', 'name', 'bases', 'body', 'decorator_list'], 'ClassDef') + def to_object(self, space): + w_node = space.call_function(get(space).w_ClassDef) + w_name = space.wrap(self.name) # identifier + space.setattr(w_node, space.wrap('name'), w_name) + if self.bases is None: + bases_w = [] else: - pass - w_list = self.w_bases - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.bases = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.bases = None - if self.bases is not None: - for node in self.bases: - node.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_decorator_list - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.decorator_list = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.decorator_list = None - if self.decorator_list is not None: - for node in self.decorator_list: - node.sync_app_attrs(space) + bases_w = [node.to_object(space) for node in self.bases] # expr + w_bases = space.newlist(bases_w) + space.setattr(w_node, space.wrap('bases'), w_bases) + if self.body is None: + body_w = [] + else: + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.decorator_list is None: + decorator_list_w = [] + else: + decorator_list_w = [node.to_object(space) for node in self.decorator_list] # expr + w_decorator_list = space.newlist(decorator_list_w) + space.setattr(w_node, space.wrap('decorator_list'), w_decorator_list) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_name = get_field(space, w_node, 'name', False) + w_bases = get_field(space, w_node, 'bases', False) + w_body = get_field(space, w_node, 'body', False) + w_decorator_list = get_field(space, w_node, 'decorator_list', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _name = space.realstr_w(w_name) + bases_w = space.unpackiterable(w_bases) + _bases = [expr.from_object(space, w_item) for w_item in bases_w] + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + decorator_list_w = space.unpackiterable(w_decorator_list) + _decorator_list = [expr.from_object(space, w_item) for w_item in decorator_list_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return ClassDef(_name, _bases, _body, _decorator_list, _lineno, _col_offset) + +State.ast_type('ClassDef', 'stmt', ['name', 'bases', 'body', 'decorator_list']) class Return(stmt): @@ -379,7 +500,6 @@ def __init__(self, value, lineno, col_offset): self.value = value stmt.__init__(self, lineno, col_offset) - self.initialization_state = 7 def walkabout(self, visitor): visitor.visit_Return(self) @@ -389,23 +509,34 @@ self.value = self.value.mutate_over(visitor) return visitor.visit_Return(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~4) ^ 3: - self.missing_field(space, ['lineno', 'col_offset', None], 'Return') - else: - if not self.initialization_state & 4: - self.value = None - if self.value: - self.value.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Return) + w_value = self.value.to_object(space) if self.value is not None else space.w_None # expr + space.setattr(w_node, space.wrap('value'), w_value) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_value = get_field(space, w_node, 'value', True) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _value = expr.from_object(space, w_value) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Return(_value, _lineno, _col_offset) + +State.ast_type('Return', 'stmt', ['value']) class Delete(stmt): def __init__(self, targets, lineno, col_offset): self.targets = targets - self.w_targets = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 7 def walkabout(self, visitor): visitor.visit_Delete(self) @@ -415,31 +546,40 @@ visitor._mutate_sequence(self.targets) return visitor.visit_Delete(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'targets'], 'Delete') + def to_object(self, space): + w_node = space.call_function(get(space).w_Delete) + if self.targets is None: + targets_w = [] else: - pass - w_list = self.w_targets - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.targets = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.targets = None - if self.targets is not None: - for node in self.targets: - node.sync_app_attrs(space) + targets_w = [node.to_object(space) for node in self.targets] # expr + w_targets = space.newlist(targets_w) + space.setattr(w_node, space.wrap('targets'), w_targets) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_targets = get_field(space, w_node, 'targets', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + targets_w = space.unpackiterable(w_targets) + _targets = [expr.from_object(space, w_item) for w_item in targets_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Delete(_targets, _lineno, _col_offset) + +State.ast_type('Delete', 'stmt', ['targets']) class Assign(stmt): def __init__(self, targets, value, lineno, col_offset): self.targets = targets - self.w_targets = None self.value = value stmt.__init__(self, lineno, col_offset) - self.initialization_state = 15 def walkabout(self, visitor): visitor.visit_Assign(self) @@ -450,22 +590,36 @@ self.value = self.value.mutate_over(visitor) return visitor.visit_Assign(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 15: - self.missing_field(space, ['lineno', 'col_offset', 'targets', 'value'], 'Assign') + def to_object(self, space): + w_node = space.call_function(get(space).w_Assign) + if self.targets is None: + targets_w = [] else: - pass - w_list = self.w_targets - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.targets = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.targets = None - if self.targets is not None: - for node in self.targets: - node.sync_app_attrs(space) - self.value.sync_app_attrs(space) + targets_w = [node.to_object(space) for node in self.targets] # expr + w_targets = space.newlist(targets_w) + space.setattr(w_node, space.wrap('targets'), w_targets) + w_value = self.value.to_object(space) # expr + space.setattr(w_node, space.wrap('value'), w_value) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_targets = get_field(space, w_node, 'targets', False) + w_value = get_field(space, w_node, 'value', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + targets_w = space.unpackiterable(w_targets) + _targets = [expr.from_object(space, w_item) for w_item in targets_w] + _value = expr.from_object(space, w_value) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Assign(_targets, _value, _lineno, _col_offset) + +State.ast_type('Assign', 'stmt', ['targets', 'value']) class AugAssign(stmt): @@ -475,7 +629,6 @@ self.op = op self.value = value stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_AugAssign(self) @@ -485,13 +638,35 @@ self.value = self.value.mutate_over(visitor) return visitor.visit_AugAssign(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 31: - self.missing_field(space, ['lineno', 'col_offset', 'target', 'op', 'value'], 'AugAssign') - else: - pass - self.target.sync_app_attrs(space) - self.value.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_AugAssign) + w_target = self.target.to_object(space) # expr + space.setattr(w_node, space.wrap('target'), w_target) + w_op = operator_to_class[self.op - 1]().to_object(space) # operator + space.setattr(w_node, space.wrap('op'), w_op) + w_value = self.value.to_object(space) # expr + space.setattr(w_node, space.wrap('value'), w_value) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_target = get_field(space, w_node, 'target', False) + w_op = get_field(space, w_node, 'op', False) + w_value = get_field(space, w_node, 'value', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _target = expr.from_object(space, w_target) + _op = operator.from_object(space, w_op) + _value = expr.from_object(space, w_value) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return AugAssign(_target, _op, _value, _lineno, _col_offset) + +State.ast_type('AugAssign', 'stmt', ['target', 'op', 'value']) class Print(stmt): @@ -499,10 +674,8 @@ def __init__(self, dest, values, nl, lineno, col_offset): self.dest = dest self.values = values - self.w_values = None self.nl = nl stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_Print(self) @@ -514,24 +687,40 @@ visitor._mutate_sequence(self.values) return visitor.visit_Print(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~4) ^ 27: - self.missing_field(space, ['lineno', 'col_offset', None, 'values', 'nl'], 'Print') + def to_object(self, space): + w_node = space.call_function(get(space).w_Print) + w_dest = self.dest.to_object(space) if self.dest is not None else space.w_None # expr + space.setattr(w_node, space.wrap('dest'), w_dest) + if self.values is None: + values_w = [] else: - if not self.initialization_state & 4: - self.dest = None - if self.dest: - self.dest.sync_app_attrs(space) - w_list = self.w_values - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.values = [space.interp_w(expr, w_obj) for w_obj in list_w] - else: - self.values = None - if self.values is not None: - for node in self.values: - node.sync_app_attrs(space) + values_w = [node.to_object(space) for node in self.values] # expr + w_values = space.newlist(values_w) + space.setattr(w_node, space.wrap('values'), w_values) + w_nl = space.wrap(self.nl) # bool + space.setattr(w_node, space.wrap('nl'), w_nl) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_dest = get_field(space, w_node, 'dest', True) + w_values = get_field(space, w_node, 'values', False) + w_nl = get_field(space, w_node, 'nl', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _dest = expr.from_object(space, w_dest) + values_w = space.unpackiterable(w_values) + _values = [expr.from_object(space, w_item) for w_item in values_w] + _nl = space.bool_w(w_nl) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Print(_dest, _values, _nl, _lineno, _col_offset) + +State.ast_type('Print', 'stmt', ['dest', 'values', 'nl']) class For(stmt): @@ -540,11 +729,8 @@ self.target = target self.iter = iter self.body = body - self.w_body = None self.orelse = orelse - self.w_orelse = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 63 def walkabout(self, visitor): visitor.visit_For(self) @@ -558,33 +744,49 @@ visitor._mutate_sequence(self.orelse) return visitor.visit_For(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 63: - self.missing_field(space, ['lineno', 'col_offset', 'target', 'iter', 'body', 'orelse'], 'For') + def to_object(self, space): + w_node = space.call_function(get(space).w_For) + w_target = self.target.to_object(space) # expr + space.setattr(w_node, space.wrap('target'), w_target) + w_iter = self.iter.to_object(space) # expr + space.setattr(w_node, space.wrap('iter'), w_iter) + if self.body is None: + body_w = [] else: - pass - self.target.sync_app_attrs(space) - self.iter.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_orelse - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.orelse = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.orelse = None - if self.orelse is not None: - for node in self.orelse: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.orelse is None: + orelse_w = [] + else: + orelse_w = [node.to_object(space) for node in self.orelse] # stmt + w_orelse = space.newlist(orelse_w) + space.setattr(w_node, space.wrap('orelse'), w_orelse) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_target = get_field(space, w_node, 'target', False) + w_iter = get_field(space, w_node, 'iter', False) + w_body = get_field(space, w_node, 'body', False) + w_orelse = get_field(space, w_node, 'orelse', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _target = expr.from_object(space, w_target) + _iter = expr.from_object(space, w_iter) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + orelse_w = space.unpackiterable(w_orelse) + _orelse = [stmt.from_object(space, w_item) for w_item in orelse_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return For(_target, _iter, _body, _orelse, _lineno, _col_offset) + +State.ast_type('For', 'stmt', ['target', 'iter', 'body', 'orelse']) class While(stmt): @@ -592,11 +794,8 @@ def __init__(self, test, body, orelse, lineno, col_offset): self.test = test self.body = body - self.w_body = None self.orelse = orelse - self.w_orelse = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_While(self) @@ -609,32 +808,45 @@ visitor._mutate_sequence(self.orelse) return visitor.visit_While(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 31: - self.missing_field(space, ['lineno', 'col_offset', 'test', 'body', 'orelse'], 'While') + def to_object(self, space): + w_node = space.call_function(get(space).w_While) + w_test = self.test.to_object(space) # expr + space.setattr(w_node, space.wrap('test'), w_test) + if self.body is None: + body_w = [] else: - pass - self.test.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_orelse - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.orelse = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.orelse = None - if self.orelse is not None: - for node in self.orelse: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.orelse is None: + orelse_w = [] + else: + orelse_w = [node.to_object(space) for node in self.orelse] # stmt + w_orelse = space.newlist(orelse_w) + space.setattr(w_node, space.wrap('orelse'), w_orelse) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_test = get_field(space, w_node, 'test', False) + w_body = get_field(space, w_node, 'body', False) + w_orelse = get_field(space, w_node, 'orelse', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _test = expr.from_object(space, w_test) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + orelse_w = space.unpackiterable(w_orelse) + _orelse = [stmt.from_object(space, w_item) for w_item in orelse_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return While(_test, _body, _orelse, _lineno, _col_offset) + +State.ast_type('While', 'stmt', ['test', 'body', 'orelse']) class If(stmt): @@ -642,11 +854,8 @@ def __init__(self, test, body, orelse, lineno, col_offset): self.test = test self.body = body - self.w_body = None self.orelse = orelse - self.w_orelse = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_If(self) @@ -659,32 +868,45 @@ visitor._mutate_sequence(self.orelse) return visitor.visit_If(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 31: - self.missing_field(space, ['lineno', 'col_offset', 'test', 'body', 'orelse'], 'If') + def to_object(self, space): + w_node = space.call_function(get(space).w_If) + w_test = self.test.to_object(space) # expr + space.setattr(w_node, space.wrap('test'), w_test) + if self.body is None: + body_w = [] else: - pass - self.test.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_orelse - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.orelse = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.orelse = None - if self.orelse is not None: - for node in self.orelse: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.orelse is None: + orelse_w = [] + else: + orelse_w = [node.to_object(space) for node in self.orelse] # stmt + w_orelse = space.newlist(orelse_w) + space.setattr(w_node, space.wrap('orelse'), w_orelse) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_test = get_field(space, w_node, 'test', False) + w_body = get_field(space, w_node, 'body', False) + w_orelse = get_field(space, w_node, 'orelse', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _test = expr.from_object(space, w_test) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + orelse_w = space.unpackiterable(w_orelse) + _orelse = [stmt.from_object(space, w_item) for w_item in orelse_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return If(_test, _body, _orelse, _lineno, _col_offset) + +State.ast_type('If', 'stmt', ['test', 'body', 'orelse']) class With(stmt): @@ -693,9 +915,7 @@ self.context_expr = context_expr self.optional_vars = optional_vars self.body = body - self.w_body = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_With(self) @@ -708,25 +928,40 @@ visitor._mutate_sequence(self.body) return visitor.visit_With(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~8) ^ 23: - self.missing_field(space, ['lineno', 'col_offset', 'context_expr', None, 'body'], 'With') + def to_object(self, space): + w_node = space.call_function(get(space).w_With) + w_context_expr = self.context_expr.to_object(space) # expr + space.setattr(w_node, space.wrap('context_expr'), w_context_expr) + w_optional_vars = self.optional_vars.to_object(space) if self.optional_vars is not None else space.w_None # expr + space.setattr(w_node, space.wrap('optional_vars'), w_optional_vars) + if self.body is None: + body_w = [] else: - if not self.initialization_state & 8: - self.optional_vars = None - self.context_expr.sync_app_attrs(space) - if self.optional_vars: - self.optional_vars.sync_app_attrs(space) - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_context_expr = get_field(space, w_node, 'context_expr', False) + w_optional_vars = get_field(space, w_node, 'optional_vars', True) + w_body = get_field(space, w_node, 'body', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _context_expr = expr.from_object(space, w_context_expr) + _optional_vars = expr.from_object(space, w_optional_vars) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return With(_context_expr, _optional_vars, _body, _lineno, _col_offset) + +State.ast_type('With', 'stmt', ['context_expr', 'optional_vars', 'body']) class Raise(stmt): @@ -736,7 +971,6 @@ self.inst = inst self.tback = tback stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_Raise(self) @@ -750,35 +984,44 @@ self.tback = self.tback.mutate_over(visitor) return visitor.visit_Raise(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~28) ^ 3: - self.missing_field(space, ['lineno', 'col_offset', None, None, None], 'Raise') - else: - if not self.initialization_state & 4: - self.type = None - if not self.initialization_state & 8: - self.inst = None - if not self.initialization_state & 16: - self.tback = None - if self.type: - self.type.sync_app_attrs(space) - if self.inst: - self.inst.sync_app_attrs(space) - if self.tback: - self.tback.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Raise) + w_type = self.type.to_object(space) if self.type is not None else space.w_None # expr + space.setattr(w_node, space.wrap('type'), w_type) + w_inst = self.inst.to_object(space) if self.inst is not None else space.w_None # expr + space.setattr(w_node, space.wrap('inst'), w_inst) + w_tback = self.tback.to_object(space) if self.tback is not None else space.w_None # expr + space.setattr(w_node, space.wrap('tback'), w_tback) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_type = get_field(space, w_node, 'type', True) + w_inst = get_field(space, w_node, 'inst', True) + w_tback = get_field(space, w_node, 'tback', True) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _type = expr.from_object(space, w_type) + _inst = expr.from_object(space, w_inst) + _tback = expr.from_object(space, w_tback) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Raise(_type, _inst, _tback, _lineno, _col_offset) + +State.ast_type('Raise', 'stmt', ['type', 'inst', 'tback']) class TryExcept(stmt): def __init__(self, body, handlers, orelse, lineno, col_offset): self.body = body - self.w_body = None self.handlers = handlers - self.w_handlers = None self.orelse = orelse - self.w_orelse = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_TryExcept(self) @@ -792,52 +1035,58 @@ visitor._mutate_sequence(self.orelse) return visitor.visit_TryExcept(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 31: - self.missing_field(space, ['lineno', 'col_offset', 'body', 'handlers', 'orelse'], 'TryExcept') + def to_object(self, space): + w_node = space.call_function(get(space).w_TryExcept) + if self.body is None: + body_w = [] else: - pass - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_handlers - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.handlers = [space.interp_w(excepthandler, w_obj) for w_obj in list_w] - else: - self.handlers = None - if self.handlers is not None: - for node in self.handlers: - node.sync_app_attrs(space) - w_list = self.w_orelse - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.orelse = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.orelse = None - if self.orelse is not None: - for node in self.orelse: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.handlers is None: + handlers_w = [] + else: + handlers_w = [node.to_object(space) for node in self.handlers] # excepthandler + w_handlers = space.newlist(handlers_w) + space.setattr(w_node, space.wrap('handlers'), w_handlers) + if self.orelse is None: + orelse_w = [] + else: + orelse_w = [node.to_object(space) for node in self.orelse] # stmt + w_orelse = space.newlist(orelse_w) + space.setattr(w_node, space.wrap('orelse'), w_orelse) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + w_handlers = get_field(space, w_node, 'handlers', False) + w_orelse = get_field(space, w_node, 'orelse', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + handlers_w = space.unpackiterable(w_handlers) + _handlers = [excepthandler.from_object(space, w_item) for w_item in handlers_w] + orelse_w = space.unpackiterable(w_orelse) + _orelse = [stmt.from_object(space, w_item) for w_item in orelse_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return TryExcept(_body, _handlers, _orelse, _lineno, _col_offset) + +State.ast_type('TryExcept', 'stmt', ['body', 'handlers', 'orelse']) class TryFinally(stmt): def __init__(self, body, finalbody, lineno, col_offset): self.body = body - self.w_body = None self.finalbody = finalbody - self.w_finalbody = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 15 def walkabout(self, visitor): visitor.visit_TryFinally(self) @@ -849,31 +1098,41 @@ visitor._mutate_sequence(self.finalbody) return visitor.visit_TryFinally(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 15: - self.missing_field(space, ['lineno', 'col_offset', 'body', 'finalbody'], 'TryFinally') + def to_object(self, space): + w_node = space.call_function(get(space).w_TryFinally) + if self.body is None: + body_w = [] else: - pass - w_list = self.w_body - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.body = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.body = None - if self.body is not None: - for node in self.body: - node.sync_app_attrs(space) - w_list = self.w_finalbody - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.finalbody = [space.interp_w(stmt, w_obj) for w_obj in list_w] - else: - self.finalbody = None - if self.finalbody is not None: - for node in self.finalbody: - node.sync_app_attrs(space) + body_w = [node.to_object(space) for node in self.body] # stmt + w_body = space.newlist(body_w) + space.setattr(w_node, space.wrap('body'), w_body) + if self.finalbody is None: + finalbody_w = [] + else: + finalbody_w = [node.to_object(space) for node in self.finalbody] # stmt + w_finalbody = space.newlist(finalbody_w) + space.setattr(w_node, space.wrap('finalbody'), w_finalbody) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + w_finalbody = get_field(space, w_node, 'finalbody', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + body_w = space.unpackiterable(w_body) + _body = [stmt.from_object(space, w_item) for w_item in body_w] + finalbody_w = space.unpackiterable(w_finalbody) + _finalbody = [stmt.from_object(space, w_item) for w_item in finalbody_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return TryFinally(_body, _finalbody, _lineno, _col_offset) + +State.ast_type('TryFinally', 'stmt', ['body', 'finalbody']) class Assert(stmt): @@ -882,7 +1141,6 @@ self.test = test self.msg = msg stmt.__init__(self, lineno, col_offset) - self.initialization_state = 15 def walkabout(self, visitor): visitor.visit_Assert(self) @@ -893,24 +1151,38 @@ self.msg = self.msg.mutate_over(visitor) return visitor.visit_Assert(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~8) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'test', None], 'Assert') - else: - if not self.initialization_state & 8: - self.msg = None - self.test.sync_app_attrs(space) - if self.msg: - self.msg.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Assert) + w_test = self.test.to_object(space) # expr + space.setattr(w_node, space.wrap('test'), w_test) + w_msg = self.msg.to_object(space) if self.msg is not None else space.w_None # expr + space.setattr(w_node, space.wrap('msg'), w_msg) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_test = get_field(space, w_node, 'test', False) + w_msg = get_field(space, w_node, 'msg', True) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _test = expr.from_object(space, w_test) + _msg = expr.from_object(space, w_msg) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Assert(_test, _msg, _lineno, _col_offset) + +State.ast_type('Assert', 'stmt', ['test', 'msg']) class Import(stmt): def __init__(self, names, lineno, col_offset): self.names = names - self.w_names = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 7 def walkabout(self, visitor): visitor.visit_Import(self) @@ -920,21 +1192,32 @@ visitor._mutate_sequence(self.names) return visitor.visit_Import(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'names'], 'Import') + def to_object(self, space): + w_node = space.call_function(get(space).w_Import) + if self.names is None: + names_w = [] else: - pass - w_list = self.w_names - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.names = [space.interp_w(alias, w_obj) for w_obj in list_w] - else: - self.names = None - if self.names is not None: - for node in self.names: - node.sync_app_attrs(space) + names_w = [node.to_object(space) for node in self.names] # alias + w_names = space.newlist(names_w) + space.setattr(w_node, space.wrap('names'), w_names) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_names = get_field(space, w_node, 'names', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + names_w = space.unpackiterable(w_names) + _names = [alias.from_object(space, w_item) for w_item in names_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Import(_names, _lineno, _col_offset) + +State.ast_type('Import', 'stmt', ['names']) class ImportFrom(stmt): @@ -942,10 +1225,8 @@ def __init__(self, module, names, level, lineno, col_offset): self.module = module self.names = names - self.w_names = None self.level = level stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_ImportFrom(self) @@ -955,24 +1236,40 @@ visitor._mutate_sequence(self.names) return visitor.visit_ImportFrom(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~20) ^ 11: - self.missing_field(space, ['lineno', 'col_offset', None, 'names', None], 'ImportFrom') + def to_object(self, space): + w_node = space.call_function(get(space).w_ImportFrom) + w_module = space.wrap(self.module) # identifier + space.setattr(w_node, space.wrap('module'), w_module) + if self.names is None: + names_w = [] else: - if not self.initialization_state & 4: - self.module = None - if not self.initialization_state & 16: - self.level = 0 - w_list = self.w_names - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.names = [space.interp_w(alias, w_obj) for w_obj in list_w] - else: - self.names = None - if self.names is not None: - for node in self.names: - node.sync_app_attrs(space) + names_w = [node.to_object(space) for node in self.names] # alias + w_names = space.newlist(names_w) + space.setattr(w_node, space.wrap('names'), w_names) + w_level = space.wrap(self.level) # int + space.setattr(w_node, space.wrap('level'), w_level) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_module = get_field(space, w_node, 'module', True) + w_names = get_field(space, w_node, 'names', False) + w_level = get_field(space, w_node, 'level', True) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _module = space.str_or_None_w(w_module) + names_w = space.unpackiterable(w_names) + _names = [alias.from_object(space, w_item) for w_item in names_w] + _level = space.int_w(w_level) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return ImportFrom(_module, _names, _level, _lineno, _col_offset) + +State.ast_type('ImportFrom', 'stmt', ['module', 'names', 'level']) class Exec(stmt): @@ -982,7 +1279,6 @@ self.globals = globals self.locals = locals stmt.__init__(self, lineno, col_offset) - self.initialization_state = 31 def walkabout(self, visitor): visitor.visit_Exec(self) @@ -995,28 +1291,42 @@ self.locals = self.locals.mutate_over(visitor) return visitor.visit_Exec(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~24) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'body', None, None], 'Exec') - else: - if not self.initialization_state & 8: - self.globals = None - if not self.initialization_state & 16: - self.locals = None - self.body.sync_app_attrs(space) - if self.globals: - self.globals.sync_app_attrs(space) - if self.locals: - self.locals.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Exec) + w_body = self.body.to_object(space) # expr + space.setattr(w_node, space.wrap('body'), w_body) + w_globals = self.globals.to_object(space) if self.globals is not None else space.w_None # expr + space.setattr(w_node, space.wrap('globals'), w_globals) + w_locals = self.locals.to_object(space) if self.locals is not None else space.w_None # expr + space.setattr(w_node, space.wrap('locals'), w_locals) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_body = get_field(space, w_node, 'body', False) + w_globals = get_field(space, w_node, 'globals', True) + w_locals = get_field(space, w_node, 'locals', True) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _body = expr.from_object(space, w_body) + _globals = expr.from_object(space, w_globals) + _locals = expr.from_object(space, w_locals) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Exec(_body, _globals, _locals, _lineno, _col_offset) + +State.ast_type('Exec', 'stmt', ['body', 'globals', 'locals']) class Global(stmt): def __init__(self, names, lineno, col_offset): self.names = names - self.w_names = None stmt.__init__(self, lineno, col_offset) - self.initialization_state = 7 def walkabout(self, visitor): visitor.visit_Global(self) @@ -1024,18 +1334,32 @@ def mutate_over(self, visitor): return visitor.visit_Global(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'names'], 'Global') + def to_object(self, space): + w_node = space.call_function(get(space).w_Global) + if self.names is None: + names_w = [] else: - pass - w_list = self.w_names - if w_list is not None: - list_w = space.listview(w_list) - if list_w: - self.names = [space.realstr_w(w_obj) for w_obj in list_w] - else: - self.names = None + names_w = [space.wrap(node) for node in self.names] # identifier + w_names = space.newlist(names_w) + space.setattr(w_node, space.wrap('names'), w_names) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_names = get_field(space, w_node, 'names', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + names_w = space.unpackiterable(w_names) + _names = [space.realstr_w(w_item) for w_item in names_w] + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Global(_names, _lineno, _col_offset) + +State.ast_type('Global', 'stmt', ['names']) class Expr(stmt): @@ -1043,7 +1367,6 @@ def __init__(self, value, lineno, col_offset): self.value = value stmt.__init__(self, lineno, col_offset) - self.initialization_state = 7 def walkabout(self, visitor): visitor.visit_Expr(self) @@ -1052,19 +1375,33 @@ self.value = self.value.mutate_over(visitor) return visitor.visit_Expr(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 7: - self.missing_field(space, ['lineno', 'col_offset', 'value'], 'Expr') - else: - pass - self.value.sync_app_attrs(space) + def to_object(self, space): + w_node = space.call_function(get(space).w_Expr) + w_value = self.value.to_object(space) # expr + space.setattr(w_node, space.wrap('value'), w_value) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_value = get_field(space, w_node, 'value', False) + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _value = expr.from_object(space, w_value) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Expr(_value, _lineno, _col_offset) + +State.ast_type('Expr', 'stmt', ['value']) class Pass(stmt): def __init__(self, lineno, col_offset): stmt.__init__(self, lineno, col_offset) - self.initialization_state = 3 def walkabout(self, visitor): visitor.visit_Pass(self) @@ -1072,18 +1409,29 @@ def mutate_over(self, visitor): return visitor.visit_Pass(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 3: - self.missing_field(space, ['lineno', 'col_offset'], 'Pass') - else: - pass + def to_object(self, space): + w_node = space.call_function(get(space).w_Pass) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Pass(_lineno, _col_offset) + +State.ast_type('Pass', 'stmt', []) class Break(stmt): def __init__(self, lineno, col_offset): stmt.__init__(self, lineno, col_offset) - self.initialization_state = 3 def walkabout(self, visitor): visitor.visit_Break(self) @@ -1091,18 +1439,29 @@ def mutate_over(self, visitor): return visitor.visit_Break(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 3: - self.missing_field(space, ['lineno', 'col_offset'], 'Break') - else: - pass + def to_object(self, space): + w_node = space.call_function(get(space).w_Break) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Break(_lineno, _col_offset) + +State.ast_type('Break', 'stmt', []) class Continue(stmt): def __init__(self, lineno, col_offset): stmt.__init__(self, lineno, col_offset) - self.initialization_state = 3 def walkabout(self, visitor): visitor.visit_Continue(self) @@ -1110,11 +1469,23 @@ def mutate_over(self, visitor): return visitor.visit_Continue(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 3: - self.missing_field(space, ['lineno', 'col_offset'], 'Continue') - else: - pass + def to_object(self, space): + w_node = space.call_function(get(space).w_Continue) + w_lineno = space.wrap(self.lineno) # int + space.setattr(w_node, space.wrap('lineno'), w_lineno) + w_col_offset = space.wrap(self.col_offset) # int + space.setattr(w_node, space.wrap('col_offset'), w_col_offset) + return w_node + + @staticmethod + def from_object(space, w_node): + w_lineno = get_field(space, w_node, 'lineno', False) + w_col_offset = get_field(space, w_node, 'col_offset', False) + _lineno = space.int_w(w_lineno) + _col_offset = space.int_w(w_col_offset) + return Continue(_lineno, _col_offset) + +State.ast_type('Continue', 'stmt', []) class expr(AST): @@ -1123,14 +1494,66 @@ self.lineno = lineno self.col_offset = col_offset + @staticmethod + def from_object(space, w_node): + if space.is_w(w_node, space.w_None): + return None + if space.isinstance_w(w_node, get(space).w_BoolOp): + return BoolOp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_BinOp): + return BinOp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_UnaryOp): + return UnaryOp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Lambda): + return Lambda.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_IfExp): + return IfExp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Dict): + return Dict.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Set): + return Set.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_ListComp): + return ListComp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_SetComp): + return SetComp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_DictComp): + return DictComp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_GeneratorExp): + return GeneratorExp.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Yield): + return Yield.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Compare): + return Compare.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Call): + return Call.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Repr): + return Repr.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Num): + return Num.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Str): + return Str.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Attribute): + return Attribute.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Subscript): + return Subscript.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Name): + return Name.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_List): + return List.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Tuple): + return Tuple.from_object(space, w_node) + if space.isinstance_w(w_node, get(space).w_Const): + return Const.from_object(space, w_node) + raise oefmt(space.w_TypeError, + "Expected expr node, got %T", w_node) +State.ast_type('expr', 'AST', None, ['lineno', 'col_offset']) + class BoolOp(expr): def __init__(self, op, values, lineno, col_offset): self.op = op self.values = values - self.w_values = None expr.__init__(self, lineno, col_offset) - self.initialization_state = 15 def walkabout(self, visitor): visitor.visit_BoolOp(self) @@ -1140,21 +1563,36 @@ visitor._mutate_sequence(self.values) return visitor.visit_BoolOp(self) - def sync_app_attrs(self, space): - if (self.initialization_state & ~0) ^ 15: - self.missing_field(space, ['lineno', 'col_offset', 'op', 'values'], 'BoolOp') + def to_object(self, space): + w_node = space.call_function(get(space).w_BoolOp) + w_op = boolop_to_class[self.op - 1]().to_object(space) # boolop + space.setattr(w_node, space.wrap('op'), w_op) + if self.values is None: + values_w = [] else: _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit