Decorators: Added capability to refer to attributes as well (like in Python).
authorDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Tue, 5 Aug 2008 09:15:02 +0000 (11:15 +0200)
committerDag Sverre Seljebotn <dagss@student.matnat.uio.no>
Tue, 5 Aug 2008 09:15:02 +0000 (11:15 +0200)
Cython/Compiler/Parsing.py
tests/errors/e_decorators.pyx [new file with mode: 0644]
tests/run/decorators.pyx

index 949438bd1d63c5d78c2a1724300f25450ca4be9c..98b3fdff5150b98a85c5c4007c6d3179204d5424 100644 (file)
@@ -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 (file)
index 0000000..85d83c6
--- /dev/null
@@ -0,0 +1,12 @@
+
+_ERRORS = u"""
+4:4 Expected a newline after decorator
+"""
+
+
+class A:
+    pass
+
+@A().a
+def f():
+    pass
index 6a5eebe503dc1d4438e6e7aa1717134db07d753a..85834eb8bd67bd0a400d6c4ec15c5d26530451cb 100644 (file)
@@ -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