Commit 06a30603 authored by Dag Sverre Seljebotn's avatar Dag Sverre Seljebotn

Decorators: Added capability to refer to attributes as well (like in Python).

parent 0817b5c8
...@@ -2125,9 +2125,13 @@ def p_decorators(s): ...@@ -2125,9 +2125,13 @@ def p_decorators(s):
while s.sy == 'DECORATOR': while s.sy == 'DECORATOR':
pos = s.position() pos = s.position()
s.next() s.next()
decorator = ExprNodes.NameNode( decstring = p_dotted_name(s, as_allowed=0)[2]
pos, name = Utils.EncodedString( names = decstring.split('.')
p_dotted_name(s, as_allowed=0)[2] )) decorator = ExprNodes.NameNode(pos, name=Utils.EncodedString(names[0]))
for name in names[1:]:
decorator = ExprNodes.AttributeNode(pos,
attribute=Utils.EncodedString(name),
obj=decorator)
if s.sy == '(': if s.sy == '(':
decorator = p_call(s, decorator) decorator = p_call(s, decorator)
decorators.append(Nodes.DecoratorNode(pos, decorator=decorator)) decorators.append(Nodes.DecoratorNode(pos, decorator=decorator))
......
_ERRORS = u"""
4:4 Expected a newline after decorator
"""
class A:
pass
@A().a
def f():
pass
...@@ -13,6 +13,10 @@ __doc__ = u""" ...@@ -13,6 +13,10 @@ __doc__ = u"""
6 6
>>> h.HERE >>> h.HERE
1 1
>>> i(4)
3
>>> i.HERE
1
""" """
class wrap: class wrap:
...@@ -47,3 +51,13 @@ def g(a,b): ...@@ -47,3 +51,13 @@ def g(a,b):
@decorate2(1,2) @decorate2(1,2)
def h(a,b): def h(a,b):
return a+b+3 return a+b+3
class A:
def decorate(self, func):
return decorate(func)
a = A()
@a.decorate
def i(x):
return x - 1
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