mirror of https://github.com/python/cpython.git
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.
This commit is contained in:
parent
801dd73653
commit
d16e81aabe
|
@ -311,9 +311,12 @@ def __repr__(self):
|
||||||
return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
|
return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
|
||||||
|
|
||||||
class Class(Node):
|
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.name = name
|
||||||
self.bases = bases
|
self.args = args
|
||||||
|
self.star_args = star_args
|
||||||
|
self.dstar_args = dstar_args
|
||||||
self.doc = doc
|
self.doc = doc
|
||||||
self.code = code
|
self.code = code
|
||||||
self.lineno = lineno
|
self.lineno = lineno
|
||||||
|
@ -321,19 +324,30 @@ def __init__(self, name, bases, doc, code, lineno=None):
|
||||||
def getChildren(self):
|
def getChildren(self):
|
||||||
children = []
|
children = []
|
||||||
children.append(self.name)
|
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.doc)
|
||||||
children.append(self.code)
|
children.append(self.code)
|
||||||
return tuple(children)
|
return tuple(children)
|
||||||
|
|
||||||
def getChildNodes(self):
|
def getChildNodes(self):
|
||||||
nodelist = []
|
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)
|
nodelist.append(self.code)
|
||||||
return tuple(nodelist)
|
return tuple(nodelist)
|
||||||
|
|
||||||
def __repr__(self):
|
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):
|
class Compare(Node):
|
||||||
def __init__(self, expr, ops, lineno=None):
|
def __init__(self, expr, ops, lineno=None):
|
||||||
|
|
|
@ -786,7 +786,6 @@ def findDepth(self, insts, debug=0):
|
||||||
'PRINT_EXPR': -1,
|
'PRINT_EXPR': -1,
|
||||||
'RETURN_VALUE': -1,
|
'RETURN_VALUE': -1,
|
||||||
'YIELD_VALUE': -1,
|
'YIELD_VALUE': -1,
|
||||||
'BUILD_CLASS': -2,
|
|
||||||
'STORE_NAME': -1,
|
'STORE_NAME': -1,
|
||||||
'STORE_ATTR': -2,
|
'STORE_ATTR': -2,
|
||||||
'DELETE_ATTR': -1,
|
'DELETE_ATTR': -1,
|
||||||
|
@ -804,6 +803,8 @@ def findDepth(self, insts, debug=0):
|
||||||
'SETUP_FINALLY': 3,
|
'SETUP_FINALLY': 3,
|
||||||
'FOR_ITER': 1,
|
'FOR_ITER': 1,
|
||||||
'WITH_CLEANUP': -1,
|
'WITH_CLEANUP': -1,
|
||||||
|
'LOAD_BUILD_CLASS': 1,
|
||||||
|
'STORE_LOCALS': -1,
|
||||||
}
|
}
|
||||||
# use pattern match
|
# use pattern match
|
||||||
patterns = [
|
patterns = [
|
||||||
|
|
|
@ -435,13 +435,10 @@ def visitClass(self, node):
|
||||||
walk(node.code, gen)
|
walk(node.code, gen)
|
||||||
gen.finish()
|
gen.finish()
|
||||||
self.set_lineno(node)
|
self.set_lineno(node)
|
||||||
self.emit('LOAD_CONST', node.name)
|
self.emit('LOAD_BUILD_CLASS')
|
||||||
for base in node.bases:
|
|
||||||
self.visit(base)
|
|
||||||
self.emit('BUILD_TUPLE', len(node.bases))
|
|
||||||
self._makeClosure(gen, 0)
|
self._makeClosure(gen, 0)
|
||||||
self.emit('CALL_FUNCTION', 0)
|
self.emit('LOAD_CONST', node.name)
|
||||||
self.emit('BUILD_CLASS')
|
self.finish_visit_call(node, 2)
|
||||||
self.storeName(node.name)
|
self.storeName(node.name)
|
||||||
|
|
||||||
# The rest are standard visitor methods
|
# The rest are standard visitor methods
|
||||||
|
@ -1115,10 +1112,11 @@ def visitAugSubscript(self, node, mode):
|
||||||
self.emit('STORE_SUBSCR')
|
self.emit('STORE_SUBSCR')
|
||||||
|
|
||||||
def visitCallFunc(self, node):
|
def visitCallFunc(self, node):
|
||||||
pos = 0
|
|
||||||
kw = 0
|
|
||||||
self.set_lineno(node)
|
self.set_lineno(node)
|
||||||
self.visit(node.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:
|
for arg in node.args:
|
||||||
self.visit(arg)
|
self.visit(arg)
|
||||||
if isinstance(arg, ast.Keyword):
|
if isinstance(arg, ast.Keyword):
|
||||||
|
@ -1467,7 +1465,7 @@ def get_module(self):
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
self.graph.startExitBlock()
|
self.graph.startExitBlock()
|
||||||
self.emit('LOAD_LOCALS')
|
self.emit('LOAD_CONST', None)
|
||||||
self.emit('RETURN_VALUE')
|
self.emit('RETURN_VALUE')
|
||||||
|
|
||||||
class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator):
|
class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator):
|
||||||
|
|
|
@ -299,7 +299,7 @@ def handle_free_vars(self, scope, parent):
|
||||||
|
|
||||||
def visitClass(self, node, parent):
|
def visitClass(self, node, parent):
|
||||||
parent.add_def(node.name)
|
parent.add_def(node.name)
|
||||||
for n in node.bases:
|
for n in node.args:
|
||||||
self.visit(n, parent)
|
self.visit(n, parent)
|
||||||
scope = ClassScope(node.name, self.module)
|
scope = ClassScope(node.name, self.module)
|
||||||
if parent.nested or isinstance(parent, FunctionScope):
|
if parent.nested or isinstance(parent, FunctionScope):
|
||||||
|
|
|
@ -288,16 +288,16 @@ def lambdef(self, nodelist):
|
||||||
old_lambdef = lambdef
|
old_lambdef = lambdef
|
||||||
|
|
||||||
def classdef(self, nodelist):
|
def classdef(self, nodelist):
|
||||||
# classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
|
# classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
||||||
|
|
||||||
name = nodelist[1][1]
|
name = nodelist[1][1]
|
||||||
doc = self.get_docstring(nodelist[-1])
|
doc = self.get_docstring(nodelist[-1])
|
||||||
if nodelist[2][0] == token.COLON:
|
if nodelist[2][0] == token.COLON:
|
||||||
bases = []
|
arglist = CallFunc(None, [])
|
||||||
elif nodelist[3][0] == token.RPAR:
|
elif nodelist[3][0] == token.RPAR:
|
||||||
bases = []
|
arglist = CallFunc(None, [])
|
||||||
else:
|
else:
|
||||||
bases = self.com_bases(nodelist[3])
|
arglist = self.com_call_function(None, nodelist[3])
|
||||||
|
|
||||||
# code for class
|
# code for class
|
||||||
code = self.com_node(nodelist[-1])
|
code = self.com_node(nodelist[-1])
|
||||||
|
@ -307,7 +307,8 @@ def classdef(self, nodelist):
|
||||||
assert isinstance(code.nodes[0], Discard)
|
assert isinstance(code.nodes[0], Discard)
|
||||||
del code.nodes[0]
|
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):
|
def stmt(self, nodelist):
|
||||||
return self.com_stmt(nodelist[0])
|
return self.com_stmt(nodelist[0])
|
||||||
|
|
|
@ -144,13 +144,9 @@ def run_tests():
|
||||||
(eval_tests, eval_results, "eval")):
|
(eval_tests, eval_results, "eval")):
|
||||||
for i, o in itertools.izip(input, output):
|
for i, o in itertools.izip(input, output):
|
||||||
ast_tree = compile(i, "?", kind, 0x400)
|
ast_tree = compile(i, "?", kind, 0x400)
|
||||||
if to_tuple(ast_tree) != o:
|
tup = to_tuple(ast_tree)
|
||||||
print("i=", i)
|
assert tup == o, ("kind=%r\ninput=%r\nexpected=%r\ngot=%r" %
|
||||||
print("o=", o)
|
(kind, i, o, tup))
|
||||||
print("kind=", kind)
|
|
||||||
print("tree=", ast_tree)
|
|
||||||
print("tuple=", to_tuple(ast_tree))
|
|
||||||
assert to_tuple(ast_tree) == o
|
|
||||||
test_order(ast_tree, (0, 0))
|
test_order(ast_tree, (0, 0))
|
||||||
|
|
||||||
#### EVERYTHING BELOW IS GENERATED #####
|
#### EVERYTHING BELOW IS GENERATED #####
|
||||||
|
|
|
@ -50,8 +50,8 @@ def testCompileLibrary(self):
|
||||||
try:
|
try:
|
||||||
compiler.compile(buf, basename, "exec")
|
compiler.compile(buf, basename, "exec")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
args = list(e.args)
|
args = list(e.args) or [""]
|
||||||
args[0] += "[in file %s]" % basename
|
args[0] = "%s [in file %s]" % (args[0], basename)
|
||||||
e.args = tuple(args)
|
e.args = tuple(args)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue