Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d16e81a

Browse files
committed
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.
1 parent 801dd73 commit d16e81a

7 files changed

Lines changed: 40 additions & 30 deletions

File tree

Lib/compiler/ast.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,29 +311,43 @@ def __repr__(self):
311311
return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
312312

313313
class Class(Node):
314-
def __init__(self, name, bases, doc, code, lineno=None):
314+
def __init__(self, name, args, star_args, dstar_args,
315+
doc, code, lineno=None):
315316
self.name = name
316-
self.bases = bases
317+
self.args = args
318+
self.star_args = star_args
319+
self.dstar_args = dstar_args
317320
self.doc = doc
318321
self.code = code
319322
self.lineno = lineno
320323

321324
def getChildren(self):
322325
children = []
323326
children.append(self.name)
324-
children.extend(flatten(self.bases))
327+
children.extend(flatten(self.args))
328+
children.extend(self.star_args)
329+
children.extend(self.dstar_args)
325330
children.append(self.doc)
326331
children.append(self.code)
327332
return tuple(children)
328333

329334
def getChildNodes(self):
330335
nodelist = []
331-
nodelist.extend(flatten_nodes(self.bases))
336+
nodelist.extend(flatten_nodes(self.args))
337+
if self.star_args is not None:
338+
nodelist.append(self.star_args)
339+
if self.dstar_args is not None:
340+
nodelist.append(self.dstar_args)
332341
nodelist.append(self.code)
333342
return tuple(nodelist)
334343

335344
def __repr__(self):
336-
return "Class(%s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code))
345+
return "Class(%r, %r, %r, %r, %r, %r)" % (self.name,
346+
self.args,
347+
self.star_args,
348+
self.dstar_args,
349+
self.doc,
350+
self.code)
337351

338352
class Compare(Node):
339353
def __init__(self, expr, ops, lineno=None):

Lib/compiler/pyassem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ def findDepth(self, insts, debug=0):
786786
'PRINT_EXPR': -1,
787787
'RETURN_VALUE': -1,
788788
'YIELD_VALUE': -1,
789-
'BUILD_CLASS': -2,
790789
'STORE_NAME': -1,
791790
'STORE_ATTR': -2,
792791
'DELETE_ATTR': -1,
@@ -804,6 +803,8 @@ def findDepth(self, insts, debug=0):
804803
'SETUP_FINALLY': 3,
805804
'FOR_ITER': 1,
806805
'WITH_CLEANUP': -1,
806+
'LOAD_BUILD_CLASS': 1,
807+
'STORE_LOCALS': -1,
807808
}
808809
# use pattern match
809810
patterns = [

Lib/compiler/pycodegen.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,10 @@ def visitClass(self, node):
435435
walk(node.code, gen)
436436
gen.finish()
437437
self.set_lineno(node)
438-
self.emit('LOAD_CONST', node.name)
439-
for base in node.bases:
440-
self.visit(base)
441-
self.emit('BUILD_TUPLE', len(node.bases))
438+
self.emit('LOAD_BUILD_CLASS')
442439
self._makeClosure(gen, 0)
443-
self.emit('CALL_FUNCTION', 0)
444-
self.emit('BUILD_CLASS')
440+
self.emit('LOAD_CONST', node.name)
441+
self.finish_visit_call(node, 2)
445442
self.storeName(node.name)
446443

447444
# The rest are standard visitor methods
@@ -1115,10 +1112,11 @@ def visitAugSubscript(self, node, mode):
11151112
self.emit('STORE_SUBSCR')
11161113

11171114
def visitCallFunc(self, node):
1118-
pos = 0
1119-
kw = 0
11201115
self.set_lineno(node)
11211116
self.visit(node.node)
1117+
self.finish_visit_call(node)
1118+
1119+
def finish_visit_call(self, node, pos=0, kw=0):
11221120
for arg in node.args:
11231121
self.visit(arg)
11241122
if isinstance(arg, ast.Keyword):
@@ -1467,7 +1465,7 @@ def get_module(self):
14671465

14681466
def finish(self):
14691467
self.graph.startExitBlock()
1470-
self.emit('LOAD_LOCALS')
1468+
self.emit('LOAD_CONST', None)
14711469
self.emit('RETURN_VALUE')
14721470

14731471
class ClassCodeGenerator(NestedScopeMixin, AbstractClassCode, CodeGenerator):

Lib/compiler/symbols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def handle_free_vars(self, scope, parent):
299299

300300
def visitClass(self, node, parent):
301301
parent.add_def(node.name)
302-
for n in node.bases:
302+
for n in node.args:
303303
self.visit(n, parent)
304304
scope = ClassScope(node.name, self.module)
305305
if parent.nested or isinstance(parent, FunctionScope):

Lib/compiler/transformer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,16 @@ def lambdef(self, nodelist):
288288
old_lambdef = lambdef
289289

290290
def classdef(self, nodelist):
291-
# classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
291+
# classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
292292

293293
name = nodelist[1][1]
294294
doc = self.get_docstring(nodelist[-1])
295295
if nodelist[2][0] == token.COLON:
296-
bases = []
296+
arglist = CallFunc(None, [])
297297
elif nodelist[3][0] == token.RPAR:
298-
bases = []
298+
arglist = CallFunc(None, [])
299299
else:
300-
bases = self.com_bases(nodelist[3])
300+
arglist = self.com_call_function(None, nodelist[3])
301301

302302
# code for class
303303
code = self.com_node(nodelist[-1])
@@ -307,7 +307,8 @@ def classdef(self, nodelist):
307307
assert isinstance(code.nodes[0], Discard)
308308
del code.nodes[0]
309309

310-
return Class(name, bases, doc, code, lineno=nodelist[1][2])
310+
return Class(name, arglist.args, arglist.star_args, arglist.dstar_args,
311+
doc, code, lineno=nodelist[1][2])
311312

312313
def stmt(self, nodelist):
313314
return self.com_stmt(nodelist[0])

Lib/test/test_ast.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,9 @@ def run_tests():
144144
(eval_tests, eval_results, "eval")):
145145
for i, o in itertools.izip(input, output):
146146
ast_tree = compile(i, "?", kind, 0x400)
147-
if to_tuple(ast_tree) != o:
148-
print("i=", i)
149-
print("o=", o)
150-
print("kind=", kind)
151-
print("tree=", ast_tree)
152-
print("tuple=", to_tuple(ast_tree))
153-
assert to_tuple(ast_tree) == o
147+
tup = to_tuple(ast_tree)
148+
assert tup == o, ("kind=%r\ninput=%r\nexpected=%r\ngot=%r" %
149+
(kind, i, o, tup))
154150
test_order(ast_tree, (0, 0))
155151

156152
#### EVERYTHING BELOW IS GENERATED #####

Lib/test/test_compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def testCompileLibrary(self):
5050
try:
5151
compiler.compile(buf, basename, "exec")
5252
except Exception as e:
53-
args = list(e.args)
54-
args[0] += "[in file %s]" % basename
53+
args = list(e.args) or [""]
54+
args[0] = "%s [in file %s]" % (args[0], basename)
5555
e.args = tuple(args)
5656
raise
5757

0 commit comments

Comments
 (0)