summaryrefslogtreecommitdiff
path: root/paste/util/threadinglocal.py
diff options
context:
space:
mode:
authorianb <devnull@localhost>2005-05-03 21:44:07 +0000
committerianb <devnull@localhost>2005-05-03 21:44:07 +0000
commit3f4349bcbf3ec9412f6184f0d8cb79f77694418f (patch)
tree62c41aa8e8aabfc6454c5ca76740c768e4790dcc /paste/util/threadinglocal.py
parentc47b53d9f666473566b0ac648e71769a0c53ec66 (diff)
downloadpaste-3f4349bcbf3ec9412f6184f0d8cb79f77694418f.tar.gz
threadlocal backward-compatibility module
Diffstat (limited to 'paste/util/threadinglocal.py')
-rw-r--r--paste/util/threadinglocal.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/paste/util/threadinglocal.py b/paste/util/threadinglocal.py
new file mode 100644
index 0000000..48559e8
--- /dev/null
+++ b/paste/util/threadinglocal.py
@@ -0,0 +1,35 @@
+try:
+ import threading
+except ImportError:
+ # No threads, so "thread local" means process-global
+ class local(object):
+ pass
+else:
+ try:
+ local = threading.local
+ except AttributeError:
+ # Added in 2.4, but now we'll have to define it ourselves
+ import thread
+ class local(object):
+
+ def __init__(self):
+ self.__dict__['__objs'] = {}
+
+ def __getattr__(self, attr, g=thread.get_ident):
+ try:
+ return self.__dict__['__objs'][g()][attr]
+ except KeyError:
+ raise AttributeError(
+ "No variable %s defined for the thread %s"
+ % (attr, g()))
+
+ def __setattr__(self, attr, value, g=thread.get_ident):
+ self.__dict__['__objs'].setdefault(g(), {})[attr] = value
+
+ def __delattr__(self, attr, g=thread.get_ident):
+ try:
+ del self.__dict__['__objs'][g()][attr]
+ except KeyError:
+ raise AttributeError(
+ "No variable %s defined for thread %s"
+ % (attr, g()))