-
-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError swallowed in decorated handlers #1530
Comments
This is the full stacktrace returned in the response body.
|
One option, that would only work on Python3, though, is to check if the "callable" has a
See https://docs.python.org/3/library/functools.html#functools.update_wrapper For Python2, at leasts the docs should suggest to set the |
An option that would work for both Python 2/3 would be to use from functools import wraps
from inspect import getargspec
import wrapt
def dec(f):
@wraps(f)
def wrapper(handler, *args, **kwargs):
return f(handler, *args, **kwargs)
return wrapper
@wrapt.decorator
def wrapt_dec(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
class HelloWorld(object):
@dec
def index(self, *args, **kwargs):
raise TypeError('OOPS')
return "Hello world!"
@wrapt_dec
def index2(self, *args, **kwargs):
raise TypeError('OOPS')
return "Hello world!"
index.exposed = True
handler = HelloWorld() >>> getargspec(handler.index)
ArgSpec(args=['handler'], varargs='args', keywords='kwargs', defaults=None)
>>> getargspec(handler.index2)
ArgSpec(args=['self'], varargs='args', keywords='kwargs', defaults=None)
>>> |
@bdeeney Sure, applications aware of this issue could write their decorators using |
@lbolla Agreed. A more accurate check might focus on, e.g.: inspect.ismethod(callable) ref: https://docs.python.org/3/library/inspect.html#inspect.ismethod |
@bdeeney Thanks for the suggestion, it's a great idea. I just tested it and it does indeed solves the issue elegantly. I will submit a PR soon. |
PR available here: #1531 |
If a
TypeError
is raised within a handler, which has been decorated by a "wrapper" function whose first argument is not calledself
, theTypeError
is swallowed and a 404 HTTP Error is returned instead.Response is a 404:
Without the decorator
@dec
, one correctly gets a 500.The problem is here: https://github.com/cherrypy/cherrypy/blob/master/cherrypy/_cpdispatch.py#L105
When inspecting the "callable", CherryPy checks for the name of the first argument and strips it out if it's called
self
. It then goes on inspecting the other arguments and raises a 404 if some arguments are missing or unknown.But if the "callable"'s first argument is not called "self", as in the decorated handler above (where it's called "handler"), then CherryPy mistakenly returns a "404 argument 'handler' is unknown".
The text was updated successfully, but these errors were encountered: