annotate Lib/test/test_functools.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 184ca6293218
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 34818
diff changeset
1 import functools
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 import unittest
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 from test import test_support
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
4 from weakref import proxy
52627
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
5 import pickle
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 @staticmethod
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 def PythonPartial(func, *args, **keywords):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 'Pure Python approximation of partial()'
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 def newfunc(*fargs, **fkeywords):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 newkeywords = keywords.copy()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 newkeywords.update(fkeywords)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 return func(*(args + fargs), **newkeywords)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 newfunc.func = func
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 newfunc.args = args
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 newfunc.keywords = keywords
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 return newfunc
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 def capture(*args, **kw):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20 """capture all positional and keyword arguments"""
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 return args, kw
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22
52627
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
23 def signature(part):
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
24 """ return the signature of a partial object """
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
25 return (part.func, part.args, part.keywords, part.__dict__)
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
26
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 class TestPartial(unittest.TestCase):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 34818
diff changeset
29 thetype = functools.partial
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 def test_basic_examples(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 p = self.thetype(capture, 1, 2, a=10, b=20)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 self.assertEqual(p(3, 4, b=30, c=40),
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35 p = self.thetype(map, lambda x: x*10)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 self.assertEqual(p([1,2,3,4]), [10, 20, 30, 40])
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 def test_attributes(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 p = self.thetype(capture, 1, 2, a=10, b=20)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 # attributes should be readable
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 self.assertEqual(p.func, capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 self.assertEqual(p.args, (1, 2))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 self.assertEqual(p.keywords, dict(a=10, b=20))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 # attributes should not be writable
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 if not isinstance(self.thetype, type):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 return
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 self.assertRaises(TypeError, setattr, p, 'func', map)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 self.assertRaises(TypeError, setattr, p, 'args', (1, 2))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 self.assertRaises(TypeError, setattr, p, 'keywords', dict(a=1, b=2))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51 def test_argument_checking(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 self.assertRaises(TypeError, self.thetype) # need at least a func arg
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 try:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 self.thetype(2)()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 except TypeError:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 pass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 else:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 self.fail('First arg not checked for callability')
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 def test_protection_of_callers_dict_argument(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 # a caller's dictionary should not be altered by partial
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 def func(a=10, b=20):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 return a
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 d = {'a':3}
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 p = self.thetype(func, a=5)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 self.assertEqual(p(**d), 3)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 self.assertEqual(d, {'a':3})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 p(b=7)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 self.assertEqual(d, {'a':3})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 def test_arg_combinations(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 # exercise special code paths for zero args in either partial
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 # object or the caller
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 p = self.thetype(capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 self.assertEqual(p(), ((), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 self.assertEqual(p(1,2), ((1,2), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 p = self.thetype(capture, 1, 2)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 self.assertEqual(p(), ((1,2), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 self.assertEqual(p(3,4), ((1,2,3,4), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 def test_kw_combinations(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 # exercise special code paths for no keyword args in
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 # either the partial object or the caller
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 p = self.thetype(capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 self.assertEqual(p(), ((), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 self.assertEqual(p(a=1), ((), {'a':1}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 p = self.thetype(capture, a=1)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 self.assertEqual(p(), ((), {'a':1}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 # keyword args in the call override those in the partial object
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 def test_positional(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 # make sure positional arguments are captured correctly
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 p = self.thetype(capture, *args)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 expected = args + ('x',)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 got, empty = p('x')
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 self.failUnless(expected == got and empty == {})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 def test_keyword(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 # make sure keyword arguments are captured correctly
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 for a in ['a', 0, None, 3.5]:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 p = self.thetype(capture, a=a)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 expected = {'a':a,'x':None}
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 empty, got = p(x=None)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 self.failUnless(expected == got and empty == ())
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 def test_no_side_effects(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 # make sure there are no side effects that affect subsequent calls
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111 p = self.thetype(capture, 0, a=1)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 args1, kw1 = p(1, b=2)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 self.failUnless(args1 == (0,1) and kw1 == {'a':1,'b':2})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 args2, kw2 = p()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 self.failUnless(args2 == (0,) and kw2 == {'a':1})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 def test_error_propagation(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 def f(x, y):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119 x / y
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 self.assertRaises(ZeroDivisionError, self.thetype(f, 1, 0))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 self.assertRaises(ZeroDivisionError, self.thetype(f, 1), 0)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 self.assertRaises(ZeroDivisionError, self.thetype(f), 1, 0)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 self.assertRaises(ZeroDivisionError, self.thetype(f, y=0), 1)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
125 def test_attributes(self):
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
126 p = self.thetype(hex)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
127 try:
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
128 del p.__dict__
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
129 except TypeError:
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
130 pass
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
131 else:
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
132 self.fail('partial object allowed __dict__ to be deleted')
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
133
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
134 def test_weakref(self):
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
135 f = self.thetype(int, base=16)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
136 p = proxy(f)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
137 self.assertEqual(f.func, p.func)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
138 f = None
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
139 self.assertRaises(ReferenceError, getattr, p, 'func')
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
140
34735
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
141 def test_with_bound_and_unbound_methods(self):
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
142 data = map(str, range(10))
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
143 join = self.thetype(str.join, '')
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
144 self.assertEqual(join(data), '0123456789')
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
145 join = self.thetype(''.join)
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
146 self.assertEqual(join(data), '0123456789')
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34735
diff changeset
147
52627
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
148 def test_pickle(self):
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
149 f = self.thetype(signature, 'asdf', bar=True)
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
150 f.add_something_to__dict__ = True
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
151 f_copy = pickle.loads(pickle.dumps(f))
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
152 self.assertEqual(signature(f), signature(f_copy))
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
153
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 34818
diff changeset
154 class PartialSubclass(functools.partial):
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155 pass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
157 class TestPartialSubclass(TestPartial):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
158
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
159 thetype = PartialSubclass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161 class TestPythonPartial(TestPartial):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 thetype = PythonPartial
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164
52627
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
165 # the python version isn't picklable
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
166 def test_pickle(self): pass
184ca6293218 #5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents: 48204
diff changeset
167
38668
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
168 class TestUpdateWrapper(unittest.TestCase):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
169
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
170 def check_wrapper(self, wrapper, wrapped,
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
171 assigned=functools.WRAPPER_ASSIGNMENTS,
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
172 updated=functools.WRAPPER_UPDATES):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
173 # Check attributes were assigned
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
174 for name in assigned:
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
175 self.failUnless(getattr(wrapper, name) is getattr(wrapped, name))
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
176 # Check attributes were updated
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
177 for name in updated:
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
178 wrapper_attr = getattr(wrapper, name)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
179 wrapped_attr = getattr(wrapped, name)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
180 for key in wrapped_attr:
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
181 self.failUnless(wrapped_attr[key] is wrapper_attr[key])
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
182
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
183 def test_default_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
184 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
185 """This is a test"""
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
186 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
187 f.attr = 'This is also a test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
188 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
189 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
190 functools.update_wrapper(wrapper, f)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
191 self.check_wrapper(wrapper, f)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
192 self.assertEqual(wrapper.__name__, 'f')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
193 self.assertEqual(wrapper.__doc__, 'This is a test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
194 self.assertEqual(wrapper.attr, 'This is also a test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
195
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
196 def test_no_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
197 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
198 """This is a test"""
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
199 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
200 f.attr = 'This is also a test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
201 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
202 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
203 functools.update_wrapper(wrapper, f, (), ())
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
204 self.check_wrapper(wrapper, f, (), ())
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
205 self.assertEqual(wrapper.__name__, 'wrapper')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
206 self.assertEqual(wrapper.__doc__, None)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
207 self.failIf(hasattr(wrapper, 'attr'))
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
208
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
209 def test_selective_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
210 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
211 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
212 f.attr = 'This is a different test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
213 f.dict_attr = dict(a=1, b=2, c=3)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
214 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
215 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
216 wrapper.dict_attr = {}
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
217 assign = ('attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
218 update = ('dict_attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
219 functools.update_wrapper(wrapper, f, assign, update)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
220 self.check_wrapper(wrapper, f, assign, update)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
221 self.assertEqual(wrapper.__name__, 'wrapper')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
222 self.assertEqual(wrapper.__doc__, None)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
223 self.assertEqual(wrapper.attr, 'This is a different test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
224 self.assertEqual(wrapper.dict_attr, f.dict_attr)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
225
40200
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
226 def test_builtin_update(self):
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
227 # Test for bug #1576241
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
228 def wrapper():
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
229 pass
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
230 functools.update_wrapper(wrapper, max)
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
231 self.assertEqual(wrapper.__name__, 'max')
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
232 self.assert_(wrapper.__doc__.startswith('max('))
38668
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
233
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
234 class TestWraps(TestUpdateWrapper):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
235
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
236 def test_default_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
237 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
238 """This is a test"""
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
239 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
240 f.attr = 'This is also a test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
241 @functools.wraps(f)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
242 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
243 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
244 self.check_wrapper(wrapper, f)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
245 self.assertEqual(wrapper.__name__, 'f')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
246 self.assertEqual(wrapper.__doc__, 'This is a test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
247 self.assertEqual(wrapper.attr, 'This is also a test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
248
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
249 def test_no_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
250 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
251 """This is a test"""
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
252 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
253 f.attr = 'This is also a test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
254 @functools.wraps(f, (), ())
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
255 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
256 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
257 self.check_wrapper(wrapper, f, (), ())
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
258 self.assertEqual(wrapper.__name__, 'wrapper')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
259 self.assertEqual(wrapper.__doc__, None)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
260 self.failIf(hasattr(wrapper, 'attr'))
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
261
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
262 def test_selective_update(self):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
263 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
264 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
265 f.attr = 'This is a different test'
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
266 f.dict_attr = dict(a=1, b=2, c=3)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
267 def add_dict_attr(f):
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
268 f.dict_attr = {}
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
269 return f
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
270 assign = ('attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
271 update = ('dict_attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
272 @functools.wraps(f, assign, update)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
273 @add_dict_attr
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
274 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
275 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
276 self.check_wrapper(wrapper, f, assign, update)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
277 self.assertEqual(wrapper.__name__, 'wrapper')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
278 self.assertEqual(wrapper.__doc__, None)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
279 self.assertEqual(wrapper.attr, 'This is a different test')
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
280 self.assertEqual(wrapper.dict_attr, f.dict_attr)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
281
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282
48204
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
283 class TestReduce(unittest.TestCase):
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
284
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
285 def test_reduce(self):
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
286 class Squares:
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
287
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
288 def __init__(self, max):
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
289 self.max = max
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
290 self.sofar = []
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
291
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
292 def __len__(self): return len(self.sofar)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
293
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
294 def __getitem__(self, i):
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
295 if not 0 <= i < self.max: raise IndexError
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
296 n = len(self.sofar)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
297 while n <= i:
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
298 self.sofar.append(n*n)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
299 n += 1
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
300 return self.sofar[i]
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
301
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
302 reduce = functools.reduce
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
303 self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc')
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
304 self.assertEqual(
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
305 reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []),
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
306 ['a','c','d','w']
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
307 )
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
308 self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
309 self.assertEqual(
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
310 reduce(lambda x, y: x*y, range(2,21), 1L),
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
311 2432902008176640000L
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
312 )
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
313 self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
314 self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
315 self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
316 self.assertRaises(TypeError, reduce)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
317 self.assertRaises(TypeError, reduce, 42, 42)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
318 self.assertRaises(TypeError, reduce, 42, 42, 42)
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
319 self.assertEqual(reduce(42, "1"), "1") # func is never called with one item
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
320 self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
321 self.assertRaises(TypeError, reduce, 42, (42, 42))
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
322
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
323
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
324
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 def test_main(verbose=None):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 import sys
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 test_classes = (
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 TestPartial,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 TestPartialSubclass,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 TestPythonPartial,
38668
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
332 TestUpdateWrapper,
48204
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
333 TestWraps,
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
334 TestReduce,
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 )
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 test_support.run_unittest(*test_classes)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338 # verify reference counting
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 if verbose and hasattr(sys, "gettotalrefcount"):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 import gc
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 counts = [None] * 5
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342 for i in xrange(len(counts)):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 test_support.run_unittest(*test_classes)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344 gc.collect()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345 counts[i] = sys.gettotalrefcount()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 print counts
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 if __name__ == '__main__':
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 test_main(verbose=True)