local.py (8573B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\werkzeug\local.py 2 try: 3 from greenlet import getcurrent as get_current_greenlet 4 except ImportError: 5 try: 6 from py.magic import greenlet 7 get_current_greenlet = greenlet.getcurrent 8 del greenlet 9 except: 10 get_current_greenlet = int 11 12 try: 13 from thread import get_ident as get_current_thread, allocate_lock 14 except ImportError: 15 from dummy_thread import get_ident as get_current_thread, allocate_lock 16 17 from werkzeug.wsgi import ClosingIterator 18 from werkzeug._internal import _patch_wrapper 19 if get_current_greenlet is int: 20 get_ident = get_current_thread 21 else: 22 get_ident = lambda : (get_current_thread(), get_current_greenlet()) 23 24 def release_local(local): 25 local.__release_local__() 26 27 28 class Local(object): 29 __slots__ = ('__storage__', '__lock__') 30 31 def __init__(self): 32 object.__setattr__(self, '__storage__', {}) 33 object.__setattr__(self, '__lock__', allocate_lock()) 34 35 def __iter__(self): 36 return self.__storage__.iteritems() 37 38 def __call__(self, proxy): 39 return LocalProxy(self, proxy) 40 41 def __release_local__(self): 42 self.__storage__.pop(get_ident(), None) 43 44 def __getattr__(self, name): 45 self.__lock__.acquire() 46 try: 47 return self.__storage__[get_ident()][name] 48 except KeyError: 49 raise AttributeError(name) 50 finally: 51 self.__lock__.release() 52 53 def __setattr__(self, name, value): 54 self.__lock__.acquire() 55 try: 56 ident = get_ident() 57 storage = self.__storage__ 58 if ident in storage: 59 storage[ident][name] = value 60 else: 61 storage[ident] = {name: value} 62 finally: 63 self.__lock__.release() 64 65 def __delattr__(self, name): 66 self.__lock__.acquire() 67 try: 68 del self.__storage__[get_ident()][name] 69 except KeyError: 70 raise AttributeError(name) 71 finally: 72 self.__lock__.release() 73 74 75 class LocalStack(object): 76 77 def __init__(self): 78 self._local = Local() 79 self._lock = allocate_lock() 80 81 def __release_local__(self): 82 self._local.__release_local__() 83 84 def __call__(self): 85 86 def _lookup(): 87 rv = self.top 88 if rv is None: 89 raise RuntimeError('object unbound') 90 return rv 91 92 return LocalProxy(_lookup) 93 94 def push(self, obj): 95 self._lock.acquire() 96 try: 97 rv = getattr(self._local, 'stack', None) 98 if rv is None: 99 self._local.stack = rv = [] 100 rv.append(obj) 101 return rv 102 finally: 103 self._lock.release() 104 105 def pop(self): 106 self._lock.acquire() 107 try: 108 stack = getattr(self._local, 'stack', None) 109 if stack is None: 110 return 111 if len(stack) == 1: 112 release_local(self._local) 113 return stack[-1] 114 return stack.pop() 115 finally: 116 self._lock.release() 117 118 @property 119 def top(self): 120 try: 121 return self._local.stack[-1] 122 except (AttributeError, IndexError): 123 return None 124 125 126 class LocalManager(object): 127 128 def __init__(self, locals = None): 129 if locals is None: 130 self.locals = [] 131 elif isinstance(locals, Local): 132 self.locals = [locals] 133 else: 134 self.locals = list(locals) 135 136 def get_ident(self): 137 return get_ident() 138 139 def cleanup(self): 140 ident = self.get_ident() 141 for local in self.locals: 142 release_local(local) 143 144 def make_middleware(self, app): 145 146 def application(environ, start_response): 147 return ClosingIterator(app(environ, start_response), self.cleanup) 148 149 return application 150 151 def middleware(self, func): 152 return _patch_wrapper(func, self.make_middleware(func)) 153 154 def __repr__(self): 155 return '<%s storages: %d>' % (self.__class__.__name__, len(self.locals)) 156 157 158 class LocalProxy(object): 159 __slots__ = ('__local', '__dict__', '__name__') 160 161 def __init__(self, local, name = None): 162 object.__setattr__(self, '_LocalProxy__local', local) 163 object.__setattr__(self, '__name__', name) 164 165 def _get_current_object(self): 166 if not hasattr(self.__local, '__release_local__'): 167 return self.__local() 168 try: 169 return getattr(self.__local, self.__name__) 170 except AttributeError: 171 raise RuntimeError('no object bound to %s' % self.__name__) 172 173 @property 174 def __dict__(self): 175 try: 176 return self._get_current_object().__dict__ 177 except RuntimeError: 178 return AttributeError('__dict__') 179 180 def __repr__(self): 181 try: 182 obj = self._get_current_object() 183 except RuntimeError: 184 return '<%s unbound>' % self.__class__.__name__ 185 186 return repr(obj) 187 188 def __nonzero__(self): 189 try: 190 return bool(self._get_current_object()) 191 except RuntimeError: 192 return False 193 194 def __unicode__(self): 195 try: 196 return unicode(self._get_current_object()) 197 except RuntimeError: 198 return repr(self) 199 200 def __dir__(self): 201 try: 202 return dir(self._get_current_object()) 203 except RuntimeError: 204 return [] 205 206 def __getattr__(self, name): 207 if name == '__members__': 208 return dir(self._get_current_object()) 209 return getattr(self._get_current_object(), name) 210 211 def __setitem__(self, key, value): 212 self._get_current_object()[key] = value 213 214 def __delitem__(self, key): 215 del self._get_current_object()[key] 216 217 def __setslice__(self, i, j, seq): 218 self._get_current_object()[i:j] = seq 219 220 def __delslice__(self, i, j): 221 del self._get_current_object()[i:j] 222 223 __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v) 224 __delattr__ = lambda x, n: delattr(x._get_current_object(), n) 225 __str__ = lambda x: str(x._get_current_object()) 226 __lt__ = lambda x, o: x._get_current_object() < o 227 __le__ = lambda x, o: x._get_current_object() <= o 228 __eq__ = lambda x, o: x._get_current_object() == o 229 __ne__ = lambda x, o: x._get_current_object() != o 230 __gt__ = lambda x, o: x._get_current_object() > o 231 __ge__ = lambda x, o: x._get_current_object() >= o 232 __cmp__ = lambda x, o: cmp(x._get_current_object(), o) 233 __hash__ = lambda x: hash(x._get_current_object()) 234 __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw) 235 __len__ = lambda x: len(x._get_current_object()) 236 __getitem__ = lambda x, i: x._get_current_object()[i] 237 __iter__ = lambda x: iter(x._get_current_object()) 238 __contains__ = lambda x, i: i in x._get_current_object() 239 __getslice__ = lambda x, i, j: x._get_current_object()[i:j] 240 __add__ = lambda x, o: x._get_current_object() + o 241 __sub__ = lambda x, o: x._get_current_object() - o 242 __mul__ = lambda x, o: x._get_current_object() * o 243 __floordiv__ = lambda x, o: x._get_current_object() // o 244 __mod__ = lambda x, o: x._get_current_object() % o 245 __divmod__ = lambda x, o: x._get_current_object().__divmod__(o) 246 __pow__ = lambda x, o: x._get_current_object() ** o 247 __lshift__ = lambda x, o: x._get_current_object() << o 248 __rshift__ = lambda x, o: x._get_current_object() >> o 249 __and__ = lambda x, o: x._get_current_object() & o 250 __xor__ = lambda x, o: x._get_current_object() ^ o 251 __or__ = lambda x, o: x._get_current_object() | o 252 __div__ = lambda x, o: x._get_current_object().__div__(o) 253 __truediv__ = lambda x, o: x._get_current_object().__truediv__(o) 254 __neg__ = lambda x: -x._get_current_object() 255 __pos__ = lambda x: +x._get_current_object() 256 __abs__ = lambda x: abs(x._get_current_object()) 257 __invert__ = lambda x: ~x._get_current_object() 258 __complex__ = lambda x: complex(x._get_current_object()) 259 __int__ = lambda x: int(x._get_current_object()) 260 __long__ = lambda x: long(x._get_current_object()) 261 __float__ = lambda x: float(x._get_current_object()) 262 __oct__ = lambda x: oct(x._get_current_object()) 263 __hex__ = lambda x: hex(x._get_current_object()) 264 __index__ = lambda x: x._get_current_object().__index__() 265 __coerce__ = lambda x, o: x.__coerce__(x, o) 266 __enter__ = lambda x: x.__enter__() 267 __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)