1
0

local.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug.local
  4. ~~~~~~~~~~~~~~
  5. This module implements context-local objects.
  6. :copyright: 2007 Pallets
  7. :license: BSD-3-Clause
  8. """
  9. import copy
  10. from functools import update_wrapper
  11. from ._compat import implements_bool
  12. from ._compat import PY2
  13. from .wsgi import ClosingIterator
  14. # since each thread has its own greenlet we can just use those as identifiers
  15. # for the context. If greenlets are not available we fall back to the
  16. # current thread ident depending on where it is.
  17. try:
  18. from greenlet import getcurrent as get_ident
  19. except ImportError:
  20. try:
  21. from thread import get_ident
  22. except ImportError:
  23. from _thread import get_ident
  24. def release_local(local):
  25. """Releases the contents of the local for the current context.
  26. This makes it possible to use locals without a manager.
  27. Example::
  28. >>> loc = Local()
  29. >>> loc.foo = 42
  30. >>> release_local(loc)
  31. >>> hasattr(loc, 'foo')
  32. False
  33. With this function one can release :class:`Local` objects as well
  34. as :class:`LocalStack` objects. However it is not possible to
  35. release data held by proxies that way, one always has to retain
  36. a reference to the underlying local object in order to be able
  37. to release it.
  38. .. versionadded:: 0.6.1
  39. """
  40. local.__release_local__()
  41. class Local(object):
  42. __slots__ = ("__storage__", "__ident_func__")
  43. def __init__(self):
  44. object.__setattr__(self, "__storage__", {})
  45. object.__setattr__(self, "__ident_func__", get_ident)
  46. def __iter__(self):
  47. return iter(self.__storage__.items())
  48. def __call__(self, proxy):
  49. """Create a proxy for a name."""
  50. return LocalProxy(self, proxy)
  51. def __release_local__(self):
  52. self.__storage__.pop(self.__ident_func__(), None)
  53. def __getattr__(self, name):
  54. try:
  55. return self.__storage__[self.__ident_func__()][name]
  56. except KeyError:
  57. raise AttributeError(name)
  58. def __setattr__(self, name, value):
  59. ident = self.__ident_func__()
  60. storage = self.__storage__
  61. try:
  62. storage[ident][name] = value
  63. except KeyError:
  64. storage[ident] = {name: value}
  65. def __delattr__(self, name):
  66. try:
  67. del self.__storage__[self.__ident_func__()][name]
  68. except KeyError:
  69. raise AttributeError(name)
  70. class LocalStack(object):
  71. """This class works similar to a :class:`Local` but keeps a stack
  72. of objects instead. This is best explained with an example::
  73. >>> ls = LocalStack()
  74. >>> ls.push(42)
  75. >>> ls.top
  76. 42
  77. >>> ls.push(23)
  78. >>> ls.top
  79. 23
  80. >>> ls.pop()
  81. 23
  82. >>> ls.top
  83. 42
  84. They can be force released by using a :class:`LocalManager` or with
  85. the :func:`release_local` function but the correct way is to pop the
  86. item from the stack after using. When the stack is empty it will
  87. no longer be bound to the current context (and as such released).
  88. By calling the stack without arguments it returns a proxy that resolves to
  89. the topmost item on the stack.
  90. .. versionadded:: 0.6.1
  91. """
  92. def __init__(self):
  93. self._local = Local()
  94. def __release_local__(self):
  95. self._local.__release_local__()
  96. @property
  97. def __ident_func__(self):
  98. return self._local.__ident_func__
  99. @__ident_func__.setter
  100. def __ident_func__(self, value):
  101. object.__setattr__(self._local, "__ident_func__", value)
  102. def __call__(self):
  103. def _lookup():
  104. rv = self.top
  105. if rv is None:
  106. raise RuntimeError("object unbound")
  107. return rv
  108. return LocalProxy(_lookup)
  109. def push(self, obj):
  110. """Pushes a new item to the stack"""
  111. rv = getattr(self._local, "stack", None)
  112. if rv is None:
  113. self._local.stack = rv = []
  114. rv.append(obj)
  115. return rv
  116. def pop(self):
  117. """Removes the topmost item from the stack, will return the
  118. old value or `None` if the stack was already empty.
  119. """
  120. stack = getattr(self._local, "stack", None)
  121. if stack is None:
  122. return None
  123. elif len(stack) == 1:
  124. release_local(self._local)
  125. return stack[-1]
  126. else:
  127. return stack.pop()
  128. @property
  129. def top(self):
  130. """The topmost item on the stack. If the stack is empty,
  131. `None` is returned.
  132. """
  133. try:
  134. return self._local.stack[-1]
  135. except (AttributeError, IndexError):
  136. return None
  137. class LocalManager(object):
  138. """Local objects cannot manage themselves. For that you need a local
  139. manager. You can pass a local manager multiple locals or add them later
  140. by appending them to `manager.locals`. Every time the manager cleans up,
  141. it will clean up all the data left in the locals for this context.
  142. The `ident_func` parameter can be added to override the default ident
  143. function for the wrapped locals.
  144. .. versionchanged:: 0.6.1
  145. Instead of a manager the :func:`release_local` function can be used
  146. as well.
  147. .. versionchanged:: 0.7
  148. `ident_func` was added.
  149. """
  150. def __init__(self, locals=None, ident_func=None):
  151. if locals is None:
  152. self.locals = []
  153. elif isinstance(locals, Local):
  154. self.locals = [locals]
  155. else:
  156. self.locals = list(locals)
  157. if ident_func is not None:
  158. self.ident_func = ident_func
  159. for local in self.locals:
  160. object.__setattr__(local, "__ident_func__", ident_func)
  161. else:
  162. self.ident_func = get_ident
  163. def get_ident(self):
  164. """Return the context identifier the local objects use internally for
  165. this context. You cannot override this method to change the behavior
  166. but use it to link other context local objects (such as SQLAlchemy's
  167. scoped sessions) to the Werkzeug locals.
  168. .. versionchanged:: 0.7
  169. You can pass a different ident function to the local manager that
  170. will then be propagated to all the locals passed to the
  171. constructor.
  172. """
  173. return self.ident_func()
  174. def cleanup(self):
  175. """Manually clean up the data in the locals for this context. Call
  176. this at the end of the request or use `make_middleware()`.
  177. """
  178. for local in self.locals:
  179. release_local(local)
  180. def make_middleware(self, app):
  181. """Wrap a WSGI application so that cleaning up happens after
  182. request end.
  183. """
  184. def application(environ, start_response):
  185. return ClosingIterator(app(environ, start_response), self.cleanup)
  186. return application
  187. def middleware(self, func):
  188. """Like `make_middleware` but for decorating functions.
  189. Example usage::
  190. @manager.middleware
  191. def application(environ, start_response):
  192. ...
  193. The difference to `make_middleware` is that the function passed
  194. will have all the arguments copied from the inner application
  195. (name, docstring, module).
  196. """
  197. return update_wrapper(self.make_middleware(func), func)
  198. def __repr__(self):
  199. return "<%s storages: %d>" % (self.__class__.__name__, len(self.locals))
  200. @implements_bool
  201. class LocalProxy(object):
  202. """Acts as a proxy for a werkzeug local. Forwards all operations to
  203. a proxied object. The only operations not supported for forwarding
  204. are right handed operands and any kind of assignment.
  205. Example usage::
  206. from werkzeug.local import Local
  207. l = Local()
  208. # these are proxies
  209. request = l('request')
  210. user = l('user')
  211. from werkzeug.local import LocalStack
  212. _response_local = LocalStack()
  213. # this is a proxy
  214. response = _response_local()
  215. Whenever something is bound to l.user / l.request the proxy objects
  216. will forward all operations. If no object is bound a :exc:`RuntimeError`
  217. will be raised.
  218. To create proxies to :class:`Local` or :class:`LocalStack` objects,
  219. call the object as shown above. If you want to have a proxy to an
  220. object looked up by a function, you can (as of Werkzeug 0.6.1) pass
  221. a function to the :class:`LocalProxy` constructor::
  222. session = LocalProxy(lambda: get_current_request().session)
  223. .. versionchanged:: 0.6.1
  224. The class can be instantiated with a callable as well now.
  225. """
  226. __slots__ = ("__local", "__dict__", "__name__", "__wrapped__")
  227. def __init__(self, local, name=None):
  228. object.__setattr__(self, "_LocalProxy__local", local)
  229. object.__setattr__(self, "__name__", name)
  230. if callable(local) and not hasattr(local, "__release_local__"):
  231. # "local" is a callable that is not an instance of Local or
  232. # LocalManager: mark it as a wrapped function.
  233. object.__setattr__(self, "__wrapped__", local)
  234. def _get_current_object(self):
  235. """Return the current object. This is useful if you want the real
  236. object behind the proxy at a time for performance reasons or because
  237. you want to pass the object into a different context.
  238. """
  239. if not hasattr(self.__local, "__release_local__"):
  240. return self.__local()
  241. try:
  242. return getattr(self.__local, self.__name__)
  243. except AttributeError:
  244. raise RuntimeError("no object bound to %s" % self.__name__)
  245. @property
  246. def __dict__(self):
  247. try:
  248. return self._get_current_object().__dict__
  249. except RuntimeError:
  250. raise AttributeError("__dict__")
  251. def __repr__(self):
  252. try:
  253. obj = self._get_current_object()
  254. except RuntimeError:
  255. return "<%s unbound>" % self.__class__.__name__
  256. return repr(obj)
  257. def __bool__(self):
  258. try:
  259. return bool(self._get_current_object())
  260. except RuntimeError:
  261. return False
  262. def __unicode__(self):
  263. try:
  264. return unicode(self._get_current_object()) # noqa
  265. except RuntimeError:
  266. return repr(self)
  267. def __dir__(self):
  268. try:
  269. return dir(self._get_current_object())
  270. except RuntimeError:
  271. return []
  272. def __getattr__(self, name):
  273. if name == "__members__":
  274. return dir(self._get_current_object())
  275. return getattr(self._get_current_object(), name)
  276. def __setitem__(self, key, value):
  277. self._get_current_object()[key] = value
  278. def __delitem__(self, key):
  279. del self._get_current_object()[key]
  280. if PY2:
  281. __getslice__ = lambda x, i, j: x._get_current_object()[i:j]
  282. def __setslice__(self, i, j, seq):
  283. self._get_current_object()[i:j] = seq
  284. def __delslice__(self, i, j):
  285. del self._get_current_object()[i:j]
  286. __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
  287. __delattr__ = lambda x, n: delattr(x._get_current_object(), n)
  288. __str__ = lambda x: str(x._get_current_object())
  289. __lt__ = lambda x, o: x._get_current_object() < o
  290. __le__ = lambda x, o: x._get_current_object() <= o
  291. __eq__ = lambda x, o: x._get_current_object() == o
  292. __ne__ = lambda x, o: x._get_current_object() != o
  293. __gt__ = lambda x, o: x._get_current_object() > o
  294. __ge__ = lambda x, o: x._get_current_object() >= o
  295. __cmp__ = lambda x, o: cmp(x._get_current_object(), o) # noqa
  296. __hash__ = lambda x: hash(x._get_current_object())
  297. __call__ = lambda x, *a, **kw: x._get_current_object()(*a, **kw)
  298. __len__ = lambda x: len(x._get_current_object())
  299. __getitem__ = lambda x, i: x._get_current_object()[i]
  300. __iter__ = lambda x: iter(x._get_current_object())
  301. __contains__ = lambda x, i: i in x._get_current_object()
  302. __add__ = lambda x, o: x._get_current_object() + o
  303. __sub__ = lambda x, o: x._get_current_object() - o
  304. __mul__ = lambda x, o: x._get_current_object() * o
  305. __floordiv__ = lambda x, o: x._get_current_object() // o
  306. __mod__ = lambda x, o: x._get_current_object() % o
  307. __divmod__ = lambda x, o: x._get_current_object().__divmod__(o)
  308. __pow__ = lambda x, o: x._get_current_object() ** o
  309. __lshift__ = lambda x, o: x._get_current_object() << o
  310. __rshift__ = lambda x, o: x._get_current_object() >> o
  311. __and__ = lambda x, o: x._get_current_object() & o
  312. __xor__ = lambda x, o: x._get_current_object() ^ o
  313. __or__ = lambda x, o: x._get_current_object() | o
  314. __div__ = lambda x, o: x._get_current_object().__div__(o)
  315. __truediv__ = lambda x, o: x._get_current_object().__truediv__(o)
  316. __neg__ = lambda x: -(x._get_current_object())
  317. __pos__ = lambda x: +(x._get_current_object())
  318. __abs__ = lambda x: abs(x._get_current_object())
  319. __invert__ = lambda x: ~(x._get_current_object())
  320. __complex__ = lambda x: complex(x._get_current_object())
  321. __int__ = lambda x: int(x._get_current_object())
  322. __long__ = lambda x: long(x._get_current_object()) # noqa
  323. __float__ = lambda x: float(x._get_current_object())
  324. __oct__ = lambda x: oct(x._get_current_object())
  325. __hex__ = lambda x: hex(x._get_current_object())
  326. __index__ = lambda x: x._get_current_object().__index__()
  327. __coerce__ = lambda x, o: x._get_current_object().__coerce__(x, o)
  328. __enter__ = lambda x: x._get_current_object().__enter__()
  329. __exit__ = lambda x, *a, **kw: x._get_current_object().__exit__(*a, **kw)
  330. __radd__ = lambda x, o: o + x._get_current_object()
  331. __rsub__ = lambda x, o: o - x._get_current_object()
  332. __rmul__ = lambda x, o: o * x._get_current_object()
  333. __rdiv__ = lambda x, o: o / x._get_current_object()
  334. if PY2:
  335. __rtruediv__ = lambda x, o: x._get_current_object().__rtruediv__(o)
  336. else:
  337. __rtruediv__ = __rdiv__
  338. __rfloordiv__ = lambda x, o: o // x._get_current_object()
  339. __rmod__ = lambda x, o: o % x._get_current_object()
  340. __rdivmod__ = lambda x, o: x._get_current_object().__rdivmod__(o)
  341. __copy__ = lambda x: copy.copy(x._get_current_object())
  342. __deepcopy__ = lambda x, memo: copy.deepcopy(x._get_current_object(), memo)