contextlib.py 27 KB

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