blob: 1ff8cdf1cecf8f4319475236b344f31baf34dfed [file] [log] [blame]
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00001"""Utilities for with-statement contexts. See PEP 343."""
Brett Cannon9e080e02016-04-08 12:15:27 -07002import abc
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003import sys
Jelle Zijlstra57161aa2017-06-09 08:21:47 -07004import _collections_abc
Nick Coghlan3267a302012-05-21 22:54:43 +10005from collections import deque
Christian Heimes81ee3ef2008-05-04 22:42:01 +00006from functools import wraps
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00007
Jesse-Bakker0784a2e2017-11-23 01:23:28 +01008__all__ = ["asynccontextmanager", "contextmanager", "closing", "nullcontext",
Jelle Zijlstra176baa32017-12-13 17:19:17 -08009 "AbstractContextManager", "AbstractAsyncContextManager",
Ilya Kulakov1aa094f2018-01-25 12:51:18 -080010 "AsyncExitStack", "ContextDecorator", "ExitStack",
Jelle Zijlstra2e624692017-04-30 18:25:58 -070011 "redirect_stdout", "redirect_stderr", "suppress"]
Brett Cannon9e080e02016-04-08 12:15:27 -070012
13
14class AbstractContextManager(abc.ABC):
15
16 """An abstract base class for context managers."""
17
18 def __enter__(self):
19 """Return `self` upon entering the runtime context."""
20 return self
21
22 @abc.abstractmethod
23 def __exit__(self, exc_type, exc_value, traceback):
24 """Raise any exception triggered within the runtime context."""
25 return None
26
27 @classmethod
28 def __subclasshook__(cls, C):
29 if cls is AbstractContextManager:
Jelle Zijlstra57161aa2017-06-09 08:21:47 -070030 return _collections_abc._check_methods(C, "__enter__", "__exit__")
Brett Cannon9e080e02016-04-08 12:15:27 -070031 return NotImplemented
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000032
Michael Foordb3a89842010-06-30 12:17:50 +000033
Jelle Zijlstra176baa32017-12-13 17:19:17 -080034class AbstractAsyncContextManager(abc.ABC):
35
36 """An abstract base class for asynchronous context managers."""
37
38 async def __aenter__(self):
39 """Return `self` upon entering the runtime context."""
40 return self
41
42 @abc.abstractmethod
43 async def __aexit__(self, exc_type, exc_value, traceback):
44 """Raise any exception triggered within the runtime context."""
45 return None
46
47 @classmethod
48 def __subclasshook__(cls, C):
49 if cls is AbstractAsyncContextManager:
50 return _collections_abc._check_methods(C, "__aenter__",
51 "__aexit__")
52 return NotImplemented
53
54
Michael Foordb3a89842010-06-30 12:17:50 +000055class ContextDecorator(object):
56 "A base class or mixin that enables context managers to work as decorators."
Nick Coghlan0ded3e32011-05-05 23:49:25 +100057
58 def _recreate_cm(self):
59 """Return a recreated instance of self.
Nick Coghlanfdc2c552011-05-06 00:02:12 +100060
Nick Coghlan3267a302012-05-21 22:54:43 +100061 Allows an otherwise one-shot context manager like
Nick Coghlan0ded3e32011-05-05 23:49:25 +100062 _GeneratorContextManager to support use as
Nick Coghlan3267a302012-05-21 22:54:43 +100063 a decorator via implicit recreation.
Nick Coghlanfdc2c552011-05-06 00:02:12 +100064
Nick Coghlan3267a302012-05-21 22:54:43 +100065 This is a private interface just for _GeneratorContextManager.
66 See issue #11647 for details.
Nick Coghlan0ded3e32011-05-05 23:49:25 +100067 """
68 return self
69
Michael Foordb3a89842010-06-30 12:17:50 +000070 def __call__(self, func):
71 @wraps(func)
72 def inner(*args, **kwds):
Nick Coghlan0ded3e32011-05-05 23:49:25 +100073 with self._recreate_cm():
Michael Foordb3a89842010-06-30 12:17:50 +000074 return func(*args, **kwds)
75 return inner
76
77
Jelle Zijlstra2e624692017-04-30 18:25:58 -070078class _GeneratorContextManagerBase:
79 """Shared functionality for @contextmanager and @asynccontextmanager."""
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000080
Serhiy Storchaka101ff352015-06-28 17:06:07 +030081 def __init__(self, func, args, kwds):
Nick Coghlan0ded3e32011-05-05 23:49:25 +100082 self.gen = func(*args, **kwds)
83 self.func, self.args, self.kwds = func, args, kwds
Nick Coghlan059def52013-10-26 18:08:15 +100084 # Issue 19330: ensure context manager instances have good docstrings
85 doc = getattr(func, "__doc__", None)
86 if doc is None:
87 doc = type(self).__doc__
88 self.__doc__ = doc
89 # Unfortunately, this still doesn't provide good help output when
90 # inspecting the created context manager instances, since pydoc
91 # currently bypasses the instance docstring and shows the docstring
92 # for the class instead.
93 # See http://bugs.python.org/issue19404 for more details.
Nick Coghlan0ded3e32011-05-05 23:49:25 +100094
Jelle Zijlstra2e624692017-04-30 18:25:58 -070095
96class _GeneratorContextManager(_GeneratorContextManagerBase,
97 AbstractContextManager,
98 ContextDecorator):
99 """Helper for @contextmanager decorator."""
100
Nick Coghlan0ded3e32011-05-05 23:49:25 +1000101 def _recreate_cm(self):
102 # _GCM instances are one-shot context managers, so the
103 # CM must be recreated each time a decorated function is
104 # called
Serhiy Storchaka101ff352015-06-28 17:06:07 +0300105 return self.__class__(self.func, self.args, self.kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000106
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000107 def __enter__(self):
Martin Teichmanndd0e0872018-01-28 05:17:46 +0100108 # do not keep args and kwds alive unnecessarily
109 # they are only needed for recreation, which is not possible anymore
110 del self.args, self.kwds, self.func
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000111 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000112 return next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000113 except StopIteration:
Nick Coghlan8608d262013-10-20 00:30:51 +1000114 raise RuntimeError("generator didn't yield") from None
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000115
116 def __exit__(self, type, value, traceback):
117 if type is None:
118 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000119 next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000120 except StopIteration:
svelankar00c75e92017-04-11 05:11:13 -0400121 return False
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000122 else:
123 raise RuntimeError("generator didn't stop")
124 else:
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000125 if value is None:
126 # Need to force instantiation so we can reliably
127 # tell if we get the same exception back
128 value = type()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000129 try:
130 self.gen.throw(type, value, traceback)
Guido van Rossumb940e112007-01-10 16:19:56 +0000131 except StopIteration as exc:
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400132 # Suppress StopIteration *unless* it's the same exception that
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 # was passed to throw(). This prevents a StopIteration
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400134 # raised inside the "with" statement from being suppressed.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 return exc is not value
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400136 except RuntimeError as exc:
Nathaniel J. Smithaf88e7e2017-02-12 03:37:24 -0800137 # Don't re-raise the passed in exception. (issue27122)
Gregory P. Smithba2ecd62016-06-14 09:19:20 -0700138 if exc is value:
139 return False
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400140 # Likewise, avoid suppressing if a StopIteration exception
141 # was passed to throw() and later wrapped into a RuntimeError
142 # (see PEP 479).
svelankar00c75e92017-04-11 05:11:13 -0400143 if type is StopIteration and exc.__cause__ is value:
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400144 return False
145 raise
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 except:
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 #
Jelle Zijlstra2e624692017-04-30 18:25:58 -0700154 # This cannot use 'except BaseException as exc' (as in the
155 # async implementation) to maintain compatibility with
156 # Python 2, where old-style class exceptions are not caught
157 # by 'except BaseException'.
svelankar00c75e92017-04-11 05:11:13 -0400158 if sys.exc_info()[1] is value:
159 return False
160 raise
161 raise RuntimeError("generator didn't stop after throw()")
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000162
163
Jelle Zijlstra176baa32017-12-13 17:19:17 -0800164class _AsyncGeneratorContextManager(_GeneratorContextManagerBase,
165 AbstractAsyncContextManager):
Jelle Zijlstra2e624692017-04-30 18:25:58 -0700166 """Helper for @asynccontextmanager."""
167
168 async def __aenter__(self):
169 try:
170 return await self.gen.__anext__()
171 except StopAsyncIteration:
172 raise RuntimeError("generator didn't yield") from None
173
174 async def __aexit__(self, typ, value, traceback):
175 if typ is None:
176 try:
177 await self.gen.__anext__()
178 except StopAsyncIteration:
179 return
180 else:
181 raise RuntimeError("generator didn't stop")
182 else:
183 if value is None:
184 value = typ()
185 # See _GeneratorContextManager.__exit__ for comments on subtleties
186 # in this implementation
187 try:
188 await self.gen.athrow(typ, value, traceback)
189 raise RuntimeError("generator didn't stop after throw()")
190 except StopAsyncIteration as exc:
191 return exc is not value
192 except RuntimeError as exc:
193 if exc is value:
194 return False
195 # Avoid suppressing if a StopIteration exception
196 # was passed to throw() and later wrapped into a RuntimeError
197 # (see PEP 479 for sync generators; async generators also
198 # have this behavior). But do this only if the exception wrapped
199 # by the RuntimeError is actully Stop(Async)Iteration (see
200 # issue29692).
201 if isinstance(value, (StopIteration, StopAsyncIteration)):
202 if exc.__cause__ is value:
203 return False
204 raise
205 except BaseException as exc:
206 if exc is not value:
207 raise
208
209
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000210def contextmanager(func):
211 """@contextmanager decorator.
212
213 Typical usage:
214
215 @contextmanager
216 def some_generator(<arguments>):
217 <setup>
218 try:
219 yield <value>
220 finally:
221 <cleanup>
222
223 This makes this:
224
225 with some_generator(<arguments>) as <variable>:
226 <body>
227
228 equivalent to this:
229
230 <setup>
231 try:
232 <variable> = <value>
233 <body>
234 finally:
235 <cleanup>
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000236 """
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000237 @wraps(func)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000238 def helper(*args, **kwds):
Serhiy Storchaka101ff352015-06-28 17:06:07 +0300239 return _GeneratorContextManager(func, args, kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000240 return helper
241
242
Jelle Zijlstra2e624692017-04-30 18:25:58 -0700243def asynccontextmanager(func):
244 """@asynccontextmanager decorator.
245
246 Typical usage:
247
248 @asynccontextmanager
249 async def some_async_generator(<arguments>):
250 <setup>
251 try:
252 yield <value>
253 finally:
254 <cleanup>
255
256 This makes this:
257
258 async with some_async_generator(<arguments>) as <variable>:
259 <body>
260
261 equivalent to this:
262
263 <setup>
264 try:
265 <variable> = <value>
266 <body>
267 finally:
268 <cleanup>
269 """
270 @wraps(func)
271 def helper(*args, **kwds):
272 return _AsyncGeneratorContextManager(func, args, kwds)
273 return helper
274
275
Brett Cannon9e080e02016-04-08 12:15:27 -0700276class closing(AbstractContextManager):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277 """Context to automatically close something at the end of a block.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000278
279 Code like this:
280
281 with closing(<module>.open(<arguments>)) as f:
282 <block>
283
284 is equivalent to this:
285
286 f = <module>.open(<arguments>)
287 try:
288 <block>
289 finally:
290 f.close()
291
292 """
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293 def __init__(self, thing):
294 self.thing = thing
295 def __enter__(self):
296 return self.thing
297 def __exit__(self, *exc_info):
298 self.thing.close()
Nick Coghlan3267a302012-05-21 22:54:43 +1000299
Berker Peksagbb44fe02014-11-28 23:28:06 +0200300
Brett Cannon9e080e02016-04-08 12:15:27 -0700301class _RedirectStream(AbstractContextManager):
Berker Peksagbb44fe02014-11-28 23:28:06 +0200302
303 _stream = None
304
305 def __init__(self, new_target):
306 self._new_target = new_target
307 # We use a list of old targets to make this CM re-entrant
308 self._old_targets = []
309
310 def __enter__(self):
311 self._old_targets.append(getattr(sys, self._stream))
312 setattr(sys, self._stream, self._new_target)
313 return self._new_target
314
315 def __exit__(self, exctype, excinst, exctb):
316 setattr(sys, self._stream, self._old_targets.pop())
317
318
319class redirect_stdout(_RedirectStream):
320 """Context manager for temporarily redirecting stdout to another file.
Nick Coghlan059def52013-10-26 18:08:15 +1000321
322 # How to send help() to stderr
323 with redirect_stdout(sys.stderr):
324 help(dir)
325
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 """
Nick Coghlan8608d262013-10-20 00:30:51 +1000331
Berker Peksagbb44fe02014-11-28 23:28:06 +0200332 _stream = "stdout"
Nick Coghlan8608d262013-10-20 00:30:51 +1000333
Nick Coghlan8608d262013-10-20 00:30:51 +1000334
Berker Peksagbb44fe02014-11-28 23:28:06 +0200335class redirect_stderr(_RedirectStream):
336 """Context manager for temporarily redirecting stderr to another file."""
337
338 _stream = "stderr"
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700339
Nick Coghlan8608d262013-10-20 00:30:51 +1000340
Brett Cannon9e080e02016-04-08 12:15:27 -0700341class suppress(AbstractContextManager):
Nick Coghlan240f86d2013-10-17 23:40:57 +1000342 """Context manager to suppress specified exceptions
Raymond Hettingere318a882013-03-10 22:26:51 -0700343
Nick Coghlan8608d262013-10-20 00:30:51 +1000344 After the exception is suppressed, execution proceeds with the next
345 statement following the with statement.
Raymond Hettingere318a882013-03-10 22:26:51 -0700346
Nick Coghlan8608d262013-10-20 00:30:51 +1000347 with suppress(FileNotFoundError):
348 os.remove(somefile)
349 # Execution still resumes here if the file was already removed
Raymond Hettingere318a882013-03-10 22:26:51 -0700350 """
Nick Coghlan059def52013-10-26 18:08:15 +1000351
352 def __init__(self, *exceptions):
353 self._exceptions = exceptions
354
355 def __enter__(self):
356 pass
357
358 def __exit__(self, exctype, excinst, exctb):
359 # Unlike isinstance and issubclass, CPython exception handling
360 # currently only looks at the concrete type hierarchy (ignoring
361 # the instance and subclass checking hooks). While Guido considers
362 # that a bug rather than a feature, it's a fairly hard one to fix
363 # due to various internal implementation details. suppress provides
364 # the simpler issubclass based semantics, rather than trying to
365 # exactly reproduce the limitations of the CPython interpreter.
366 #
367 # See http://bugs.python.org/issue12029 for more details
368 return exctype is not None and issubclass(exctype, self._exceptions)
369
Nick Coghlan3267a302012-05-21 22:54:43 +1000370
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800371class _BaseExitStack:
372 """A base class for ExitStack and AsyncExitStack."""
Nick Coghlan3267a302012-05-21 22:54:43 +1000373
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800374 @staticmethod
375 def _create_exit_wrapper(cm, cm_exit):
376 def _exit_wrapper(exc_type, exc, tb):
377 return cm_exit(cm, exc_type, exc, tb)
378 return _exit_wrapper
Nick Coghlan3267a302012-05-21 22:54:43 +1000379
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800380 @staticmethod
381 def _create_cb_wrapper(callback, *args, **kwds):
382 def _exit_wrapper(exc_type, exc, tb):
383 callback(*args, **kwds)
384 return _exit_wrapper
Nick Coghlan3267a302012-05-21 22:54:43 +1000385
Nick Coghlan3267a302012-05-21 22:54:43 +1000386 def __init__(self):
387 self._exit_callbacks = deque()
388
389 def pop_all(self):
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800390 """Preserve the context stack by transferring it to a new instance."""
Nick Coghlan3267a302012-05-21 22:54:43 +1000391 new_stack = type(self)()
392 new_stack._exit_callbacks = self._exit_callbacks
393 self._exit_callbacks = deque()
394 return new_stack
395
Nick Coghlan3267a302012-05-21 22:54:43 +1000396 def push(self, exit):
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800397 """Registers a callback with the standard __exit__ method signature.
Nick Coghlan3267a302012-05-21 22:54:43 +1000398
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800399 Can suppress exceptions the same way __exit__ method can.
Nick Coghlan3267a302012-05-21 22:54:43 +1000400 Also accepts any object with an __exit__ method (registering a call
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800401 to the method instead of the object itself).
Nick Coghlan3267a302012-05-21 22:54:43 +1000402 """
403 # We use an unbound method rather than a bound method to follow
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800404 # the standard lookup behaviour for special methods.
Nick Coghlan3267a302012-05-21 22:54:43 +1000405 _cb_type = type(exit)
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800406
Nick Coghlan3267a302012-05-21 22:54:43 +1000407 try:
408 exit_method = _cb_type.__exit__
409 except AttributeError:
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800410 # Not a context manager, so assume it's a callable.
411 self._push_exit_callback(exit)
Nick Coghlan3267a302012-05-21 22:54:43 +1000412 else:
413 self._push_cm_exit(exit, exit_method)
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800414 return exit # Allow use as a decorator.
Nick Coghlan3267a302012-05-21 22:54:43 +1000415
416 def enter_context(self, cm):
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800417 """Enters the supplied context manager.
Nick Coghlan3267a302012-05-21 22:54:43 +1000418
419 If successful, also pushes its __exit__ method as a callback and
420 returns the result of the __enter__ method.
421 """
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800422 # We look up the special methods on the type to match the with
423 # statement.
Nick Coghlan3267a302012-05-21 22:54:43 +1000424 _cm_type = type(cm)
425 _exit = _cm_type.__exit__
426 result = _cm_type.__enter__(cm)
427 self._push_cm_exit(cm, _exit)
428 return result
429
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800430 def callback(self, callback, *args, **kwds):
431 """Registers an arbitrary callback and arguments.
432
433 Cannot suppress exceptions.
434 """
435 _exit_wrapper = self._create_cb_wrapper(callback, *args, **kwds)
436
437 # We changed the signature, so using @wraps is not appropriate, but
438 # setting __wrapped__ may still help with introspection.
439 _exit_wrapper.__wrapped__ = callback
440 self._push_exit_callback(_exit_wrapper)
441 return callback # Allow use as a decorator
442
443 def _push_cm_exit(self, cm, cm_exit):
444 """Helper to correctly register callbacks to __exit__ methods."""
445 _exit_wrapper = self._create_exit_wrapper(cm, cm_exit)
446 _exit_wrapper.__self__ = cm
447 self._push_exit_callback(_exit_wrapper, True)
448
449 def _push_exit_callback(self, callback, is_sync=True):
450 self._exit_callbacks.append((is_sync, callback))
451
452
453# Inspired by discussions on http://bugs.python.org/issue13585
454class ExitStack(_BaseExitStack, AbstractContextManager):
455 """Context manager for dynamic management of a stack of exit callbacks.
456
457 For example:
458 with ExitStack() as stack:
459 files = [stack.enter_context(open(fname)) for fname in filenames]
460 # All opened files will automatically be closed at the end of
461 # the with statement, even if attempts to open files later
462 # in the list raise an exception.
463 """
464
465 def __enter__(self):
466 return self
Nick Coghlan3267a302012-05-21 22:54:43 +1000467
Nick Coghlan3267a302012-05-21 22:54:43 +1000468 def __exit__(self, *exc_details):
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000469 received_exc = exc_details[0] is not None
470
Nick Coghlan77452fc2012-06-01 22:48:32 +1000471 # We manipulate the exception state so it behaves as though
472 # we were actually nesting multiple with statements
473 frame_exc = sys.exc_info()[1]
474 def _fix_exception_context(new_exc, old_exc):
Nick Coghlanadd94c92014-01-24 23:05:45 +1000475 # Context may not be correct, so find the end of the chain
Nick Coghlan77452fc2012-06-01 22:48:32 +1000476 while 1:
477 exc_context = new_exc.__context__
Nick Coghlan09761e72014-01-22 22:24:46 +1000478 if exc_context is old_exc:
479 # Context is already set correctly (see issue 20317)
480 return
481 if exc_context is None or exc_context is frame_exc:
Nick Coghlan77452fc2012-06-01 22:48:32 +1000482 break
483 new_exc = exc_context
Nick Coghlan09761e72014-01-22 22:24:46 +1000484 # Change the end of the chain to point to the exception
485 # we expect it to reference
Nick Coghlan77452fc2012-06-01 22:48:32 +1000486 new_exc.__context__ = old_exc
487
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000488 # Callbacks are invoked in LIFO order to match the behaviour of
489 # nested context managers
490 suppressed_exc = False
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000491 pending_raise = False
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000492 while self._exit_callbacks:
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800493 is_sync, cb = self._exit_callbacks.pop()
494 assert is_sync
Nick Coghlan3267a302012-05-21 22:54:43 +1000495 try:
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000496 if cb(*exc_details):
497 suppressed_exc = True
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000498 pending_raise = False
Nick Coghlan3267a302012-05-21 22:54:43 +1000499 exc_details = (None, None, None)
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000500 except:
501 new_exc_details = sys.exc_info()
Nick Coghlan77452fc2012-06-01 22:48:32 +1000502 # simulate the stack of exceptions by setting the context
503 _fix_exception_context(new_exc_details[1], exc_details[1])
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000504 pending_raise = True
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000505 exc_details = new_exc_details
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000506 if pending_raise:
507 try:
508 # bare "raise exc_details[1]" replaces our carefully
509 # set-up context
510 fixed_ctx = exc_details[1].__context__
511 raise exc_details[1]
512 except BaseException:
513 exc_details[1].__context__ = fixed_ctx
514 raise
515 return received_exc and suppressed_exc
Jesse-Bakker0784a2e2017-11-23 01:23:28 +0100516
Ilya Kulakov1aa094f2018-01-25 12:51:18 -0800517 def close(self):
518 """Immediately unwind the context stack."""
519 self.__exit__(None, None, None)
520
521
522# Inspired by discussions on https://bugs.python.org/issue29302
523class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
524 """Async context manager for dynamic management of a stack of exit
525 callbacks.
526
527 For example:
528 async with AsyncExitStack() as stack:
529 connections = [await stack.enter_async_context(get_connection())
530 for i in range(5)]
531 # All opened connections will automatically be released at the
532 # end of the async with statement, even if attempts to open a
533 # connection later in the list raise an exception.
534 """
535
536 @staticmethod
537 def _create_async_exit_wrapper(cm, cm_exit):
538 async def _exit_wrapper(exc_type, exc, tb):
539 return await cm_exit(cm, exc_type, exc, tb)
540 return _exit_wrapper
541
542 @staticmethod
543 def _create_async_cb_wrapper(callback, *args, **kwds):
544 async def _exit_wrapper(exc_type, exc, tb):
545 await callback(*args, **kwds)
546 return _exit_wrapper
547
548 async def enter_async_context(self, cm):
549 """Enters the supplied async context manager.
550
551 If successful, also pushes its __aexit__ method as a callback and
552 returns the result of the __aenter__ method.
553 """
554 _cm_type = type(cm)
555 _exit = _cm_type.__aexit__
556 result = await _cm_type.__aenter__(cm)
557 self._push_async_cm_exit(cm, _exit)
558 return result
559
560 def push_async_exit(self, exit):
561 """Registers a coroutine function with the standard __aexit__ method
562 signature.
563
564 Can suppress exceptions the same way __aexit__ method can.
565 Also accepts any object with an __aexit__ method (registering a call
566 to the method instead of the object itself).
567 """
568 _cb_type = type(exit)
569 try:
570 exit_method = _cb_type.__aexit__
571 except AttributeError:
572 # Not an async context manager, so assume it's a coroutine function
573 self._push_exit_callback(exit, False)
574 else:
575 self._push_async_cm_exit(exit, exit_method)
576 return exit # Allow use as a decorator
577
578 def push_async_callback(self, callback, *args, **kwds):
579 """Registers an arbitrary coroutine function and arguments.
580
581 Cannot suppress exceptions.
582 """
583 _exit_wrapper = self._create_async_cb_wrapper(callback, *args, **kwds)
584
585 # We changed the signature, so using @wraps is not appropriate, but
586 # setting __wrapped__ may still help with introspection.
587 _exit_wrapper.__wrapped__ = callback
588 self._push_exit_callback(_exit_wrapper, False)
589 return callback # Allow use as a decorator
590
591 async def aclose(self):
592 """Immediately unwind the context stack."""
593 await self.__aexit__(None, None, None)
594
595 def _push_async_cm_exit(self, cm, cm_exit):
596 """Helper to correctly register coroutine function to __aexit__
597 method."""
598 _exit_wrapper = self._create_async_exit_wrapper(cm, cm_exit)
599 _exit_wrapper.__self__ = cm
600 self._push_exit_callback(_exit_wrapper, False)
601
602 async def __aenter__(self):
603 return self
604
605 async def __aexit__(self, *exc_details):
606 received_exc = exc_details[0] is not None
607
608 # We manipulate the exception state so it behaves as though
609 # we were actually nesting multiple with statements
610 frame_exc = sys.exc_info()[1]
611 def _fix_exception_context(new_exc, old_exc):
612 # Context may not be correct, so find the end of the chain
613 while 1:
614 exc_context = new_exc.__context__
615 if exc_context is old_exc:
616 # Context is already set correctly (see issue 20317)
617 return
618 if exc_context is None or exc_context is frame_exc:
619 break
620 new_exc = exc_context
621 # Change the end of the chain to point to the exception
622 # we expect it to reference
623 new_exc.__context__ = old_exc
624
625 # Callbacks are invoked in LIFO order to match the behaviour of
626 # nested context managers
627 suppressed_exc = False
628 pending_raise = False
629 while self._exit_callbacks:
630 is_sync, cb = self._exit_callbacks.pop()
631 try:
632 if is_sync:
633 cb_suppress = cb(*exc_details)
634 else:
635 cb_suppress = await cb(*exc_details)
636
637 if cb_suppress:
638 suppressed_exc = True
639 pending_raise = False
640 exc_details = (None, None, None)
641 except:
642 new_exc_details = sys.exc_info()
643 # simulate the stack of exceptions by setting the context
644 _fix_exception_context(new_exc_details[1], exc_details[1])
645 pending_raise = True
646 exc_details = new_exc_details
647 if pending_raise:
648 try:
649 # bare "raise exc_details[1]" replaces our carefully
650 # set-up context
651 fixed_ctx = exc_details[1].__context__
652 raise exc_details[1]
653 except BaseException:
654 exc_details[1].__context__ = fixed_ctx
655 raise
656 return received_exc and suppressed_exc
657
Jesse-Bakker0784a2e2017-11-23 01:23:28 +0100658
659class nullcontext(AbstractContextManager):
660 """Context manager that does no additional processing.
661
662 Used as a stand-in for a normal context manager, when a particular
663 block of code is only sometimes used with a normal context manager:
664
665 cm = optional_cm if condition else nullcontext()
666 with cm:
667 # Perform operation, using optional_cm if condition is True
668 """
669
670 def __init__(self, enter_result=None):
671 self.enter_result = enter_result
672
673 def __enter__(self):
674 return self.enter_result
675
676 def __exit__(self, *excinfo):
677 pass