Skip to content

Commit 212e1a0

Browse files
author
facundo.batista
committed
Issue 2117. Update compiler module to handle class decorators. Thanks Thomas Herve git-svn-id: http://svn.python.org/projects/python/trunk@61067 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 10bf9fb commit 212e1a0

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

Lib/compiler/ast.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,12 @@ def __repr__(self):
308308
return "CallFunc(%s, %s, %s, %s)" % (repr(self.node), repr(self.args), repr(self.star_args), repr(self.dstar_args))
309309

310310
class Class(Node):
311-
def __init__(self, name, bases, doc, code, lineno=None):
311+
def __init__(self, name, bases, doc, code, decorators = None, lineno=None):
312312
self.name = name
313313
self.bases = bases
314314
self.doc = doc
315315
self.code = code
316+
self.decorators = decorators
316317
self.lineno = lineno
317318

318319
def getChildren(self):
@@ -321,16 +322,19 @@ def getChildren(self):
321322
children.extend(flatten(self.bases))
322323
children.append(self.doc)
323324
children.append(self.code)
325+
children.append(self.decorators)
324326
return tuple(children)
325327

326328
def getChildNodes(self):
327329
nodelist = []
328330
nodelist.extend(flatten_nodes(self.bases))
329331
nodelist.append(self.code)
332+
if self.decorators is not None:
333+
nodelist.append(self.decorators)
330334
return tuple(nodelist)
331335

332336
def __repr__(self):
333-
return "Class(%s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code))
337+
return "Class(%s, %s, %s, %s, %s)" % (repr(self.name), repr(self.bases), repr(self.doc), repr(self.code), repr(self.decorators))
334338

335339
class Compare(Node):
336340
def __init__(self, expr, ops, lineno=None):

Lib/compiler/transformer.py

+12
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ def decorators(self, nodelist):
232232
items.append(self.decorator(dec_nodelist[1:]))
233233
return Decorators(items)
234234

235+
def decorated(self, nodelist):
236+
assert nodelist[0][0] == symbol.decorators
237+
if nodelist[1][0] == symbol.funcdef:
238+
n = [nodelist[0]] + list(nodelist[1][1:])
239+
return self.funcdef(n)
240+
elif nodelist[1][0] == symbol.classdef:
241+
decorators = self.decorators(nodelist[0][1:])
242+
cls = self.classdef(nodelist[1][1:])
243+
cls.decorators = decorators
244+
return cls
245+
raise WalkerError()
246+
235247
def funcdef(self, nodelist):
236248
# -6 -5 -4 -3 -2 -1
237249
# funcdef: [decorators] 'def' NAME parameters ':' suite

Lib/token.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
for _name, _value in globals().items():
7272
if type(_value) is type(0):
7373
tok_name[_value] = _name
74+
del _name, _value
7475

7576

7677
def ISTERMINAL(x):

Tools/compiler/ast.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Stmt: nodes!
1414
Decorators: nodes!
1515
Function: decorators&, name*, argnames*, defaults!, flags*, doc*, code
1616
Lambda: argnames*, defaults!, flags*, code
17-
Class: name*, bases!, doc*, code
17+
Class: name*, bases!, doc*, code, decorators& = None
1818
Pass:
1919
Break:
2020
Continue:
@@ -97,7 +97,7 @@ init(Lambda):
9797
self.kwargs = 1
9898

9999
init(GenExpr):
100-
self.argnames = ['[outmost-iterable]']
100+
self.argnames = ['.0']
101101
self.varargs = self.kwargs = None
102102

103103
init(GenExprFor):

0 commit comments

Comments
 (0)