[pypy-commit] pypy py3k: Assign correct scope for expressions used in the class header

amauryfa noreply at buildbot.pypy.org
Fri Feb 3 00:34:53 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r52042:873f1a85f982
Date: 2012-02-02 21:47 +0100
http://bitbucket.org/pypy/pypy/changeset/873f1a85f982/

Log:	Assign correct scope for expressions used in the class header class
	X(__metaclass=expr1, *expr2, **expr3)

	Remember to do the same thing when we add argument annotations!

diff --git a/pypy/interpreter/astcompiler/symtable.py b/pypy/interpreter/astcompiler/symtable.py
--- a/pypy/interpreter/astcompiler/symtable.py
+++ b/pypy/interpreter/astcompiler/symtable.py
@@ -370,6 +370,11 @@
     def visit_ClassDef(self, clsdef):
         self.note_symbol(clsdef.name, SYM_ASSIGNED)
         self.visit_sequence(clsdef.bases)
+        self.visit_sequence(clsdef.keywords)
+        if clsdef.starargs:
+            clsdef.starargs.walkabout(self)
+        if clsdef.kwargs:
+            clsdef.kwargs.walkabout(self)
         self.visit_sequence(clsdef.decorator_list)
         self.push_scope(ClassScope(clsdef), clsdef)
         self.note_symbol('@__class__', SYM_ASSIGNED)
diff --git a/pypy/interpreter/astcompiler/test/test_symtable.py b/pypy/interpreter/astcompiler/test/test_symtable.py
--- a/pypy/interpreter/astcompiler/test/test_symtable.py
+++ b/pypy/interpreter/astcompiler/test/test_symtable.py
@@ -217,6 +217,15 @@
         xscp = cscp.children[1]
         assert xscp.lookup("n") == symtable.SCOPE_FREE
 
+    def test_class_kwargs(self):
+        scp = self.func_scope("""def f(n):
+            class X(meta=Z, *args, **kwargs):
+                 pass""")
+        assert scp.lookup("X") == symtable.SCOPE_LOCAL
+        assert scp.lookup("Z") == symtable.SCOPE_GLOBAL_IMPLICIT
+        assert scp.lookup("args") == symtable.SCOPE_GLOBAL_IMPLICIT
+        assert scp.lookup("kwargs") == symtable.SCOPE_GLOBAL_IMPLICIT
+
     def test_lambda(self):
         scp = self.mod_scope("lambda x: y")
         self.check_unknown(scp, "x", "y")
diff --git a/pypy/interpreter/astcompiler/tools/Python.asdl b/pypy/interpreter/astcompiler/tools/Python.asdl
--- a/pypy/interpreter/astcompiler/tools/Python.asdl
+++ b/pypy/interpreter/astcompiler/tools/Python.asdl
@@ -1,4 +1,4 @@
--- ASDL's five builtin types are identifier, int, string, object, bool
+-- ASDL's four builtin types are identifier, int, string, object
 
 module Python version "$Revision: 43614 $"
 {
@@ -103,7 +103,7 @@
 
 	-- not sure what to call the first argument for raise and except
 	excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
-	                attributes(int lineno, int col_offset)
+	                attributes (int lineno, int col_offset)
 
 	arguments = (expr* args, identifier? vararg, expr* kwonlyargs, 
 		     identifier? kwarg, expr* defaults)
@@ -114,3 +114,4 @@
         -- import name with optional 'as' alias.
         alias = (identifier name, identifier? asname)
 }
+
diff --git a/pypy/module/cpyext/test/test_cpyext.py b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -204,7 +204,8 @@
         filename.
         """
         name = name.encode()
-        init = init.encode()
+        if init is not None:
+            init = init.encode()
         body = body.encode()
         if init is not None:
             code = """


More information about the pypy-commit mailing list