Hi! Today I'm writing here to relate a little problem I had today, its fix and a lesson learned! [Thanks 'fumanchu' at #cherrypy for helping me debug some problems I was having with BRisa's webserver (the problem was not with CherryPy but debugging inside it made me find the problem on the webserver).]
The problem was that CherryPy wasn't finding (404) my resource, but it was there and it was exposed properly. Something else was keeping cherrypy from calling my handler. After some prints inside CherryPy, I found the bottom line of the problem: at _cpdispatch.Dispatcher.__call__. It had an evaluation of the handler function to be called, something like this:
if func:
# Set the handler
# ...
else:
# Raise NotFound exception (which leads to a 404 response)
So, the only possible explanation left was that my class was evaluating to False. I wasn't aware that python had this __nonzero__ method and this was a very particular case where I needed it. Just to sum up the my situation, let one class be named A, and let B inherit from A and from "dict" (!). Then, if "B's dict" is empty, bool(B) will evaluate to False (doc). So, when CherryPy did that evaluation on my instance of B, it was evaluating to False and returning 404. One "fix" (not really a bug) to this is to overwrite __nonzero__ and make it return True (if this is the behavior you really want).
Lesson learned: be careful with inheritance!
0 comentários:
Post a Comment