1. Pypy
  2. Untitled project
  3. pypy

Commits

Amaury Forgeot d'Arc  committed c862040

Add _attributes

  • Participants
  • Parent commits f4e2683
  • Branches split-ast-classes

Comments (0)

Files changed (4)

File pypy/interpreter/astcompiler/ast.py

View file
     AST_TYPES = []
 
     @classmethod
-    def ast_type(cls, name, base, fields):
-        cls.AST_TYPES.append((name, base, fields))
+    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) in self.AST_TYPES:
-            self.make_new_type(space, name, base, fields)
+        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):
+    def make_new_type(self, space, name, base, fields, attributes):
         w_base = getattr(self, 'w_%s' % base)
         w_dict = space.newdict()
         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)
             return Suite.from_object(space, w_node)
         raise operationerrfmt(space.w_TypeError,
                 "Expected mod node, got %T", w_node)
-State.ast_type('mod', 'AST', None)
+State.ast_type('mod', 'AST', None, [])
 
 class Module(mod):
 
             return Continue.from_object(space, w_node)
         raise operationerrfmt(space.w_TypeError,
                 "Expected stmt node, got %T", w_node)
-State.ast_type('stmt', 'AST', None)
+State.ast_type('stmt', 'AST', None, ['lineno', 'col_offset'])
 
 class FunctionDef(stmt):
 
             return Const.from_object(space, w_node)
         raise operationerrfmt(space.w_TypeError,
                 "Expected expr node, got %T", w_node)
-State.ast_type('expr', 'AST', None)
+State.ast_type('expr', 'AST', None, ['lineno', 'col_offset'])
 
 class BoolOp(expr):
 
             return Index.from_object(space, w_node)
         raise operationerrfmt(space.w_TypeError,
                 "Expected slice node, got %T", w_node)
-State.ast_type('slice', 'AST', None)
+State.ast_type('slice', 'AST', None, [])
 
 class Ellipsis(slice):
 
             return ExceptHandler.from_object(space, w_node)
         raise operationerrfmt(space.w_TypeError,
                 "Expected excepthandler node, got %T", w_node)
-State.ast_type('excepthandler', 'AST', None)
+State.ast_type('excepthandler', 'AST', None, ['lineno', 'col_offset'])
 
 class ExceptHandler(excepthandler):
 

File pypy/interpreter/astcompiler/test/test_ast.py

View file
         assert node.n is value
 
     def test_fields(self, space):
-        w_fields = space.getattr(ast.get(space).w_FunctionDef, space.wrap("_fields"))
-        assert (space.listview_str(w_fields) == 
-                ['name', 'args', 'body', 'decorator_list'])
+        w_fields = space.getattr(ast.get(space).w_FunctionDef,
+                                 space.wrap("_fields"))
+        assert space.eq_w(w_fields, space.wrap(
+            ('name', 'args', 'body', 'decorator_list')))
         
+    def test_attributes(self, space):
+        w_attrs = space.getattr(ast.get(space).w_FunctionDef,
+                                space.wrap("_attributes"))
+        assert space.eq_w(w_attrs, space.wrap(('lineno', 'col_offset')))
+        

File pypy/interpreter/astcompiler/tools/asdl_py.py

View file
                           % (typ.name,), 3)
             self.emit("raise operationerrfmt(space.w_TypeError,", 2)
             self.emit("        \"Expected %s node, got %%T\", w_node)" % (base,), 2)
-            self.emit("State.ast_type('%r', 'AST', None)" % (base,))
+            self.emit("State.ast_type('%r', 'AST', None, %s)" %
+                      (base, [repr(attr.name) for attr in sum.attributes]))
             self.emit("")
             for cons in sum.types:
                 self.visit(cons, base, sum.attributes)
     AST_TYPES = []
 
     @classmethod
-    def ast_type(cls, name, base, fields):
-        cls.AST_TYPES.append((name, base, fields))
+    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) in self.AST_TYPES:
-            self.make_new_type(space, name, base, fields)
+        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):
+    def make_new_type(self, space, name, base, fields, attributes):
         w_base = getattr(self, 'w_%s' % base)
         w_dict = space.newdict()
         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)

File pypy/module/_ast/__init__.py

View file
 def _setup():
     defs = Module.interpleveldefs
     defs['AST'] = "pypy.interpreter.astcompiler.ast.get(space).w_AST"
-    for (name, base, fields) in ast.State.AST_TYPES:
+    for (name, base, fields, attributes) in ast.State.AST_TYPES:
         defs[name] = "pypy.interpreter.astcompiler.ast.get(space).w_" + name
 _setup()