1 __all__
= ('decorated', 'deprecator', 'memoize', 'interruptable')
6 def decorator(caller
, func
=None):
10 decorator(caller) converts a caller function into a decorator;
11 decorator(caller, func) decorates a function using a caller.
16 def _decorator(f
, *args
, **opts
):
17 def _caller(*args
, **opts
):
18 return caller(f
, *args
, **opts
)
22 # return a decorated function
23 def _decorated(*args
, **opts
):
24 return caller(func
, *args
, **opts
)
29 def deprecated(func
, *args
, **kw
):
30 "A decorator for deprecated functions"
32 warnings
.warn('Calling deprecated function %r' % func
.__name
__,
33 DeprecationWarning, stacklevel
=3)
34 return func(*args
, **kw
)
39 A decorator for memoizing function calls
41 http://en.wikipedia.org/wiki/Memoization
45 return decorator(_memoize
, func
)
48 def _memoize(func
, *args
, **opts
):
49 """Implements memoized cache lookups"""
50 if opts
: # frozenset is used to ensure hashability
51 key
= args
, frozenset(opts
.items())
54 cache
= func
.cache
# attribute added by memoize
58 result
= cache
[key
] = func(*args
, **opts
)
63 def interruptable(func
, *args
, **opts
):
64 """Handle interruptable system calls
66 OSX and others are known to interrupt system calls
68 http://en.wikipedia.org/wiki/PCLSRing
69 http://en.wikipedia.org/wiki/Unix_philosophy#Worse_is_better
71 The @interruptable decorator handles this situation
76 result
= func(*args
, **opts
)
78 if e
.errno
== errno
.EINTR
:
82 if e
.errno
== errno
.EINTR
: