Convert from long to Py_ssize_t.
[python.git] / Lib / functools.py
blob30e1d24fd2849269789cf76c6c9855949c4a99ad
1 """functools.py - Tools for working with functions and callable objects
2 """
3 # Python module wrapper for _functools C module
4 # to allow utilities written in Python to be added
5 # to the functools module.
6 # Written by Nick Coghlan <ncoghlan at gmail.com>
7 # Copyright (C) 2006 Python Software Foundation.
8 # See C source code for _functools credits/copyright
10 from _functools import partial
11 from __builtin__ import reduce
13 # update_wrapper() and wraps() are tools to help write
14 # wrapper functions that can handle naive introspection
16 WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
17 WRAPPER_UPDATES = ('__dict__',)
18 def update_wrapper(wrapper,
19 wrapped,
20 assigned = WRAPPER_ASSIGNMENTS,
21 updated = WRAPPER_UPDATES):
22 """Update a wrapper function to look like the wrapped function
24 wrapper is the function to be updated
25 wrapped is the original function
26 assigned is a tuple naming the attributes assigned directly
27 from the wrapped function to the wrapper function (defaults to
28 functools.WRAPPER_ASSIGNMENTS)
29 updated is a tuple naming the attributes of the wrapper that
30 are updated with the corresponding attribute from the wrapped
31 function (defaults to functools.WRAPPER_UPDATES)
32 """
33 for attr in assigned:
34 setattr(wrapper, attr, getattr(wrapped, attr))
35 for attr in updated:
36 getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
37 # Return the wrapper so this can be used as a decorator via partial()
38 return wrapper
40 def wraps(wrapped,
41 assigned = WRAPPER_ASSIGNMENTS,
42 updated = WRAPPER_UPDATES):
43 """Decorator factory to apply update_wrapper() to a wrapper function
45 Returns a decorator that invokes update_wrapper() with the decorated
46 function as the wrapper argument and the arguments to wraps() as the
47 remaining arguments. Default arguments are as for update_wrapper().
48 This is a convenience function to simplify applying partial() to
49 update_wrapper().
50 """
51 return partial(update_wrapper, wrapped=wrapped,
52 assigned=assigned, updated=updated)