Changeset 1081
- Timestamp:
- 04/26/06 18:56:41
- Files:
-
- trunk/cherrypy/test/test_custom_filters.py (modified) (6 diffs)
- trunk/cherrypy/tools.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/test/test_custom_filters.py
r1077 r1081 39 39 self.counter = 0 40 40 self.ended = {} 41 class ToolMixin(object):42 def _cp_setup(me):43 self.setup(None)44 self.Mixin = ToolMixin45 41 46 42 def nadsat(self): … … 57 53 self.ended[cherrypy.request.counter] = True 58 54 59 def setup(self, conf ):55 def setup(self, conf=None): 60 56 cherrypy.request.counter = self.counter = self.counter + 1 61 57 self.ended[cherrypy.request.counter] = False … … 86 82 87 83 # METHOD ONE: 88 # Use tool.Mixin 89 class CPFilterList(Test, tools.nadsat.Mixin): 84 # Use _cp_tools 85 class Demo(Test): 86 87 _cp_tools = [tools.nadsat] 90 88 91 89 def index(self): … … 119 117 # METHOD THREE: 120 118 # Do it all in config 121 '/ cpfilterlist': {119 '/demo': { 122 120 'tools.numerify.on': True, 123 121 'tools.numerify.map': {"pie": "3.14159"}, 124 122 }, 125 '/ cpfilterlist/restricted': {123 '/demo/restricted': { 126 124 'server.show_tracebacks': False, 127 125 }, 128 '/ cpfilterlist/errinstream': {126 '/demo/errinstream': { 129 127 'stream_response': True, 130 128 }, 131 '/ cpfilterlist/err_in_onstart': {129 '/demo/err_in_onstart': { 132 130 # Because this isn't a dict, on_start_resource will error. 133 131 'tools.numerify.map': "pie->3.14159" … … 143 141 class FilterTests(helper.CPWebCase): 144 142 145 def test CPFilterList(self):146 ## self.getPage("/cpfilterlist/")147 ### If body is "razdrez", then on_end_request is being called too early.148 ##self.assertBody("A horrorshow lomtick of cherry 3.14159")149 ### If this fails, then on_end_request isn't being called at all.150 ## self.getPage("/cpfilterlist/ended/1")151 ##self.assertBody("True")152 ##153 ##valerr = '\n raise ValueError()\nValueError'154 ## self.getPage("/cpfilterlist/err")155 ### If body is "razdrez", then on_end_request is being called too early.156 ##self.assertErrorPage(500, pattern=valerr)157 ### If this fails, then on_end_request isn't being called at all.158 ## self.getPage("/cpfilterlist/ended/3")159 ##self.assertBody("True")160 ##161 ### If body is "razdrez", then on_end_request is being called too early.162 ## self.getPage("/cpfilterlist/errinstream")163 ### Because this error is raised after the response body has164 ### started, the status should not change to an error status.165 ##self.assertStatus("200 OK")166 ##self.assertBody("Unrecoverable error in the server.")167 ### If this fails, then on_end_request isn't being called at all.168 ## self.getPage("/cpfilterlist/ended/5")169 ##self.assertBody("True")170 ##143 def testDemo(self): 144 self.getPage("/demo/") 145 # If body is "razdrez", then on_end_request is being called too early. 146 self.assertBody("A horrorshow lomtick of cherry 3.14159") 147 # If this fails, then on_end_request isn't being called at all. 148 self.getPage("/demo/ended/1") 149 self.assertBody("True") 150 151 valerr = '\n raise ValueError()\nValueError' 152 self.getPage("/demo/err") 153 # If body is "razdrez", then on_end_request is being called too early. 154 self.assertErrorPage(500, pattern=valerr) 155 # If this fails, then on_end_request isn't being called at all. 156 self.getPage("/demo/ended/3") 157 self.assertBody("True") 158 159 # If body is "razdrez", then on_end_request is being called too early. 160 self.getPage("/demo/errinstream") 161 # Because this error is raised after the response body has 162 # started, the status should not change to an error status. 163 self.assertStatus("200 OK") 164 self.assertBody("Unrecoverable error in the server.") 165 # If this fails, then on_end_request isn't being called at all. 166 self.getPage("/demo/ended/5") 167 self.assertBody("True") 168 171 169 # Test the decorator technique. 172 self.getPage("/ cpfilterlist/restricted")170 self.getPage("/demo/restricted") 173 171 self.assertErrorPage(401) 174 172 … … 180 178 # but because that failure is logged and passed over, the error 181 179 # page we obtain in the user agent should be from before_finalize. 182 self.getPage("/ cpfilterlist/err_in_onstart")180 self.getPage("/demo/err_in_onstart") 183 181 self.assertErrorPage(500) 184 182 self.assertInBody("AttributeError: 'Request' object has no " trunk/cherrypy/tools.py
r1078 r1081 37 37 38 38 def attach(self, point, callback, conf=None): 39 if conf is None: 39 if not conf: 40 # No point adding a wrapper if there's no conf 40 41 self.callbacks[point].append(callback) 41 42 else: … … 52 53 g[toolname].setup(conf) 53 54 54 # Run _cp_ setup functions55 # Run _cp_tools setup functions 55 56 mounted_app_roots = cherrypy.tree.mount_points.values() 56 57 objectList = _cputil.get_object_trail() 57 58 objectList.reverse() 58 59 for objname, obj in objectList: 59 s = getattr(obj, "_cp_setup", None) 60 if s: 61 s() 60 for tool in getattr(obj, "_cp_tools", []): 61 tool.setup() 62 62 if obj in mounted_app_roots: 63 63 break … … 114 114 # TODO: add an attribute to self for each arg 115 115 # in inspect.getargspec(callable) 116 117 class ToolMixin(object):118 def _cp_setup(me):119 self.setup(None)120 self.Mixin = ToolMixin121 116 122 117 def __call__(self, *args, **kwargs): … … 140 135 return deco 141 136 142 def setup(self, conf ):137 def setup(self, conf=None): 143 138 """Hook this tool into cherrypy.request using the given conf. 144 139 … … 157 152 158 153 def __init__(self, callable, name=None): 159 self.point = 'before_main' 160 self.callable = callable 161 if name is None: 162 name = callable.__name__ 163 self.name = name 154 Tool.__init__(self, 'before_main', callable, name) 164 155 165 156 def handler(self, *args, **kwargs): … … 198 189 return deco 199 190 200 def setup(self, conf ):191 def setup(self, conf=None): 201 192 """Hook this tool into cherrypy.request using the given conf. 202 193 … … 204 195 method when the tool is "turned on" in config. 205 196 """ 197 conf = conf or {} 206 198 def wrapper(): 207 199 if self.callable(**conf): … … 228 220 from cherrypy.lib import static 229 221 class _StaticDirTool(MainTool): 230 def setup(self, conf ):222 def setup(self, conf=None): 231 223 """Hook this tool into cherrypy.request using the given conf.""" 232 224 # Stick the section where "dir" was defined into the params 225 conf = conf or {} 233 226 conf['section'] = cherrypy.config.get('tools.staticdir.dir', 234 227 return_section=True) 235 def wrapper(): 236 if self.callable(**conf): 237 cherrypy.request.dispatch = None 238 # Don't pass conf (or our wrapper will get wrapped!) 239 cherrypy.request.hooks.attach(self.point, wrapper) 228 MainTool.setup(self, conf) 240 229 staticdir = _StaticDirTool(static.staticdir) 241 230 staticfile = MainTool(static.staticfile) … … 267 256 return deco 268 257 269 def setup(self, conf ):258 def setup(self, conf=None): 270 259 """Hook this tool into cherrypy.request using the given conf. 271 260 … … 273 262 method when the tool is "turned on" in config. 274 263 """ 264 conf = conf or {} 275 265 def init(): 276 266 s = cherrypy.request._session = _sessions.Session() … … 281 271 if not hasattr(cherrypy, "session"): 282 272 cherrypy.session = _sessions.SessionWrapper() 273 # init must be bound after headers are read 283 274 cherrypy.request.hooks.attach('before_request_body', init) 284 285 275 cherrypy.request.hooks.attach('before_finalize', _sessions.save) 286 276 cherrypy.request.hooks.attach('on_end_request', _sessions.cleanup) … … 308 298 path = _xmlrpc.patched_path(path, rpcmethod) 309 299 310 from cherrypy import _cprequest 311 handler, opath, vpath = _cprequest.find_handler(path) 300 handler, opath, vpath = _cputil.find_handler(path) 312 301 313 302 # Decode any leftover %2F in the virtual_path atoms. … … 320 309 conf.get('allow_none', 0)) 321 310 322 def setup(self, conf ):311 def setup(self, conf=None): 323 312 """Hook this tool into cherrypy.request using the given conf.""" 324 313 cherrypy.request.dispatch = self.dispatch

