0001try:
0002    import threading
0003except ImportError:
0004    # No threads, so "thread local" means process-global
0005    class local(object):
0006        pass
0007else:
0008    try:
0009        local = threading.local
0010    except AttributeError:
0011        # Added in 2.4, but now we'll have to define it ourselves
0012        import thread
0013        class local(object):
0014
0015            def __init__(self):
0016                self.__dict__['__objs'] = {}
0017
0018            def __getattr__(self, attr, g=thread.get_ident):
0019                try:
0020                    return self.__dict__['__objs'][g()][attr]
0021                except KeyError:
0022                    raise AttributeError(
0023                        "No variable %s defined for the thread %s"
0024                        % (attr, g()))
0025
0026            def __setattr__(self, attr, value, g=thread.get_ident):
0027                self.__dict__['__objs'].setdefault(g(), {})[attr] = value
0028
0029            def __delattr__(self, attr, g=thread.get_ident):
0030                try:
0031                    del self.__dict__['__objs'][g()][attr]
0032                except KeyError:
0033                    raise AttributeError(
0034                        "No variable %s defined for thread %s"
0035                        % (attr, g()))