annotate Lib/test/test_functools.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 8365f82f8a13
children 241b655c23b6 5f4a871759ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
1 import functools
69810
2675415c57c1 Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
Raymond Hettinger <python@rcn.com>
parents: 68257
diff changeset
2 import collections
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
3 import sys
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42142
diff changeset
5 from test import support
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6 from weakref import proxy
52637
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
7 import pickle
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
8 from random import choice
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
9
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
10 @staticmethod
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
11 def PythonPartial(func, *args, **keywords):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
12 'Pure Python approximation of partial()'
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
13 def newfunc(*fargs, **fkeywords):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14 newkeywords = keywords.copy()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
15 newkeywords.update(fkeywords)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
16 return func(*(args + fargs), **newkeywords)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
17 newfunc.func = func
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
18 newfunc.args = args
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
19 newfunc.keywords = keywords
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
20 return newfunc
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22 def capture(*args, **kw):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
23 """capture all positional and keyword arguments"""
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
24 return args, kw
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
25
52637
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
26 def signature(part):
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
27 """ return the signature of a partial object """
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
28 return (part.func, part.args, part.keywords, part.__dict__)
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
29
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 class TestPartial(unittest.TestCase):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
31
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
32 thetype = functools.partial
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
33
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
34 def test_basic_examples(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
35 p = self.thetype(capture, 1, 2, a=10, b=20)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
36 self.assertEqual(p(3, 4, b=30, c=40),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
37 ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
38 p = self.thetype(map, lambda x: x*10)
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
39 self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40])
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
40
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
41 def test_attributes(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
42 p = self.thetype(capture, 1, 2, a=10, b=20)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
43 # attributes should be readable
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
44 self.assertEqual(p.func, capture)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
45 self.assertEqual(p.args, (1, 2))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
46 self.assertEqual(p.keywords, dict(a=10, b=20))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
47 # attributes should not be writable
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
48 if not isinstance(self.thetype, type):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
49 return
59613
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
50 self.assertRaises(AttributeError, setattr, p, 'func', map)
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
51 self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
52 self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2))
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
53
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
54 p = self.thetype(hex)
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
55 try:
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
56 del p.__dict__
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
57 except TypeError:
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
58 pass
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
59 else:
77c71f258df7 Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents: 59091
diff changeset
60 self.fail('partial object allowed __dict__ to be deleted')
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
61
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
62 def test_argument_checking(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
63 self.assertRaises(TypeError, self.thetype) # need at least a func arg
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
64 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 self.thetype(2)()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
66 except TypeError:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
67 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
68 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 self.fail('First arg not checked for callability')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
70
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 def test_protection_of_callers_dict_argument(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
72 # a caller's dictionary should not be altered by partial
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
73 def func(a=10, b=20):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
74 return a
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75 d = {'a':3}
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
76 p = self.thetype(func, a=5)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
77 self.assertEqual(p(**d), 3)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
78 self.assertEqual(d, {'a':3})
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
79 p(b=7)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
80 self.assertEqual(d, {'a':3})
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
81
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
82 def test_arg_combinations(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
83 # exercise special code paths for zero args in either partial
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
84 # object or the caller
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
85 p = self.thetype(capture)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
86 self.assertEqual(p(), ((), {}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
87 self.assertEqual(p(1,2), ((1,2), {}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
88 p = self.thetype(capture, 1, 2)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
89 self.assertEqual(p(), ((1,2), {}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
90 self.assertEqual(p(3,4), ((1,2,3,4), {}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
91
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
92 def test_kw_combinations(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
93 # exercise special code paths for no keyword args in
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
94 # either the partial object or the caller
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
95 p = self.thetype(capture)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
96 self.assertEqual(p(), ((), {}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
97 self.assertEqual(p(a=1), ((), {'a':1}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
98 p = self.thetype(capture, a=1)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
99 self.assertEqual(p(), ((), {'a':1}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
100 self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
101 # keyword args in the call override those in the partial object
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
102 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
103
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
104 def test_positional(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
105 # make sure positional arguments are captured correctly
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
106 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
107 p = self.thetype(capture, *args)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
108 expected = args + ('x',)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
109 got, empty = p('x')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
110 self.assertTrue(expected == got and empty == {})
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
111
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
112 def test_keyword(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
113 # make sure keyword arguments are captured correctly
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
114 for a in ['a', 0, None, 3.5]:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
115 p = self.thetype(capture, a=a)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
116 expected = {'a':a,'x':None}
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
117 empty, got = p(x=None)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
118 self.assertTrue(expected == got and empty == ())
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
119
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
120 def test_no_side_effects(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
121 # make sure there are no side effects that affect subsequent calls
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
122 p = self.thetype(capture, 0, a=1)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
123 args1, kw1 = p(1, b=2)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
124 self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2})
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
125 args2, kw2 = p()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
126 self.assertTrue(args2 == (0,) and kw2 == {'a':1})
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
127
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
128 def test_error_propagation(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
129 def f(x, y):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
130 x / y
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
131 self.assertRaises(ZeroDivisionError, self.thetype(f, 1, 0))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
132 self.assertRaises(ZeroDivisionError, self.thetype(f, 1), 0)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
133 self.assertRaises(ZeroDivisionError, self.thetype(f), 1, 0)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
134 self.assertRaises(ZeroDivisionError, self.thetype(f, y=0), 1)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
135
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
136 def test_weakref(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
137 f = self.thetype(int, base=16)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
138 p = proxy(f)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
139 self.assertEqual(f.func, p.func)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
140 f = None
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
141 self.assertRaises(ReferenceError, getattr, p, 'func')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
142
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
143 def test_with_bound_and_unbound_methods(self):
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
144 data = list(map(str, range(10)))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
145 join = self.thetype(str.join, '')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
146 self.assertEqual(join(data), '0123456789')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
147 join = self.thetype(''.join)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
148 self.assertEqual(join(data), '0123456789')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
149
66587
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
150 def test_repr(self):
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
151 args = (object(), object())
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
152 args_repr = ', '.join(repr(a) for a in args)
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
153 kwargs = {'a': object(), 'b': object()}
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
154 kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items())
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
155 if self.thetype is functools.partial:
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
156 name = 'functools.partial'
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
157 else:
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
158 name = self.thetype.__name__
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
159
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
160 f = self.thetype(capture)
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
161 self.assertEqual('{}({!r})'.format(name, capture),
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
162 repr(f))
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
163
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
164 f = self.thetype(capture, *args)
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
165 self.assertEqual('{}({!r}, {})'.format(name, capture, args_repr),
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
166 repr(f))
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
167
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
168 f = self.thetype(capture, **kwargs)
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
169 self.assertEqual('{}({!r}, {})'.format(name, capture, kwargs_repr),
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
170 repr(f))
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
171
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
172 f = self.thetype(capture, *args, **kwargs)
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
173 self.assertEqual('{}({!r}, {}, {})'.format(name, capture, args_repr, kwargs_repr),
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
174 repr(f))
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
175
52637
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
176 def test_pickle(self):
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
177 f = self.thetype(signature, 'asdf', bar=True)
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
178 f.add_something_to__dict__ = True
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
179 f_copy = pickle.loads(pickle.dumps(f))
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
180 self.assertEqual(signature(f), signature(f_copy))
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
181
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
182 class PartialSubclass(functools.partial):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
183 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
184
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
185 class TestPartialSubclass(TestPartial):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
186
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
187 thetype = PartialSubclass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
188
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
189 class TestPythonPartial(TestPartial):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
190
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
191 thetype = PythonPartial
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
192
66587
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
193 # the python version hasn't a nice repr
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
194 def test_repr(self): pass
2baad8bd0b4f Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66582
diff changeset
195
52637
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
196 # the python version isn't picklable
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
197 def test_pickle(self): pass
ffa81bd2ec72 Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents: 47201
diff changeset
198
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
199 class TestUpdateWrapper(unittest.TestCase):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
200
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
201 def check_wrapper(self, wrapper, wrapped,
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
202 assigned=functools.WRAPPER_ASSIGNMENTS,
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
203 updated=functools.WRAPPER_UPDATES):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
204 # Check attributes were assigned
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
205 for name in assigned:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
206 self.assertTrue(getattr(wrapper, name) is getattr(wrapped, name))
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
207 # Check attributes were updated
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
208 for name in updated:
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
209 wrapper_attr = getattr(wrapper, name)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
210 wrapped_attr = getattr(wrapped, name)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
211 for key in wrapped_attr:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
212 self.assertTrue(wrapped_attr[key] is wrapper_attr[key])
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
213
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
214 def _default_update(self):
63810
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
215 def f(a:'This is a new annotation'):
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
216 """This is a test"""
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
217 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
218 f.attr = 'This is also a test'
63810
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
219 def wrapper(b:'This is the prior annotation'):
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
220 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
221 functools.update_wrapper(wrapper, f)
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
222 return wrapper, f
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
223
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
224 def test_default_update(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
225 wrapper, f = self._default_update()
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
226 self.check_wrapper(wrapper, f)
64102
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
227 self.assertIs(wrapper.__wrapped__, f)
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
228 self.assertEqual(wrapper.__name__, 'f')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
229 self.assertEqual(wrapper.attr, 'This is also a test')
63810
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
230 self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation')
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
231 self.assertNotIn('b', wrapper.__annotations__)
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
232
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
233 @unittest.skipIf(sys.flags.optimize >= 2,
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
234 "Docstrings are omitted with -O2 and above")
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
235 def test_default_update_doc(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
236 wrapper, f = self._default_update()
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
237 self.assertEqual(wrapper.__doc__, 'This is a test')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
238
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
239 def test_no_update(self):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
240 def f():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
241 """This is a test"""
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
242 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
243 f.attr = 'This is also a test'
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
244 def wrapper():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
245 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
246 functools.update_wrapper(wrapper, f, (), ())
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
247 self.check_wrapper(wrapper, f, (), ())
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
248 self.assertEqual(wrapper.__name__, 'wrapper')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
249 self.assertEqual(wrapper.__doc__, None)
63810
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
250 self.assertEqual(wrapper.__annotations__, {})
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
251 self.assertFalse(hasattr(wrapper, 'attr'))
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
252
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
253 def test_selective_update(self):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
254 def f():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
255 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
256 f.attr = 'This is a different test'
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
257 f.dict_attr = dict(a=1, b=2, c=3)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
258 def wrapper():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
259 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
260 wrapper.dict_attr = {}
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
261 assign = ('attr',)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
262 update = ('dict_attr',)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
263 functools.update_wrapper(wrapper, f, assign, update)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
264 self.check_wrapper(wrapper, f, assign, update)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
265 self.assertEqual(wrapper.__name__, 'wrapper')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
266 self.assertEqual(wrapper.__doc__, None)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
267 self.assertEqual(wrapper.attr, 'This is a different test')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
268 self.assertEqual(wrapper.dict_attr, f.dict_attr)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
269
64102
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
270 def test_missing_attributes(self):
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
271 def f():
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
272 pass
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
273 def wrapper():
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
274 pass
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
275 wrapper.dict_attr = {}
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
276 assign = ('attr',)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
277 update = ('dict_attr',)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
278 # Missing attributes on wrapped object are ignored
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
279 functools.update_wrapper(wrapper, f, assign, update)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
280 self.assertNotIn('attr', wrapper.__dict__)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
281 self.assertEqual(wrapper.dict_attr, {})
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
282 # Wrapper must have expected attributes for updating
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
283 del wrapper.dict_attr
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
284 with self.assertRaises(AttributeError):
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
285 functools.update_wrapper(wrapper, f, assign, update)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
286 wrapper.dict_attr = 1
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
287 with self.assertRaises(AttributeError):
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
288 functools.update_wrapper(wrapper, f, assign, update)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
289
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
290 @unittest.skipIf(sys.flags.optimize >= 2,
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
291 "Docstrings are omitted with -O2 and above")
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
292 def test_builtin_update(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
293 # Test for bug #1576241
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
294 def wrapper():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
295 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
296 functools.update_wrapper(wrapper, max)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39691
diff changeset
297 self.assertEqual(wrapper.__name__, 'max')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
298 self.assertTrue(wrapper.__doc__.startswith('max('))
63810
15839ca43d96 Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
299 self.assertEqual(wrapper.__annotations__, {})
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
300
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
301 class TestWraps(TestUpdateWrapper):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
302
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
303 def _default_update(self):
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
304 def f():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
305 """This is a test"""
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
306 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
307 f.attr = 'This is also a test'
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
308 @functools.wraps(f)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
309 def wrapper():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
310 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
311 self.check_wrapper(wrapper, f)
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
312 return wrapper
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
313
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
314 def test_default_update(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
315 wrapper = self._default_update()
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
316 self.assertEqual(wrapper.__name__, 'f')
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
317 self.assertEqual(wrapper.attr, 'This is also a test')
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
318
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
319 @unittest.skipIf(not sys.flags.optimize <= 1,
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
320 "Docstrings are omitted with -O2 and above")
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
321 def test_default_update_doc(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55593
diff changeset
322 wrapper = self._default_update()
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
323 self.assertEqual(wrapper.__doc__, 'This is a test')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
324
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
325 def test_no_update(self):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
326 def f():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
327 """This is a test"""
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
328 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
329 f.attr = 'This is also a test'
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
330 @functools.wraps(f, (), ())
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
331 def wrapper():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
332 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
333 self.check_wrapper(wrapper, f, (), ())
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
334 self.assertEqual(wrapper.__name__, 'wrapper')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
335 self.assertEqual(wrapper.__doc__, None)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52637
diff changeset
336 self.assertFalse(hasattr(wrapper, 'attr'))
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
337
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
338 def test_selective_update(self):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
339 def f():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
340 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
341 f.attr = 'This is a different test'
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
342 f.dict_attr = dict(a=1, b=2, c=3)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
343 def add_dict_attr(f):
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
344 f.dict_attr = {}
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
345 return f
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
346 assign = ('attr',)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
347 update = ('dict_attr',)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
348 @functools.wraps(f, assign, update)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
349 @add_dict_attr
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
350 def wrapper():
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
351 pass
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
352 self.check_wrapper(wrapper, f, assign, update)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
353 self.assertEqual(wrapper.__name__, 'wrapper')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
354 self.assertEqual(wrapper.__doc__, None)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
355 self.assertEqual(wrapper.attr, 'This is a different test')
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
356 self.assertEqual(wrapper.dict_attr, f.dict_attr)
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
357
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
358 class TestReduce(unittest.TestCase):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
359 func = functools.reduce
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
360
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
361 def test_reduce(self):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
362 class Squares:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
363 def __init__(self, max):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
364 self.max = max
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
365 self.sofar = []
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
366
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
367 def __len__(self):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
368 return len(self.sofar)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
369
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
370 def __getitem__(self, i):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
371 if not 0 <= i < self.max: raise IndexError
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
372 n = len(self.sofar)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
373 while n <= i:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
374 self.sofar.append(n*n)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
375 n += 1
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
376 return self.sofar[i]
64074
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
377 def add(x, y):
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
378 return x + y
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
379 self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc')
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
380 self.assertEqual(
64074
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
381 self.func(add, [['a', 'c'], [], ['d', 'w']], []),
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
382 ['a','c','d','w']
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
383 )
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
384 self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
385 self.assertEqual(
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
386 self.func(lambda x, y: x*y, range(2,21), 1),
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
387 2432902008176640000
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
388 )
64074
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
389 self.assertEqual(self.func(add, Squares(10)), 285)
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
390 self.assertEqual(self.func(add, Squares(10), 0), 285)
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
391 self.assertEqual(self.func(add, Squares(0), 0), 0)
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
392 self.assertRaises(TypeError, self.func)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
393 self.assertRaises(TypeError, self.func, 42, 42)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
394 self.assertRaises(TypeError, self.func, 42, 42, 42)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
395 self.assertEqual(self.func(42, "1"), "1") # func is never called with one item
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
396 self.assertEqual(self.func(42, "", "1"), "1") # func is never called with one item
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
397 self.assertRaises(TypeError, self.func, 42, (42, 42))
64074
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
398 self.assertRaises(TypeError, self.func, add, []) # arg 2 must not be empty sequence with no initial value
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
399 self.assertRaises(TypeError, self.func, add, "")
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
400 self.assertRaises(TypeError, self.func, add, ())
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
401 self.assertRaises(TypeError, self.func, add, object())
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
402
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
403 class TestFailingIter:
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
404 def __iter__(self):
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
405 raise RuntimeError
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
406 self.assertRaises(RuntimeError, self.func, add, TestFailingIter())
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
407
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
408 self.assertEqual(self.func(add, [], None), None)
dee30c46739e Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 63810
diff changeset
409 self.assertEqual(self.func(add, [], 42), 42)
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
410
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
411 class BadSeq:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
412 def __getitem__(self, index):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
413 raise ValueError
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
414 self.assertRaises(ValueError, self.func, 42, BadSeq())
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
415
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
416 # Test reduce()'s use of iterators.
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
417 def test_iterator_usage(self):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
418 class SequenceClass:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
419 def __init__(self, n):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
420 self.n = n
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
421 def __getitem__(self, i):
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
422 if 0 <= i < self.n:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
423 return i
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
424 else:
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
425 raise IndexError
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
426
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
427 from operator import add
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
428 self.assertEqual(self.func(add, SequenceClass(5)), 10)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
429 self.assertEqual(self.func(add, SequenceClass(5), 42), 52)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
430 self.assertRaises(TypeError, self.func, add, SequenceClass(0))
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
431 self.assertEqual(self.func(add, SequenceClass(0), 42), 42)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
432 self.assertEqual(self.func(add, SequenceClass(1)), 0)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
433 self.assertEqual(self.func(add, SequenceClass(1), 42), 42)
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
434
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
435 d = {"one": 1, "two": 2, "three": 3}
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
436 self.assertEqual(self.func(add, d), "".join(d.keys()))
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
437
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
438 class TestCmpToKey(unittest.TestCase):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
439 def test_cmp_to_key(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
440 def mycmp(x, y):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
441 return y - x
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
442 self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)),
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
443 [4, 3, 2, 1, 0])
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
444
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
445 def test_hash(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
446 def mycmp(x, y):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
447 return y - x
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
448 key = functools.cmp_to_key(mycmp)
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
449 k = key(10)
69810
2675415c57c1 Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
Raymond Hettinger <python@rcn.com>
parents: 68257
diff changeset
450 self.assertRaises(TypeError, hash, k)
2675415c57c1 Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
Raymond Hettinger <python@rcn.com>
parents: 68257
diff changeset
451 self.assertFalse(isinstance(k, collections.Hashable))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
452
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
453 class TestTotalOrdering(unittest.TestCase):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
454
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
455 def test_total_ordering_lt(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
456 @functools.total_ordering
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
457 class A:
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
458 def __init__(self, value):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
459 self.value = value
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
460 def __lt__(self, other):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
461 return self.value < other.value
67458
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
462 def __eq__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
463 return self.value == other.value
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
464 self.assertTrue(A(1) < A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
465 self.assertTrue(A(2) > A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
466 self.assertTrue(A(1) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
467 self.assertTrue(A(2) >= A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
468 self.assertTrue(A(2) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
469 self.assertTrue(A(2) >= A(2))
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
470
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
471 def test_total_ordering_le(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
472 @functools.total_ordering
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
473 class A:
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
474 def __init__(self, value):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
475 self.value = value
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
476 def __le__(self, other):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
477 return self.value <= other.value
67458
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
478 def __eq__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
479 return self.value == other.value
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
480 self.assertTrue(A(1) < A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
481 self.assertTrue(A(2) > A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
482 self.assertTrue(A(1) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
483 self.assertTrue(A(2) >= A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
484 self.assertTrue(A(2) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
485 self.assertTrue(A(2) >= A(2))
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
486
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
487 def test_total_ordering_gt(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
488 @functools.total_ordering
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
489 class A:
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
490 def __init__(self, value):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
491 self.value = value
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
492 def __gt__(self, other):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
493 return self.value > other.value
67458
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
494 def __eq__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
495 return self.value == other.value
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
496 self.assertTrue(A(1) < A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
497 self.assertTrue(A(2) > A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
498 self.assertTrue(A(1) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
499 self.assertTrue(A(2) >= A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
500 self.assertTrue(A(2) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
501 self.assertTrue(A(2) >= A(2))
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
502
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
503 def test_total_ordering_ge(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
504 @functools.total_ordering
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
505 class A:
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
506 def __init__(self, value):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
507 self.value = value
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
508 def __ge__(self, other):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
509 return self.value >= other.value
67458
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
510 def __eq__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
511 return self.value == other.value
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
512 self.assertTrue(A(1) < A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
513 self.assertTrue(A(2) > A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
514 self.assertTrue(A(1) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
515 self.assertTrue(A(2) >= A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
516 self.assertTrue(A(2) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
517 self.assertTrue(A(2) >= A(2))
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
518
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
519 def test_total_ordering_no_overwrite(self):
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
520 # new methods should not overwrite existing
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
521 @functools.total_ordering
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 59613
diff changeset
522 class A(int):
64243
51a24a915408 run total_ordering() tests, and fix the function (default comparisons shouldn't be considered)
Benjamin Peterson <benjamin@python.org>
parents: 64102
diff changeset
523 pass
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
524 self.assertTrue(A(1) < A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
525 self.assertTrue(A(2) > A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
526 self.assertTrue(A(1) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
527 self.assertTrue(A(2) >= A(1))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
528 self.assertTrue(A(2) <= A(2))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
529 self.assertTrue(A(2) >= A(2))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
530
60494
e0e355ae99b2 Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 60370
diff changeset
531 def test_no_operations_defined(self):
e0e355ae99b2 Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 60370
diff changeset
532 with self.assertRaises(ValueError):
e0e355ae99b2 Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 60370
diff changeset
533 @functools.total_ordering
e0e355ae99b2 Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 60370
diff changeset
534 class A:
e0e355ae99b2 Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 60370
diff changeset
535 pass
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
536
67458
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
537 def test_bug_10042(self):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
538 @functools.total_ordering
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
539 class TestTO:
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
540 def __init__(self, value):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
541 self.value = value
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
542 def __eq__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
543 if isinstance(other, TestTO):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
544 return self.value == other.value
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
545 return False
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
546 def __lt__(self, other):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
547 if isinstance(other, TestTO):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
548 return self.value < other.value
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
549 raise TypeError
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
550 with self.assertRaises(TypeError):
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
551 TestTO(8) <= ()
6492a713887e Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents: 66587
diff changeset
552
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
553 class TestLRU(unittest.TestCase):
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
554
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
555 def test_lru(self):
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
556 def orig(x, y):
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
557 return 3*x+y
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
558 f = functools.lru_cache(maxsize=20)(orig)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
559 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
560 self.assertEqual(maxsize, 20)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
561 self.assertEqual(currsize, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
562 self.assertEqual(hits, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
563 self.assertEqual(misses, 0)
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
564
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
565 domain = range(5)
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
566 for i in range(1000):
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
567 x, y = choice(domain), choice(domain)
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
568 actual = f(x, y)
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
569 expected = orig(x, y)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64448
diff changeset
570 self.assertEqual(actual, expected)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
571 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
572 self.assertTrue(hits > misses)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
573 self.assertEqual(hits + misses, 1000)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
574 self.assertEqual(currsize, 20)
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
575
64448
61c44793238d Adopt more descriptive attribute names as suggested on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 64243
diff changeset
576 f.cache_clear() # test clearing
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
577 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
578 self.assertEqual(hits, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
579 self.assertEqual(misses, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
580 self.assertEqual(currsize, 0)
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
581 f(x, y)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
582 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
583 self.assertEqual(hits, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
584 self.assertEqual(misses, 1)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
585 self.assertEqual(currsize, 1)
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
586
64102
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
587 # Test bypassing the cache
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
588 self.assertIs(f.__wrapped__, orig)
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
589 f.__wrapped__(x, y)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
590 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
591 self.assertEqual(hits, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
592 self.assertEqual(misses, 1)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
593 self.assertEqual(currsize, 1)
64102
77bf58296c87 Document and test the resolution of issue 3445 (tolerate missing attributes in functools.update_wrapper, previously implemented as a side effect of the __annotations__ copying patch) and implement issue 9567 (add a __wrapped__ attribute when using update_wrapper)
Nick Coghlan <ncoghlan@gmail.com>
parents: 64068
diff changeset
594
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
595 # test size zero (which means "never-cache")
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
596 @functools.lru_cache(0)
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
597 def f():
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
598 nonlocal f_cnt
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
599 f_cnt += 1
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
600 return 20
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
601 self.assertEqual(f.cache_info().maxsize, 0)
64027
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
602 f_cnt = 0
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
603 for i in range(5):
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
604 self.assertEqual(f(), 20)
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
605 self.assertEqual(f_cnt, 5)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
606 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
607 self.assertEqual(hits, 0)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
608 self.assertEqual(misses, 5)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
609 self.assertEqual(currsize, 0)
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
610
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
611 # test size one
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
612 @functools.lru_cache(1)
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
613 def f():
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
614 nonlocal f_cnt
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
615 f_cnt += 1
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
616 return 20
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
617 self.assertEqual(f.cache_info().maxsize, 1)
64027
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
618 f_cnt = 0
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
619 for i in range(5):
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
620 self.assertEqual(f(), 20)
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
621 self.assertEqual(f_cnt, 1)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
622 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
623 self.assertEqual(hits, 4)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
624 self.assertEqual(misses, 1)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
625 self.assertEqual(currsize, 1)
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
626
64027
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
627 # test size two
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
628 @functools.lru_cache(2)
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
629 def f(x):
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
630 nonlocal f_cnt
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
631 f_cnt += 1
64027
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
632 return x*10
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
633 self.assertEqual(f.cache_info().maxsize, 2)
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
634 f_cnt = 0
64027
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
635 for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7:
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
636 # * * * *
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
637 self.assertEqual(f(x), x*10)
6dfc7da17914 Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents: 64025
diff changeset
638 self.assertEqual(f_cnt, 4)
66573
2b2fc4262170 Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents: 66550
diff changeset
639 hits, misses, maxsize, currsize = f.cache_info()
66550
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
640 self.assertEqual(hits, 12)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
641 self.assertEqual(misses, 4)
312b2916cfd4 Issue 10586: change the new functools.lru_cache implementation to expose the maximum and current cache sizes through the public statistics API. This API is now a single function that returns a named tuple.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66310
diff changeset
642 self.assertEqual(currsize, 2)
64025
8d0ab555a160 Support cache sizes.
Raymond Hettinger <python@rcn.com>
parents: 63745
diff changeset
643
66582
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
644 def test_lru_with_maxsize_none(self):
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
645 @functools.lru_cache(maxsize=None)
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
646 def fib(n):
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
647 if n < 2:
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
648 return n
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
649 return fib(n-1) + fib(n-2)
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
650 self.assertEqual([fib(n) for n in range(16)],
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
651 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
652 self.assertEqual(fib.cache_info(),
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
653 functools._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
654 fib.cache_clear()
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
655 self.assertEqual(fib.cache_info(),
00c8cdf53936 Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents: 66573
diff changeset
656 functools._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
657
72943
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
658 def test_lru_with_exceptions(self):
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
659 # Verify that user_function exceptions get passed through without
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
660 # creating a hard-to-read chained exception.
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
661 # http://bugs.python.org/issue13177
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
662 for maxsize in (None, 100):
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
663 @functools.lru_cache(maxsize)
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
664 def func(i):
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
665 return 'abc'[i]
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
666 self.assertEqual(func(0), 'a')
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
667 with self.assertRaises(IndexError) as cm:
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
668 func(15)
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
669 self.assertIsNone(cm.exception.__context__)
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
670 # Verify that the previous exception did not result in a cached entry
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
671 with self.assertRaises(IndexError):
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
672 func(15)
8365f82f8a13 Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
Raymond Hettinger <python@rcn.com>
parents: 69810
diff changeset
673
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
674 def test_main(verbose=None):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
675 test_classes = (
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
676 TestPartial,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
677 TestPartialSubclass,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
678 TestPythonPartial,
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
679 TestUpdateWrapper,
64243
51a24a915408 run total_ordering() tests, and fix the function (default comparisons shouldn't be considered)
Benjamin Peterson <benjamin@python.org>
parents: 64102
diff changeset
680 TestTotalOrdering,
69810
2675415c57c1 Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
Raymond Hettinger <python@rcn.com>
parents: 68257
diff changeset
681 TestCmpToKey,
39691
c4d8c054616c Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents: 38673
diff changeset
682 TestWraps,
63370
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
683 TestReduce,
c55876a6fa3c Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents: 63357
diff changeset
684 TestLRU,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
685 )
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42142
diff changeset
686 support.run_unittest(*test_classes)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
687
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
688 # verify reference counting
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
689 if verbose and hasattr(sys, "gettotalrefcount"):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
690 import gc
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
691 counts = [None] * 5
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
692 for i in range(len(counts)):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 42142
diff changeset
693 support.run_unittest(*test_classes)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
694 gc.collect()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
695 counts[i] = sys.gettotalrefcount()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
696 print(counts)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
697
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
698 if __name__ == '__main__':
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff changeset
699 test_main(verbose=True)