Commits

Robert Brewer committed 8f251ef

Fix for #826 (@cherrypy.expose() fails with no argument).

Comments (0)

Files changed (2)

cherrypy/__init__.py

 
 def expose(func=None, alias=None):
     """Expose the function, optionally providing an alias or set of aliases."""
-    
     def expose_(func):
         func.exposed = True
         if alias is not None:
     
     import sys, types
     if isinstance(func, (types.FunctionType, types.MethodType)):
-        # expose is being called directly, before the method has been bound
-        parents = sys._getframe(1).f_locals
-        return expose_(func)
-    else:
         if alias is None:
-            # expose is being called as a decorator "@expose"
+            # @expose
             func.exposed = True
             return func
         else:
-            # expose is returning a decorator "@expose(alias=...)"
+            # func = expose(func, alias)
+            parents = sys._getframe(1).f_locals
+            return expose_(func)
+    elif func is None:
+        if alias is None:
+            # @expose()
             parents = sys._getframe(1).f_locals
             return expose_
+        else:
+            # @expose(alias="alias") or
+            # @expose(alias=["alias1", "alias2"])
+            parents = sys._getframe(1).f_locals
+            return expose_
+    else:
+        # @expose("alias") or
+        # @expose(["alias1", "alias2"])
+        parents = sys._getframe(1).f_locals
+        alias = func
+        return expose_
+
 
 def url(path="", qs="", script_name=None, base=None, relative=None):
     """Create an absolute URL for the given path.

cherrypy/test/test_core.py

 
 import os
 localDir = os.path.dirname(__file__)
+import sys
 import types
 
 import cherrypy
             cherrypy.request.asdf = "rassfrassin"
             return existing
     
+    if sys.version_info >= (2, 5):
+        from cherrypy.test import py25
+        Root.expose_dec = py25.ExposeExamples()
+    
     cherrypy.config.update({
         'environment': 'test_suite',
         'server.max_request_body_size': 200,
         self.assertBody('/page1')
         self.getPage('/url/?path_info=/page1&relative=server')
         self.assertBody('/page1')
+    
+    def test_expose_decorator(self):
+        if not sys.version_info >= (2, 5):
+            print "skipped (Python 2.5+ only)",
+            return
+        
+        # Test @expose
+        self.getPage("/expose_dec/no_call")
+        self.assertStatus(200)
+        self.assertBody("Mr E. R. Bradshaw")
+        
+        # Test @expose()
+        self.getPage("/expose_dec/call_empty")
+        self.assertStatus(200)
+        self.assertBody("Mrs. B.J. Smegma")
+        
+        # Test @expose("alias")
+        self.getPage("/expose_dec/call_alias")
+        self.assertStatus(200)
+        self.assertBody("Mr Nesbitt")
+        # Does the original name work?
+        self.getPage("/expose_dec/nesbitt")
+        self.assertStatus(200)
+        self.assertBody("Mr Nesbitt")
+        
+        # Test @expose(["alias1", "alias2"])
+        self.getPage("/expose_dec/alias1")
+        self.assertStatus(200)
+        self.assertBody("Mr Ken Andrews")
+        self.getPage("/expose_dec/alias2")
+        self.assertStatus(200)
+        self.assertBody("Mr Ken Andrews")
+        # Does the original name work?
+        self.getPage("/expose_dec/andrews")
+        self.assertStatus(200)
+        self.assertBody("Mr Ken Andrews")
+        
+        # Test @expose(alias="alias")
+        self.getPage("/expose_dec/alias3")
+        self.assertStatus(200)
+        self.assertBody("Mr. and Mrs. Watson")
 
 
 if __name__ == '__main__':