I hate variable arguments. ;)
Any better?
Trully sorry for the inconviniance everyone.
Cheers,
Simon
Ksenia Marasanova wrote:
Fastdata troubles here. I am getting this error in "default" method of
my controller based on DataController:
Traceback (most recent call last):
File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py",
line 99, in _run
self.main()
File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py",
line 247, in main
body = page_handler(*virtual_path, **self.params)
File "<string>", line 3, in default
TypeError: expose() takes exactly 2 non-keyword arguments (3 given)
Are there any tricks for debugging decorators?
2006/2/2, Kevin Dangoor <[EMAIL PROTECTED]>:
I just re-committed the controllers code with the decorator fix that
Simon posted last night. If you run into problems, please report them
here... but, rather than leaving you stuck, you can call this
function:
turbogears.controllers.use_old_controllers()
This will put you back on the old code. Note that you'd need to do
this before you import your controller module.
Kevin
--
Kevin Dangoor
Author of the Zesty News RSS newsreader
email: [EMAIL PROTECTED]
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com
--
Ksenia
Index: decorator.py
===================================================================
--- decorator.py (revision 623)
+++ decorator.py (working copy)
@@ -12,12 +12,19 @@
# Argument sink
if kwargs is None:
kwargs = "tg_kw"
- counter = itertools.count()
+ defval_count = itertools.count()
+
+ def formatarg(arg):
+ if arg == argnames[0]:
+ return arg
+ else:
+ return "%s=%s" % (arg, arg)
+
full_sign = formatargspec(argnames, varargs, kwargs, defaults,
formatvalue=lambda value: "=defaults[%i]" % (
- counter.next()))[1:-1]
+ defval_count.next()))[1:-1]
short_sign = formatargspec(argnames, varargs, kwargs, defaults,
- formatarg=lambda arg: "%s=%s" % (arg, arg),
+ formatarg=formatarg,
formatvalue=lambda value: "")[1:-1]
exec_dict = dict(func=func, caller=caller, defaults=defaults or ())
func_str = """
Index: controllers.py
===================================================================
--- controllers.py (revision 623)
+++ controllers.py (working copy)
@@ -114,7 +114,7 @@
def validate(form=None, validators=None):
def entangle(func):
recursion_guard = dict(func=func)
- def validate(func, self, **kw):
+ def validate(func, self, *args, **kw):
if _infinite_recursion("validate", recursion_guard, 4):
return func(self, **kw)
if form:
@@ -150,9 +150,9 @@
output = self.validation_error(func.func_name, kw, errors)
else:
kw["tg_errors"] = errors
- output = dispatch_error(func, self, **kw)
+ output = dispatch_error(func, self, *args, **kw)
else:
- output = func(self, **kw)
+ output = func(self, *args, **kw)
return output
return decorator(validate)(func)
return entangle
@@ -187,7 +187,7 @@
html = template
def entangle(func):
- def expose(func, self, **kw):
+ def expose(func, self, *args, **kw):
tg_format = kw.pop("tg_format", format)
cherrypy.response.headers['Content-Type']= content_type
# Allow specification of the format based on Accept header
@@ -208,11 +208,11 @@
not json_allowed:
raise BadFormatError("JSON output is not allowed for that
method")
if hasattr(cherrypy.request, "in_transaction"):
- output = _execute_func(self, func, tg_format, html, fragment,
**kw)
+ output = _execute_func(self, func, tg_format, html, fragment,
*args, **kw)
else:
cherrypy.request.in_transaction = True
output = database.run_with_transaction(_execute_func, self,
- func, tg_format, html, fragment, **kw)
+ func, tg_format, html, fragment, *args, **kw)
return output
if inputform or validators:
@@ -222,17 +222,19 @@
return expose
return entangle
-def _remove_excess_args(func, kw):
+def _remove_excess_args(func, args=[], kw={}):
""" Remove all excess arguments. """
argnames, varargs, kwargs = inspect.getargspec(func)[:-1]
if kwargs in (None, "tg_kw"):
kw = dict(ifilter(lambda (key, val): key in argnames, kw.iteritems()))
- return kw
+ if varargs is None:
+ args = []
+ return (args, kw)
-def _execute_func(self, func, tg_format, html, fragment, **kw):
- kw = _remove_excess_args(func, kw)
+def _execute_func(self, func, tg_format, html, fragment, *args, **kw):
+ args, kw = _remove_excess_args(func, args, kw)
try:
- output = func(self, **kw)
+ output = func(self, *args, **kw)
except Exception, error:
if isinstance(error, cherrypy.HTTPRedirect) or \
_infinite_recursion("dispatch_error",
@@ -240,7 +242,7 @@
raise error
else:
kw["tg_errors"] = error
- output = dispatch_error(func, self, **kw)
+ output = dispatch_error(func, self, *args, **kw)
if isinstance(output, dict) and output.has_key("tg_template"):
html = output.pop("tg_template")
if html and html.startswith("."):
@@ -278,7 +280,7 @@
return message
-def dispatch_error(error_source, self, tg_errors, **kw):
+def dispatch_error(error_source, self, tg_errors, *args, **kw):
""" Dispatch error.
Error handler is a function registered via error_handler decorator or
@@ -286,13 +288,13 @@
"""
dispatch_error = generic()(dispatch_error)
-def _default_error_handler(error_source, self, tg_errors, **kw):
+def _default_error_handler(error_source, self, tg_errors, *args, **kw):
""" Default (recursive) error handler. """
if isinstance(tg_errors, dict):
kw.update(izip(ifilter(kw.has_key, tg_errors.iterkeys()),
repeat(None)))
kw["tg_errors"] = tg_errors
# Call fully decorated method.
- return getattr(self, error_source.__name__ )(**kw)
+ return getattr(self, error_source.__name__ )(*args, **kw)
_default_error_handler = dispatch_error.when(strategy.default)(
_default_error_handler)
@@ -302,10 +304,10 @@
The actual handler will receive only arguments explicitly
declared.
"""
- def signature(error_source, self, tg_errors, **kw):
+ def signature(error_source, self, tg_errors, *args, **kw):
kw["tg_errors"] = tg_errors
- kw = _remove_excess_args(func, kw)
- return func(self, **kw)
+ args, kw = _remove_excess_args(func, args, kw)
+ return func(self, *args, **kw)
return signature
def error_handler(handler=None, rules=None):
Index: conditions.py
===================================================================
--- conditions.py (revision 623)
+++ conditions.py (working copy)
@@ -224,17 +224,17 @@
groups specified and has the permissions required.
'''
def entangle(fn):
- def require(func, self, **kwargs):
+ def require(func, self, *args, **kwargs):
try:
errors= []
if predicate is None or \
predicate.eval_with_object(current, errors):
- return fn(self, **kwargs)
+ return fn(self, *args, **kwargs)
except IdentityException, e:
errors= [str(e)]
-
+
raise IdentityFailure(errors)
-
+
return decorator(require)(fn)
return entangle