distutils and decorators
Benji York
benji at benjiyork.com
Wed Sep 21 20:27:01 EDT 2005
Robert Brewer wrote:
> Actually, in this case we most definitely want to test 2.4's "@" syntax.
> The decorator in question is an aliaser, and therefore is one of the few
> decorators which must be implemented differently for the 2.3-style
> decoration and the 2.4-style. See the "expose" function at:
> http://www.cherrypy.org/file/trunk/cherrypy/__init__.py?rev=654
I extracted "expose" from the above URL and wrote this test script (mind
the wrapping):
import sys, types
def expose(func=None, alias=None):
"""Expose the function, optionally providing an alias or set of
aliases."""
def expose_(func):
func.exposed = True
if alias is not None:
if isinstance(alias, basestring):
parentDict[alias] = func
else:
for a in alias:
parentDict[a] = func
return func
parentDict = sys._getframe(1).f_locals
if isinstance(func, (types.FunctionType, types.MethodType)):
# expose is being called directly, before the method has been bound
print 'direct'
return expose_(func)
else:
# expose is being called as a decorator
print 'decorator'
if alias is None:
alias = func
return expose_
print 'before decorator (no args)'
@expose
def foo():
pass
print 'before decorator (with args)'
@expose("1")
def foo():
pass
print 'before direct 1'
def bar():
pass
baz = expose(bar)
print 'before direct 2'
bux = expose("1")(bar)
Here's it's output (2.4.1 on Linux):
% python /tmp/1.py
before decorator (no args)
direct
before decorator (with args)
decorator
before direct 1
direct
before direct 2
decorator
What am I missing?
--
Benji York
More information about the Python-list
mailing list