| 1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
|---|
| 2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
|---|
| 3 | |
|---|
| 4 | try: |
|---|
| 5 | import threading |
|---|
| 6 | except ImportError: |
|---|
| 7 | # No threads, so "thread local" means process-global |
|---|
| 8 | class local(object): |
|---|
| 9 | pass |
|---|
| 10 | else: |
|---|
| 11 | try: |
|---|
| 12 | local = threading.local |
|---|
| 13 | except AttributeError: |
|---|
| 14 | # Added in 2.4, but now we'll have to define it ourselves |
|---|
| 15 | import thread |
|---|
| 16 | class local(object): |
|---|
| 17 | |
|---|
| 18 | def __init__(self): |
|---|
| 19 | self.__dict__['__objs'] = {} |
|---|
| 20 | |
|---|
| 21 | def __getattr__(self, attr, g=thread.get_ident): |
|---|
| 22 | try: |
|---|
| 23 | return self.__dict__['__objs'][g()][attr] |
|---|
| 24 | except KeyError: |
|---|
| 25 | raise AttributeError( |
|---|
| 26 | "No variable %s defined for the thread %s" |
|---|
| 27 | % (attr, g())) |
|---|
| 28 | |
|---|
| 29 | def __setattr__(self, attr, value, g=thread.get_ident): |
|---|
| 30 | self.__dict__['__objs'].setdefault(g(), {})[attr] = value |
|---|
| 31 | |
|---|
| 32 | def __delattr__(self, attr, g=thread.get_ident): |
|---|
| 33 | try: |
|---|
| 34 | del self.__dict__['__objs'][g()][attr] |
|---|
| 35 | except KeyError: |
|---|
| 36 | raise AttributeError( |
|---|
| 37 | "No variable %s defined for thread %s" |
|---|
| 38 | % (attr, g())) |
|---|
| 39 | |
|---|