[Checkins] SVN: bobo/trunk/bobo Fixed a handler cache bug a slightly different way.

Jim Fulton jim at zope.com
Mon Jan 18 14:52:18 EST 2010


Log message for revision 108218:
  Fixed a handler cache bug a slightly different way.
  

Changed:
  U   bobo/trunk/bobo/src/bobo.py
  U   bobo/trunk/bobodoctestumentation/src/bobodoctestumentation/decorator.test

-=-
Modified: bobo/trunk/bobo/src/bobo.py
===================================================================
--- bobo/trunk/bobo/src/bobo.py	2010-01-18 19:49:21 UTC (rev 108217)
+++ bobo/trunk/bobo/src/bobo.py	2010-01-18 19:52:18 UTC (rev 108218)
@@ -524,6 +524,12 @@
     """
     return order() + _early_base
 
+class _cached_property(object):
+    def __init__(self, func):
+        self.func = func
+    def __get__(self, inst, class_):
+        return self.func(inst)
+
 _ext_re = re.compile('/(\w+)').search
 class _Handler:
 
@@ -554,29 +560,32 @@
         if order_ is None:
             order_ = order()
         self.bobo_order = order_
-        self._bobo_handle()
-        self._match()
 
-    def _bobo_handle(self):
+    @_cached_property
+    def bobo_handle(self):
         func = original = self.bobo_original
         if self.params:
             func = _make_caller(func, self.params)
         func = _make_bobo_handle(func, original, self.check, self.content_type)
         self.__dict__['bobo_handle'] = func
+        return func
 
-    def _match(self):
+    @_cached_property
+    def match(self):
         route_data = _compile_route(self.bobo_route, self.partial)
         methods = self.bobo_methods
         if methods is None:
-            match = route_data
-        else:
-            def match(request, path, method):
-                data = route_data(request, path)
-                if data is not None:
-                    if method not in methods:
-                        raise MethodNotAllowed(methods)
-                    return data
+            return route_data
+
+        def match(request, path, method):
+            data = route_data(request, path)
+            if data is not None:
+                if method not in methods:
+                    raise MethodNotAllowed(methods)
+                return data
+
         self.__dict__['match'] = match
+        return match
 
     def bobo_response(self, *args):
         request, path, method = args[-3:]

Modified: bobo/trunk/bobodoctestumentation/src/bobodoctestumentation/decorator.test
===================================================================
--- bobo/trunk/bobodoctestumentation/src/bobodoctestumentation/decorator.test	2010-01-18 19:49:21 UTC (rev 108217)
+++ bobo/trunk/bobodoctestumentation/src/bobodoctestumentation/decorator.test	2010-01-18 19:52:18 UTC (rev 108218)
@@ -829,3 +829,33 @@
 
     >>> call_resource(Traverse, '/Traverse/x')
     Traverse
+
+check caching of handler setup
+------------------------------
+
+    >>> original_make_bobo_handle = bobo._make_bobo_handle
+    >>> def test_make_bobo_handle(*args):
+    ...     print '_make_bobo_handle', args[0].__name__
+    ...     return original_make_bobo_handle(*args)
+    >>> bobo._make_bobo_handle = test_make_bobo_handle
+
+    >>> original_compile_route = bobo._compile_route
+    >>> def test_compile_route(*args):
+    ...     print '_compile_route', args[0]
+    ...     return original_compile_route(*args)
+    >>> bobo._compile_route = test_compile_route
+
+    >>> @bobo.resource('/test_route')
+    ... def test_func(r):
+    ...     return 'hi'
+
+    >>> call_resource(test_func, '/test_route') # doctest: +ELLIPSIS
+    _compile_route /test_route
+    _make_bobo_handle test_func
+    BoboException:...
+
+    >>> call_resource(test_func, '/test_route') # doctest: +ELLIPSIS
+    BoboException:...
+
+    >>> bobo._make_bobo_handle = original_make_bobo_handle
+    >>> bobo._compile_route = original_compile_route



More information about the checkins mailing list