Changeset 1611
- Timestamp:
- 02/05/07 13:12:08
- Files:
-
- trunk/cherrypy/__init__.py (modified) (4 diffs)
- trunk/cherrypy/lib/sessions.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1610 r1611 159 159 from cherrypy import _cpdispatch as dispatch 160 160 from cherrypy import _cprequest 161 from cherrypy.lib import http as _http 161 162 from cherrypy import _cpengine 162 163 engine = _cpengine.Engine() … … 191 192 # in a thread-safe way. 192 193 class _Serving(_local): 193 """An interface for registering request and response objects.""" 194 """An interface for registering request and response objects. 195 196 Rather than have a separate "thread local" object for the request and 197 the response, this class works as a single threadlocal container for 198 both objects. 199 """ 200 201 __metaclass__ = _AttributeDocstrings 202 203 request = _cprequest.Request(_http.Host("localhost", 80), 204 _http.Host("localhost", 1111)) 205 request__doc = """ 206 The request object for the current thread. In the main thread, 207 and any threads which are not receiving HTTP requests, this is None.""" 208 209 response = _cprequest.Response() 210 response__doc = """ 211 The response object for the current thread. In the main thread, 212 and any threads which are not receiving HTTP requests, this is None.""" 194 213 195 214 def load(self, request, response): … … 206 225 class _ThreadLocalProxy(object): 207 226 208 __slots__ = ['__attrname__', '_ default_child', '__dict__']209 210 def __init__(self, attrname , default):227 __slots__ = ['__attrname__', '__dict__'] 228 229 def __init__(self, attrname): 211 230 self.__attrname__ = attrname 212 self._default_child = default213 214 def _get_child(self):215 try:216 return getattr(_serving, self.__attrname__)217 except AttributeError:218 # Bind dummy instances of default objects to help introspection.219 return self._default_child220 231 221 232 def __getattr__(self, name): 222 return getattr(self._get_child(), name) 233 child = getattr(serving, self.__attrname__) 234 return getattr(child, name) 223 235 224 236 def __setattr__(self, name, value): 225 if name in ("__attrname__", "_default_child"):237 if name in ("__attrname__", ): 226 238 object.__setattr__(self, name, value) 227 239 else: 228 setattr(self._get_child(), name, value) 240 child = getattr(serving, self.__attrname__) 241 setattr(child, name, value) 229 242 230 243 def __delattr__(self, name): 231 delattr(self._get_child(), name) 244 child = getattr(serving, self.__attrname__) 245 delattr(child, name) 232 246 233 247 def _get_dict(self): 234 child object = self._get_child()235 d = child object.__class__.__dict__.copy()236 d.update(child object.__dict__)248 child = getattr(serving, self.__attrname__) 249 d = child.__class__.__dict__.copy() 250 d.update(child.__dict__) 237 251 return d 238 252 __dict__ = property(_get_dict) 239 253 240 254 def __getitem__(self, key): 241 return self._get_child()[key] 255 child = getattr(serving, self.__attrname__) 256 return child[key] 242 257 243 258 def __setitem__(self, key, value): 244 self._get_child()[key] = value 259 child = getattr(serving, self.__attrname__) 260 child[key] = value 245 261 246 262 def __delitem__(self, key): 247 del self._get_child()[key] 263 child = getattr(serving, self.__attrname__) 264 del child[key] 248 265 249 266 def __contains__(self, key): 250 return key in self._get_child() 267 child = getattr(serving, self.__attrname__) 268 return key in child 251 269 252 270 253 271 # Create request and response object (the same objects will be used 254 272 # throughout the entire life of the webserver, but will redirect 255 # to the "_serving" object) 256 from cherrypy.lib import http as _http 257 request = _ThreadLocalProxy('request', 258 _cprequest.Request(_http.Host("localhost", 80), 259 _http.Host("localhost", 1111))) 260 response = _ThreadLocalProxy('response', _cprequest.Response()) 273 # to the "serving" object) 274 request = _ThreadLocalProxy('request') 275 response = _ThreadLocalProxy('response') 261 276 262 277 # Create thread_data object as a thread-specific all-purpose storage … … 271 286 """Given an object or a path to an object, get the object and its name.""" 272 287 if isinstance(thing, _ThreadLocalProxy): 273 thing = thing._get_child()288 thing = getattr(serving, thing.__attrname__) 274 289 return pydoc._builtin_resolve(thing, forceload) 275 290 trunk/cherrypy/lib/sessions.py
r1600 r1611 37 37 38 38 class Session(object): 39 """A CherryPy dict-like Session object (one per request). 40 41 id: current session ID. 42 expiration_time (datetime): when the current session will expire. 43 timeout (minutes): used to calculate expiration_time from now. 44 clean_freq (minutes): the poll rate for expired session cleanup. 45 locked: If True, this session instance has exclusive read/write access 46 to session data. 47 loaded: If True, data has been retrieved from storage. This should 48 happen automatically on the first attempt to access session data. 49 """ 39 """A CherryPy dict-like Session object (one per request).""" 40 41 __metaclass__ = cherrypy._AttributeDocstrings 42 43 id = None 44 id__doc = "The current session ID." 45 46 timeout = 60 47 timeout__doc = "Number of minutes after which to delete session data." 48 49 locked = False 50 locked__doc = """ 51 If True, this session instance has exclusive read/write access 52 to session data.""" 53 54 loaded = False 55 loaded__doc = """ 56 If True, data has been retrieved from storage. This should happen 57 automatically on the first attempt to access session data.""" 50 58 51 59 clean_thread = None 60 clean_thread__doc = "Class-level PerpetualTimer which calls self.clean_up." 61 62 clean_freq = 5 63 clean_freq__doc = "The poll rate for expired session cleanup in minutes." 52 64 53 65 def __init__(self, id=None, **kwargs): 54 self.locked = False55 self.loaded = False56 66 self._data = {} 57 67 … … 413 423 414 424 415 _def_session = RamSession()416 417 425 def init(storage_type='ram', path=None, path_header=None, name='session_id', 418 426 timeout=60, domain=None, secure=False, clean_freq=5, **kwargs): … … 440 448 441 449 # Guard against running twice 442 if hasattr( cherrypy._serving, "session"):450 if hasattr(request, "_session_init_flag"): 443 451 return 452 request._session_init_flag = True 444 453 445 454 # Check if request came with a session ID … … 457 466 458 467 if not hasattr(cherrypy, "session"): 459 cherrypy.session = cherrypy._ThreadLocalProxy('session' , _def_session)468 cherrypy.session = cherrypy._ThreadLocalProxy('session') 460 469 if hasattr(sess, "setup"): 461 470 sess.setup()

