diff options
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r-- | Lib/contextlib.py | 42 |
1 files changed, 11 insertions, 31 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 6cf112af0d..5377987ddf 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -1,34 +1,11 @@ """Utilities for with-statement contexts. See PEP 343.""" -import abc + import sys from collections import deque from functools import wraps -__all__ = ["contextmanager", "closing", "AbstractContextManager", - "ContextDecorator", "ExitStack", "redirect_stdout", - "redirect_stderr", "suppress"] - - -class AbstractContextManager(abc.ABC): - - """An abstract base class for context managers.""" - - def __enter__(self): - """Return `self` upon entering the runtime context.""" - return self - - @abc.abstractmethod - def __exit__(self, exc_type, exc_value, traceback): - """Raise any exception triggered within the runtime context.""" - return None - - @classmethod - def __subclasshook__(cls, C): - if cls is AbstractContextManager: - if (any("__enter__" in B.__dict__ for B in C.__mro__) and - any("__exit__" in B.__dict__ for B in C.__mro__)): - return True - return NotImplemented +__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack", + "redirect_stdout", "redirect_stderr", "suppress"] class ContextDecorator(object): @@ -54,7 +31,7 @@ class ContextDecorator(object): return inner -class _GeneratorContextManager(ContextDecorator, AbstractContextManager): +class _GeneratorContextManager(ContextDecorator): """Helper for @contextmanager decorator.""" def __init__(self, func, args, kwds): @@ -157,7 +134,7 @@ def contextmanager(func): return helper -class closing(AbstractContextManager): +class closing(object): """Context to automatically close something at the end of a block. Code like this: @@ -182,7 +159,7 @@ class closing(AbstractContextManager): self.thing.close() -class _RedirectStream(AbstractContextManager): +class _RedirectStream: _stream = None @@ -222,7 +199,7 @@ class redirect_stderr(_RedirectStream): _stream = "stderr" -class suppress(AbstractContextManager): +class suppress: """Context manager to suppress specified exceptions After the exception is suppressed, execution proceeds with the next @@ -253,7 +230,7 @@ class suppress(AbstractContextManager): # Inspired by discussions on http://bugs.python.org/issue13585 -class ExitStack(AbstractContextManager): +class ExitStack(object): """Context manager for dynamic management of a stack of exit callbacks For example: @@ -332,6 +309,9 @@ class ExitStack(AbstractContextManager): """Immediately unwind the context stack""" self.__exit__(None, None, None) + def __enter__(self): + return self + def __exit__(self, *exc_details): received_exc = exc_details[0] is not None |