From e1fcc940d53854a3c08e03681229265c1df56a98 Mon Sep 17 00:00:00 2001 From: bbangert Date: Sun, 12 Mar 2006 02:58:18 +0000 Subject: 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 --- paste/registry.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'paste/registry.py') 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. -- cgit v1.2.1