[pypy-commit] pypy split-ast-classes: Add _attributes

amauryfa noreply at buildbot.pypy.org
Wed Jun 26 23:22:13 CEST 2013


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: split-ast-classes
Changeset: r65021:c8620404fbf2
Date: 2013-06-24 23:39 +0200
http://bitbucket.org/pypy/pypy/changeset/c8620404fbf2/

Log:	Add _attributes

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
@@ -112,20 +112,23 @@
     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)
@@ -149,7 +152,7 @@
             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):
 
@@ -334,7 +337,7 @@
             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):
 
@@ -1450,7 +1453,7 @@
             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):
 
@@ -2507,7 +2510,7 @@
             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):
 
@@ -2979,7 +2982,7 @@
             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):
 
diff --git a/pypy/interpreter/astcompiler/test/test_ast.py b/pypy/interpreter/astcompiler/test/test_ast.py
--- a/pypy/interpreter/astcompiler/test/test_ast.py
+++ b/pypy/interpreter/astcompiler/test/test_ast.py
@@ -38,7 +38,13 @@
         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')))
+        
diff --git a/pypy/interpreter/astcompiler/tools/asdl_py.py b/pypy/interpreter/astcompiler/tools/asdl_py.py
--- a/pypy/interpreter/astcompiler/tools/asdl_py.py
+++ b/pypy/interpreter/astcompiler/tools/asdl_py.py
@@ -101,7 +101,8 @@
                           % (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)
@@ -489,20 +490,23 @@
     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)
diff --git a/pypy/module/_ast/__init__.py b/pypy/module/_ast/__init__.py
--- a/pypy/module/_ast/__init__.py
+++ b/pypy/module/_ast/__init__.py
@@ -14,6 +14,6 @@
 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()


More information about the pypy-commit mailing list