OpenSecurity/install/web.py-0.37/build/lib/web/python23.py
author om
Mon, 02 Dec 2013 14:02:05 +0100
changeset 3 65432e6c6042
permissions -rwxr-xr-x
initial deployment and project layout commit
     1 """Python 2.3 compatabilty"""
     2 import threading
     3 
     4 class threadlocal(object):
     5     """Implementation of threading.local for python2.3.
     6     """
     7     def __getattribute__(self, name):
     8         if name == "__dict__":
     9             return threadlocal._getd(self)
    10         else:
    11             try:
    12                 return object.__getattribute__(self, name)
    13             except AttributeError:
    14                 try:
    15                     return self.__dict__[name]
    16                 except KeyError:
    17                     raise AttributeError, name
    18             
    19     def __setattr__(self, name, value):
    20         self.__dict__[name] = value
    21         
    22     def __delattr__(self, name):
    23         try:
    24             del self.__dict__[name]
    25         except KeyError:
    26             raise AttributeError, name
    27     
    28     def _getd(self):
    29         t = threading.currentThread()
    30         if not hasattr(t, '_d'):
    31             # using __dict__ of thread as thread local storage
    32             t._d = {}
    33         
    34         _id = id(self)
    35         # there could be multiple instances of threadlocal.
    36         # use id(self) as key
    37         if _id not in t._d:
    38             t._d[_id] = {}
    39         return t._d[_id]
    40         
    41 if __name__ == '__main__':
    42      d = threadlocal()
    43      d.x = 1
    44      print d.__dict__
    45      print d.x
    46