contextlib.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. """Utilities for with-statement contexts. See PEP 343."""
  2. import abc
  3. import sys
  4. import _collections_abc
  5. from collections import deque
  6. from functools import wraps
  7. from types import MethodType, GenericAlias
  8. __all__ = ["asynccontextmanager", "contextmanager", "closing", "nullcontext",
  9. "AbstractContextManager", "AbstractAsyncContextManager",
  10. "AsyncExitStack", "ContextDecorator", "ExitStack",
  11. "redirect_stdout", "redirect_stderr", "suppress"]
  12. class AbstractContextManager(abc.ABC):
  13. """An abstract base class for context managers."""
  14. __class_getitem__ = classmethod(GenericAlias)
  15. def __enter__(self):
  16. """Return `self` upon entering the runtime context."""
  17. return self
  18. @abc.abstractmethod
  19. def __exit__(self, exc_type, exc_value, traceback):
  20. """Raise any exception triggered within the runtime context."""
  21. return None
  22. @classmethod
  23. def __subclasshook__(cls, C):
  24. if cls is AbstractContextManager:
  25. return _collections_abc._check_methods(C, "__enter__", "__exit__")
  26. return NotImplemented
  27. class AbstractAsyncContextManager(abc.ABC):
  28. """An abstract base class for asynchronous context managers."""
  29. __class_getitem__ = classmethod(GenericAlias)
  30. async def __aenter__(self):
  31. """Return `self` upon entering the runtime context."""
  32. return self
  33. @abc.abstractmethod
  34. async def __aexit__(self, exc_type, exc_value, traceback):
  35. """Raise any exception triggered within the runtime context."""
  36. return None
  37. @classmethod
  38. def __subclasshook__(cls, C):
  39. if cls is AbstractAsyncContextManager:
  40. return _collections_abc._check_methods(C, "__aenter__",
  41. "__aexit__")
  42. return NotImplemented
  43. class ContextDecorator(object):
  44. "A base class or mixin that enables context managers to work as decorators."
  45. def _recreate_cm(self):
  46. """Return a recreated instance of self.
  47. Allows an otherwise one-shot context manager like
  48. _GeneratorContextManager to support use as
  49. a decorator via implicit recreation.
  50. This is a private interface just for _GeneratorContextManager.
  51. See issue #11647 for details.
  52. """
  53. return self
  54. def __call__(self, func):
  55. @wraps(func)
  56. def inner(*args, **kwds):
  57. with self._recreate_cm():
  58. return func(*args, **kwds)
  59. return inner
  60. class _GeneratorContextManagerBase:
  61. """Shared functionality for @contextmanager and @asynccontextmanager."""
  62. def __init__(self, func, args, kwds):
  63. self.gen = func(*args, **kwds)
  64. self.func, self.args, self.kwds = func, args, kwds
  65. # Issue 19330: ensure context manager instances have good docstrings
  66. doc = getattr(func, "__doc__", None)
  67. if doc is None:
  68. doc = type(self).__doc__
  69. self.__doc__ = doc
  70. # Unfortunately, this still doesn't provide good help output when
  71. # inspecting the created context manager instances, since pydoc
  72. # currently bypasses the instance docstring and shows the docstring
  73. # for the class instead.
  74. # See http://bugs.python.org/issue19404 for more details.
  75. def _recreate_cm(self):
  76. # _GCMB instances are one-shot context managers, so the
  77. # CM must be recreated each time a decorated function is
  78. # called
  79. return self.__class__(self.func, self.args, self.kwds)
  80. class _GeneratorContextManager(
  81. _GeneratorContextManagerBase,
  82. AbstractContextManager,
  83. ContextDecorator,
  84. ):
  85. """Helper for @contextmanager decorator."""
  86. def __enter__(self):
  87. # do not keep args and kwds alive unnecessarily
  88. # they are only needed for recreation, which is not possible anymore
  89. del self.args, self.kwds, self.func
  90. try:
  91. return next(self.gen)
  92. except StopIteration:
  93. raise RuntimeError("generator didn't yield") from None
  94. def __exit__(self, typ, value, traceback):
  95. if typ is None:
  96. try:
  97. next(self.gen)
  98. except StopIteration:
  99. return False
  100. else:
  101. raise RuntimeError("generator didn't stop")
  102. else:
  103. if value is None:
  104. # Need to force instantiation so we can reliably
  105. # tell if we get the same exception back
  106. value = typ()
  107. try:
  108. self.gen.throw(typ, value, traceback)
  109. except StopIteration as exc:
  110. # Suppress StopIteration *unless* it's the same exception that
  111. # was passed to throw(). This prevents a StopIteration
  112. # raised inside the "with" statement from being suppressed.
  113. return exc is not value
  114. except RuntimeError as exc:
  115. # Don't re-raise the passed in exception. (issue27122)
  116. if exc is value:
  117. return False
  118. # Avoid suppressing if a StopIteration exception
  119. # was passed to throw() and later wrapped into a RuntimeError
  120. # (see PEP 479 for sync generators; async generators also
  121. # have this behavior). But do this only if the exception wrapped
  122. # by the RuntimeError is actually Stop(Async)Iteration (see
  123. # issue29692).
  124. if (
  125. isinstance(value, StopIteration)
  126. and exc.__cause__ is value
  127. ):
  128. return False
  129. raise
  130. except BaseException as exc:
  131. # only re-raise if it's *not* the exception that was
  132. # passed to throw(), because __exit__() must not raise
  133. # an exception unless __exit__() itself failed. But throw()
  134. # has to raise the exception to signal propagation, so this
  135. # fixes the impedance mismatch between the throw() protocol
  136. # and the __exit__() protocol.
  137. if exc is not value:
  138. raise
  139. return False
  140. raise RuntimeError("generator didn't stop after throw()")
  141. class _AsyncGeneratorContextManager(_GeneratorContextManagerBase,
  142. AbstractAsyncContextManager):
  143. """Helper for @asynccontextmanager decorator."""
  144. async def __aenter__(self):
  145. # do not keep args and kwds alive unnecessarily
  146. # they are only needed for recreation, which is not possible anymore
  147. del self.args, self.kwds, self.func
  148. try:
  149. return await self.gen.__anext__()
  150. except StopAsyncIteration:
  151. raise RuntimeError("generator didn't yield") from None
  152. async def __aexit__(self, typ, value, traceback):
  153. if typ is None:
  154. try:
  155. await self.gen.__anext__()
  156. except StopAsyncIteration:
  157. return False
  158. else:
  159. raise RuntimeError("generator didn't stop")
  160. else:
  161. if value is None:
  162. # Need to force instantiation so we can reliably
  163. # tell if we get the same exception back
  164. value = typ()
  165. try:
  166. await self.gen.athrow(typ, value, traceback)
  167. except StopAsyncIteration as exc:
  168. # Suppress StopIteration *unless* it's the same exception that
  169. # was passed to throw(). This prevents a StopIteration
  170. # raised inside the "with" statement from being suppressed.
  171. return exc is not value
  172. except RuntimeError as exc:
  173. # Don't re-raise the passed in exception. (issue27122)
  174. if exc is value:
  175. return False
  176. # Avoid suppressing if a Stop(Async)Iteration exception
  177. # was passed to athrow() and later wrapped into a RuntimeError
  178. # (see PEP 479 for sync generators; async generators also
  179. # have this behavior). But do this only if the exception wrapped
  180. # by the RuntimeError is actully Stop(Async)Iteration (see
  181. # issue29692).
  182. if (
  183. isinstance(value, (StopIteration, StopAsyncIteration))
  184. and exc.__cause__ is value
  185. ):
  186. return False
  187. raise
  188. except BaseException as exc:
  189. # only re-raise if it's *not* the exception that was
  190. # passed to throw(), because __exit__() must not raise
  191. # an exception unless __exit__() itself failed. But throw()
  192. # has to raise the exception to signal propagation, so this
  193. # fixes the impedance mismatch between the throw() protocol
  194. # and the __exit__() protocol.
  195. if exc is not value:
  196. raise
  197. return False
  198. raise RuntimeError("generator didn't stop after athrow()")
  199. def contextmanager(func):
  200. """@contextmanager decorator.
  201. Typical usage:
  202. @contextmanager
  203. def some_generator(<arguments>):
  204. <setup>
  205. try:
  206. yield <value>
  207. finally:
  208. <cleanup>
  209. This makes this:
  210. with some_generator(<arguments>) as <variable>:
  211. <body>
  212. equivalent to this:
  213. <setup>
  214. try:
  215. <variable> = <value>
  216. <body>
  217. finally:
  218. <cleanup>
  219. """
  220. @wraps(func)
  221. def helper(*args, **kwds):
  222. return _GeneratorContextManager(func, args, kwds)
  223. return helper
  224. def asynccontextmanager(func):
  225. """@asynccontextmanager decorator.
  226. Typical usage:
  227. @asynccontextmanager
  228. async def some_async_generator(<arguments>):
  229. <setup>
  230. try:
  231. yield <value>
  232. finally:
  233. <cleanup>
  234. This makes this:
  235. async with some_async_generator(<arguments>) as <variable>:
  236. <body>
  237. equivalent to this:
  238. <setup>
  239. try:
  240. <variable> = <value>
  241. <body>
  242. finally:
  243. <cleanup>
  244. """
  245. @wraps(func)
  246. def helper(*args, **kwds):
  247. return _AsyncGeneratorContextManager(func, args, kwds)
  248. return helper
  249. class closing(AbstractContextManager):
  250. """Context to automatically close something at the end of a block.
  251. Code like this:
  252. with closing(<module>.open(<arguments>)) as f:
  253. <block>
  254. is equivalent to this:
  255. f = <module>.open(<arguments>)
  256. try:
  257. <block>
  258. finally:
  259. f.close()
  260. """
  261. def __init__(self, thing):
  262. self.thing = thing
  263. def __enter__(self):
  264. return self.thing
  265. def __exit__(self, *exc_info):
  266. self.thing.close()
  267. class _RedirectStream(AbstractContextManager):
  268. _stream = None
  269. def __init__(self, new_target):
  270. self._new_target = new_target
  271. # We use a list of old targets to make this CM re-entrant
  272. self._old_targets = []
  273. def __enter__(self):
  274. self._old_targets.append(getattr(sys, self._stream))
  275. setattr(sys, self._stream, self._new_target)
  276. return self._new_target
  277. def __exit__(self, exctype, excinst, exctb):
  278. setattr(sys, self._stream, self._old_targets.pop())
  279. class redirect_stdout(_RedirectStream):
  280. """Context manager for temporarily redirecting stdout to another file.
  281. # How to send help() to stderr
  282. with redirect_stdout(sys.stderr):
  283. help(dir)
  284. # How to write help() to a file
  285. with open('help.txt', 'w') as f:
  286. with redirect_stdout(f):
  287. help(pow)
  288. """
  289. _stream = "stdout"
  290. class redirect_stderr(_RedirectStream):
  291. """Context manager for temporarily redirecting stderr to another file."""
  292. _stream = "stderr"
  293. class suppress(AbstractContextManager):
  294. """Context manager to suppress specified exceptions
  295. After the exception is suppressed, execution proceeds with the next
  296. statement following the with statement.
  297. with suppress(FileNotFoundError):
  298. os.remove(somefile)
  299. # Execution still resumes here if the file was already removed
  300. """
  301. def __init__(self, *exceptions):
  302. self._exceptions = exceptions
  303. def __enter__(self):
  304. pass
  305. def __exit__(self, exctype, excinst, exctb):
  306. # Unlike isinstance and issubclass, CPython exception handling
  307. # currently only looks at the concrete type hierarchy (ignoring
  308. # the instance and subclass checking hooks). While Guido considers
  309. # that a bug rather than a feature, it's a fairly hard one to fix
  310. # due to various internal implementation details. suppress provides
  311. # the simpler issubclass based semantics, rather than trying to
  312. # exactly reproduce the limitations of the CPython interpreter.
  313. #
  314. # See http://bugs.python.org/issue12029 for more details
  315. return exctype is not None and issubclass(exctype, self._exceptions)
  316. class _BaseExitStack:
  317. """A base class for ExitStack and AsyncExitStack."""
  318. @staticmethod
  319. def _create_exit_wrapper(cm, cm_exit):
  320. return MethodType(cm_exit, cm)
  321. @staticmethod
  322. def _create_cb_wrapper(callback, /, *args, **kwds):
  323. def _exit_wrapper(exc_type, exc, tb):
  324. callback(*args, **kwds)
  325. return _exit_wrapper
  326. def __init__(self):
  327. self._exit_callbacks = deque()
  328. def pop_all(self):
  329. """Preserve the context stack by transferring it to a new instance."""
  330. new_stack = type(self)()
  331. new_stack._exit_callbacks = self._exit_callbacks
  332. self._exit_callbacks = deque()
  333. return new_stack
  334. def push(self, exit):
  335. """Registers a callback with the standard __exit__ method signature.
  336. Can suppress exceptions the same way __exit__ method can.
  337. Also accepts any object with an __exit__ method (registering a call
  338. to the method instead of the object itself).
  339. """
  340. # We use an unbound method rather than a bound method to follow
  341. # the standard lookup behaviour for special methods.
  342. _cb_type = type(exit)
  343. try:
  344. exit_method = _cb_type.__exit__
  345. except AttributeError:
  346. # Not a context manager, so assume it's a callable.
  347. self._push_exit_callback(exit)
  348. else:
  349. self._push_cm_exit(exit, exit_method)
  350. return exit # Allow use as a decorator.
  351. def enter_context(self, cm):
  352. """Enters the supplied context manager.
  353. If successful, also pushes its __exit__ method as a callback and
  354. returns the result of the __enter__ method.
  355. """
  356. # We look up the special methods on the type to match the with
  357. # statement.
  358. _cm_type = type(cm)
  359. _exit = _cm_type.__exit__
  360. result = _cm_type.__enter__(cm)
  361. self._push_cm_exit(cm, _exit)
  362. return result
  363. def callback(self, callback, /, *args, **kwds):
  364. """Registers an arbitrary callback and arguments.
  365. Cannot suppress exceptions.
  366. """
  367. _exit_wrapper = self._create_cb_wrapper(callback, *args, **kwds)
  368. # We changed the signature, so using @wraps is not appropriate, but
  369. # setting __wrapped__ may still help with introspection.
  370. _exit_wrapper.__wrapped__ = callback
  371. self._push_exit_callback(_exit_wrapper)
  372. return callback # Allow use as a decorator
  373. def _push_cm_exit(self, cm, cm_exit):
  374. """Helper to correctly register callbacks to __exit__ methods."""
  375. _exit_wrapper = self._create_exit_wrapper(cm, cm_exit)
  376. self._push_exit_callback(_exit_wrapper, True)
  377. def _push_exit_callback(self, callback, is_sync=True):
  378. self._exit_callbacks.append((is_sync, callback))
  379. # Inspired by discussions on http://bugs.python.org/issue13585
  380. class ExitStack(_BaseExitStack, AbstractContextManager):
  381. """Context manager for dynamic management of a stack of exit callbacks.
  382. For example:
  383. with ExitStack() as stack:
  384. files = [stack.enter_context(open(fname)) for fname in filenames]
  385. # All opened files will automatically be closed at the end of
  386. # the with statement, even if attempts to open files later
  387. # in the list raise an exception.
  388. """
  389. def __enter__(self):
  390. return self
  391. def __exit__(self, *exc_details):
  392. received_exc = exc_details[0] is not None
  393. # We manipulate the exception state so it behaves as though
  394. # we were actually nesting multiple with statements
  395. frame_exc = sys.exc_info()[1]
  396. def _fix_exception_context(new_exc, old_exc):
  397. # Context may not be correct, so find the end of the chain
  398. while 1:
  399. exc_context = new_exc.__context__
  400. if exc_context is None or exc_context is old_exc:
  401. # Context is already set correctly (see issue 20317)
  402. return
  403. if exc_context is frame_exc:
  404. break
  405. new_exc = exc_context
  406. # Change the end of the chain to point to the exception
  407. # we expect it to reference
  408. new_exc.__context__ = old_exc
  409. # Callbacks are invoked in LIFO order to match the behaviour of
  410. # nested context managers
  411. suppressed_exc = False
  412. pending_raise = False
  413. while self._exit_callbacks:
  414. is_sync, cb = self._exit_callbacks.pop()
  415. assert is_sync
  416. try:
  417. if cb(*exc_details):
  418. suppressed_exc = True
  419. pending_raise = False
  420. exc_details = (None, None, None)
  421. except:
  422. new_exc_details = sys.exc_info()
  423. # simulate the stack of exceptions by setting the context
  424. _fix_exception_context(new_exc_details[1], exc_details[1])
  425. pending_raise = True
  426. exc_details = new_exc_details
  427. if pending_raise:
  428. try:
  429. # bare "raise exc_details[1]" replaces our carefully
  430. # set-up context
  431. fixed_ctx = exc_details[1].__context__
  432. raise exc_details[1]
  433. except BaseException:
  434. exc_details[1].__context__ = fixed_ctx
  435. raise
  436. return received_exc and suppressed_exc
  437. def close(self):
  438. """Immediately unwind the context stack."""
  439. self.__exit__(None, None, None)
  440. # Inspired by discussions on https://bugs.python.org/issue29302
  441. class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
  442. """Async context manager for dynamic management of a stack of exit
  443. callbacks.
  444. For example:
  445. async with AsyncExitStack() as stack:
  446. connections = [await stack.enter_async_context(get_connection())
  447. for i in range(5)]
  448. # All opened connections will automatically be released at the
  449. # end of the async with statement, even if attempts to open a
  450. # connection later in the list raise an exception.
  451. """
  452. @staticmethod
  453. def _create_async_exit_wrapper(cm, cm_exit):
  454. return MethodType(cm_exit, cm)
  455. @staticmethod
  456. def _create_async_cb_wrapper(callback, /, *args, **kwds):
  457. async def _exit_wrapper(exc_type, exc, tb):
  458. await callback(*args, **kwds)
  459. return _exit_wrapper
  460. async def enter_async_context(self, cm):
  461. """Enters the supplied async context manager.
  462. If successful, also pushes its __aexit__ method as a callback and
  463. returns the result of the __aenter__ method.
  464. """
  465. _cm_type = type(cm)
  466. _exit = _cm_type.__aexit__
  467. result = await _cm_type.__aenter__(cm)
  468. self._push_async_cm_exit(cm, _exit)
  469. return result
  470. def push_async_exit(self, exit):
  471. """Registers a coroutine function with the standard __aexit__ method
  472. signature.
  473. Can suppress exceptions the same way __aexit__ method can.
  474. Also accepts any object with an __aexit__ method (registering a call
  475. to the method instead of the object itself).
  476. """
  477. _cb_type = type(exit)
  478. try:
  479. exit_method = _cb_type.__aexit__
  480. except AttributeError:
  481. # Not an async context manager, so assume it's a coroutine function
  482. self._push_exit_callback(exit, False)
  483. else:
  484. self._push_async_cm_exit(exit, exit_method)
  485. return exit # Allow use as a decorator
  486. def push_async_callback(self, callback, /, *args, **kwds):
  487. """Registers an arbitrary coroutine function and arguments.
  488. Cannot suppress exceptions.
  489. """
  490. _exit_wrapper = self._create_async_cb_wrapper(callback, *args, **kwds)
  491. # We changed the signature, so using @wraps is not appropriate, but
  492. # setting __wrapped__ may still help with introspection.
  493. _exit_wrapper.__wrapped__ = callback
  494. self._push_exit_callback(_exit_wrapper, False)
  495. return callback # Allow use as a decorator
  496. async def aclose(self):
  497. """Immediately unwind the context stack."""
  498. await self.__aexit__(None, None, None)
  499. def _push_async_cm_exit(self, cm, cm_exit):
  500. """Helper to correctly register coroutine function to __aexit__
  501. method."""
  502. _exit_wrapper = self._create_async_exit_wrapper(cm, cm_exit)
  503. self._push_exit_callback(_exit_wrapper, False)
  504. async def __aenter__(self):
  505. return self
  506. async def __aexit__(self, *exc_details):
  507. received_exc = exc_details[0] is not None
  508. # We manipulate the exception state so it behaves as though
  509. # we were actually nesting multiple with statements
  510. frame_exc = sys.exc_info()[1]
  511. def _fix_exception_context(new_exc, old_exc):
  512. # Context may not be correct, so find the end of the chain
  513. while 1:
  514. exc_context = new_exc.__context__
  515. if exc_context is None or exc_context is old_exc:
  516. # Context is already set correctly (see issue 20317)
  517. return
  518. if exc_context is frame_exc:
  519. break
  520. new_exc = exc_context
  521. # Change the end of the chain to point to the exception
  522. # we expect it to reference
  523. new_exc.__context__ = old_exc
  524. # Callbacks are invoked in LIFO order to match the behaviour of
  525. # nested context managers
  526. suppressed_exc = False
  527. pending_raise = False
  528. while self._exit_callbacks:
  529. is_sync, cb = self._exit_callbacks.pop()
  530. try:
  531. if is_sync:
  532. cb_suppress = cb(*exc_details)
  533. else:
  534. cb_suppress = await cb(*exc_details)
  535. if cb_suppress:
  536. suppressed_exc = True
  537. pending_raise = False
  538. exc_details = (None, None, None)
  539. except:
  540. new_exc_details = sys.exc_info()
  541. # simulate the stack of exceptions by setting the context
  542. _fix_exception_context(new_exc_details[1], exc_details[1])
  543. pending_raise = True
  544. exc_details = new_exc_details
  545. if pending_raise:
  546. try:
  547. # bare "raise exc_details[1]" replaces our carefully
  548. # set-up context
  549. fixed_ctx = exc_details[1].__context__
  550. raise exc_details[1]
  551. except BaseException:
  552. exc_details[1].__context__ = fixed_ctx
  553. raise
  554. return received_exc and suppressed_exc
  555. class nullcontext(AbstractContextManager):
  556. """Context manager that does no additional processing.
  557. Used as a stand-in for a normal context manager, when a particular
  558. block of code is only sometimes used with a normal context manager:
  559. cm = optional_cm if condition else nullcontext()
  560. with cm:
  561. # Perform operation, using optional_cm if condition is True
  562. """
  563. def __init__(self, enter_result=None):
  564. self.enter_result = enter_result
  565. def __enter__(self):
  566. return self.enter_result
  567. def __exit__(self, *excinfo):
  568. pass