Changeset 620
- Timestamp:
- 09/09/05 18:24:53
- Files:
-
- trunk/cherrypy/__init__.py (modified) (2 diffs)
- trunk/cherrypy/test/cp_decorator_tests.py (added)
- trunk/cherrypy/test/test_objectmapping.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r597 r620 34 34 35 35 import datetime 36 import sys 37 import types 36 38 37 39 from _cperror import * … … 68 70 _sessionLastCleanUpTime = datetime.datetime.now() 69 71 70 # decorator function for exposing methods 71 def expose(func): 72 func.exposed = True 73 return func 72 def expose(func=None, alias=None): 73 """Expose the function, optionally providing an alias or set of aliases.""" 74 75 def expose_(func): 76 func.exposed = True 77 if alias is not None: 78 if isinstance(alias, basestring): 79 parentDict[alias] = func 80 else: 81 for a in alias: 82 parentDict[a] = func 83 return func 84 85 parentDict = sys._getframe(1).f_locals 86 if isinstance(func, (types.FunctionType, types.MethodType)): 87 # expose is being called directly, before the method has been bound 88 return expose_(func) 89 else: 90 # expose is being called as a decorator 91 if alias is None: 92 alias = func 93 return expose_ 74 94 75 95 def log(msg, context='', severity=0): trunk/cherrypy/test/test_objectmapping.py
r561 r620 26 26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 27 """ 28 29 import sys 28 30 29 31 import cherrypy … … 58 60 mapped_func.exposed = True 59 61 setattr(Root, "Von B\xfclow", mapped_func) 62 63 64 if sys.hexversion > 0x020400A2: 65 from cp_decorator_tests import Exposing, ExposingNewStyle 66 else: 67 class Exposing: 68 def base(self): 69 return "expose works!" 70 cherrypy.expose(base, "1") 71 cherrypy.expose(base, "2") 72 73 class ExposingNewStyle(object): 74 def base(self): 75 return "expose works!" 76 cherrypy.expose(base, "1") 77 cherrypy.expose(base, "2") 78 60 79 61 80 … … 94 113 95 114 cherrypy.root = Root() 115 cherrypy.root.exposing = Exposing() 116 cherrypy.root.exposingnew = ExposingNewStyle() 96 117 cherrypy.root.dir1 = Dir1() 97 118 cherrypy.root.dir1.dir2 = Dir2() … … 147 168 self.getPage("/Von%20B%fclow?ID=14") 148 169 self.assertBody("ID is 14") 170 171 def testExpose(self): 172 # Test the cherrypy.expose function/decorator 173 self.getPage("/exposing/1") 174 self.assertBody("expose works!") 175 176 self.getPage("/exposing/2") 177 self.assertBody("expose works!") 178 179 self.getPage("/exposingnew/1") 180 self.assertBody("expose works!") 181 182 self.getPage("/exposingnew/2") 183 self.assertBody("expose works!") 184 149 185 150 186

