Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 1 | """functools.py - Tools for working with functions and callable objects |
Nick Coghlan | 0849014 | 2006-05-29 20:27:44 +0000 | [diff] [blame] | 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> |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 7 | # Copyright (C) 2006 Python Software Foundation. |
| 8 | # See C source code for _functools credits/copyright |
Nick Coghlan | 0849014 | 2006-05-29 20:27:44 +0000 | [diff] [blame] | 9 | |
| 10 | from _functools import partial |
Guido van Rossum | d51b579 | 2007-08-27 20:51:00 +0000 | [diff] [blame] | 11 | from __builtin__ import reduce |
Nick Coghlan | 0849014 | 2006-05-29 20:27:44 +0000 | [diff] [blame] | 12 | |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 13 | # update_wrapper() and wraps() are tools to help write |
| 14 | # wrapper functions that can handle naive introspection |
| 15 | |
| 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 |
| 23 | |
| 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) |
Andrew M. Kuchling | efb5707 | 2006-10-26 19:16:46 +0000 | [diff] [blame] | 29 | updated is a tuple naming the attributes of the wrapper that |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 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: |
Andrew M. Kuchling | 41eb716 | 2006-10-27 16:39:10 +0000 | [diff] [blame] | 36 | getattr(wrapper, attr).update(getattr(wrapped, attr, {})) |
Nick Coghlan | 676725d | 2006-06-08 13:54:49 +0000 | [diff] [blame] | 37 | # Return the wrapper so this can be used as a decorator via partial() |
| 38 | return wrapper |
| 39 | |
| 40 | def wraps(wrapped, |
| 41 | assigned = WRAPPER_ASSIGNMENTS, |
| 42 | updated = WRAPPER_UPDATES): |
| 43 | """Decorator factory to apply update_wrapper() to a wrapper function |
| 44 | |
| 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) |