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