annotate Lib/test/test_decorators.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 393822f2ef90
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
1 import unittest
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
2 from test import test_support
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
3
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
4 def funcattrs(**kwds):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
5 def decorate(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
6 func.__dict__.update(kwds)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
7 return func
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
8 return decorate
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
9
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
10 class MiscDecorators (object):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
11 @staticmethod
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
12 def author(name):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
13 def decorate(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
14 func.__dict__['author'] = name
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
15 return func
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
16 return decorate
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
17
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
18 # -----------------------------------------------
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
19
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
20 class DbcheckError (Exception):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
21 def __init__(self, exprstr, func, args, kwds):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
22 # A real version of this would set attributes here
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
23 Exception.__init__(self, "dbcheck %r failed (func=%s args=%s kwds=%s)" %
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
24 (exprstr, func, args, kwds))
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
25
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
26
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
27 def dbcheck(exprstr, globals=None, locals=None):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
28 "Decorator to implement debugging assertions"
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
29 def decorate(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
30 expr = compile(exprstr, "dbcheck-%s" % func.func_name, "eval")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
31 def check(*args, **kwds):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
32 if not eval(expr, globals, locals):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
33 raise DbcheckError(exprstr, func, args, kwds)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
34 return func(*args, **kwds)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
35 return check
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
36 return decorate
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
37
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
38 # -----------------------------------------------
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
39
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
40 def countcalls(counts):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
41 "Decorator to count calls to a function"
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
42 def decorate(func):
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
43 func_name = func.func_name
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
44 counts[func_name] = 0
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
45 def call(*args, **kwds):
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
46 counts[func_name] += 1
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
47 return func(*args, **kwds)
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
48 call.func_name = func_name
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
49 return call
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
50 return decorate
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
51
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
52 # -----------------------------------------------
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
53
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
54 def memoize(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
55 saved = {}
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
56 def call(*args):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
57 try:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
58 return saved[args]
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
59 except KeyError:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
60 res = func(*args)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
61 saved[args] = res
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
62 return res
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
63 except TypeError:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
64 # Unhashable argument
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
65 return func(*args)
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
66 call.func_name = func.func_name
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
67 return call
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
68
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
69 # -----------------------------------------------
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
70
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
71 class TestDecorators(unittest.TestCase):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
72
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
73 def test_single(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
74 class C(object):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
75 @staticmethod
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
76 def foo(): return 42
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
77 self.assertEqual(C.foo(), 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
78 self.assertEqual(C().foo(), 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
79
32893
51d7a12244af and a unit test for the staticmethod-of-a-non-method failure just fixed
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
80 def test_staticmethod_function(self):
51d7a12244af and a unit test for the staticmethod-of-a-non-method failure just fixed
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
81 @staticmethod
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
82 def notamethod(x):
32893
51d7a12244af and a unit test for the staticmethod-of-a-non-method failure just fixed
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
83 return x
51d7a12244af and a unit test for the staticmethod-of-a-non-method failure just fixed
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
84 self.assertRaises(TypeError, notamethod, 1)
51d7a12244af and a unit test for the staticmethod-of-a-non-method failure just fixed
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
85
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
86 def test_dotted(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
87 decorators = MiscDecorators()
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
88 @decorators.author('Cleese')
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
89 def foo(): return 42
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
90 self.assertEqual(foo(), 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
91 self.assertEqual(foo.author, 'Cleese')
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
92
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
93 def test_argforms(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
94 # A few tests of argument passing, as we use restricted form
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
95 # of expressions for decorators.
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
96
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
97 def noteargs(*args, **kwds):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
98 def decorate(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
99 setattr(func, 'dbval', (args, kwds))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
100 return func
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
101 return decorate
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
102
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
103 args = ( 'Now', 'is', 'the', 'time' )
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
104 kwds = dict(one=1, two=2)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
105 @noteargs(*args, **kwds)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
106 def f1(): return 42
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
107 self.assertEqual(f1(), 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
108 self.assertEqual(f1.dbval, (args, kwds))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
109
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
110 @noteargs('terry', 'gilliam', eric='idle', john='cleese')
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
111 def f2(): return 84
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
112 self.assertEqual(f2(), 84)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
113 self.assertEqual(f2.dbval, (('terry', 'gilliam'),
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
114 dict(eric='idle', john='cleese')))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
115
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
116 @noteargs(1, 2,)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
117 def f3(): pass
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
118 self.assertEqual(f3.dbval, ((1, 2), {}))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
119
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
120 def test_dbcheck(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
121 @dbcheck('args[1] is not None')
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
122 def f(a, b):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
123 return a + b
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
124 self.assertEqual(f(1, 2), 3)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
125 self.assertRaises(DbcheckError, f, 1, None)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
126
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
127 def test_memoize(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
128 counts = {}
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
129
33133
3db9a932775e Correct the order of application for decorators. Meant to be bottom-up and not
Brett Cannon <bcannon@gmail.com>
parents: 32949
diff changeset
130 @memoize
3db9a932775e Correct the order of application for decorators. Meant to be bottom-up and not
Brett Cannon <bcannon@gmail.com>
parents: 32949
diff changeset
131 @countcalls(counts)
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
132 def double(x):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
133 return x * 2
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
134 self.assertEqual(double.func_name, 'double')
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
135
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
136 self.assertEqual(counts, dict(double=0))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
137
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
138 # Only the first call with a given argument bumps the call count:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
139 #
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
140 self.assertEqual(double(2), 4)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
141 self.assertEqual(counts['double'], 1)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
142 self.assertEqual(double(2), 4)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
143 self.assertEqual(counts['double'], 1)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
144 self.assertEqual(double(3), 6)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
145 self.assertEqual(counts['double'], 2)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
146
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
147 # Unhashable arguments do not get memoized:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
148 #
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
149 self.assertEqual(double([10]), [10, 10])
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
150 self.assertEqual(counts['double'], 3)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
151 self.assertEqual(double([10]), [10, 10])
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
152 self.assertEqual(counts['double'], 4)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
153
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
154 def test_errors(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
155 # Test syntax restrictions - these are all compile-time errors:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
156 #
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
157 for expr in [ "1+2", "x[3]", "(1, 2)" ]:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
158 # Sanity check: is expr is a valid expression by itself?
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
159 compile(expr, "testexpr", "exec")
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
160
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
161 codestr = "@%s\ndef f(): pass" % expr
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
162 self.assertRaises(SyntaxError, compile, codestr, "test", "exec")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
163
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
164 # You can't put multiple decorators on a single line:
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
165 #
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
166 self.assertRaises(SyntaxError, compile,
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
167 "@f1 @f2\ndef f(): pass", "test", "exec")
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
168
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
169 # Test runtime errors
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
170
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
171 def unimp(func):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
172 raise NotImplementedError
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
173 context = dict(nullval=None, unimp=unimp)
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32893
diff changeset
174
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
175 for expr, exc in [ ("undef", NameError),
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
176 ("nullval", TypeError),
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
177 ("nullval.attr", AttributeError),
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
178 ("unimp", NotImplementedError)]:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
179 codestr = "@%s\ndef f(): pass\nassert f() is None" % expr
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
180 code = compile(codestr, "test", "exec")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
181 self.assertRaises(exc, eval, code, context)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
182
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
183 def test_double(self):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
184 class C(object):
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
185 @funcattrs(abc=1, xyz="haha")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
186 @funcattrs(booh=42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
187 def foo(self): return 42
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
188 self.assertEqual(C().foo(), 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
189 self.assertEqual(C.foo.abc, 1)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
190 self.assertEqual(C.foo.xyz, "haha")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
191 self.assertEqual(C.foo.booh, 42)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
192
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
193 def test_order(self):
33185
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
194 # Test that decorators are applied in the proper order to the function
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
195 # they are decorating.
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
196 def callnum(num):
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
197 """Decorator factory that returns a decorator that replaces the
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
198 passed-in function with one that returns the value of 'num'"""
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
199 def deco(func):
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
200 return lambda: num
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
201 return deco
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
202 @callnum(2)
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
203 @callnum(1)
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
204 def foo(): return 42
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
205 self.assertEqual(foo(), 2,
feb5247ba0c8 Rewrite test_order so as to be more "proper". Originally relied on an
Brett Cannon <bcannon@gmail.com>
parents: 33166
diff changeset
206 "Application order of decorators is incorrect")
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
207
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
208 def test_eval_order(self):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
209 # Evaluating a decorated function involves four steps for each
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
210 # decorator-maker (the function that returns a decorator):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
211 #
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
212 # 1: Evaluate the decorator-maker name
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
213 # 2: Evaluate the decorator-maker arguments (if any)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
214 # 3: Call the decorator-maker to make a decorator
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
215 # 4: Call the decorator
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
216 #
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
217 # When there are multiple decorators, these steps should be
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
218 # performed in the above order for each decorator, but we should
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
219 # iterate through the decorators in the reverse of the order they
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
220 # appear in the source.
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
221
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
222 actions = []
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
223
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
224 def make_decorator(tag):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
225 actions.append('makedec' + tag)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
226 def decorate(func):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
227 actions.append('calldec' + tag)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
228 return func
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
229 return decorate
33133
3db9a932775e Correct the order of application for decorators. Meant to be bottom-up and not
Brett Cannon <bcannon@gmail.com>
parents: 32949
diff changeset
230
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
231 class NameLookupTracer (object):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
232 def __init__(self, index):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
233 self.index = index
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
234
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
235 def __getattr__(self, fname):
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
236 if fname == 'make_decorator':
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
237 opname, res = ('evalname', make_decorator)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
238 elif fname == 'arg':
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
239 opname, res = ('evalargs', str(self.index))
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
240 else:
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
241 assert False, "Unknown attrname %s" % fname
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
242 actions.append('%s%d' % (opname, self.index))
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
243 return res
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
244
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
245 c1, c2, c3 = map(NameLookupTracer, [ 1, 2, 3 ])
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
246
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
247 expected_actions = [ 'evalname1', 'evalargs1', 'makedec1',
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
248 'evalname2', 'evalargs2', 'makedec2',
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
249 'evalname3', 'evalargs3', 'makedec3',
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
250 'calldec3', 'calldec2', 'calldec1' ]
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
251
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
252 actions = []
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
253 @c1.make_decorator(c1.arg)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
254 @c2.make_decorator(c2.arg)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
255 @c3.make_decorator(c3.arg)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
256 def foo(): return 42
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
257 self.assertEqual(foo(), 42)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
258
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
259 self.assertEqual(actions, expected_actions)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
260
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
261 # Test the equivalence claim in chapter 7 of the reference manual.
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
262 #
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
263 actions = []
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
264 def bar(): return 42
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
265 bar = c1.make_decorator(c1.arg)(c2.make_decorator(c2.arg)(c3.make_decorator(c3.arg)(bar)))
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
266 self.assertEqual(bar(), 42)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
267 self.assertEqual(actions, expected_actions)
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
268
45447
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
269 class TestClassDecorators(unittest.TestCase):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
270
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
271 def test_simple(self):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
272 def plain(x):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
273 x.extra = 'Hello'
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
274 return x
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
275 @plain
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
276 class C(object): pass
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
277 self.assertEqual(C.extra, 'Hello')
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
278
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
279 def test_double(self):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
280 def ten(x):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
281 x.extra = 10
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
282 return x
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
283 def add_five(x):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
284 x.extra += 5
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
285 return x
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
286
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
287 @add_five
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
288 @ten
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
289 class C(object): pass
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
290 self.assertEqual(C.extra, 15)
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
291
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
292 def test_order(self):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
293 def applied_first(x):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
294 x.extra = 'first'
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
295 return x
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
296 def applied_second(x):
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
297 x.extra = 'second'
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
298 return x
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
299 @applied_second
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
300 @applied_first
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
301 class C(object): pass
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
302 self.assertEqual(C.extra, 'second')
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
303
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
304 def test_main():
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
305 test_support.run_unittest(TestDecorators)
45447
393822f2ef90 Patch #1759: Backport of PEP 3129 class decorators
Christian Heimes <christian@cheimes.de>
parents: 33185
diff changeset
306 test_support.run_unittest(TestClassDecorators)
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
307
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
308 if __name__=="__main__":
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
309 test_main()