contextlib.py 24 KB

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