Permalink
Browse files

* Added ability for __before__ to reference a callable function. Patch

  contributed by mverdone.

--HG--
branch : trunk
  • Loading branch information...
1 parent 7f5d447 commit 89a00d9a8d3f39515e7e326cfbf463c4a2186769 @bbangert bbangert committed Dec 18, 2010
Showing with 39 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +4 −2 pylons/controllers/core.py
  3. +33 −0 tests/test_units/test_controller.py
View
2 CHANGELOG
@@ -2,6 +2,8 @@ Pylons Changelog
================
1.1 (**tip**)
+* Added ability for __before__ to reference a callable function. Patch
+ contributed by mverdone.
* Pulled in JSON-RPC support from agentultra's pylons fork.
* Apply patch for proper pylons.__version__ under Windows. Contributed by
Christoph Zwerschke.
View
6 pylons/controllers/core.py
@@ -73,10 +73,12 @@ def _inspect_call(self, func):
except AttributeError:
self.__class__._cached_argspecs = cached_argspecs = {}
+ # function could be callable
+ func_key = getattr(func, 'im_func', func.__call__)
try:
- argspec = cached_argspecs[func.im_func]
+ argspec = cached_argspecs[func_key]
except KeyError:
- argspec = cached_argspecs[func.im_func] = inspect.getargspec(func)
+ argspec = cached_argspecs[func_key] = inspect.getargspec(func_key)
kargs = self._get_method_args()
log_debug = self._pylons_log_debug
View
33 tests/test_units/test_controller.py
@@ -216,3 +216,36 @@ def test_after_string_response(self):
def test_start_response(self):
self.baseenviron['pylons.routes_dict']['action'] = 'start_response'
self.app.get('/', status=404)
+
+class TestBeforeAfterAsCallables(TestWSGIController):
+ def __init__(self, *args, **kargs):
+ TestWSGIController.__init__(self, *args, **kargs)
+
+ class Callable(object):
+ def __init__(self): self.called = False
+ def __call__(self, *args, **kargs): self.called = True
+
+ self.before_callable = Callable()
+ self.after_callable = Callable()
+
+ from pylons.controllers import WSGIController
+ class Controller(WSGIController):
+ def index(self): return 'index'
+ __before__ = self.before_callable
+ __after__ = self.after_callable
+
+ from pylons.testutil import ControllerWrap, SetupCacheGlobal
+ self.baseenviron = {}
+ app = ControllerWrap(Controller)
+ app = self.sap = SetupCacheGlobal(app, self.baseenviron)
+ app = RegistryManager(app)
+ self.app = TestApp(app)
+
+ def test_after_called(self):
+ self.get_response(action='index')
+ assert self.after_callable.called
+
+ def test_before_called(self):
+ self.get_response(action='index')
+ assert self.before_callable.called
+

0 comments on commit 89a00d9

Please sign in to comment.