local.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. from __future__ import annotations
  2. import copy
  3. import math
  4. import operator
  5. import typing as t
  6. from contextvars import ContextVar
  7. from functools import partial
  8. from functools import update_wrapper
  9. from operator import attrgetter
  10. from .wsgi import ClosingIterator
  11. if t.TYPE_CHECKING:
  12. from _typeshed.wsgi import StartResponse
  13. from _typeshed.wsgi import WSGIApplication
  14. from _typeshed.wsgi import WSGIEnvironment
  15. T = t.TypeVar("T")
  16. F = t.TypeVar("F", bound=t.Callable[..., t.Any])
  17. def release_local(local: Local | LocalStack[t.Any]) -> None:
  18. """Release the data for the current context in a :class:`Local` or
  19. :class:`LocalStack` without using a :class:`LocalManager`.
  20. This should not be needed for modern use cases, and may be removed
  21. in the future.
  22. .. versionadded:: 0.6.1
  23. """
  24. local.__release_local__()
  25. class Local:
  26. """Create a namespace of context-local data. This wraps a
  27. :class:`ContextVar` containing a :class:`dict` value.
  28. This may incur a performance penalty compared to using individual
  29. context vars, as it has to copy data to avoid mutating the dict
  30. between nested contexts.
  31. :param context_var: The :class:`~contextvars.ContextVar` to use as
  32. storage for this local. If not given, one will be created.
  33. Context vars not created at the global scope may interfere with
  34. garbage collection.
  35. .. versionchanged:: 2.0
  36. Uses ``ContextVar`` instead of a custom storage implementation.
  37. """
  38. __slots__ = ("__storage",)
  39. def __init__(self, context_var: ContextVar[dict[str, t.Any]] | None = None) -> None:
  40. if context_var is None:
  41. # A ContextVar not created at global scope interferes with
  42. # Python's garbage collection. However, a local only makes
  43. # sense defined at the global scope as well, in which case
  44. # the GC issue doesn't seem relevant.
  45. context_var = ContextVar(f"werkzeug.Local<{id(self)}>.storage")
  46. object.__setattr__(self, "_Local__storage", context_var)
  47. def __iter__(self) -> t.Iterator[tuple[str, t.Any]]:
  48. return iter(self.__storage.get({}).items())
  49. def __call__(
  50. self, name: str, *, unbound_message: str | None = None
  51. ) -> LocalProxy[t.Any]:
  52. """Create a :class:`LocalProxy` that access an attribute on this
  53. local namespace.
  54. :param name: Proxy this attribute.
  55. :param unbound_message: The error message that the proxy will
  56. show if the attribute isn't set.
  57. """
  58. return LocalProxy(self, name, unbound_message=unbound_message)
  59. def __release_local__(self) -> None:
  60. self.__storage.set({})
  61. def __getattr__(self, name: str) -> t.Any:
  62. values = self.__storage.get({})
  63. if name in values:
  64. return values[name]
  65. raise AttributeError(name)
  66. def __setattr__(self, name: str, value: t.Any) -> None:
  67. values = self.__storage.get({}).copy()
  68. values[name] = value
  69. self.__storage.set(values)
  70. def __delattr__(self, name: str) -> None:
  71. values = self.__storage.get({})
  72. if name in values:
  73. values = values.copy()
  74. del values[name]
  75. self.__storage.set(values)
  76. else:
  77. raise AttributeError(name)
  78. class LocalStack(t.Generic[T]):
  79. """Create a stack of context-local data. This wraps a
  80. :class:`ContextVar` containing a :class:`list` value.
  81. This may incur a performance penalty compared to using individual
  82. context vars, as it has to copy data to avoid mutating the list
  83. between nested contexts.
  84. :param context_var: The :class:`~contextvars.ContextVar` to use as
  85. storage for this local. If not given, one will be created.
  86. Context vars not created at the global scope may interfere with
  87. garbage collection.
  88. .. versionchanged:: 2.0
  89. Uses ``ContextVar`` instead of a custom storage implementation.
  90. .. versionadded:: 0.6.1
  91. """
  92. __slots__ = ("_storage",)
  93. def __init__(self, context_var: ContextVar[list[T]] | None = None) -> None:
  94. if context_var is None:
  95. # A ContextVar not created at global scope interferes with
  96. # Python's garbage collection. However, a local only makes
  97. # sense defined at the global scope as well, in which case
  98. # the GC issue doesn't seem relevant.
  99. context_var = ContextVar(f"werkzeug.LocalStack<{id(self)}>.storage")
  100. self._storage = context_var
  101. def __release_local__(self) -> None:
  102. self._storage.set([])
  103. def push(self, obj: T) -> list[T]:
  104. """Add a new item to the top of the stack."""
  105. stack = self._storage.get([]).copy()
  106. stack.append(obj)
  107. self._storage.set(stack)
  108. return stack
  109. def pop(self) -> T | None:
  110. """Remove the top item from the stack and return it. If the
  111. stack is empty, return ``None``.
  112. """
  113. stack = self._storage.get([])
  114. if len(stack) == 0:
  115. return None
  116. rv = stack[-1]
  117. self._storage.set(stack[:-1])
  118. return rv
  119. @property
  120. def top(self) -> T | None:
  121. """The topmost item on the stack. If the stack is empty,
  122. `None` is returned.
  123. """
  124. stack = self._storage.get([])
  125. if len(stack) == 0:
  126. return None
  127. return stack[-1]
  128. def __call__(
  129. self, name: str | None = None, *, unbound_message: str | None = None
  130. ) -> LocalProxy[t.Any]:
  131. """Create a :class:`LocalProxy` that accesses the top of this
  132. local stack.
  133. :param name: If given, the proxy access this attribute of the
  134. top item, rather than the item itself.
  135. :param unbound_message: The error message that the proxy will
  136. show if the stack is empty.
  137. """
  138. return LocalProxy(self, name, unbound_message=unbound_message)
  139. class LocalManager:
  140. """Manage releasing the data for the current context in one or more
  141. :class:`Local` and :class:`LocalStack` objects.
  142. This should not be needed for modern use cases, and may be removed
  143. in the future.
  144. :param locals: A local or list of locals to manage.
  145. .. versionchanged:: 2.1
  146. The ``ident_func`` was removed.
  147. .. versionchanged:: 0.7
  148. The ``ident_func`` parameter was added.
  149. .. versionchanged:: 0.6.1
  150. The :func:`release_local` function can be used instead of a
  151. manager.
  152. """
  153. __slots__ = ("locals",)
  154. def __init__(
  155. self,
  156. locals: None
  157. | (Local | LocalStack[t.Any] | t.Iterable[Local | LocalStack[t.Any]]) = None,
  158. ) -> None:
  159. if locals is None:
  160. self.locals = []
  161. elif isinstance(locals, Local):
  162. self.locals = [locals]
  163. else:
  164. self.locals = list(locals) # type: ignore[arg-type]
  165. def cleanup(self) -> None:
  166. """Release the data in the locals for this context. Call this at
  167. the end of each request or use :meth:`make_middleware`.
  168. """
  169. for local in self.locals:
  170. release_local(local)
  171. def make_middleware(self, app: WSGIApplication) -> WSGIApplication:
  172. """Wrap a WSGI application so that local data is released
  173. automatically after the response has been sent for a request.
  174. """
  175. def application(
  176. environ: WSGIEnvironment, start_response: StartResponse
  177. ) -> t.Iterable[bytes]:
  178. return ClosingIterator(app(environ, start_response), self.cleanup)
  179. return application
  180. def middleware(self, func: WSGIApplication) -> WSGIApplication:
  181. """Like :meth:`make_middleware` but used as a decorator on the
  182. WSGI application function.
  183. .. code-block:: python
  184. @manager.middleware
  185. def application(environ, start_response):
  186. ...
  187. """
  188. return update_wrapper(self.make_middleware(func), func)
  189. def __repr__(self) -> str:
  190. return f"<{type(self).__name__} storages: {len(self.locals)}>"
  191. class _ProxyLookup:
  192. """Descriptor that handles proxied attribute lookup for
  193. :class:`LocalProxy`.
  194. :param f: The built-in function this attribute is accessed through.
  195. Instead of looking up the special method, the function call
  196. is redone on the object.
  197. :param fallback: Return this function if the proxy is unbound
  198. instead of raising a :exc:`RuntimeError`.
  199. :param is_attr: This proxied name is an attribute, not a function.
  200. Call the fallback immediately to get the value.
  201. :param class_value: Value to return when accessed from the
  202. ``LocalProxy`` class directly. Used for ``__doc__`` so building
  203. docs still works.
  204. """
  205. __slots__ = ("bind_f", "fallback", "is_attr", "class_value", "name")
  206. def __init__(
  207. self,
  208. f: t.Callable[..., t.Any] | None = None,
  209. fallback: t.Callable[[LocalProxy[t.Any]], t.Any] | None = None,
  210. class_value: t.Any | None = None,
  211. is_attr: bool = False,
  212. ) -> None:
  213. bind_f: t.Callable[[LocalProxy[t.Any], t.Any], t.Callable[..., t.Any]] | None
  214. if hasattr(f, "__get__"):
  215. # A Python function, can be turned into a bound method.
  216. def bind_f(
  217. instance: LocalProxy[t.Any], obj: t.Any
  218. ) -> t.Callable[..., t.Any]:
  219. return f.__get__(obj, type(obj)) # type: ignore
  220. elif f is not None:
  221. # A C function, use partial to bind the first argument.
  222. def bind_f(
  223. instance: LocalProxy[t.Any], obj: t.Any
  224. ) -> t.Callable[..., t.Any]:
  225. return partial(f, obj)
  226. else:
  227. # Use getattr, which will produce a bound method.
  228. bind_f = None
  229. self.bind_f = bind_f
  230. self.fallback = fallback
  231. self.class_value = class_value
  232. self.is_attr = is_attr
  233. def __set_name__(self, owner: LocalProxy[t.Any], name: str) -> None:
  234. self.name = name
  235. def __get__(self, instance: LocalProxy[t.Any], owner: type | None = None) -> t.Any:
  236. if instance is None:
  237. if self.class_value is not None:
  238. return self.class_value
  239. return self
  240. try:
  241. obj = instance._get_current_object()
  242. except RuntimeError:
  243. if self.fallback is None:
  244. raise
  245. fallback = self.fallback.__get__(instance, owner)
  246. if self.is_attr:
  247. # __class__ and __doc__ are attributes, not methods.
  248. # Call the fallback to get the value.
  249. return fallback()
  250. return fallback
  251. if self.bind_f is not None:
  252. return self.bind_f(instance, obj)
  253. return getattr(obj, self.name)
  254. def __repr__(self) -> str:
  255. return f"proxy {self.name}"
  256. def __call__(
  257. self, instance: LocalProxy[t.Any], *args: t.Any, **kwargs: t.Any
  258. ) -> t.Any:
  259. """Support calling unbound methods from the class. For example,
  260. this happens with ``copy.copy``, which does
  261. ``type(x).__copy__(x)``. ``type(x)`` can't be proxied, so it
  262. returns the proxy type and descriptor.
  263. """
  264. return self.__get__(instance, type(instance))(*args, **kwargs)
  265. class _ProxyIOp(_ProxyLookup):
  266. """Look up an augmented assignment method on a proxied object. The
  267. method is wrapped to return the proxy instead of the object.
  268. """
  269. __slots__ = ()
  270. def __init__(
  271. self,
  272. f: t.Callable[..., t.Any] | None = None,
  273. fallback: t.Callable[[LocalProxy[t.Any]], t.Any] | None = None,
  274. ) -> None:
  275. super().__init__(f, fallback)
  276. def bind_f(instance: LocalProxy[t.Any], obj: t.Any) -> t.Callable[..., t.Any]:
  277. def i_op(self: t.Any, other: t.Any) -> LocalProxy[t.Any]:
  278. f(self, other) # type: ignore
  279. return instance
  280. return i_op.__get__(obj, type(obj)) # type: ignore
  281. self.bind_f = bind_f
  282. def _l_to_r_op(op: F) -> F:
  283. """Swap the argument order to turn an l-op into an r-op."""
  284. def r_op(obj: t.Any, other: t.Any) -> t.Any:
  285. return op(other, obj)
  286. return t.cast(F, r_op)
  287. def _identity(o: T) -> T:
  288. return o
  289. class LocalProxy(t.Generic[T]):
  290. """A proxy to the object bound to a context-local object. All
  291. operations on the proxy are forwarded to the bound object. If no
  292. object is bound, a ``RuntimeError`` is raised.
  293. :param local: The context-local object that provides the proxied
  294. object.
  295. :param name: Proxy this attribute from the proxied object.
  296. :param unbound_message: The error message to show if the
  297. context-local object is unbound.
  298. Proxy a :class:`~contextvars.ContextVar` to make it easier to
  299. access. Pass a name to proxy that attribute.
  300. .. code-block:: python
  301. _request_var = ContextVar("request")
  302. request = LocalProxy(_request_var)
  303. session = LocalProxy(_request_var, "session")
  304. Proxy an attribute on a :class:`Local` namespace by calling the
  305. local with the attribute name:
  306. .. code-block:: python
  307. data = Local()
  308. user = data("user")
  309. Proxy the top item on a :class:`LocalStack` by calling the local.
  310. Pass a name to proxy that attribute.
  311. .. code-block::
  312. app_stack = LocalStack()
  313. current_app = app_stack()
  314. g = app_stack("g")
  315. Pass a function to proxy the return value from that function. This
  316. was previously used to access attributes of local objects before
  317. that was supported directly.
  318. .. code-block:: python
  319. session = LocalProxy(lambda: request.session)
  320. ``__repr__`` and ``__class__`` are proxied, so ``repr(x)`` and
  321. ``isinstance(x, cls)`` will look like the proxied object. Use
  322. ``issubclass(type(x), LocalProxy)`` to check if an object is a
  323. proxy.
  324. .. code-block:: python
  325. repr(user) # <User admin>
  326. isinstance(user, User) # True
  327. issubclass(type(user), LocalProxy) # True
  328. .. versionchanged:: 2.2.2
  329. ``__wrapped__`` is set when wrapping an object, not only when
  330. wrapping a function, to prevent doctest from failing.
  331. .. versionchanged:: 2.2
  332. Can proxy a ``ContextVar`` or ``LocalStack`` directly.
  333. .. versionchanged:: 2.2
  334. The ``name`` parameter can be used with any proxied object, not
  335. only ``Local``.
  336. .. versionchanged:: 2.2
  337. Added the ``unbound_message`` parameter.
  338. .. versionchanged:: 2.0
  339. Updated proxied attributes and methods to reflect the current
  340. data model.
  341. .. versionchanged:: 0.6.1
  342. The class can be instantiated with a callable.
  343. """
  344. __slots__ = ("__wrapped", "_get_current_object")
  345. _get_current_object: t.Callable[[], T]
  346. """Return the current object this proxy is bound to. If the proxy is
  347. unbound, this raises a ``RuntimeError``.
  348. This should be used if you need to pass the object to something that
  349. doesn't understand the proxy. It can also be useful for performance
  350. if you are accessing the object multiple times in a function, rather
  351. than going through the proxy multiple times.
  352. """
  353. def __init__(
  354. self,
  355. local: ContextVar[T] | Local | LocalStack[T] | t.Callable[[], T],
  356. name: str | None = None,
  357. *,
  358. unbound_message: str | None = None,
  359. ) -> None:
  360. if name is None:
  361. get_name = _identity
  362. else:
  363. get_name = attrgetter(name) # type: ignore[assignment]
  364. if unbound_message is None:
  365. unbound_message = "object is not bound"
  366. if isinstance(local, Local):
  367. if name is None:
  368. raise TypeError("'name' is required when proxying a 'Local' object.")
  369. def _get_current_object() -> T:
  370. try:
  371. return get_name(local) # type: ignore[return-value]
  372. except AttributeError:
  373. raise RuntimeError(unbound_message) from None
  374. elif isinstance(local, LocalStack):
  375. def _get_current_object() -> T:
  376. obj = local.top
  377. if obj is None:
  378. raise RuntimeError(unbound_message)
  379. return get_name(obj)
  380. elif isinstance(local, ContextVar):
  381. def _get_current_object() -> T:
  382. try:
  383. obj = local.get()
  384. except LookupError:
  385. raise RuntimeError(unbound_message) from None
  386. return get_name(obj)
  387. elif callable(local):
  388. def _get_current_object() -> T:
  389. return get_name(local())
  390. else:
  391. raise TypeError(f"Don't know how to proxy '{type(local)}'.")
  392. object.__setattr__(self, "_LocalProxy__wrapped", local)
  393. object.__setattr__(self, "_get_current_object", _get_current_object)
  394. __doc__ = _ProxyLookup( # type: ignore[assignment]
  395. class_value=__doc__, fallback=lambda self: type(self).__doc__, is_attr=True
  396. )
  397. __wrapped__ = _ProxyLookup(
  398. fallback=lambda self: self._LocalProxy__wrapped, # type: ignore[attr-defined]
  399. is_attr=True,
  400. )
  401. # __del__ should only delete the proxy
  402. __repr__ = _ProxyLookup( # type: ignore[assignment]
  403. repr, fallback=lambda self: f"<{type(self).__name__} unbound>"
  404. )
  405. __str__ = _ProxyLookup(str) # type: ignore[assignment]
  406. __bytes__ = _ProxyLookup(bytes)
  407. __format__ = _ProxyLookup() # type: ignore[assignment]
  408. __lt__ = _ProxyLookup(operator.lt)
  409. __le__ = _ProxyLookup(operator.le)
  410. __eq__ = _ProxyLookup(operator.eq) # type: ignore[assignment]
  411. __ne__ = _ProxyLookup(operator.ne) # type: ignore[assignment]
  412. __gt__ = _ProxyLookup(operator.gt)
  413. __ge__ = _ProxyLookup(operator.ge)
  414. __hash__ = _ProxyLookup(hash) # type: ignore[assignment]
  415. __bool__ = _ProxyLookup(bool, fallback=lambda self: False)
  416. __getattr__ = _ProxyLookup(getattr)
  417. # __getattribute__ triggered through __getattr__
  418. __setattr__ = _ProxyLookup(setattr) # type: ignore[assignment]
  419. __delattr__ = _ProxyLookup(delattr) # type: ignore[assignment]
  420. __dir__ = _ProxyLookup(dir, fallback=lambda self: []) # type: ignore[assignment]
  421. # __get__ (proxying descriptor not supported)
  422. # __set__ (descriptor)
  423. # __delete__ (descriptor)
  424. # __set_name__ (descriptor)
  425. # __objclass__ (descriptor)
  426. # __slots__ used by proxy itself
  427. # __dict__ (__getattr__)
  428. # __weakref__ (__getattr__)
  429. # __init_subclass__ (proxying metaclass not supported)
  430. # __prepare__ (metaclass)
  431. __class__ = _ProxyLookup(fallback=lambda self: type(self), is_attr=True) # type: ignore[assignment]
  432. __instancecheck__ = _ProxyLookup(lambda self, other: isinstance(other, self))
  433. __subclasscheck__ = _ProxyLookup(lambda self, other: issubclass(other, self))
  434. # __class_getitem__ triggered through __getitem__
  435. __call__ = _ProxyLookup(lambda self, *args, **kwargs: self(*args, **kwargs))
  436. __len__ = _ProxyLookup(len)
  437. __length_hint__ = _ProxyLookup(operator.length_hint)
  438. __getitem__ = _ProxyLookup(operator.getitem)
  439. __setitem__ = _ProxyLookup(operator.setitem)
  440. __delitem__ = _ProxyLookup(operator.delitem)
  441. # __missing__ triggered through __getitem__
  442. __iter__ = _ProxyLookup(iter)
  443. __next__ = _ProxyLookup(next)
  444. __reversed__ = _ProxyLookup(reversed)
  445. __contains__ = _ProxyLookup(operator.contains)
  446. __add__ = _ProxyLookup(operator.add)
  447. __sub__ = _ProxyLookup(operator.sub)
  448. __mul__ = _ProxyLookup(operator.mul)
  449. __matmul__ = _ProxyLookup(operator.matmul)
  450. __truediv__ = _ProxyLookup(operator.truediv)
  451. __floordiv__ = _ProxyLookup(operator.floordiv)
  452. __mod__ = _ProxyLookup(operator.mod)
  453. __divmod__ = _ProxyLookup(divmod)
  454. __pow__ = _ProxyLookup(pow)
  455. __lshift__ = _ProxyLookup(operator.lshift)
  456. __rshift__ = _ProxyLookup(operator.rshift)
  457. __and__ = _ProxyLookup(operator.and_)
  458. __xor__ = _ProxyLookup(operator.xor)
  459. __or__ = _ProxyLookup(operator.or_)
  460. __radd__ = _ProxyLookup(_l_to_r_op(operator.add))
  461. __rsub__ = _ProxyLookup(_l_to_r_op(operator.sub))
  462. __rmul__ = _ProxyLookup(_l_to_r_op(operator.mul))
  463. __rmatmul__ = _ProxyLookup(_l_to_r_op(operator.matmul))
  464. __rtruediv__ = _ProxyLookup(_l_to_r_op(operator.truediv))
  465. __rfloordiv__ = _ProxyLookup(_l_to_r_op(operator.floordiv))
  466. __rmod__ = _ProxyLookup(_l_to_r_op(operator.mod))
  467. __rdivmod__ = _ProxyLookup(_l_to_r_op(divmod))
  468. __rpow__ = _ProxyLookup(_l_to_r_op(pow))
  469. __rlshift__ = _ProxyLookup(_l_to_r_op(operator.lshift))
  470. __rrshift__ = _ProxyLookup(_l_to_r_op(operator.rshift))
  471. __rand__ = _ProxyLookup(_l_to_r_op(operator.and_))
  472. __rxor__ = _ProxyLookup(_l_to_r_op(operator.xor))
  473. __ror__ = _ProxyLookup(_l_to_r_op(operator.or_))
  474. __iadd__ = _ProxyIOp(operator.iadd)
  475. __isub__ = _ProxyIOp(operator.isub)
  476. __imul__ = _ProxyIOp(operator.imul)
  477. __imatmul__ = _ProxyIOp(operator.imatmul)
  478. __itruediv__ = _ProxyIOp(operator.itruediv)
  479. __ifloordiv__ = _ProxyIOp(operator.ifloordiv)
  480. __imod__ = _ProxyIOp(operator.imod)
  481. __ipow__ = _ProxyIOp(operator.ipow)
  482. __ilshift__ = _ProxyIOp(operator.ilshift)
  483. __irshift__ = _ProxyIOp(operator.irshift)
  484. __iand__ = _ProxyIOp(operator.iand)
  485. __ixor__ = _ProxyIOp(operator.ixor)
  486. __ior__ = _ProxyIOp(operator.ior)
  487. __neg__ = _ProxyLookup(operator.neg)
  488. __pos__ = _ProxyLookup(operator.pos)
  489. __abs__ = _ProxyLookup(abs)
  490. __invert__ = _ProxyLookup(operator.invert)
  491. __complex__ = _ProxyLookup(complex)
  492. __int__ = _ProxyLookup(int)
  493. __float__ = _ProxyLookup(float)
  494. __index__ = _ProxyLookup(operator.index)
  495. __round__ = _ProxyLookup(round)
  496. __trunc__ = _ProxyLookup(math.trunc)
  497. __floor__ = _ProxyLookup(math.floor)
  498. __ceil__ = _ProxyLookup(math.ceil)
  499. __enter__ = _ProxyLookup()
  500. __exit__ = _ProxyLookup()
  501. __await__ = _ProxyLookup()
  502. __aiter__ = _ProxyLookup()
  503. __anext__ = _ProxyLookup()
  504. __aenter__ = _ProxyLookup()
  505. __aexit__ = _ProxyLookup()
  506. __copy__ = _ProxyLookup(copy.copy)
  507. __deepcopy__ = _ProxyLookup(copy.deepcopy)
  508. # __getnewargs_ex__ (pickle through proxy not supported)
  509. # __getnewargs__ (pickle)
  510. # __getstate__ (pickle)
  511. # __setstate__ (pickle)
  512. # __reduce__ (pickle)
  513. # __reduce_ex__ (pickle)