[Python-3000-checkins] r54438 - in python/branches/p3yk/Lib: compiler/ast.py compiler/pyassem.py compiler/pycodegen.py compiler/symbols.py compiler/transformer.py test/test_ast.py test/test_compiler.py

guido.van.rossum python-3000-checkins at python.org
Mon Mar 19 18:56:08 CET 2007


Author: guido.van.rossum
Date: Mon Mar 19 18:56:01 2007
New Revision: 54438

Modified:
   python/branches/p3yk/Lib/compiler/ast.py
   python/branches/p3yk/Lib/compiler/pyassem.py
   python/branches/p3yk/Lib/compiler/pycodegen.py
   python/branches/p3yk/Lib/compiler/symbols.py
   python/branches/p3yk/Lib/compiler/transformer.py
   python/branches/p3yk/Lib/test/test_ast.py
   python/branches/p3yk/Lib/test/test_compiler.py
Log:
Fix the compiler package w.r.t. the new metaclass syntax.
(It is still broken w.r.t. the new nonlocal keyword.)

Remove a series of debug prints I accidentally left in test_ast.py.


Modified: python/branches/p3yk/Lib/compiler/ast.py
==============================================================================
--- python/branches/p3yk/Lib/compiler/ast.py	(original)
+++ python/branches/p3yk/Lib/compiler/ast.py	Mon Mar 19 18:56:01 2007
@@ -311,9 +311,12 @@
         return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
 
 class Class(Node):
-    def __init__(self, name, bases, doc, code, lineno=None):
+    def __init__(self, name, args, star_args, dstar_args,
+                 doc, code, lineno=None):
         self.name = name
-        self.bases = bases
+        self.args = args
+        self.star_args = star_args
+        self.dstar_args = dstar_args
         self.doc = doc
         self.code = code
         self.lineno = lineno
@@ -321,19 +324,30 @@
     def getChildren(self):
         children = []
         children.append(self.name)
-        children.extend(flatten(self.bases))
+        children.extend(flatten(self.args))
+        children.extend(self.star_args)
+        children.extend(self.dstar_args)
         children.append(self.doc)
         children.append(self.code)
         return tuple(children)
 
     def getChildNodes(self):
         nodelist = []
-        nodelist.extend(flatten_nodes(self.bases))
+        nodelist.extend(flatten_nodes(self.args))
+        if self.star_args is not None:
+            nodelist.append(self.star_args)
+        if self.dstar_args is not None:
+            nodelist.append(self.dstar_args)
         nodelist.append(self.code)
         return tuple(nodelist)
 
     def __repr__(self):
-        return "Class(%s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code))
+        return "Class(%r, %r, %r, %r, %r, %r)" % (self.name,
+                                                  self.args,
+                                                  self.star_args,
+                                                  self.dstar_args,
+                                                  self.doc,
+                                                  self.code)
 
 class Compare(Node):
     def __init__(self, expr, ops, lineno=None):

Modified: python/branches/p3yk/Lib/compiler/pyassem.py
==============================================================================
--- python/branches/p3yk/Lib/compiler/pyassem.py	(original)
+++ python/branches/p3yk/Lib/compiler/pyassem.py	Mon Mar 19 18:56:01 2007
@@ -786,7 +786,6 @@
         'PRINT_EXPR': -1,
         'RETURN_VALUE': -1,
         'YIELD_VALUE': -1,
-        'BUILD_CLASS': -2,
         'STORE_NAME': -1,
         'STORE_ATTR': -2,
         'DELETE_ATTR': -1,
@@ -804,6 +803,8 @@
         'SETUP_FINALLY': 3,
         'FOR_ITER': 1,
         'WITH_CLEANUP': -1,
+        'LOAD_BUILD_CLASS': 1,
+        'STORE_LOCALS': -1,
         }
     # use pattern match
     patterns = [

Modified: python/branches/p3yk/Lib/compiler/pycodegen.py
==============================================================================
--- python/branches/p3yk/Lib/compiler/pycodegen.py	(original)
+++ python/branches/p3yk/Lib/compiler/pycodegen.py	Mon Mar 19 18:56:01 2007
@@ -435,13 +435,10 @@
         walk(node.code, gen)
         gen.finish()
         self.set_lineno(node)
-        self.emit('LOAD_CONST', node.name)
-        for base in node.bases:
-            self.visit(base)
-        self.emit('BUILD_TUPLE', len(node.bases))
+        self.emit('LOAD_BUILD_CLASS')
         self._makeClosure(gen, 0)
-        self.emit('CALL_FUNCTION', 0)
-        self.emit('BUILD_CLASS')
+        self.emit('LOAD_CONST', node.name)
+        self.finish_visit_call(node, 2)
         self.storeName(node.name)
 
     # The rest are standard visitor methods
@@ -1115,10 +1112,11 @@
             self.emit('STORE_SUBSCR')
 
     def visitCallFunc(self, node):
-        pos = 0
-        kw = 0
         self.set_lineno(node)
         self.visit(node.node)
+        self.finish_visit_call(node)
+
+    def finish_visit_call(self, node, pos=0, kw=0):
         for arg in node.args:
             self.visit(arg)
             if isinstance(arg, ast.Keyword):
@@ -1467,7 +1465,7 @@
 
     def finish(self):
         self.graph.startExitBlock()
-        self.emit('LOAD_LOCALS')
+        self.emit('LOAD_CONST', None)
         self.emit('RETURN_VALUE')
 
 class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator):

Modified: python/branches/p3yk/Lib/compiler/symbols.py
==============================================================================
--- python/branches/p3yk/Lib/compiler/symbols.py	(original)
+++ python/branches/p3yk/Lib/compiler/symbols.py	Mon Mar 19 18:56:01 2007
@@ -299,7 +299,7 @@
 
     def visitClass(self, node, parent):
         parent.add_def(node.name)
-        for n in node.bases:
+        for n in node.args:
             self.visit(n, parent)
         scope = ClassScope(node.name, self.module)
         if parent.nested or isinstance(parent, FunctionScope):

Modified: python/branches/p3yk/Lib/compiler/transformer.py
==============================================================================
--- python/branches/p3yk/Lib/compiler/transformer.py	(original)
+++ python/branches/p3yk/Lib/compiler/transformer.py	Mon Mar 19 18:56:01 2007
@@ -288,16 +288,16 @@
     old_lambdef = lambdef
 
     def classdef(self, nodelist):
-        # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
+        # classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
 
         name = nodelist[1][1]
         doc = self.get_docstring(nodelist[-1])
         if nodelist[2][0] == token.COLON:
-            bases = []
+            arglist = CallFunc(None, [])
         elif nodelist[3][0] == token.RPAR:
-            bases = []
+            arglist = CallFunc(None, [])
         else:
-            bases = self.com_bases(nodelist[3])
+            arglist = self.com_call_function(None, nodelist[3])
 
         # code for class
         code = self.com_node(nodelist[-1])
@@ -307,7 +307,8 @@
             assert isinstance(code.nodes[0], Discard)
             del code.nodes[0]
 
-        return Class(name, bases, doc, code, lineno=nodelist[1][2])
+        return Class(name, arglist.args, arglist.star_args, arglist.dstar_args,
+                     doc, code, lineno=nodelist[1][2])
 
     def stmt(self, nodelist):
         return self.com_stmt(nodelist[0])

Modified: python/branches/p3yk/Lib/test/test_ast.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_ast.py	(original)
+++ python/branches/p3yk/Lib/test/test_ast.py	Mon Mar 19 18:56:01 2007
@@ -144,13 +144,9 @@
                                 (eval_tests, eval_results, "eval")):
         for i, o in itertools.izip(input, output):
             ast_tree = compile(i, "?", kind, 0x400)
-            if to_tuple(ast_tree) != o:
-                print("i=", i)
-                print("o=", o)
-                print("kind=", kind)
-                print("tree=", ast_tree)
-                print("tuple=", to_tuple(ast_tree))
-            assert to_tuple(ast_tree) == o
+            tup = to_tuple(ast_tree)
+            assert tup == o, ("kind=%r\ninput=%r\nexpected=%r\ngot=%r" %
+                              (kind, i, o, tup))
             test_order(ast_tree, (0, 0))
 
 #### EVERYTHING BELOW IS GENERATED #####

Modified: python/branches/p3yk/Lib/test/test_compiler.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_compiler.py	(original)
+++ python/branches/p3yk/Lib/test/test_compiler.py	Mon Mar 19 18:56:01 2007
@@ -50,8 +50,8 @@
                     try:
                         compiler.compile(buf, basename, "exec")
                     except Exception as e:
-                        args = list(e.args)
-                        args[0] += "[in file %s]" % basename
+                        args = list(e.args) or [""]
+                        args[0] = "%s [in file %s]" % (args[0], basename)
                         e.args = tuple(args)
                         raise
 


More information about the Python-3000-checkins mailing list