contextlib.py 24 KB

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