1. Iyed B.
  2. CherryPy


Robert Brewer  committed 93d84be

Test and fix for #661 (Cannot create tool out of an object callable). Also a better docstring for HandlerTool.

  • Participants
  • Parent commits 7faab78
  • Branches default

Comments (0)

Files changed (2)

File cherrypy/_cptools.py

View file
  • Ignore whitespace
                 setattr(self, arg, None)
         except (ImportError, AttributeError):
+        except TypeError:
+            if hasattr(self.callable, "__call__"):
+                for arg in inspect.getargspec(self.callable.__call__)[0]:
+                    setattr(self, arg, None)
         # IronPython 1.0 raises NotImplementedError because
         # inspect.getargspec tries to access Python bytecode
         # in co_code attribute.
 class HandlerTool(Tool):
     """Tool which is called 'before main', that may skip normal handlers.
-    The callable provided should return True if processing should skip
-    the normal page handler, and False if it should not.
+    If the tool successfully handles the request (by setting response.body),
+    if should return True. This will cause CherryPy to skip any 'normal' page
+    handler. If the tool did not handle the request, it should return False
+    to tell CherryPy to continue on and call the normal page handler. If the
+    tool is declared AS a page handler (see the 'handler' method), returning
+    False will raise NotFound.
     def __init__(self, callable, name=None):

File cherrypy/test/test_tools.py

View file
  • Ignore whitespace
         clen = int(cherrypy.request.headers['Content-Length'])
         cherrypy.request.body = cherrypy.request.rfile.read(clen)
+    # Assert that we can use a callable object instead of a function.
+    class Rotator(object):
+        def __call__(self, scale):
+            r = cherrypy.response
+            r.collapse_body()
+            r.body = [chr(ord(x) + scale) for x in r.body]
+    cherrypy.tools.rotator = cherrypy.Tool('before_finalize', Rotator())
     class Root:
         def index(self):
             return "Howdy earth!"