_cpthreadinglocal.py (2801B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\_cpthreadinglocal.py 2 3 4 class _localbase(object): 5 __slots__ = ('_local__key', '_local__args', '_local__lock') 6 7 def __new__(cls, *args, **kw): 8 self = object.__new__(cls) 9 key = 'thread.local.' + str(id(self)) 10 object.__setattr__(self, '_local__key', key) 11 object.__setattr__(self, '_local__args', (args, kw)) 12 object.__setattr__(self, '_local__lock', RLock()) 13 if args or kw and cls.__init__ is object.__init__: 14 raise TypeError('Initialization arguments are not supported') 15 dict = object.__getattribute__(self, '__dict__') 16 currentThread().__dict__[key] = dict 17 return self 18 19 20 def _patch(self): 21 key = object.__getattribute__(self, '_local__key') 22 d = currentThread().__dict__.get(key) 23 if d is None: 24 d = {} 25 currentThread().__dict__[key] = d 26 object.__setattr__(self, '__dict__', d) 27 cls = type(self) 28 if cls.__init__ is not object.__init__: 29 args, kw = object.__getattribute__(self, '_local__args') 30 cls.__init__(self, *args, **kw) 31 else: 32 object.__setattr__(self, '__dict__', d) 33 34 35 class local(_localbase): 36 37 def __getattribute__(self, name): 38 lock = object.__getattribute__(self, '_local__lock') 39 lock.acquire() 40 try: 41 _patch(self) 42 return object.__getattribute__(self, name) 43 finally: 44 lock.release() 45 46 def __setattr__(self, name, value): 47 lock = object.__getattribute__(self, '_local__lock') 48 lock.acquire() 49 try: 50 _patch(self) 51 return object.__setattr__(self, name, value) 52 finally: 53 lock.release() 54 55 def __delattr__(self, name): 56 lock = object.__getattribute__(self, '_local__lock') 57 lock.acquire() 58 try: 59 _patch(self) 60 return object.__delattr__(self, name) 61 finally: 62 lock.release() 63 64 def __del__(): 65 threading_enumerate = enumerate 66 __getattribute__ = object.__getattribute__ 67 68 def __del__(self): 69 key = __getattribute__(self, '_local__key') 70 try: 71 threads = list(threading_enumerate()) 72 except: 73 return 74 75 for thread in threads: 76 try: 77 __dict__ = thread.__dict__ 78 except AttributeError: 79 continue 80 81 if key in __dict__: 82 try: 83 del __dict__[key] 84 except KeyError: 85 pass 86 87 return __del__ 88 89 __del__ = __del__() 90 91 92 from threading import currentThread, enumerate, RLock