Commit d16e81aa authored by Guido van Rossum's avatar Guido van Rossum

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.
parent 801dd736
......@@ -311,9 +311,12 @@ class CallFunc(Node):
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 @@ class Class(Node):
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):
......
......@@ -786,7 +786,6 @@ class StackDepthTracker:
'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 @@ class StackDepthTracker:
'SETUP_FINALLY': 3,
'FOR_ITER': 1,
'WITH_CLEANUP': -1,
'LOAD_BUILD_CLASS': 1,
'STORE_LOCALS': -1,
}
# use pattern match
patterns = [
......
......@@ -435,13 +435,10 @@ class CodeGenerator:
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 @@ class CodeGenerator:
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 @@ class AbstractClassCode:
def finish(self):
self.graph.startExitBlock()
self.emit('LOAD_LOCALS')
self.emit('LOAD_CONST', None)
self.emit('RETURN_VALUE')
class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator):
......
......@@ -299,7 +299,7 @@ class SymbolVisitor:
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):
......
......@@ -288,16 +288,16 @@ class Transformer:
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 @@ class Transformer:
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])
......
......@@ -144,13 +144,9 @@ def run_tests():
(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 #####
......
......@@ -50,8 +50,8 @@ class CompilerTest(unittest.TestCase):
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment