contextlib.py 27 KB

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