Mercurial > cpython-lru_cache-weakref
annotate Lib/functools.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 136298326897 |
| children |
| rev | line source |
|---|---|
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
1 """functools.py - Tools for working with functions and callable objects |
|
38486
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2 """ |
|
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
3 # Python module wrapper for _functools C module |
|
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
4 # to allow utilities written in Python to be added |
|
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
5 # to the functools module. |
|
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
6 # Written by Nick Coghlan <ncoghlan at gmail.com> |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
7 # Copyright (C) 2006 Python Software Foundation. |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
8 # See C source code for _functools credits/copyright |
|
38486
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
9 |
|
48204
b3abff17e2dd
Copy reduce() to _functools so to have functools.reduce() not raise a warning
Brett Cannon <bcannon@gmail.com>
parents:
42913
diff
changeset
|
10 from _functools import partial, reduce |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
11 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
12 # update_wrapper() and wraps() are tools to help write |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
13 # wrapper functions that can handle naive introspection |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
14 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
15 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__') |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
16 WRAPPER_UPDATES = ('__dict__',) |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
17 def update_wrapper(wrapper, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
18 wrapped, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
19 assigned = WRAPPER_ASSIGNMENTS, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
20 updated = WRAPPER_UPDATES): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
21 """Update a wrapper function to look like the wrapped function |
|
38486
8ac603e570a0
When adding a module like functools, it helps to let SVN know about the file.
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
22 |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
23 wrapper is the function to be updated |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
24 wrapped is the original function |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
25 assigned is a tuple naming the attributes assigned directly |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
26 from the wrapped function to the wrapper function (defaults to |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
27 functools.WRAPPER_ASSIGNMENTS) |
| 40175 | 28 updated is a tuple naming the attributes of the wrapper that |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
29 are updated with the corresponding attribute from the wrapped |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
30 function (defaults to functools.WRAPPER_UPDATES) |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
31 """ |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
32 for attr in assigned: |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
33 setattr(wrapper, attr, getattr(wrapped, attr)) |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
34 for attr in updated: |
|
40201
f45b2f0c542f
[Bug #1576241] Let functools.wraps work with built-in functions
Andrew M. Kuchling <amk@amk.ca>
parents:
38668
diff
changeset
|
35 getattr(wrapper, attr).update(getattr(wrapped, attr, {})) |
|
38668
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
36 # Return the wrapper so this can be used as a decorator via partial() |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
37 return wrapper |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
38 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
39 def wraps(wrapped, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
40 assigned = WRAPPER_ASSIGNMENTS, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
41 updated = WRAPPER_UPDATES): |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
42 """Decorator factory to apply update_wrapper() to a wrapper function |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
43 |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
44 Returns a decorator that invokes update_wrapper() with the decorated |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
45 function as the wrapper argument and the arguments to wraps() as the |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
46 remaining arguments. Default arguments are as for update_wrapper(). |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
47 This is a convenience function to simplify applying partial() to |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
48 update_wrapper(). |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
49 """ |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
50 return partial(update_wrapper, wrapped=wrapped, |
|
dee73fa8110f
Add functools.update_wrapper() and functools.wraps() as described in PEP 356
Nick Coghlan <ncoghlan@gmail.com>
parents:
38496
diff
changeset
|
51 assigned=assigned, updated=updated) |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
52 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
53 def total_ordering(cls): |
|
60595
21267bf0720e
#5341: fix typo and adapt docstring syntax.
Georg Brandl <georg@python.org>
parents:
60478
diff
changeset
|
54 """Class decorator that fills in missing ordering methods""" |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
55 convert = { |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
56 '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
57 ('__le__', lambda self, other: self < other or self == other), |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
58 ('__ge__', lambda self, other: not self < other)], |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
59 '__le__': [('__ge__', lambda self, other: not self <= other or self == other), |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
60 ('__lt__', lambda self, other: self <= other and not self == other), |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
61 ('__gt__', lambda self, other: not self <= other)], |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
62 '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
63 ('__ge__', lambda self, other: self > other or self == other), |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
64 ('__le__', lambda self, other: not self > other)], |
|
68672
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
65 '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), |
|
94c158199277
Fix the total_ordering decorator to handle cross-type comparisons
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
66 ('__gt__', lambda self, other: self >= other and not self == other), |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
67 ('__lt__', lambda self, other: not self >= other)] |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
68 } |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
69 roots = set(dir(cls)) & set(convert) |
|
60478
2baf1cee71f2
Issue 8361: Remove assert from functools.total_ordering
Raymond Hettinger <python@rcn.com>
parents:
60368
diff
changeset
|
70 if not roots: |
|
2baf1cee71f2
Issue 8361: Remove assert from functools.total_ordering
Raymond Hettinger <python@rcn.com>
parents:
60368
diff
changeset
|
71 raise ValueError('must define at least one ordering operation: < > <= >=') |
|
2baf1cee71f2
Issue 8361: Remove assert from functools.total_ordering
Raymond Hettinger <python@rcn.com>
parents:
60368
diff
changeset
|
72 root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
73 for opname, opfunc in convert[root]: |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
74 if opname not in roots: |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
75 opfunc.__name__ = opname |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
76 opfunc.__doc__ = getattr(int, opname).__doc__ |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
77 setattr(cls, opname, opfunc) |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
78 return cls |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
79 |
|
60315
5dda3f32a2ce
Add tests for cmp_to_key.
Raymond Hettinger <python@rcn.com>
parents:
60310
diff
changeset
|
80 def cmp_to_key(mycmp): |
|
60595
21267bf0720e
#5341: fix typo and adapt docstring syntax.
Georg Brandl <georg@python.org>
parents:
60478
diff
changeset
|
81 """Convert a cmp= function into a key= function""" |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
82 class K(object): |
|
68835
136298326897
Issue #11628: cmp_to_key should use__slots__.
Raymond Hettinger <python@rcn.com>
parents:
68672
diff
changeset
|
83 __slots__ = ['obj'] |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
84 def __init__(self, obj, *args): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
85 self.obj = obj |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
86 def __lt__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
87 return mycmp(self.obj, other.obj) < 0 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
88 def __gt__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
89 return mycmp(self.obj, other.obj) > 0 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
90 def __eq__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
91 return mycmp(self.obj, other.obj) == 0 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
92 def __le__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
93 return mycmp(self.obj, other.obj) <= 0 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
94 def __ge__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
95 return mycmp(self.obj, other.obj) >= 0 |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
96 def __ne__(self, other): |
|
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
97 return mycmp(self.obj, other.obj) != 0 |
|
60368
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
98 def __hash__(self): |
|
3c9f0887ee3f
Classes that override __eq__ also need to define __hash__.
Raymond Hettinger <python@rcn.com>
parents:
60315
diff
changeset
|
99 raise TypeError('hash not implemented') |
|
60310
1e6f268016e4
Add functools.CmpToKey()
Raymond Hettinger <python@rcn.com>
parents:
48204
diff
changeset
|
100 return K |
