source: branches/asap-3.x/python/compatibility.py@ 2908

Last change on this file since 2908 was 1690, checked in by Malte Marquarding, 15 years ago

Added compatibility.py for functools.wraps decorator in python <2.5

File size: 5.2 KB
Line 
1# License for code in this file that was taken from Python 2.5.
2
3# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
4# --------------------------------------------
5#
6# 1. This LICENSE AGREEMENT is between the Python Software Foundation
7# ("PSF"), and the Individual or Organization ("Licensee") accessing and
8# otherwise using this software ("Python") in source or binary form and
9# its associated documentation.
10#
11# 2. Subject to the terms and conditions of this License Agreement, PSF
12# hereby grants Licensee a nonexclusive, royalty-free, world-wide
13# license to reproduce, analyze, test, perform and/or display publicly,
14# prepare derivative works, distribute, and otherwise use Python
15# alone or in any derivative version, provided, however, that PSF's
16# License Agreement and PSF's notice of copyright, i.e., "Copyright (c)
17# 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation;
18# All Rights Reserved" are retained in Python alone or in any derivative
19# version prepared by Licensee.
20#
21# 3. In the event Licensee prepares a derivative work that is based on
22# or incorporates Python or any part thereof, and wants to make
23# the derivative work available to others as provided herein, then
24# Licensee hereby agrees to include in any such work a brief summary of
25# the changes made to Python.
26#
27# 4. PSF is making Python available to Licensee on an "AS IS"
28# basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
29# IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
30# DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
31# FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
32# INFRINGE ANY THIRD PARTY RIGHTS.
33#
34# 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
35# FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
36# A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,
37# OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
38#
39# 6. This License Agreement will automatically terminate upon a material
40# breach of its terms and conditions.
41#
42# 7. Nothing in this License Agreement shall be deemed to create any
43# relationship of agency, partnership, or joint venture between PSF and
44# Licensee. This License Agreement does not grant permission to use PSF
45# trademarks or trade name in a trademark sense to endorse or promote
46# products or services of Licensee, or any third party.
47#
48# 8. By copying, installing or otherwise using Python, Licensee
49# agrees to be bound by the terms and conditions of this License
50# Agreement.
51
52
53def curry(_curried_func, *args, **kwargs):
54 def _curried(*moreargs, **morekwargs):
55 return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
56 return _curried
57
58### Begin from Python 2.5 functools.py ########################################
59
60# Summary of changes made to the Python 2.5 code below:
61# * swapped ``partial`` for ``curry`` to maintain backwards-compatibility
62# in Django.
63# * Wrapped the ``setattr`` call in ``update_wrapper`` with a try-except
64# block to make it compatible with Python 2.3, which doesn't allow
65# assigning to ``__name__``.
66
67# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation.
68# All Rights Reserved.
69
70###############################################################################
71
72# update_wrapper() and wraps() are tools to help write
73# wrapper functions that can handle naive introspection
74
75WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
76WRAPPER_UPDATES = ('__dict__',)
77def update_wrapper(wrapper,
78 wrapped,
79 assigned = WRAPPER_ASSIGNMENTS,
80 updated = WRAPPER_UPDATES):
81 """Update a wrapper function to look like the wrapped function
82
83 wrapper is the function to be updated
84 wrapped is the original function
85 assigned is a tuple naming the attributes assigned directly
86 from the wrapped function to the wrapper function (defaults to
87 functools.WRAPPER_ASSIGNMENTS)
88 updated is a tuple naming the attributes off the wrapper that
89 are updated with the corresponding attribute from the wrapped
90 function (defaults to functools.WRAPPER_UPDATES)
91 """
92 for attr in assigned:
93 try:
94 setattr(wrapper, attr, getattr(wrapped, attr))
95 except TypeError: # Python 2.3 doesn't allow assigning to __name__.
96 pass
97 for attr in updated:
98 getattr(wrapper, attr).update(getattr(wrapped, attr))
99 # Return the wrapper so this can be used as a decorator via curry()
100 return wrapper
101
102def wraps(wrapped,
103 assigned = WRAPPER_ASSIGNMENTS,
104 updated = WRAPPER_UPDATES):
105 """Decorator factory to apply update_wrapper() to a wrapper function
106
107 Returns a decorator that invokes update_wrapper() with the decorated
108 function as the wrapper argument and the arguments to wraps() as the
109 remaining arguments. Default arguments are as for update_wrapper().
110 This is a convenience function to simplify applying curry() to
111 update_wrapper().
112 """
113 return curry(update_wrapper, wrapped=wrapped,
114 assigned=assigned, updated=updated)
115
116### End from Python 2.5 functools.py ##########################################
Note: See TracBrowser for help on using the repository browser.