diff options
author | bbangert <devnull@localhost> | 2006-03-12 02:58:18 +0000 |
---|---|---|
committer | bbangert <devnull@localhost> | 2006-03-12 02:58:18 +0000 |
commit | e1fcc940d53854a3c08e03681229265c1df56a98 (patch) | |
tree | b8569a9e5b6b07094914b9c8333bc6b21a3d9ce1 /paste/registry.py | |
parent | fffca229d33a4267b807ac2542774fc1c255db8d (diff) | |
download | paste-e1fcc940d53854a3c08e03681229265c1df56a98.tar.gz |
Added unit tests to ensure that TypeError is thrown if no default is provided and no object is pushed to the Proxy.
Added ability to define a default object for a StackedObjectProxy
Diffstat (limited to 'paste/registry.py')
-rw-r--r-- | paste/registry.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/paste/registry.py b/paste/registry.py index 44d6f71..fd1ffae 100644 --- a/paste/registry.py +++ b/paste/registry.py @@ -65,9 +65,16 @@ class StackedObjectProxy(object): objects can be removed with pop_object. """ - def __init__(self): - """Create a new StackedObjectProxy""" + def __init__(self, default=None): + """Create a new StackedObjectProxy + + If a default is given, its used in every thread if no other object + has been pushed on. + + """ self.__dict__['local'] = threadinglocal.local() + if default: + self.__dict__['_default_object'] = default def __getattr__(self, attr): return getattr(self.current_obj(), attr) @@ -99,13 +106,22 @@ class StackedObjectProxy(object): return self.current_obj().has_key(key) def current_obj(self): - """Returns the current active object being proxied to""" + """Returns the current active object being proxied to + + In the event that no object was pushed, the default object if + provided will be used. Otherwise, a TypeError will be raised. + + """ objects = getattr(self.__dict__['local'], 'objects', None) if objects: return objects[-1] else: - raise TypeError( - "No object has been registered for this thread") + object = self.__dict__.get('_default_object') + if object: + return object + else: + raise TypeError( + "No object has been registered for this thread") def push_object(self, obj): """Make ``obj`` the active object for this thread-local. |