Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  contributed by mverdone.

--HG--
branch : trunk
  • Loading branch information...
commit 89a00d9a8d3f39515e7e326cfbf463c4a2186769 1 parent 7f5d447
Ben Bangert authored December 18, 2010
2  CHANGELOG
@@ -2,6 +2,8 @@ Pylons Changelog
2 2
 ================
3 3
 
4 4
 1.1 (**tip**)
  5
+* Added ability for __before__ to reference a callable function. Patch
  6
+  contributed by mverdone.
5 7
 * Pulled in JSON-RPC support from agentultra's pylons fork.
6 8
 * Apply patch for proper pylons.__version__ under Windows. Contributed by
7 9
   Christoph Zwerschke.
6  pylons/controllers/core.py
@@ -73,10 +73,12 @@ def _inspect_call(self, func):
73 73
         except AttributeError:
74 74
             self.__class__._cached_argspecs = cached_argspecs = {}
75 75
         
  76
+        # function could be callable
  77
+        func_key = getattr(func, 'im_func', func.__call__)
76 78
         try:
77  
-            argspec = cached_argspecs[func.im_func]
  79
+            argspec = cached_argspecs[func_key]
78 80
         except KeyError:
79  
-            argspec = cached_argspecs[func.im_func] = inspect.getargspec(func)
  81
+            argspec = cached_argspecs[func_key] = inspect.getargspec(func_key)
80 82
         kargs = self._get_method_args()
81 83
                 
82 84
         log_debug = self._pylons_log_debug
33  tests/test_units/test_controller.py
@@ -216,3 +216,36 @@ def test_after_string_response(self):
216 216
     def test_start_response(self):
217 217
         self.baseenviron['pylons.routes_dict']['action'] = 'start_response'
218 218
         self.app.get('/', status=404)
  219
+
  220
+class TestBeforeAfterAsCallables(TestWSGIController):
  221
+    def __init__(self, *args, **kargs):
  222
+        TestWSGIController.__init__(self, *args, **kargs)
  223
+
  224
+        class Callable(object):
  225
+            def __init__(self): self.called = False
  226
+            def __call__(self, *args, **kargs): self.called = True
  227
+
  228
+        self.before_callable = Callable()
  229
+        self.after_callable = Callable()
  230
+
  231
+        from pylons.controllers import WSGIController
  232
+        class Controller(WSGIController):
  233
+            def index(self): return 'index'
  234
+            __before__ = self.before_callable
  235
+            __after__ = self.after_callable
  236
+
  237
+        from pylons.testutil import ControllerWrap, SetupCacheGlobal
  238
+        self.baseenviron = {}
  239
+        app = ControllerWrap(Controller)
  240
+        app = self.sap = SetupCacheGlobal(app, self.baseenviron)
  241
+        app = RegistryManager(app)
  242
+        self.app = TestApp(app)
  243
+
  244
+    def test_after_called(self):
  245
+        self.get_response(action='index')
  246
+        assert self.after_callable.called
  247
+
  248
+    def test_before_called(self):
  249
+        self.get_response(action='index')
  250
+        assert self.before_callable.called
  251
+

0 notes on commit 89a00d9

Please sign in to comment.
Something went wrong with that request. Please try again.