Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_functools.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 18a44d65d34a |
| children |
| rev | line source |
|---|---|
|
86867
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
1 import abc |
|
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 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
3 from itertools import permutations |
|
52637
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
4 import pickle |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
5 from random import choice |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
6 import sys |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
7 from test import support |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
8 import unittest |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
9 from weakref import proxy |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
10 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
11 import functools |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
12 |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
13 py_functools = support.import_fresh_module('functools', blocked=['_functools']) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
14 c_functools = support.import_fresh_module('functools', fresh=['_functools']) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
15 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
16 decimal = support.import_fresh_module('decimal', fresh=['_decimal']) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
17 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
18 |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
19 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
|
20 """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
|
21 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
|
22 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
23 |
|
52637
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
24 def signature(part): |
|
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
25 """ return the signature of a partial object """ |
|
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
26 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
|
27 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
28 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
29 class TestPartial: |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
30 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
31 def test_basic_examples(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
32 p = self.partial(capture, 1, 2, a=10, b=20) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
33 self.assertTrue(callable(p)) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
34 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
|
35 ((1, 2, 3, 4), dict(a=10, b=30, c=40))) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
36 p = self.partial(map, lambda x: x*10) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
37 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
|
38 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
39 def test_attributes(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
40 p = self.partial(capture, 1, 2, a=10, b=20) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
41 # 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
|
42 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
|
43 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
|
44 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
|
45 # attributes should not be writable |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
46 if not isinstance(self.partial, type): |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
52 p = self.partial(hex) |
|
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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
60 def test_argument_checking(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
61 self.assertRaises(TypeError, self.partial) # need at least a func arg |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
62 try: |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
63 self.partial(2)() |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
64 except TypeError: |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
65 pass |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
66 else: |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
67 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
|
68 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
69 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
|
70 # 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
|
71 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
|
72 return a |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
73 d = {'a':3} |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
74 p = self.partial(func, a=5) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
75 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
|
76 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
|
77 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
|
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 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
80 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
|
81 # 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
|
82 # object or the caller |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
83 p = self.partial(capture) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
84 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
|
85 self.assertEqual(p(1,2), ((1,2), {})) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
86 p = self.partial(capture, 1, 2) |
|
38669
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), {})) |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
88 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
|
89 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
90 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
|
91 # 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
|
92 # either the partial object or the caller |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
93 p = self.partial(capture) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
94 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
|
95 self.assertEqual(p(a=1), ((), {'a':1})) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
96 p = self.partial(capture, 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
|
97 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
|
98 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
|
99 # 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
|
100 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
|
101 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
102 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
|
103 # 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
|
104 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]: |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
105 p = self.partial(capture, *args) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
106 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
|
107 got, empty = p('x') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
108 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
|
109 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
110 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
|
111 # 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
|
112 for a in ['a', 0, None, 3.5]: |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
113 p = self.partial(capture, a=a) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
114 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
|
115 empty, got = p(x=None) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
116 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
|
117 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
118 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
|
119 # make sure there are no side effects that affect subsequent calls |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
120 p = self.partial(capture, 0, 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
|
121 args1, kw1 = p(1, b=2) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
122 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
|
123 args2, kw2 = p() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
124 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
|
125 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
126 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
|
127 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
|
128 x / y |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
129 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
130 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
131 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
132 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
133 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
134 def test_weakref(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
135 f = self.partial(int, base=16) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
136 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
|
137 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
|
138 f = None |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
139 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
|
140 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
141 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
|
142 data = list(map(str, range(10))) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
143 join = self.partial(str.join, '') |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
144 self.assertEqual(join(data), '0123456789') |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
145 join = self.partial(''.join) |
|
38669
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 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
148 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
149 @unittest.skipUnless(c_functools, 'requires the C _functools module') |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
150 class TestPartialC(TestPartial, unittest.TestCase): |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
151 if c_functools: |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
152 partial = c_functools.partial |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
153 |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
154 def test_repr(self): |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
155 args = (object(), object()) |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
156 args_repr = ', '.join(repr(a) for a in args) |
|
87329
18a44d65d34a
Issue #19681: Apply a quick and minimal band-aid.
Christian Heimes <christian@cheimes.de>
parents:
86867
diff
changeset
|
157 #kwargs = {'a': object(), 'b': object()} |
|
18a44d65d34a
Issue #19681: Apply a quick and minimal band-aid.
Christian Heimes <christian@cheimes.de>
parents:
86867
diff
changeset
|
158 kwargs = {'a': object()} |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
159 kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items()) |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
160 if self.partial is c_functools.partial: |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
161 name = 'functools.partial' |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
162 else: |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
163 name = self.partial.__name__ |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
164 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
165 f = self.partial(capture) |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
166 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
|
167 repr(f)) |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
168 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
169 f = self.partial(capture, *args) |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
170 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
|
171 repr(f)) |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
172 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
173 f = self.partial(capture, **kwargs) |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
174 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
|
175 repr(f)) |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
176 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
177 f = self.partial(capture, *args, **kwargs) |
|
66587
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
178 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
|
179 repr(f)) |
|
2baad8bd0b4f
Issue #4113: Added custom __repr__ method to functools.partial.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66582
diff
changeset
|
180 |
|
52637
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
181 def test_pickle(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
182 f = self.partial(signature, 'asdf', bar=True) |
|
52637
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
183 f.add_something_to__dict__ = True |
|
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
184 f_copy = pickle.loads(pickle.dumps(f)) |
|
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
185 self.assertEqual(signature(f), signature(f_copy)) |
|
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
186 |
|
81987
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
187 # Issue 6083: Reference counting bug |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
188 def test_setstate_refcount(self): |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
189 class BadSequence: |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
190 def __len__(self): |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
191 return 4 |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
192 def __getitem__(self, key): |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
193 if key == 0: |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
194 return max |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
195 elif key == 1: |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
196 return tuple(range(1000000)) |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
197 elif key in (2, 3): |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
198 return {} |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
199 raise IndexError |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
200 |
|
81989
3e3a7d825736
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
diff
changeset
|
201 f = self.partial(object) |
|
81987
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
202 self.assertRaisesRegex(SystemError, |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
203 "new style getargs format but argument is not a tuple", |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
204 f.__setstate__, BadSequence()) |
|
4bac47eb444c
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
Serhiy Storchaka <storchaka@gmail.com>
parents:
81791
diff
changeset
|
205 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
206 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
207 class TestPartialPy(TestPartial, unittest.TestCase): |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
208 partial = staticmethod(py_functools.partial) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
209 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
210 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
211 if c_functools: |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
212 class PartialSubclass(c_functools.partial): |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
213 pass |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
214 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
215 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
216 @unittest.skipUnless(c_functools, 'requires the C _functools module') |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
217 class TestPartialCSubclass(TestPartialC): |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
218 if c_functools: |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
219 partial = PartialSubclass |
|
52637
ffa81bd2ec72
Merged revisions 70931 via svnmerge from
Jack Diederich <jackdied@gmail.com>
parents:
47201
diff
changeset
|
220 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
221 |
|
86867
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
222 class TestPartialMethod(unittest.TestCase): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
223 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
224 class A(object): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
225 nothing = functools.partialmethod(capture) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
226 positional = functools.partialmethod(capture, 1) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
227 keywords = functools.partialmethod(capture, a=2) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
228 both = functools.partialmethod(capture, 3, b=4) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
229 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
230 nested = functools.partialmethod(positional, 5) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
231 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
232 over_partial = functools.partialmethod(functools.partial(capture, c=6), 7) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
233 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
234 static = functools.partialmethod(staticmethod(capture), 8) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
235 cls = functools.partialmethod(classmethod(capture), d=9) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
236 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
237 a = A() |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
238 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
239 def test_arg_combinations(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
240 self.assertEqual(self.a.nothing(), ((self.a,), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
241 self.assertEqual(self.a.nothing(5), ((self.a, 5), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
242 self.assertEqual(self.a.nothing(c=6), ((self.a,), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
243 self.assertEqual(self.a.nothing(5, c=6), ((self.a, 5), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
244 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
245 self.assertEqual(self.a.positional(), ((self.a, 1), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
246 self.assertEqual(self.a.positional(5), ((self.a, 1, 5), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
247 self.assertEqual(self.a.positional(c=6), ((self.a, 1), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
248 self.assertEqual(self.a.positional(5, c=6), ((self.a, 1, 5), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
249 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
250 self.assertEqual(self.a.keywords(), ((self.a,), {'a': 2})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
251 self.assertEqual(self.a.keywords(5), ((self.a, 5), {'a': 2})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
252 self.assertEqual(self.a.keywords(c=6), ((self.a,), {'a': 2, 'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
253 self.assertEqual(self.a.keywords(5, c=6), ((self.a, 5), {'a': 2, 'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
254 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
255 self.assertEqual(self.a.both(), ((self.a, 3), {'b': 4})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
256 self.assertEqual(self.a.both(5), ((self.a, 3, 5), {'b': 4})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
257 self.assertEqual(self.a.both(c=6), ((self.a, 3), {'b': 4, 'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
258 self.assertEqual(self.a.both(5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
259 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
260 self.assertEqual(self.A.both(self.a, 5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
261 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
262 def test_nested(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
263 self.assertEqual(self.a.nested(), ((self.a, 1, 5), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
264 self.assertEqual(self.a.nested(6), ((self.a, 1, 5, 6), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
265 self.assertEqual(self.a.nested(d=7), ((self.a, 1, 5), {'d': 7})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
266 self.assertEqual(self.a.nested(6, d=7), ((self.a, 1, 5, 6), {'d': 7})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
267 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
268 self.assertEqual(self.A.nested(self.a, 6, d=7), ((self.a, 1, 5, 6), {'d': 7})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
269 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
270 def test_over_partial(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
271 self.assertEqual(self.a.over_partial(), ((self.a, 7), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
272 self.assertEqual(self.a.over_partial(5), ((self.a, 7, 5), {'c': 6})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
273 self.assertEqual(self.a.over_partial(d=8), ((self.a, 7), {'c': 6, 'd': 8})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
274 self.assertEqual(self.a.over_partial(5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
275 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
276 self.assertEqual(self.A.over_partial(self.a, 5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
277 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
278 def test_bound_method_introspection(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
279 obj = self.a |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
280 self.assertIs(obj.both.__self__, obj) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
281 self.assertIs(obj.nested.__self__, obj) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
282 self.assertIs(obj.over_partial.__self__, obj) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
283 self.assertIs(obj.cls.__self__, self.A) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
284 self.assertIs(self.A.cls.__self__, self.A) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
285 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
286 def test_unbound_method_retrieval(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
287 obj = self.A |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
288 self.assertFalse(hasattr(obj.both, "__self__")) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
289 self.assertFalse(hasattr(obj.nested, "__self__")) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
290 self.assertFalse(hasattr(obj.over_partial, "__self__")) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
291 self.assertFalse(hasattr(obj.static, "__self__")) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
292 self.assertFalse(hasattr(self.a.static, "__self__")) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
293 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
294 def test_descriptors(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
295 for obj in [self.A, self.a]: |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
296 with self.subTest(obj=obj): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
297 self.assertEqual(obj.static(), ((8,), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
298 self.assertEqual(obj.static(5), ((8, 5), {})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
299 self.assertEqual(obj.static(d=8), ((8,), {'d': 8})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
300 self.assertEqual(obj.static(5, d=8), ((8, 5), {'d': 8})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
301 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
302 self.assertEqual(obj.cls(), ((self.A,), {'d': 9})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
303 self.assertEqual(obj.cls(5), ((self.A, 5), {'d': 9})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
304 self.assertEqual(obj.cls(c=8), ((self.A,), {'c': 8, 'd': 9})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
305 self.assertEqual(obj.cls(5, c=8), ((self.A, 5), {'c': 8, 'd': 9})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
306 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
307 def test_overriding_keywords(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
308 self.assertEqual(self.a.keywords(a=3), ((self.a,), {'a': 3})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
309 self.assertEqual(self.A.keywords(self.a, a=3), ((self.a,), {'a': 3})) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
310 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
311 def test_invalid_args(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
312 with self.assertRaises(TypeError): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
313 class B(object): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
314 method = functools.partialmethod(None, 1) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
315 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
316 def test_repr(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
317 self.assertEqual(repr(vars(self.A)['both']), |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
318 'functools.partialmethod({}, 3, b=4)'.format(capture)) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
319 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
320 def test_abstract(self): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
321 class Abstract(abc.ABCMeta): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
322 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
323 @abc.abstractmethod |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
324 def add(self, x, y): |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
325 pass |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
326 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
327 add5 = functools.partialmethod(add, 5) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
328 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
329 self.assertTrue(Abstract.add.__isabstractmethod__) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
330 self.assertTrue(Abstract.add5.__isabstractmethod__) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
331 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
332 for func in [self.A.static, self.A.cls, self.A.over_partial, self.A.nested, self.A.both]: |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
333 self.assertFalse(getattr(func, '__isabstractmethod__', False)) |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
334 |
|
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
335 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
336 class TestUpdateWrapper(unittest.TestCase): |
|
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 check_wrapper(self, wrapper, wrapped, |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
339 assigned=functools.WRAPPER_ASSIGNMENTS, |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
340 updated=functools.WRAPPER_UPDATES): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
341 # Check attributes were assigned |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
342 for name in assigned: |
|
84629
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
343 self.assertIs(getattr(wrapper, name), getattr(wrapped, name)) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
344 # Check attributes were updated |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
345 for name in updated: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
346 wrapper_attr = getattr(wrapper, name) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
347 wrapped_attr = getattr(wrapped, name) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
348 for key in wrapped_attr: |
|
84629
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
349 if name == "__dict__" and key == "__wrapped__": |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
350 # __wrapped__ is overwritten by the update code |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
351 continue |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
352 self.assertIs(wrapped_attr[key], wrapper_attr[key]) |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
353 # Check __wrapped__ |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
354 self.assertIs(wrapper.__wrapped__, wrapped) |
|
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
355 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
356 |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
357 def _default_update(self): |
|
63810
15839ca43d96
Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
358 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
|
359 """This is a test""" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
360 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
361 f.attr = 'This is also a test' |
|
84629
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
362 f.__wrapped__ = "This is a bald faced lie" |
|
63810
15839ca43d96
Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
363 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
|
364 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
365 functools.update_wrapper(wrapper, f) |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
366 return wrapper, f |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
367 |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
368 def test_default_update(self): |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
369 wrapper, f = self._default_update() |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
370 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
|
371 self.assertIs(wrapper.__wrapped__, f) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
372 self.assertEqual(wrapper.__name__, 'f') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
373 self.assertEqual(wrapper.__qualname__, f.__qualname__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
374 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
|
375 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
|
376 self.assertNotIn('b', wrapper.__annotations__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
377 |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
378 @unittest.skipIf(sys.flags.optimize >= 2, |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
379 "Docstrings are omitted with -O2 and above") |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
380 def test_default_update_doc(self): |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
381 wrapper, f = self._default_update() |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
382 self.assertEqual(wrapper.__doc__, 'This is a test') |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
383 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
384 def test_no_update(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
385 def f(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
386 """This is a test""" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
387 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
388 f.attr = 'This is also a test' |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
389 def wrapper(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
390 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
391 functools.update_wrapper(wrapper, f, (), ()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
392 self.check_wrapper(wrapper, f, (), ()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
393 self.assertEqual(wrapper.__name__, 'wrapper') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
394 self.assertNotEqual(wrapper.__qualname__, f.__qualname__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
395 self.assertEqual(wrapper.__doc__, None) |
|
63810
15839ca43d96
Issue 8814: functools.wraps() did not copy __annotations__.
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
396 self.assertEqual(wrapper.__annotations__, {}) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
397 self.assertFalse(hasattr(wrapper, 'attr')) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
398 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
399 def test_selective_update(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
400 def f(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
401 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
402 f.attr = 'This is a different test' |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
403 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
|
404 def wrapper(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
405 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
406 wrapper.dict_attr = {} |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
407 assign = ('attr',) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
408 update = ('dict_attr',) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
409 functools.update_wrapper(wrapper, f, assign, update) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
410 self.check_wrapper(wrapper, f, assign, update) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
411 self.assertEqual(wrapper.__name__, 'wrapper') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
412 self.assertNotEqual(wrapper.__qualname__, f.__qualname__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
413 self.assertEqual(wrapper.__doc__, None) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
414 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
|
415 self.assertEqual(wrapper.dict_attr, f.dict_attr) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
416 |
|
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
|
417 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
|
418 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
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 # 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
|
426 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
|
427 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
|
428 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
|
429 # 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
|
430 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
|
431 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
|
432 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
|
433 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
|
434 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
|
435 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
|
436 |
|
81781
241b655c23b6
Issue #17041: Fix testing when Python is configured with the
Serhiy Storchaka <storchaka@gmail.com>
parents:
72943
diff
changeset
|
437 @support.requires_docstrings |
|
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
|
438 @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
|
439 "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
|
440 def test_builtin_update(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39691
diff
changeset
|
441 # Test for bug #1576241 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39691
diff
changeset
|
442 def wrapper(): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39691
diff
changeset
|
443 pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39691
diff
changeset
|
444 functools.update_wrapper(wrapper, max) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39691
diff
changeset
|
445 self.assertEqual(wrapper.__name__, 'max') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
446 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
|
447 self.assertEqual(wrapper.__annotations__, {}) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
448 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
449 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
450 class TestWraps(TestUpdateWrapper): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
451 |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
452 def _default_update(self): |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
453 def f(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
454 """This is a test""" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
455 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
456 f.attr = 'This is also a test' |
|
84629
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
457 f.__wrapped__ = "This is still a bald faced lie" |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
458 @functools.wraps(f) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
459 def wrapper(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
460 pass |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
461 return wrapper, f |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
462 |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
463 def test_default_update(self): |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
464 wrapper, f = self._default_update() |
|
84629
13b8fd71db46
Close issue 17482: don't overwrite __wrapped__
Nick Coghlan <ncoghlan@gmail.com>
parents:
84411
diff
changeset
|
465 self.check_wrapper(wrapper, f) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
466 self.assertEqual(wrapper.__name__, 'f') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
467 self.assertEqual(wrapper.__qualname__, f.__qualname__) |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
468 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
|
469 |
|
81791
d4bdd6ec4594
Fix skip conditions in some docstings tests.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81781
diff
changeset
|
470 @unittest.skipIf(sys.flags.optimize >= 2, |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
471 "Docstrings are omitted with -O2 and above") |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55593
diff
changeset
|
472 def test_default_update_doc(self): |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
473 wrapper, _ = self._default_update() |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
474 self.assertEqual(wrapper.__doc__, 'This is a test') |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
475 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
476 def test_no_update(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
477 def f(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
478 """This is a test""" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
479 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
480 f.attr = 'This is also a test' |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
481 @functools.wraps(f, (), ()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
482 def wrapper(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
483 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
484 self.check_wrapper(wrapper, f, (), ()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
485 self.assertEqual(wrapper.__name__, 'wrapper') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
486 self.assertNotEqual(wrapper.__qualname__, f.__qualname__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
487 self.assertEqual(wrapper.__doc__, None) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52637
diff
changeset
|
488 self.assertFalse(hasattr(wrapper, 'attr')) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
489 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
490 def test_selective_update(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
491 def f(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
492 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
493 f.attr = 'This is a different test' |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
494 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
|
495 def add_dict_attr(f): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
496 f.dict_attr = {} |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
497 return f |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
498 assign = ('attr',) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
499 update = ('dict_attr',) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
500 @functools.wraps(f, assign, update) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
501 @add_dict_attr |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
502 def wrapper(): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
503 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
504 self.check_wrapper(wrapper, f, assign, update) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
505 self.assertEqual(wrapper.__name__, 'wrapper') |
|
73943
963e98f5ad31
Issue #13544: Add __qualname__ to functools.WRAPPER_ASSIGNMENTS.
Meador Inge <meadori@gmail.com>
parents:
73019
diff
changeset
|
506 self.assertNotEqual(wrapper.__qualname__, f.__qualname__) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
507 self.assertEqual(wrapper.__doc__, None) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
508 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
|
509 self.assertEqual(wrapper.dict_attr, f.dict_attr) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
510 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
511 |
|
39691
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
512 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
|
513 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
|
514 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 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
|
519 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
|
520 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
521 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
|
522 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
|
523 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 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
|
528 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
|
529 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
|
530 return self.sofar[i] |
|
64074
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
531 def add(x, y): |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
532 return x + y |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
533 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
|
534 self.assertEqual( |
|
64074
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
535 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
|
536 ['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
|
537 ) |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
538 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
|
539 self.assertEqual( |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
540 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
|
541 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
|
542 ) |
|
64074
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 self.assertRaises(TypeError, self.func, add, "") |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
554 self.assertRaises(TypeError, self.func, add, ()) |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
555 self.assertRaises(TypeError, self.func, add, object()) |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
556 |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
557 class TestFailingIter: |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
558 def __iter__(self): |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
559 raise RuntimeError |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
560 self.assertRaises(RuntimeError, self.func, add, TestFailingIter()) |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
561 |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
562 self.assertEqual(self.func(add, [], None), None) |
|
dee30c46739e
Merged revisions 84098 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
63810
diff
changeset
|
563 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
|
564 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
565 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
|
566 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
|
567 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
|
568 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
|
569 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
570 # 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
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 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
|
577 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
|
578 else: |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
579 raise IndexError |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
580 |
|
39691
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
581 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
|
582 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
|
583 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
|
584 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
|
585 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
|
586 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
|
587 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
|
588 |
|
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
589 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
|
590 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
|
591 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
592 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
593 class TestCmpToKey: |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
594 |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
595 def test_cmp_to_key(self): |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
596 def cmp1(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
597 return (x > y) - (x < y) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
598 key = self.cmp_to_key(cmp1) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
599 self.assertEqual(key(3), key(3)) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
600 self.assertGreater(key(3), key(1)) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
601 self.assertGreaterEqual(key(3), key(3)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
602 |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
603 def cmp2(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
604 return int(x) - int(y) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
605 key = self.cmp_to_key(cmp2) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
606 self.assertEqual(key(4.0), key('4')) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
607 self.assertLess(key(2), key('35')) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
608 self.assertLessEqual(key(2), key('35')) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
609 self.assertNotEqual(key(2), key('35')) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
610 |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
611 def test_cmp_to_key_arguments(self): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
612 def cmp1(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
613 return (x > y) - (x < y) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
614 key = self.cmp_to_key(mycmp=cmp1) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
615 self.assertEqual(key(obj=3), key(obj=3)) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
616 self.assertGreater(key(obj=3), key(obj=1)) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
617 with self.assertRaises((TypeError, AttributeError)): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
618 key(3) > 1 # rhs is not a K object |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
619 with self.assertRaises((TypeError, AttributeError)): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
620 1 < key(3) # lhs is not a K object |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
621 with self.assertRaises(TypeError): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
622 key = self.cmp_to_key() # too few args |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
623 with self.assertRaises(TypeError): |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
624 key = self.cmp_to_key(cmp1, None) # too many args |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
625 key = self.cmp_to_key(cmp1) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
626 with self.assertRaises(TypeError): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
627 key() # too few args |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
628 with self.assertRaises(TypeError): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
629 key(None, None) # too many args |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
630 |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
631 def test_bad_cmp(self): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
632 def cmp1(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
633 raise ZeroDivisionError |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
634 key = self.cmp_to_key(cmp1) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
635 with self.assertRaises(ZeroDivisionError): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
636 key(3) > key(1) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
637 |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
638 class BadCmp: |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
639 def __lt__(self, other): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
640 raise ZeroDivisionError |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
641 def cmp1(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
642 return BadCmp() |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
643 with self.assertRaises(ZeroDivisionError): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
644 key(3) > key(1) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
645 |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
646 def test_obj_field(self): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
647 def cmp1(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
648 return (x > y) - (x < y) |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
649 key = self.cmp_to_key(mycmp=cmp1) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
650 self.assertEqual(key(50).obj, 50) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
651 |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
652 def test_sort_int(self): |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
653 def mycmp(x, y): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
654 return y - x |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
655 self.assertEqual(sorted(range(5), key=self.cmp_to_key(mycmp)), |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
656 [4, 3, 2, 1, 0]) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
657 |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
658 def test_sort_int_str(self): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
659 def mycmp(x, y): |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
660 x, y = int(x), int(y) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
661 return (x > y) - (x < y) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
662 values = [5, '3', 7, 2, '0', '1', 4, '10', 1] |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
663 values = sorted(values, key=self.cmp_to_key(mycmp)) |
|
69150
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
664 self.assertEqual([int(value) for value in values], |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
665 [0, 1, 1, 2, 3, 4, 5, 7, 10]) |
|
a03fb2fc3ed8
Issue #11707: Fast C version of functools.cmp_to_key()
Raymond Hettinger <python@rcn.com>
parents:
68257
diff
changeset
|
666 |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
667 def test_hash(self): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
668 def mycmp(x, y): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
669 return y - x |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
670 key = self.cmp_to_key(mycmp) |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
671 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
|
672 self.assertRaises(TypeError, hash, k) |
|
69811
27e5c10ad0bf
Fix __hash__ in functools.cmp_to_key() to work with collections.Hashable.
Raymond Hettinger <python@rcn.com>
diff
changeset
|
673 self.assertNotIsInstance(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
|
674 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
675 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
676 @unittest.skipUnless(c_functools, 'requires the C _functools module') |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
677 class TestCmpToKeyC(TestCmpToKey, unittest.TestCase): |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
678 if c_functools: |
|
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
679 cmp_to_key = c_functools.cmp_to_key |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
680 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
681 |
|
82025
2a369c32f1f1
Issue #17122: Fix and cleanup test_functools.py.
Serhiy Storchaka <storchaka@gmail.com>
parents:
81989
diff
changeset
|
682 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
683 cmp_to_key = staticmethod(py_functools.cmp_to_key) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
684 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
685 |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
686 class TestTotalOrdering(unittest.TestCase): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
687 |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
688 def test_total_ordering_lt(self): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
689 @functools.total_ordering |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
690 class A: |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
691 def __init__(self, value): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
692 self.value = value |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
693 def __lt__(self, other): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
694 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
|
695 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
|
696 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
|
697 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
|
698 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
|
699 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
|
700 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
|
701 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
|
702 self.assertTrue(A(2) >= A(2)) |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
703 self.assertFalse(A(1) > A(2)) |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
704 |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
705 def test_total_ordering_le(self): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
706 @functools.total_ordering |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
707 class A: |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
708 def __init__(self, value): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
709 self.value = value |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
710 def __le__(self, other): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
711 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
|
712 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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 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
|
719 self.assertTrue(A(2) >= A(2)) |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
720 self.assertFalse(A(1) >= A(2)) |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
721 |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
722 def test_total_ordering_gt(self): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
723 @functools.total_ordering |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
724 class A: |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
725 def __init__(self, value): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
726 self.value = value |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
727 def __gt__(self, other): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
728 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
|
729 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
|
730 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
|
731 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
|
732 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
|
733 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
|
734 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
|
735 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
|
736 self.assertTrue(A(2) >= A(2)) |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
737 self.assertFalse(A(2) < A(1)) |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
738 |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
739 def test_total_ordering_ge(self): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
740 @functools.total_ordering |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
741 class A: |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
742 def __init__(self, value): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
743 self.value = value |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
744 def __ge__(self, other): |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
745 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
|
746 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
|
747 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
|
748 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
|
749 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
|
750 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
|
751 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
|
752 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
|
753 self.assertTrue(A(2) >= A(2)) |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
754 self.assertFalse(A(2) <= A(1)) |
|
60370
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
755 |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
756 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
|
757 # new methods should not overwrite existing |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
758 @functools.total_ordering |
|
3ce66c6922ab
Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents:
59613
diff
changeset
|
759 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
|
760 pass |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64448
diff
changeset
|
761 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
|
762 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
|
763 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
|
764 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
|
765 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
|
766 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
|
767 |
|
60494
e0e355ae99b2
Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
60370
diff
changeset
|
768 def test_no_operations_defined(self): |
|
e0e355ae99b2
Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
60370
diff
changeset
|
769 with self.assertRaises(ValueError): |
|
e0e355ae99b2
Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
60370
diff
changeset
|
770 @functools.total_ordering |
|
e0e355ae99b2
Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
60370
diff
changeset
|
771 class A: |
|
e0e355ae99b2
Merged revisions 79944 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
60370
diff
changeset
|
772 pass |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
773 |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
774 def test_type_error_when_not_implemented(self): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
775 # bug 10042; ensure stack overflow does not occur |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
776 # when decorated types return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
777 @functools.total_ordering |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
778 class ImplementsLessThan: |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
779 def __init__(self, value): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
780 self.value = value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
781 def __eq__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
782 if isinstance(other, ImplementsLessThan): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
783 return self.value == other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
784 return False |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
785 def __lt__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
786 if isinstance(other, ImplementsLessThan): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
787 return self.value < other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
788 return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
789 |
|
67458
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
790 @functools.total_ordering |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
791 class ImplementsGreaterThan: |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
792 def __init__(self, value): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
793 self.value = value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
794 def __eq__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
795 if isinstance(other, ImplementsGreaterThan): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
796 return self.value == other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
797 return False |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
798 def __gt__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
799 if isinstance(other, ImplementsGreaterThan): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
800 return self.value > other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
801 return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
802 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
803 @functools.total_ordering |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
804 class ImplementsLessThanEqualTo: |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
805 def __init__(self, value): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
806 self.value = value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
807 def __eq__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
808 if isinstance(other, ImplementsLessThanEqualTo): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
809 return self.value == other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
810 return False |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
811 def __le__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
812 if isinstance(other, ImplementsLessThanEqualTo): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
813 return self.value <= other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
814 return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
815 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
816 @functools.total_ordering |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
817 class ImplementsGreaterThanEqualTo: |
|
67458
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
818 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
|
819 self.value = value |
|
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
820 def __eq__(self, other): |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
821 if isinstance(other, ImplementsGreaterThanEqualTo): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
822 return self.value == other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
823 return False |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
824 def __ge__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
825 if isinstance(other, ImplementsGreaterThanEqualTo): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
826 return self.value >= other.value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
827 return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
828 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
829 @functools.total_ordering |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
830 class ComparatorNotImplemented: |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
831 def __init__(self, value): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
832 self.value = value |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
833 def __eq__(self, other): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
834 if isinstance(other, ComparatorNotImplemented): |
|
67458
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
835 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
|
836 return False |
|
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
837 def __lt__(self, other): |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
838 return NotImplemented |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
839 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
840 with self.subTest("LT < 1"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
841 ImplementsLessThan(-1) < 1 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
842 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
843 with self.subTest("LT < LE"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
844 ImplementsLessThan(0) < ImplementsLessThanEqualTo(0) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
845 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
846 with self.subTest("LT < GT"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
847 ImplementsLessThan(1) < ImplementsGreaterThan(1) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
848 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
849 with self.subTest("LE <= LT"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
850 ImplementsLessThanEqualTo(2) <= ImplementsLessThan(2) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
851 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
852 with self.subTest("LE <= GE"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
853 ImplementsLessThanEqualTo(3) <= ImplementsGreaterThanEqualTo(3) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
854 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
855 with self.subTest("GT > GE"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
856 ImplementsGreaterThan(4) > ImplementsGreaterThanEqualTo(4) |
|
67458
6492a713887e
Issue #10042: Fixed the total_ordering decorator to handle cross-type
Raymond Hettinger <python@rcn.com>
parents:
66587
diff
changeset
|
857 |
|
85913
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
858 with self.subTest("GT > LT"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
859 ImplementsGreaterThan(5) > ImplementsLessThan(5) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
860 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
861 with self.subTest("GE >= GT"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
862 ImplementsGreaterThanEqualTo(6) >= ImplementsGreaterThan(6) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
863 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
864 with self.subTest("GE >= LE"), self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
865 ImplementsGreaterThanEqualTo(7) >= ImplementsLessThanEqualTo(7) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
866 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
867 with self.subTest("GE when equal"): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
868 a = ComparatorNotImplemented(8) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
869 b = ComparatorNotImplemented(8) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
870 self.assertEqual(a, b) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
871 with self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
872 a >= b |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
873 |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
874 with self.subTest("LE when equal"): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
875 a = ComparatorNotImplemented(9) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
876 b = ComparatorNotImplemented(9) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
877 self.assertEqual(a, b) |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
878 with self.assertRaises(TypeError): |
|
ad9f207645ab
Close #10042: functools.total_ordering now handles NotImplemented
Nick Coghlan <ncoghlan@gmail.com>
parents:
84629
diff
changeset
|
879 a <= b |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
880 |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
881 class TestLRU(unittest.TestCase): |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
882 |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
883 def test_lru(self): |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
884 def orig(x, y): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
885 return 3 * x + y |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
886 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
|
887 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
|
888 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
|
889 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
|
890 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
|
891 self.assertEqual(misses, 0) |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
892 |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
893 domain = range(5) |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
894 for i in range(1000): |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
895 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
|
896 actual = f(x, y) |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
897 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
|
898 self.assertEqual(actual, expected) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
899 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
|
900 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
|
901 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
|
902 self.assertEqual(currsize, 20) |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
903 |
|
64448
61c44793238d
Adopt more descriptive attribute names as suggested on python-dev.
Raymond Hettinger <python@rcn.com>
parents:
64243
diff
changeset
|
904 f.cache_clear() # test clearing |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
905 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
|
906 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
|
907 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
|
908 self.assertEqual(currsize, 0) |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
909 f(x, y) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
910 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
|
911 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
|
912 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
|
913 self.assertEqual(currsize, 1) |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
914 |
|
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
|
915 # 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
|
916 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
|
917 f.__wrapped__(x, y) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
918 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
|
919 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
|
920 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
|
921 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
|
922 |
| 64025 | 923 # test size zero (which means "never-cache") |
| 924 @functools.lru_cache(0) | |
| 925 def f(): | |
| 926 nonlocal f_cnt | |
| 927 f_cnt += 1 | |
| 928 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
|
929 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
|
930 f_cnt = 0 |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
931 for i in range(5): |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
932 self.assertEqual(f(), 20) |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
933 self.assertEqual(f_cnt, 5) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
934 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
|
935 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
|
936 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
|
937 self.assertEqual(currsize, 0) |
| 64025 | 938 |
| 939 # test size one | |
| 940 @functools.lru_cache(1) | |
| 941 def f(): | |
| 942 nonlocal f_cnt | |
| 943 f_cnt += 1 | |
| 944 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
|
945 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
|
946 f_cnt = 0 |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
947 for i in range(5): |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
948 self.assertEqual(f(), 20) |
| 64025 | 949 self.assertEqual(f_cnt, 1) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
950 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
|
951 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
|
952 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
|
953 self.assertEqual(currsize, 1) |
| 64025 | 954 |
|
64027
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
955 # test size two |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
956 @functools.lru_cache(2) |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
957 def f(x): |
| 64025 | 958 nonlocal f_cnt |
| 959 f_cnt += 1 | |
|
64027
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
960 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
|
961 self.assertEqual(f.cache_info().maxsize, 2) |
| 64025 | 962 f_cnt = 0 |
|
64027
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
963 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
|
964 # * * * * |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
965 self.assertEqual(f(x), x*10) |
|
6dfc7da17914
Remove the lfu_cache. Add more tests.
Raymond Hettinger <python@rcn.com>
parents:
64025
diff
changeset
|
966 self.assertEqual(f_cnt, 4) |
|
66573
2b2fc4262170
Add example, tighten text, and minor clean-ups.
Raymond Hettinger <python@rcn.com>
parents:
66550
diff
changeset
|
967 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
|
968 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
|
969 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
|
970 self.assertEqual(currsize, 2) |
| 64025 | 971 |
|
66582
00c8cdf53936
Issue 10593: Adopt Nick's suggestion for an lru_cache with maxsize=None.
Raymond Hettinger <python@rcn.com>
parents:
66573
diff
changeset
|
972 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
|
973 @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
|
974 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
|
975 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
|
976 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
|
977 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
|
978 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
|
979 [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
|
980 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
|
981 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
|
982 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
|
983 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
|
984 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
|
985 |
|
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
|
986 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
|
987 # 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
|
988 # 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
|
989 # http://bugs.python.org/issue13177 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
990 for maxsize in (None, 128): |
|
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
|
991 @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
|
992 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
|
993 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
|
994 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
|
995 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
|
996 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
|
997 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
|
998 # 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
|
999 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
|
1000 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
|
1001 |
|
73019
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1002 def test_lru_with_types(self): |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1003 for maxsize in (None, 128): |
|
73019
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1004 @functools.lru_cache(maxsize=maxsize, typed=True) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1005 def square(x): |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1006 return x * x |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1007 self.assertEqual(square(3), 9) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1008 self.assertEqual(type(square(3)), type(9)) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1009 self.assertEqual(square(3.0), 9.0) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1010 self.assertEqual(type(square(3.0)), type(9.0)) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1011 self.assertEqual(square(x=3), 9) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1012 self.assertEqual(type(square(x=3)), type(9)) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1013 self.assertEqual(square(x=3.0), 9.0) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1014 self.assertEqual(type(square(x=3.0)), type(9.0)) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1015 self.assertEqual(square.cache_info().hits, 4) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1016 self.assertEqual(square.cache_info().misses, 4) |
|
cba503a2c468
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
Raymond Hettinger <python@rcn.com>
parents:
72944
diff
changeset
|
1017 |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1018 def test_lru_with_keyword_args(self): |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1019 @functools.lru_cache() |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1020 def fib(n): |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1021 if n < 2: |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1022 return n |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1023 return fib(n=n-1) + fib(n=n-2) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1024 self.assertEqual( |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1025 [fib(n=number) for number in range(16)], |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1026 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1027 ) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1028 self.assertEqual(fib.cache_info(), |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1029 functools._CacheInfo(hits=28, misses=16, maxsize=128, currsize=16)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1030 fib.cache_clear() |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1031 self.assertEqual(fib.cache_info(), |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1032 functools._CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1033 |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1034 def test_lru_with_keyword_args_maxsize_none(self): |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1035 @functools.lru_cache(maxsize=None) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1036 def fib(n): |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1037 if n < 2: |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1038 return n |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1039 return fib(n=n-1) + fib(n=n-2) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1040 self.assertEqual([fib(n=number) for number in range(16)], |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1041 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1042 self.assertEqual(fib.cache_info(), |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1043 functools._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1044 fib.cache_clear() |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1045 self.assertEqual(fib.cache_info(), |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1046 functools._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1047 |
|
82466
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1048 def test_need_for_rlock(self): |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1049 # This will deadlock on an LRU cache that uses a regular lock |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1050 |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1051 @functools.lru_cache(maxsize=10) |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1052 def test_func(x): |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1053 'Used to demonstrate a reentrant lru_cache call within a single thread' |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1054 return x |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1055 |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1056 class DoubleEq: |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1057 'Demonstrate a reentrant lru_cache call within a single thread' |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1058 def __init__(self, x): |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1059 self.x = x |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1060 def __hash__(self): |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1061 return self.x |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1062 def __eq__(self, other): |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1063 if self.x == 2: |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1064 test_func(DoubleEq(1)) |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1065 return self.x == other.x |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1066 |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1067 test_func(DoubleEq(1)) # Load the cache |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1068 test_func(DoubleEq(2)) # Load the cache |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1069 self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1070 DoubleEq(2)) # Verify the correct return value |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1071 |
|
f27eaeffc4c0
Add test for RLock in the lru_cache().
Raymond Hettinger <python@rcn.com>
parents:
81988
diff
changeset
|
1072 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1073 class TestSingleDispatch(unittest.TestCase): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1074 def test_simple_overloads(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1075 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1076 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1077 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1078 def g_int(i): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1079 return "integer" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1080 g.register(int, g_int) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1081 self.assertEqual(g("str"), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1082 self.assertEqual(g(1), "integer") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1083 self.assertEqual(g([1,2,3]), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1084 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1085 def test_mro(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1086 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1087 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1088 return "base" |
|
84053
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1089 class A: |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1090 pass |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1091 class C(A): |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1092 pass |
|
84053
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1093 class B(A): |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1094 pass |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1095 class D(C, B): |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1096 pass |
|
84053
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1097 def g_A(a): |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1098 return "A" |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1099 def g_B(b): |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1100 return "B" |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1101 g.register(A, g_A) |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1102 g.register(B, g_B) |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1103 self.assertEqual(g(A()), "A") |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1104 self.assertEqual(g(B()), "B") |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1105 self.assertEqual(g(C()), "A") |
|
a16bebe653b1
Fixed #18150: duplicate test inside TestSingleDispatch
Łukasz Langa <lukasz@langa.pl>
parents:
84039
diff
changeset
|
1106 self.assertEqual(g(D()), "B") |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1107 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1108 def test_register_decorator(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1109 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1110 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1111 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1112 @g.register(int) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1113 def g_int(i): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1114 return "int %s" % (i,) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1115 self.assertEqual(g(""), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1116 self.assertEqual(g(12), "int 12") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1117 self.assertIs(g.dispatch(int), g_int) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1118 self.assertIs(g.dispatch(object), g.dispatch(str)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1119 # Note: in the assert above this is not g. |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1120 # @singledispatch returns the wrapper. |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1121 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1122 def test_wrapping_attributes(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1123 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1124 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1125 "Simple test" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1126 return "Test" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1127 self.assertEqual(g.__name__, "g") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1128 self.assertEqual(g.__doc__, "Simple test") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1129 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1130 @unittest.skipUnless(decimal, 'requires _decimal') |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1131 @support.cpython_only |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1132 def test_c_classes(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1133 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1134 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1135 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1136 @g.register(decimal.DecimalException) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1137 def _(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1138 return obj.args |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1139 subn = decimal.Subnormal("Exponent < Emin") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1140 rnd = decimal.Rounded("Number got rounded") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1141 self.assertEqual(g(subn), ("Exponent < Emin",)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1142 self.assertEqual(g(rnd), ("Number got rounded",)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1143 @g.register(decimal.Subnormal) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1144 def _(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1145 return "Too small to care." |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1146 self.assertEqual(g(subn), "Too small to care.") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1147 self.assertEqual(g(rnd), ("Number got rounded",)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1148 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1149 def test_compose_mro(self): |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1150 # None of the examples in this test depend on haystack ordering. |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1151 c = collections |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1152 mro = functools._compose_mro |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1153 bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1154 for haystack in permutations(bases): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1155 m = mro(dict, haystack) |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1156 self.assertEqual(m, [dict, c.MutableMapping, c.Mapping, c.Sized, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1157 c.Iterable, c.Container, object]) |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1158 bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1159 for haystack in permutations(bases): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1160 m = mro(c.ChainMap, haystack) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1161 self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping, |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1162 c.Sized, c.Iterable, c.Container, object]) |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1163 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1164 # If there's a generic function with implementations registered for |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1165 # both Sized and Container, passing a defaultdict to it results in an |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1166 # ambiguous dispatch which will cause a RuntimeError (see |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1167 # test_mro_conflicts). |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1168 bases = [c.Container, c.Sized, str] |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1169 for haystack in permutations(bases): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1170 m = mro(c.defaultdict, [c.Sized, c.Container, str]) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1171 self.assertEqual(m, [c.defaultdict, dict, c.Sized, c.Container, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1172 object]) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1173 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1174 # MutableSequence below is registered directly on D. In other words, it |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1175 # preceeds MutableMapping which means single dispatch will always |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1176 # choose MutableSequence here. |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1177 class D(c.defaultdict): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1178 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1179 c.MutableSequence.register(D) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1180 bases = [c.MutableSequence, c.MutableMapping] |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1181 for haystack in permutations(bases): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1182 m = mro(D, bases) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1183 self.assertEqual(m, [D, c.MutableSequence, c.Sequence, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1184 c.defaultdict, dict, c.MutableMapping, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1185 c.Mapping, c.Sized, c.Iterable, c.Container, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1186 object]) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1187 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1188 # Container and Callable are registered on different base classes and |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1189 # a generic function supporting both should always pick the Callable |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1190 # implementation if a C instance is passed. |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1191 class C(c.defaultdict): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1192 def __call__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1193 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1194 bases = [c.Sized, c.Callable, c.Container, c.Mapping] |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1195 for haystack in permutations(bases): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1196 m = mro(C, haystack) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1197 self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1198 c.Sized, c.Iterable, c.Container, object]) |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1199 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1200 def test_register_abc(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1201 c = collections |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1202 d = {"a": "b"} |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1203 l = [1, 2, 3] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1204 s = {object(), None} |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1205 f = frozenset(s) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1206 t = (1, 2, 3) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1207 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1208 def g(obj): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1209 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1210 self.assertEqual(g(d), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1211 self.assertEqual(g(l), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1212 self.assertEqual(g(s), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1213 self.assertEqual(g(f), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1214 self.assertEqual(g(t), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1215 g.register(c.Sized, lambda obj: "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1216 self.assertEqual(g(d), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1217 self.assertEqual(g(l), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1218 self.assertEqual(g(s), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1219 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1220 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1221 g.register(c.MutableMapping, lambda obj: "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1222 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1223 self.assertEqual(g(l), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1224 self.assertEqual(g(s), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1225 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1226 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1227 g.register(c.ChainMap, lambda obj: "chainmap") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1228 self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1229 self.assertEqual(g(l), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1230 self.assertEqual(g(s), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1231 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1232 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1233 g.register(c.MutableSequence, lambda obj: "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1234 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1235 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1236 self.assertEqual(g(s), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1237 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1238 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1239 g.register(c.MutableSet, lambda obj: "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1240 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1241 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1242 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1243 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1244 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1245 g.register(c.Mapping, lambda obj: "mapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1246 self.assertEqual(g(d), "mutablemapping") # not specific enough |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1247 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1248 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1249 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1250 self.assertEqual(g(t), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1251 g.register(c.Sequence, lambda obj: "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1252 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1253 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1254 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1255 self.assertEqual(g(f), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1256 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1257 g.register(c.Set, lambda obj: "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1258 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1259 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1260 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1261 self.assertEqual(g(f), "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1262 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1263 g.register(dict, lambda obj: "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1264 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1265 self.assertEqual(g(l), "mutablesequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1266 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1267 self.assertEqual(g(f), "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1268 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1269 g.register(list, lambda obj: "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1270 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1271 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1272 self.assertEqual(g(s), "mutableset") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1273 self.assertEqual(g(f), "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1274 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1275 g.register(set, lambda obj: "concrete-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1276 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1277 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1278 self.assertEqual(g(s), "concrete-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1279 self.assertEqual(g(f), "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1280 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1281 g.register(frozenset, lambda obj: "frozen-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1282 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1283 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1284 self.assertEqual(g(s), "concrete-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1285 self.assertEqual(g(f), "frozen-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1286 self.assertEqual(g(t), "sequence") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1287 g.register(tuple, lambda obj: "tuple") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1288 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1289 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1290 self.assertEqual(g(s), "concrete-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1291 self.assertEqual(g(f), "frozen-set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1292 self.assertEqual(g(t), "tuple") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1293 |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1294 def test_c3_abc(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1295 c = collections |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1296 mro = functools._c3_mro |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1297 class A(object): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1298 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1299 class B(A): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1300 def __len__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1301 return 0 # implies Sized |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1302 @c.Container.register |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1303 class C(object): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1304 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1305 class D(object): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1306 pass # unrelated |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1307 class X(D, C, B): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1308 def __call__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1309 pass # implies Callable |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1310 expected = [X, c.Callable, D, C, c.Container, B, c.Sized, A, object] |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1311 for abcs in permutations([c.Sized, c.Callable, c.Container]): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1312 self.assertEqual(mro(X, abcs=abcs), expected) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1313 # unrelated ABCs don't appear in the resulting MRO |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1314 many_abcs = [c.Mapping, c.Sized, c.Callable, c.Container, c.Iterable] |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1315 self.assertEqual(mro(X, abcs=many_abcs), expected) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1316 |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1317 def test_mro_conflicts(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1318 c = collections |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1319 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1320 def g(arg): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1321 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1322 class O(c.Sized): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1323 def __len__(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1324 return 0 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1325 o = O() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1326 self.assertEqual(g(o), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1327 g.register(c.Iterable, lambda arg: "iterable") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1328 g.register(c.Container, lambda arg: "container") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1329 g.register(c.Sized, lambda arg: "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1330 g.register(c.Set, lambda arg: "set") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1331 self.assertEqual(g(o), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1332 c.Iterable.register(O) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1333 self.assertEqual(g(o), "sized") # because it's explicitly in __mro__ |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1334 c.Container.register(O) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1335 self.assertEqual(g(o), "sized") # see above: Sized is in __mro__ |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1336 c.Set.register(O) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1337 self.assertEqual(g(o), "set") # because c.Set is a subclass of |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1338 # c.Sized and c.Container |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1339 class P: |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1340 pass |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1341 p = P() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1342 self.assertEqual(g(p), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1343 c.Iterable.register(P) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1344 self.assertEqual(g(p), "iterable") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1345 c.Container.register(P) |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1346 with self.assertRaises(RuntimeError) as re_one: |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1347 g(p) |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1348 self.assertIn( |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1349 str(re_one.exception), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1350 (("Ambiguous dispatch: <class 'collections.abc.Container'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1351 "or <class 'collections.abc.Iterable'>"), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1352 ("Ambiguous dispatch: <class 'collections.abc.Iterable'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1353 "or <class 'collections.abc.Container'>")), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1354 ) |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1355 class Q(c.Sized): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1356 def __len__(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1357 return 0 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1358 q = Q() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1359 self.assertEqual(g(q), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1360 c.Iterable.register(Q) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1361 self.assertEqual(g(q), "sized") # because it's explicitly in __mro__ |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1362 c.Set.register(Q) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1363 self.assertEqual(g(q), "set") # because c.Set is a subclass of |
|
84411
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1364 # c.Sized and c.Iterable |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1365 @functools.singledispatch |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1366 def h(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1367 return "base" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1368 @h.register(c.Sized) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1369 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1370 return "sized" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1371 @h.register(c.Container) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1372 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1373 return "container" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1374 # Even though Sized and Container are explicit bases of MutableMapping, |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1375 # this ABC is implicitly registered on defaultdict which makes all of |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1376 # MutableMapping's bases implicit as well from defaultdict's |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1377 # perspective. |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1378 with self.assertRaises(RuntimeError) as re_two: |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1379 h(c.defaultdict(lambda: 0)) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1380 self.assertIn( |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1381 str(re_two.exception), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1382 (("Ambiguous dispatch: <class 'collections.abc.Container'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1383 "or <class 'collections.abc.Sized'>"), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1384 ("Ambiguous dispatch: <class 'collections.abc.Sized'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1385 "or <class 'collections.abc.Container'>")), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1386 ) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1387 class R(c.defaultdict): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1388 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1389 c.MutableSequence.register(R) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1390 @functools.singledispatch |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1391 def i(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1392 return "base" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1393 @i.register(c.MutableMapping) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1394 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1395 return "mapping" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1396 @i.register(c.MutableSequence) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1397 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1398 return "sequence" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1399 r = R() |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1400 self.assertEqual(i(r), "sequence") |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1401 class S: |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1402 pass |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1403 class T(S, c.Sized): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1404 def __len__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1405 return 0 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1406 t = T() |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1407 self.assertEqual(h(t), "sized") |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1408 c.Container.register(T) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1409 self.assertEqual(h(t), "sized") # because it's explicitly in the MRO |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1410 class U: |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1411 def __len__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1412 return 0 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1413 u = U() |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1414 self.assertEqual(h(u), "sized") # implicit Sized subclass inferred |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1415 # from the existence of __len__() |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1416 c.Container.register(U) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1417 # There is no preference for registered versus inferred ABCs. |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1418 with self.assertRaises(RuntimeError) as re_three: |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1419 h(u) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1420 self.assertIn( |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1421 str(re_three.exception), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1422 (("Ambiguous dispatch: <class 'collections.abc.Container'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1423 "or <class 'collections.abc.Sized'>"), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1424 ("Ambiguous dispatch: <class 'collections.abc.Sized'> " |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1425 "or <class 'collections.abc.Container'>")), |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1426 ) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1427 class V(c.Sized, S): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1428 def __len__(self): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1429 return 0 |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1430 @functools.singledispatch |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1431 def j(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1432 return "base" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1433 @j.register(S) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1434 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1435 return "s" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1436 @j.register(c.Container) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1437 def _(arg): |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1438 return "container" |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1439 v = V() |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1440 self.assertEqual(j(v), "s") |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1441 c.Container.register(V) |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1442 self.assertEqual(j(v), "container") # because it ends up right after |
|
a2672dc7c805
Issue #18244: Adopt C3-based linearization in functools.singledispatch for improved ABC support
Łukasz Langa <lukasz@langa.pl>
parents:
84053
diff
changeset
|
1443 # Sized in the MRO |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1444 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1445 def test_cache_invalidation(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1446 from collections import UserDict |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1447 class TracingDict(UserDict): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1448 def __init__(self, *args, **kwargs): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1449 super(TracingDict, self).__init__(*args, **kwargs) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1450 self.set_ops = [] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1451 self.get_ops = [] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1452 def __getitem__(self, key): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1453 result = self.data[key] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1454 self.get_ops.append(key) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1455 return result |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1456 def __setitem__(self, key, value): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1457 self.set_ops.append(key) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1458 self.data[key] = value |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1459 def clear(self): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1460 self.data.clear() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1461 _orig_wkd = functools.WeakKeyDictionary |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1462 td = TracingDict() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1463 functools.WeakKeyDictionary = lambda: td |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1464 c = collections |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1465 @functools.singledispatch |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1466 def g(arg): |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1467 return "base" |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1468 d = {} |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1469 l = [] |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1470 self.assertEqual(len(td), 0) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1471 self.assertEqual(g(d), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1472 self.assertEqual(len(td), 1) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1473 self.assertEqual(td.get_ops, []) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1474 self.assertEqual(td.set_ops, [dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1475 self.assertEqual(td.data[dict], g.registry[object]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1476 self.assertEqual(g(l), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1477 self.assertEqual(len(td), 2) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1478 self.assertEqual(td.get_ops, []) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1479 self.assertEqual(td.set_ops, [dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1480 self.assertEqual(td.data[dict], g.registry[object]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1481 self.assertEqual(td.data[list], g.registry[object]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1482 self.assertEqual(td.data[dict], td.data[list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1483 self.assertEqual(g(l), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1484 self.assertEqual(g(d), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1485 self.assertEqual(td.get_ops, [list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1486 self.assertEqual(td.set_ops, [dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1487 g.register(list, lambda arg: "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1488 self.assertEqual(td.get_ops, [list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1489 self.assertEqual(len(td), 0) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1490 self.assertEqual(g(d), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1491 self.assertEqual(len(td), 1) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1492 self.assertEqual(td.get_ops, [list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1493 self.assertEqual(td.set_ops, [dict, list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1494 self.assertEqual(td.data[dict], |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1495 functools._find_impl(dict, g.registry)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1496 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1497 self.assertEqual(len(td), 2) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1498 self.assertEqual(td.get_ops, [list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1499 self.assertEqual(td.set_ops, [dict, list, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1500 self.assertEqual(td.data[list], |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1501 functools._find_impl(list, g.registry)) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1502 class X: |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1503 pass |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1504 c.MutableMapping.register(X) # Will not invalidate the cache, |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1505 # not using ABCs yet. |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1506 self.assertEqual(g(d), "base") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1507 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1508 self.assertEqual(td.get_ops, [list, dict, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1509 self.assertEqual(td.set_ops, [dict, list, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1510 g.register(c.Sized, lambda arg: "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1511 self.assertEqual(len(td), 0) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1512 self.assertEqual(g(d), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1513 self.assertEqual(len(td), 1) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1514 self.assertEqual(td.get_ops, [list, dict, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1515 self.assertEqual(td.set_ops, [dict, list, dict, list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1516 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1517 self.assertEqual(len(td), 2) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1518 self.assertEqual(td.get_ops, [list, dict, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1519 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1520 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1521 self.assertEqual(g(d), "sized") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1522 self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1523 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1524 g.dispatch(list) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1525 g.dispatch(dict) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1526 self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict, |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1527 list, dict]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1528 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1529 c.MutableSet.register(X) # Will invalidate the cache. |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1530 self.assertEqual(len(td), 2) # Stale cache. |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1531 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1532 self.assertEqual(len(td), 1) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1533 g.register(c.MutableMapping, lambda arg: "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1534 self.assertEqual(len(td), 0) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1535 self.assertEqual(g(d), "mutablemapping") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1536 self.assertEqual(len(td), 1) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1537 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1538 self.assertEqual(len(td), 2) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1539 g.register(dict, lambda arg: "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1540 self.assertEqual(g(d), "dict") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1541 self.assertEqual(g(l), "list") |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1542 g._clear_cache() |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1543 self.assertEqual(len(td), 0) |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1544 functools.WeakKeyDictionary = _orig_wkd |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1545 |
|
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1546 |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1547 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
|
1548 test_classes = ( |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1549 TestPartialC, |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1550 TestPartialPy, |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1551 TestPartialCSubclass, |
|
86867
46d3c5539981
Issue #4331: Added functools.partialmethod
Nick Coghlan <ncoghlan@gmail.com>
parents:
85913
diff
changeset
|
1552 TestPartialMethod, |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38669
diff
changeset
|
1553 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
|
1554 TestTotalOrdering, |
|
80421
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1555 TestCmpToKeyC, |
|
fcfaca024160
Issue #12428: Add a pure Python implementation of functools.partial().
Antoine Pitrou <solipsis@pitrou.net>
parents:
73943
diff
changeset
|
1556 TestCmpToKeyPy, |
|
39691
c4d8c054616c
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
Guido van Rossum <guido@python.org>
parents:
38673
diff
changeset
|
1557 TestWraps, |
|
63370
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
1558 TestReduce, |
|
c55876a6fa3c
Re-commit r83327 now that the release is done.
Georg Brandl <georg@python.org>
parents:
63357
diff
changeset
|
1559 TestLRU, |
|
84039
dfcb64f51f7b
Add reference implementation for PEP 443
Łukasz Langa <lukasz@langa.pl>
parents:
82467
diff
changeset
|
1560 TestSingleDispatch, |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1561 ) |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
42142
diff
changeset
|
1562 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
|
1563 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1564 # 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
|
1565 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
|
1566 import gc |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1567 counts = [None] * 5 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
1568 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
|
1569 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
|
1570 gc.collect() |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1571 counts[i] = sys.gettotalrefcount() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
1572 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
|
1573 |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
1574 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
|
1575 test_main(verbose=True) |
