Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_functools.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 94c158199277 |
| children | 6c0d9d1ce63e |
| 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 |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
2 import sys |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
3 import unittest |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
4 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
|
5 from weakref import proxy |
|
52627
184ca6293218
#5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents:
48204
diff
changeset
|
6 import pickle |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
7 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
8 @staticmethod |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
9 def PythonPartial(func, *args, **keywords): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
10 'Pure Python approximation of partial()' |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
11 def newfunc(*fargs, **fkeywords): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
12 newkeywords = keywords.copy() |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
13 newkeywords.update(fkeywords) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
14 return func(*(args + fargs), **newkeywords) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
15 newfunc.func = func |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
16 newfunc.args = args |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
17 newfunc.keywords = keywords |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
18 return newfunc |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
19 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
20 def capture(*args, **kw): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
21 """capture all positional and keyword arguments""" |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
22 return args, kw |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
23 |
|
52627
184ca6293218
#5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents:
48204
diff
changeset
|
24 def signature(part): |
|
184ca6293218
#5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents:
48204
diff
changeset
|
25 """ return the signature of a partial object """ |
|
184ca6293218
#5228: add pickle support to functools.partial
Jack Diederich <jackdied@gmail.com>
parents:
48204
diff
changeset
|
26 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
|
27 |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
28 class TestPartial(unittest.TestCase): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
29 |
|
38477
aa065f820252
Apply modified version of Collin Winter's patch #1478788
Nick Coghlan <ncoghlan@gmail.com>
parents:
34818
diff
changeset
|
30 thetype = functools.partial |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
31 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
32 def test_basic_examples(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 ((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
|
36 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
|
37 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
|
38 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
39 def test_attributes(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
40 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
|
41 # attributes should be readable |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
42 self.assertEqual(p.func, capture) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
43 self.assertEqual(p.args, (1, 2)) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
44 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
|
45 # attributes should not be writable |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
46 if not isinstance(self.thetype, type): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
47 return |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 |
|
58807
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
52 p = self.thetype(hex) |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
53 try: |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
54 del p.__dict__ |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
55 except TypeError: |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
56 pass |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
57 else: |
|
dfd46a5db872
Merged revisions 78075-78079 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48204
diff
changeset
|
58 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
|
59 |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
60 def test_argument_checking(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
61 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
|
62 try: |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
63 self.thetype(2)() |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
64 except TypeError: |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
65 pass |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
66 else: |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
67 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
|
68 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
69 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
|
70 # 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
|
71 def func(a=10, b=20): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
72 return a |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
73 d = {'a':3} |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
74 p = self.thetype(func, a=5) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
75 self.assertEqual(p(**d), 3) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
76 self.assertEqual(d, {'a':3}) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
77 p(b=7) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
78 self.assertEqual(d, {'a':3}) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
79 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
80 def test_arg_combinations(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
81 # 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
|
82 # object or the caller |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
83 p = self.thetype(capture) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
84 self.assertEqual(p(), ((), {})) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
85 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
|
86 p = self.thetype(capture, 1, 2) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
87 self.assertEqual(p(), ((1,2), {})) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
88 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
|
89 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
90 def test_kw_combinations(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
91 # 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
|
92 # 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
|
93 p = self.thetype(capture) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
94 self.assertEqual(p(), ((), {})) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
95 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
|
96 p = self.thetype(capture, a=1) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
97 self.assertEqual(p(), ((), {'a':1})) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
98 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
|
99 # 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
|
100 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
|
101 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
102 def test_positional(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
103 # 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
|
104 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
|
105 p = self.thetype(capture, *args) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
106 expected = args + ('x',) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
107 got, empty = p('x') |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
108 self.assertTrue(expected == got and empty == {}) |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
109 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
110 def test_keyword(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
111 # 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
|
112 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
|
113 p = self.thetype(capture, a=a) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
114 expected = {'a':a,'x':None} |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
115 empty, got = p(x=None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
116 self.assertTrue(expected == got and empty == ()) |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
117 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
118 def test_no_side_effects(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
119 # 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
|
120 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
|
121 args1, kw1 = p(1, b=2) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
122 self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2}) |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
123 args2, kw2 = p() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
124 self.assertTrue(args2 == (0,) and kw2 == {'a':1}) |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
125 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
126 def test_error_propagation(self): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
127 def f(x, y): |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58807
diff
changeset
|
128 x // y |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 |
|
34703
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: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
175 self.assertTrue(getattr(wrapper, name) is getattr(wrapped, name)) |
|
38668
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: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
181 self.assertTrue(wrapped_attr[key] is wrapper_attr[key]) |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
182 |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
183 def _default_update(self): |
|
38668
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) |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
191 return wrapper, f |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
192 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
193 def test_default_update(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
194 wrapper, f = self._default_update() |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
195 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
|
196 self.assertEqual(wrapper.__name__, 'f') |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
197 self.assertEqual(wrapper.attr, 'This is also a test') |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
198 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
199 @unittest.skipIf(sys.flags.optimize >= 2, |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
200 "Docstrings are omitted with -O2 and above") |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
201 def test_default_update_doc(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
202 wrapper, f = self._default_update() |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
203 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
|
204 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
205 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
|
206 def f(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
207 """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
|
208 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
209 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
|
210 def wrapper(): |
|
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 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
|
213 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
|
214 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
|
215 self.assertEqual(wrapper.__doc__, None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
216 self.assertFalse(hasattr(wrapper, 'attr')) |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
217 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
218 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
|
219 def f(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
220 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
221 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
|
222 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
|
223 def wrapper(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
224 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
225 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
|
226 assign = ('attr',) |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 |
|
40200
e3b9c6397723
[Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents:
38668
diff
changeset
|
235 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
|
236 # 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
|
237 def wrapper(): |
|
e3b9c6397723
[Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents:
38668
diff
changeset
|
238 pass |
|
e3b9c6397723
[Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents:
38668
diff
changeset
|
239 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
|
240 self.assertEqual(wrapper.__name__, 'max') |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
241 self.assertTrue(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
|
242 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
243 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
|
244 |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
245 def _default_update(self): |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
246 def f(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
247 """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
|
248 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
249 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
|
250 @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
|
251 def wrapper(): |
|
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 self.check_wrapper(wrapper, f) |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
254 return wrapper |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
255 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
256 def test_default_update(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
257 wrapper = self._default_update() |
|
38668
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__, 'f') |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
259 self.assertEqual(wrapper.attr, 'This is also a test') |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
260 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
261 @unittest.skipIf(not sys.flags.optimize <= 1, |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
262 "Docstrings are omitted with -O2 and above") |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
263 def test_default_update_doc(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58802
diff
changeset
|
264 wrapper = self._default_update() |
|
38668
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.__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
|
266 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
267 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
|
268 def f(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
269 """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
|
270 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
271 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
|
272 @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
|
273 def wrapper(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
274 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
275 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
|
276 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
|
277 self.assertEqual(wrapper.__doc__, None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52627
diff
changeset
|
278 self.assertFalse(hasattr(wrapper, 'attr')) |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
279 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
280 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
|
281 def f(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
282 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 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
|
287 return f |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
288 assign = ('attr',) |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
289 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
|
290 @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
|
291 @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
|
292 def wrapper(): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
293 pass |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
294 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
300 |
|
48204
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
301 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
|
302 |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
303 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
|
304 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
|
305 |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
306 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
|
307 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
|
308 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
|
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 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
|
311 |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
320 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
|
321 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
|
322 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
|
323 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
|
324 ['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
|
325 ) |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 2432902008176640000L |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
330 ) |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 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
|
340 |
|
60315
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
341 class TestCmpToKey(unittest.TestCase): |
|
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
342 def test_cmp_to_key(self): |
|
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
343 def mycmp(x, y): |
|
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
344 return y - x |
|
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
345 self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)), |
|
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
59045
diff
changeset
|
346 [4, 3, 2, 1, 0]) |
|
48204
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
347 |
|
60368
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
348 def test_hash(self): |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
349 def mycmp(x, y): |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
350 return y - x |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
351 key = functools.cmp_to_key(mycmp) |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
352 k = key(10) |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60320
diff
changeset
|
353 self.assertRaises(TypeError, hash(k)) |
|
48204
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
354 |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
355 class TestTotalOrdering(unittest.TestCase): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
356 |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
357 def test_total_ordering_lt(self): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
358 @functools.total_ordering |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
359 class A: |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
360 def __init__(self, value): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
361 self.value = value |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
362 def __lt__(self, other): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
363 return self.value < other.value |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
364 def __eq__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
365 return self.value == other.value |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
366 self.assertTrue(A(1) < A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
367 self.assertTrue(A(2) > A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
368 self.assertTrue(A(1) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
369 self.assertTrue(A(2) >= A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
370 self.assertTrue(A(2) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
371 self.assertTrue(A(2) >= A(2)) |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
372 |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
373 def test_total_ordering_le(self): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
374 @functools.total_ordering |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
375 class A: |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
376 def __init__(self, value): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
377 self.value = value |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
378 def __le__(self, other): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
379 return self.value <= other.value |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
380 def __eq__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
381 return self.value == other.value |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
382 self.assertTrue(A(1) < A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
383 self.assertTrue(A(2) > A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
384 self.assertTrue(A(1) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
385 self.assertTrue(A(2) >= A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
386 self.assertTrue(A(2) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
387 self.assertTrue(A(2) >= A(2)) |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
388 |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
389 def test_total_ordering_gt(self): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
390 @functools.total_ordering |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
391 class A: |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
392 def __init__(self, value): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
393 self.value = value |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
394 def __gt__(self, other): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
395 return self.value > other.value |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
396 def __eq__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
397 return self.value == other.value |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
398 self.assertTrue(A(1) < A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
399 self.assertTrue(A(2) > A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
400 self.assertTrue(A(1) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
401 self.assertTrue(A(2) >= A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
402 self.assertTrue(A(2) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
403 self.assertTrue(A(2) >= A(2)) |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
404 |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
405 def test_total_ordering_ge(self): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
406 @functools.total_ordering |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
407 class A: |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
408 def __init__(self, value): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
409 self.value = value |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
410 def __ge__(self, other): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
411 return self.value >= other.value |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
412 def __eq__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
413 return self.value == other.value |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
414 self.assertTrue(A(1) < A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
415 self.assertTrue(A(2) > A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
416 self.assertTrue(A(1) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
417 self.assertTrue(A(2) >= A(1)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
418 self.assertTrue(A(2) <= A(2)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
419 self.assertTrue(A(2) >= A(2)) |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
420 |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
421 def test_total_ordering_no_overwrite(self): |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
422 # new methods should not overwrite existing |
|
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
423 @functools.total_ordering |
|
64244
b535cde93273
enable and fix TestTotalOrdering
Benjamin Peterson <benjamin@python.org>
parents:
60492
diff
changeset
|
424 class A(str): |
|
60320
b6c6f42cddde
Add tests for functools.total_ordering.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
425 pass |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
426 self.assertTrue(A("a") < A("b")) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
427 self.assertTrue(A("b") > A("a")) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
428 self.assertTrue(A("a") <= A("b")) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
429 self.assertTrue(A("b") >= A("a")) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
430 self.assertTrue(A("b") <= A("b")) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64244
diff
changeset
|
431 self.assertTrue(A("b") >= A("b")) |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
432 |
|
60492
cf7c9ba2de6e
add test for class with no operations defined
Benjamin Peterson <benjamin@python.org>
parents:
60368
diff
changeset
|
433 def test_no_operations_defined(self): |
|
cf7c9ba2de6e
add test for class with no operations defined
Benjamin Peterson <benjamin@python.org>
parents:
60368
diff
changeset
|
434 with self.assertRaises(ValueError): |
|
cf7c9ba2de6e
add test for class with no operations defined
Benjamin Peterson <benjamin@python.org>
parents:
60368
diff
changeset
|
435 @functools.total_ordering |
|
cf7c9ba2de6e
add test for class with no operations defined
Benjamin Peterson <benjamin@python.org>
parents:
60368
diff
changeset
|
436 class A: |
|
cf7c9ba2de6e
add test for class with no operations defined
Benjamin Peterson <benjamin@python.org>
parents:
60368
diff
changeset
|
437 pass |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
438 |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
439 def test_bug_10042(self): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
440 @functools.total_ordering |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
441 class TestTO: |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
442 def __init__(self, value): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
443 self.value = value |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
444 def __eq__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
445 if isinstance(other, TestTO): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
446 return self.value == other.value |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
447 return False |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
448 def __lt__(self, other): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
449 if isinstance(other, TestTO): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
450 return self.value < other.value |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
451 raise TypeError |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
452 with self.assertRaises(TypeError): |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
453 TestTO(8) <= () |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
454 |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
455 def test_main(verbose=None): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
456 test_classes = ( |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
457 TestPartial, |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
458 TestPartialSubclass, |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
459 TestPythonPartial, |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38477
diff
changeset
|
460 TestUpdateWrapper, |
|
64244
b535cde93273
enable and fix TestTotalOrdering
Benjamin Peterson <benjamin@python.org>
parents:
60492
diff
changeset
|
461 TestTotalOrdering, |
|
48204
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
462 TestWraps, |
|
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
40200
diff
changeset
|
463 TestReduce, |
|
34636
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
464 ) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
465 test_support.run_unittest(*test_classes) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
466 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
467 # verify reference counting |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
468 if verbose and hasattr(sys, "gettotalrefcount"): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
469 import gc |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
470 counts = [None] * 5 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
471 for i in xrange(len(counts)): |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
472 test_support.run_unittest(*test_classes) |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
473 gc.collect() |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
474 counts[i] = sys.gettotalrefcount() |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
475 print counts |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
476 |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
477 if __name__ == '__main__': |
|
3d0bb3b05d39
SF patch #941881: PEP 309 Implementation (Partial Function Application).
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
478 test_main(verbose=True) |
