annotate Lib/test/test_functools.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents dfd46a5db872
children dba99828d91c
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
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 @staticmethod
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 def PythonPartial(func, *args, **keywords):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 'Pure Python approximation of partial()'
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 def newfunc(*fargs, **fkeywords):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 newkeywords = keywords.copy()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 newkeywords.update(fkeywords)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 return func(*(args + fargs), **newkeywords)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 newfunc.func = func
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 newfunc.args = args
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 newfunc.keywords = keywords
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 return newfunc
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18 def capture(*args, **kw):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 """capture all positional and keyword arguments"""
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20 return args, kw
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22 class TestPartial(unittest.TestCase):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 34818
diff changeset
24 thetype = functools.partial
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 def test_basic_examples(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 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
28 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
29 ((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
30 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
31 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
32
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 def test_attributes(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 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
35 # attributes should be readable
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 self.assertEqual(p.func, capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 self.assertEqual(p.args, (1, 2))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 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
39 # attributes should not be writable
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 if not isinstance(self.thetype, type):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 return
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 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
43 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
44 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
45
58807
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
46 p = self.thetype(hex)
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
47 try:
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
48 del p.__dict__
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
49 except TypeError:
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
50 pass
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
51 else:
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
52 self.fail('partial object allowed __dict__ to be deleted')
dfd46a5db872 Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48204
diff changeset
53
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 def test_argument_checking(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 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
56 try:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 self.thetype(2)()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 except TypeError:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 pass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 else:
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 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
62
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 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
64 # 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
65 def func(a=10, b=20):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 return a
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 d = {'a':3}
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 p = self.thetype(func, a=5)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 self.assertEqual(p(**d), 3)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 self.assertEqual(d, {'a':3})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 p(b=7)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 self.assertEqual(d, {'a':3})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 def test_arg_combinations(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 # 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
76 # object or the caller
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 p = self.thetype(capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 self.assertEqual(p(), ((), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 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
80 p = self.thetype(capture, 1, 2)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 self.assertEqual(p(), ((1,2), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 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
83
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 def test_kw_combinations(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 # 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
86 # 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
87 p = self.thetype(capture)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 self.assertEqual(p(), ((), {}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 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
90 p = self.thetype(capture, a=1)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 self.assertEqual(p(), ((), {'a':1}))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 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
93 # 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
94 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
95
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 def test_positional(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 # 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
98 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
99 p = self.thetype(capture, *args)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 expected = args + ('x',)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 got, empty = p('x')
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 self.failUnless(expected == got and empty == {})
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 def test_keyword(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 # 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
106 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
107 p = self.thetype(capture, a=a)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108 expected = {'a':a,'x':None}
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 empty, got = p(x=None)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 self.failUnless(expected == got and empty == ())
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 def test_no_side_effects(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 # 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
114 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
115 args1, kw1 = p(1, b=2)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 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
117 args2, kw2 = p()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 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
119
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 def test_error_propagation(self):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 def f(x, y):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 x / y
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, 1, 0))
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 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
125 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
126 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
127
34703
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
128 def test_weakref(self):
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
129 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
130 p = proxy(f)
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
131 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
132 f = None
7de76f34b3da Make functional.partial() more closely match the spec by emulating
Raymond Hettinger <python@rcn.com>
parents: 34636
diff changeset
133 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
134
34735
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
135 def test_with_bound_and_unbound_methods(self):
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
136 data = map(str, range(10))
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
137 join = self.thetype(str.join, '')
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
138 self.assertEqual(join(data), '0123456789')
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
139 join = self.thetype(''.join)
65e8aacb0e94 Test partial() with bound/unbound methods.
Raymond Hettinger <python@rcn.com>
parents: 34703
diff changeset
140 self.assertEqual(join(data), '0123456789')
34818
4eaab38566c9 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34735
diff changeset
141
38477
aa065f820252 Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents: 34818
diff changeset
142 class PartialSubclass(functools.partial):
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 pass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145 class TestPartialSubclass(TestPartial):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 thetype = PartialSubclass
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 class TestPythonPartial(TestPartial):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 thetype = PythonPartial
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153
38668
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
154 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
155
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
156 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
157 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
158 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
159 # 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
160 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
161 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
162 # 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
163 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
164 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
165 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
166 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
167 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
168
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
169 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
170 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
171 """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
172 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
173 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
174 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
175 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
176 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
177 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
178 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
179 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
180 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
181
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
182 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
183 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
184 """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
185 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
186 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
187 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
188 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
189 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
190 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
191 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
192 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
193 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
194
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
195 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
196 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
197 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
198 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
199 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
200 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
201 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
202 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
203 assign = ('attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211
40200
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
212 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
213 # 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
214 def wrapper():
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
215 pass
e3b9c6397723 [Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents: 38668
diff changeset
216 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
217 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
218 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
219
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
220 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
221
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
222 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
223 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
224 """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
225 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
226 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
227 @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
228 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
229 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
230 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
231 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
232 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
233 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
234
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
235 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
236 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
237 """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
238 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
239 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
240 @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
241 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
242 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
243 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
244 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
245 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
246 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
247
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
248 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
249 def f():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
250 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
251 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
252 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
253 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
254 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
255 return f
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
256 assign = ('attr',)
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
257 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
258 @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
259 @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
260 def wrapper():
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
261 pass
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
262 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
263 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
264 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
265 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
266 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
267
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268
48204
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
269 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
270
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
271 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
272 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
273
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
274 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
275 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
276 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
277
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
278 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
279
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
280 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
281 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
282 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
283 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
284 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
285 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
286 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
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 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
289 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
290 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
291 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
292 ['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
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 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
295 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
296 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
297 2432902008176640000L
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
298 )
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
299 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
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
309
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
310
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
312 def test_main(verbose=None):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 import sys
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 test_classes = (
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 TestPartial,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 TestPartialSubclass,
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 TestPythonPartial,
38668
dee73fa8110f Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents: 38477
diff changeset
318 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
319 TestWraps,
b3abff17e2dd Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents: 40200
diff changeset
320 TestReduce,
34636
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 )
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 test_support.run_unittest(*test_classes)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 # verify reference counting
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325 if verbose and hasattr(sys, "gettotalrefcount"):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 import gc
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 counts = [None] * 5
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 for i in xrange(len(counts)):
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 test_support.run_unittest(*test_classes)
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 gc.collect()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 counts[i] = sys.gettotalrefcount()
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332 print counts
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 if __name__ == '__main__':
3d0bb3b05d39 SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 test_main(verbose=True)