Robert Brewer avatar Robert Brewer committed 93d84be

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

Comments (0)

Files changed (2)

cherrypy/_cptools.py

                 setattr(self, arg, None)
         except (ImportError, AttributeError):
             pass
+        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):

cherrypy/test/test_tools.py

         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!"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.