From 06a30603499bab63345f2755eeb6cc37fa302df9 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Tue, 5 Aug 2008 11:15:02 +0200 Subject: [PATCH] Decorators: Added capability to refer to attributes as well (like in Python). --- Cython/Compiler/Parsing.py | 10 +++++++--- tests/errors/e_decorators.pyx | 12 ++++++++++++ tests/run/decorators.pyx | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/errors/e_decorators.pyx diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 949438bd..98b3fdff 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2125,9 +2125,13 @@ def p_decorators(s): while s.sy == 'DECORATOR': pos = s.position() s.next() - decorator = ExprNodes.NameNode( - pos, name = Utils.EncodedString( - p_dotted_name(s, as_allowed=0)[2] )) + decstring = p_dotted_name(s, as_allowed=0)[2] + names = decstring.split('.') + 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 == '(': decorator = p_call(s, decorator) decorators.append(Nodes.DecoratorNode(pos, decorator=decorator)) diff --git a/tests/errors/e_decorators.pyx b/tests/errors/e_decorators.pyx new file mode 100644 index 00000000..85d83c6e --- /dev/null +++ b/tests/errors/e_decorators.pyx @@ -0,0 +1,12 @@ + +_ERRORS = u""" +4:4 Expected a newline after decorator +""" + + +class A: + pass + +@A().a +def f(): + pass diff --git a/tests/run/decorators.pyx b/tests/run/decorators.pyx index 6a5eebe5..85834eb8 100644 --- a/tests/run/decorators.pyx +++ b/tests/run/decorators.pyx @@ -13,6 +13,10 @@ __doc__ = u""" 6 >>> h.HERE 1 + >>> i(4) + 3 + >>> i.HERE + 1 """ class wrap: @@ -47,3 +51,13 @@ def g(a,b): @decorate2(1,2) def h(a,b): return a+b+3 + +class A: + def decorate(self, func): + return decorate(func) + + +a = A() +@a.decorate +def i(x): + return x - 1 -- 2.26.2