Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 1 | """Utilities for with-statement contexts. See PEP 343.""" |
| 2 | |
| 3 | import sys |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 4 | from collections import deque |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 5 | from functools import wraps |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 6 | |
Raymond Hettinger | 088cbf2 | 2013-10-10 00:46:57 -0700 | [diff] [blame] | 7 | __all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack", |
Berker Peksag | bb44fe0 | 2014-11-28 23:28:06 +0200 | [diff] [blame] | 8 | "redirect_stdout", "redirect_stderr", "suppress"] |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 9 | |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 10 | |
| 11 | class ContextDecorator(object): |
| 12 | "A base class or mixin that enables context managers to work as decorators." |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 13 | |
| 14 | def _recreate_cm(self): |
| 15 | """Return a recreated instance of self. |
Nick Coghlan | fdc2c55 | 2011-05-06 00:02:12 +1000 | [diff] [blame] | 16 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 17 | Allows an otherwise one-shot context manager like |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 18 | _GeneratorContextManager to support use as |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 19 | a decorator via implicit recreation. |
Nick Coghlan | fdc2c55 | 2011-05-06 00:02:12 +1000 | [diff] [blame] | 20 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 21 | This is a private interface just for _GeneratorContextManager. |
| 22 | See issue #11647 for details. |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 23 | """ |
| 24 | return self |
| 25 | |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 26 | def __call__(self, func): |
| 27 | @wraps(func) |
| 28 | def inner(*args, **kwds): |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 29 | with self._recreate_cm(): |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 30 | return func(*args, **kwds) |
| 31 | return inner |
| 32 | |
| 33 | |
Antoine Pitrou | 67b212e | 2011-01-08 09:55:31 +0000 | [diff] [blame] | 34 | class _GeneratorContextManager(ContextDecorator): |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 35 | """Helper for @contextmanager decorator.""" |
| 36 | |
Serhiy Storchaka | 101ff35 | 2015-06-28 17:06:07 +0300 | [diff] [blame] | 37 | def __init__(self, func, args, kwds): |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 38 | self.gen = func(*args, **kwds) |
| 39 | self.func, self.args, self.kwds = func, args, kwds |
Nick Coghlan | 059def5 | 2013-10-26 18:08:15 +1000 | [diff] [blame] | 40 | # Issue 19330: ensure context manager instances have good docstrings |
| 41 | doc = getattr(func, "__doc__", None) |
| 42 | if doc is None: |
| 43 | doc = type(self).__doc__ |
| 44 | self.__doc__ = doc |
| 45 | # Unfortunately, this still doesn't provide good help output when |
| 46 | # inspecting the created context manager instances, since pydoc |
| 47 | # currently bypasses the instance docstring and shows the docstring |
| 48 | # for the class instead. |
| 49 | # See http://bugs.python.org/issue19404 for more details. |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 50 | |
| 51 | def _recreate_cm(self): |
| 52 | # _GCM instances are one-shot context managers, so the |
| 53 | # CM must be recreated each time a decorated function is |
| 54 | # called |
Serhiy Storchaka | 101ff35 | 2015-06-28 17:06:07 +0300 | [diff] [blame] | 55 | return self.__class__(self.func, self.args, self.kwds) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 57 | def __enter__(self): |
| 58 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 59 | return next(self.gen) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 60 | except StopIteration: |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 61 | raise RuntimeError("generator didn't yield") from None |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 62 | |
| 63 | def __exit__(self, type, value, traceback): |
| 64 | if type is None: |
| 65 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 66 | next(self.gen) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 67 | except StopIteration: |
| 68 | return |
| 69 | else: |
| 70 | raise RuntimeError("generator didn't stop") |
| 71 | else: |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 72 | if value is None: |
| 73 | # Need to force instantiation so we can reliably |
| 74 | # tell if we get the same exception back |
| 75 | value = type() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 76 | try: |
| 77 | self.gen.throw(type, value, traceback) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | raise RuntimeError("generator didn't stop after throw()") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 79 | except StopIteration as exc: |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 80 | # Suppress StopIteration *unless* it's the same exception that |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | # was passed to throw(). This prevents a StopIteration |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 82 | # raised inside the "with" statement from being suppressed. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 83 | return exc is not value |
Yury Selivanov | 8170e8c | 2015-05-09 11:44:30 -0400 | [diff] [blame] | 84 | except RuntimeError as exc: |
| 85 | # Likewise, avoid suppressing if a StopIteration exception |
| 86 | # was passed to throw() and later wrapped into a RuntimeError |
| 87 | # (see PEP 479). |
| 88 | if exc.__cause__ is value: |
| 89 | return False |
| 90 | raise |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | except: |
| 92 | # only re-raise if it's *not* the exception that was |
| 93 | # passed to throw(), because __exit__() must not raise |
| 94 | # an exception unless __exit__() itself failed. But throw() |
| 95 | # has to raise the exception to signal propagation, so this |
| 96 | # fixes the impedance mismatch between the throw() protocol |
| 97 | # and the __exit__() protocol. |
| 98 | # |
| 99 | if sys.exc_info()[1] is not value: |
| 100 | raise |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def contextmanager(func): |
| 104 | """@contextmanager decorator. |
| 105 | |
| 106 | Typical usage: |
| 107 | |
| 108 | @contextmanager |
| 109 | def some_generator(<arguments>): |
| 110 | <setup> |
| 111 | try: |
| 112 | yield <value> |
| 113 | finally: |
| 114 | <cleanup> |
| 115 | |
| 116 | This makes this: |
| 117 | |
| 118 | with some_generator(<arguments>) as <variable>: |
| 119 | <body> |
| 120 | |
| 121 | equivalent to this: |
| 122 | |
| 123 | <setup> |
| 124 | try: |
| 125 | <variable> = <value> |
| 126 | <body> |
| 127 | finally: |
| 128 | <cleanup> |
| 129 | |
| 130 | """ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 131 | @wraps(func) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 132 | def helper(*args, **kwds): |
Serhiy Storchaka | 101ff35 | 2015-06-28 17:06:07 +0300 | [diff] [blame] | 133 | return _GeneratorContextManager(func, args, kwds) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 134 | return helper |
| 135 | |
| 136 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 137 | class closing(object): |
| 138 | """Context to automatically close something at the end of a block. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 139 | |
| 140 | Code like this: |
| 141 | |
| 142 | with closing(<module>.open(<arguments>)) as f: |
| 143 | <block> |
| 144 | |
| 145 | is equivalent to this: |
| 146 | |
| 147 | f = <module>.open(<arguments>) |
| 148 | try: |
| 149 | <block> |
| 150 | finally: |
| 151 | f.close() |
| 152 | |
| 153 | """ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 154 | def __init__(self, thing): |
| 155 | self.thing = thing |
| 156 | def __enter__(self): |
| 157 | return self.thing |
| 158 | def __exit__(self, *exc_info): |
| 159 | self.thing.close() |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 160 | |
Berker Peksag | bb44fe0 | 2014-11-28 23:28:06 +0200 | [diff] [blame] | 161 | |
| 162 | class _RedirectStream: |
| 163 | |
| 164 | _stream = None |
| 165 | |
| 166 | def __init__(self, new_target): |
| 167 | self._new_target = new_target |
| 168 | # We use a list of old targets to make this CM re-entrant |
| 169 | self._old_targets = [] |
| 170 | |
| 171 | def __enter__(self): |
| 172 | self._old_targets.append(getattr(sys, self._stream)) |
| 173 | setattr(sys, self._stream, self._new_target) |
| 174 | return self._new_target |
| 175 | |
| 176 | def __exit__(self, exctype, excinst, exctb): |
| 177 | setattr(sys, self._stream, self._old_targets.pop()) |
| 178 | |
| 179 | |
| 180 | class redirect_stdout(_RedirectStream): |
| 181 | """Context manager for temporarily redirecting stdout to another file. |
Nick Coghlan | 059def5 | 2013-10-26 18:08:15 +1000 | [diff] [blame] | 182 | |
| 183 | # How to send help() to stderr |
| 184 | with redirect_stdout(sys.stderr): |
| 185 | help(dir) |
| 186 | |
| 187 | # How to write help() to a file |
| 188 | with open('help.txt', 'w') as f: |
| 189 | with redirect_stdout(f): |
| 190 | help(pow) |
| 191 | """ |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 192 | |
Berker Peksag | bb44fe0 | 2014-11-28 23:28:06 +0200 | [diff] [blame] | 193 | _stream = "stdout" |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 194 | |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 195 | |
Berker Peksag | bb44fe0 | 2014-11-28 23:28:06 +0200 | [diff] [blame] | 196 | class redirect_stderr(_RedirectStream): |
| 197 | """Context manager for temporarily redirecting stderr to another file.""" |
| 198 | |
| 199 | _stream = "stderr" |
Raymond Hettinger | 088cbf2 | 2013-10-10 00:46:57 -0700 | [diff] [blame] | 200 | |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 201 | |
Nick Coghlan | 059def5 | 2013-10-26 18:08:15 +1000 | [diff] [blame] | 202 | class suppress: |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 203 | """Context manager to suppress specified exceptions |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 204 | |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 205 | After the exception is suppressed, execution proceeds with the next |
| 206 | statement following the with statement. |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 207 | |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 208 | with suppress(FileNotFoundError): |
| 209 | os.remove(somefile) |
| 210 | # Execution still resumes here if the file was already removed |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 211 | """ |
Nick Coghlan | 059def5 | 2013-10-26 18:08:15 +1000 | [diff] [blame] | 212 | |
| 213 | def __init__(self, *exceptions): |
| 214 | self._exceptions = exceptions |
| 215 | |
| 216 | def __enter__(self): |
| 217 | pass |
| 218 | |
| 219 | def __exit__(self, exctype, excinst, exctb): |
| 220 | # Unlike isinstance and issubclass, CPython exception handling |
| 221 | # currently only looks at the concrete type hierarchy (ignoring |
| 222 | # the instance and subclass checking hooks). While Guido considers |
| 223 | # that a bug rather than a feature, it's a fairly hard one to fix |
| 224 | # due to various internal implementation details. suppress provides |
| 225 | # the simpler issubclass based semantics, rather than trying to |
| 226 | # exactly reproduce the limitations of the CPython interpreter. |
| 227 | # |
| 228 | # See http://bugs.python.org/issue12029 for more details |
| 229 | return exctype is not None and issubclass(exctype, self._exceptions) |
| 230 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 231 | |
| 232 | # Inspired by discussions on http://bugs.python.org/issue13585 |
| 233 | class ExitStack(object): |
| 234 | """Context manager for dynamic management of a stack of exit callbacks |
| 235 | |
| 236 | For example: |
| 237 | |
| 238 | with ExitStack() as stack: |
| 239 | files = [stack.enter_context(open(fname)) for fname in filenames] |
| 240 | # All opened files will automatically be closed at the end of |
| 241 | # the with statement, even if attempts to open files later |
Andrew Svetlov | 5b89840 | 2012-12-18 21:26:36 +0200 | [diff] [blame] | 242 | # in the list raise an exception |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 243 | |
| 244 | """ |
| 245 | def __init__(self): |
| 246 | self._exit_callbacks = deque() |
| 247 | |
| 248 | def pop_all(self): |
| 249 | """Preserve the context stack by transferring it to a new instance""" |
| 250 | new_stack = type(self)() |
| 251 | new_stack._exit_callbacks = self._exit_callbacks |
| 252 | self._exit_callbacks = deque() |
| 253 | return new_stack |
| 254 | |
| 255 | def _push_cm_exit(self, cm, cm_exit): |
| 256 | """Helper to correctly register callbacks to __exit__ methods""" |
| 257 | def _exit_wrapper(*exc_details): |
| 258 | return cm_exit(cm, *exc_details) |
| 259 | _exit_wrapper.__self__ = cm |
| 260 | self.push(_exit_wrapper) |
| 261 | |
| 262 | def push(self, exit): |
| 263 | """Registers a callback with the standard __exit__ method signature |
| 264 | |
| 265 | Can suppress exceptions the same way __exit__ methods can. |
| 266 | |
| 267 | Also accepts any object with an __exit__ method (registering a call |
| 268 | to the method instead of the object itself) |
| 269 | """ |
| 270 | # We use an unbound method rather than a bound method to follow |
| 271 | # the standard lookup behaviour for special methods |
| 272 | _cb_type = type(exit) |
| 273 | try: |
| 274 | exit_method = _cb_type.__exit__ |
| 275 | except AttributeError: |
| 276 | # Not a context manager, so assume its a callable |
| 277 | self._exit_callbacks.append(exit) |
| 278 | else: |
| 279 | self._push_cm_exit(exit, exit_method) |
| 280 | return exit # Allow use as a decorator |
| 281 | |
| 282 | def callback(self, callback, *args, **kwds): |
| 283 | """Registers an arbitrary callback and arguments. |
| 284 | |
| 285 | Cannot suppress exceptions. |
| 286 | """ |
| 287 | def _exit_wrapper(exc_type, exc, tb): |
| 288 | callback(*args, **kwds) |
| 289 | # We changed the signature, so using @wraps is not appropriate, but |
| 290 | # setting __wrapped__ may still help with introspection |
| 291 | _exit_wrapper.__wrapped__ = callback |
| 292 | self.push(_exit_wrapper) |
| 293 | return callback # Allow use as a decorator |
| 294 | |
| 295 | def enter_context(self, cm): |
| 296 | """Enters the supplied context manager |
| 297 | |
| 298 | If successful, also pushes its __exit__ method as a callback and |
| 299 | returns the result of the __enter__ method. |
| 300 | """ |
| 301 | # We look up the special methods on the type to match the with statement |
| 302 | _cm_type = type(cm) |
| 303 | _exit = _cm_type.__exit__ |
| 304 | result = _cm_type.__enter__(cm) |
| 305 | self._push_cm_exit(cm, _exit) |
| 306 | return result |
| 307 | |
| 308 | def close(self): |
| 309 | """Immediately unwind the context stack""" |
| 310 | self.__exit__(None, None, None) |
| 311 | |
| 312 | def __enter__(self): |
| 313 | return self |
| 314 | |
| 315 | def __exit__(self, *exc_details): |
Nick Coghlan | 1a33b2f | 2013-10-01 23:24:56 +1000 | [diff] [blame] | 316 | received_exc = exc_details[0] is not None |
| 317 | |
Nick Coghlan | 77452fc | 2012-06-01 22:48:32 +1000 | [diff] [blame] | 318 | # We manipulate the exception state so it behaves as though |
| 319 | # we were actually nesting multiple with statements |
| 320 | frame_exc = sys.exc_info()[1] |
| 321 | def _fix_exception_context(new_exc, old_exc): |
Nick Coghlan | add94c9 | 2014-01-24 23:05:45 +1000 | [diff] [blame] | 322 | # Context may not be correct, so find the end of the chain |
Nick Coghlan | 77452fc | 2012-06-01 22:48:32 +1000 | [diff] [blame] | 323 | while 1: |
| 324 | exc_context = new_exc.__context__ |
Nick Coghlan | 09761e7 | 2014-01-22 22:24:46 +1000 | [diff] [blame] | 325 | if exc_context is old_exc: |
| 326 | # Context is already set correctly (see issue 20317) |
| 327 | return |
| 328 | if exc_context is None or exc_context is frame_exc: |
Nick Coghlan | 77452fc | 2012-06-01 22:48:32 +1000 | [diff] [blame] | 329 | break |
| 330 | new_exc = exc_context |
Nick Coghlan | 09761e7 | 2014-01-22 22:24:46 +1000 | [diff] [blame] | 331 | # Change the end of the chain to point to the exception |
| 332 | # we expect it to reference |
Nick Coghlan | 77452fc | 2012-06-01 22:48:32 +1000 | [diff] [blame] | 333 | new_exc.__context__ = old_exc |
| 334 | |
Nick Coghlan | a5bd2a1 | 2012-06-01 00:00:38 +1000 | [diff] [blame] | 335 | # Callbacks are invoked in LIFO order to match the behaviour of |
| 336 | # nested context managers |
| 337 | suppressed_exc = False |
Nick Coghlan | 1a33b2f | 2013-10-01 23:24:56 +1000 | [diff] [blame] | 338 | pending_raise = False |
Nick Coghlan | a5bd2a1 | 2012-06-01 00:00:38 +1000 | [diff] [blame] | 339 | while self._exit_callbacks: |
| 340 | cb = self._exit_callbacks.pop() |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 341 | try: |
Nick Coghlan | a5bd2a1 | 2012-06-01 00:00:38 +1000 | [diff] [blame] | 342 | if cb(*exc_details): |
| 343 | suppressed_exc = True |
Nick Coghlan | 1a33b2f | 2013-10-01 23:24:56 +1000 | [diff] [blame] | 344 | pending_raise = False |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 345 | exc_details = (None, None, None) |
Nick Coghlan | a5bd2a1 | 2012-06-01 00:00:38 +1000 | [diff] [blame] | 346 | except: |
| 347 | new_exc_details = sys.exc_info() |
Nick Coghlan | 77452fc | 2012-06-01 22:48:32 +1000 | [diff] [blame] | 348 | # simulate the stack of exceptions by setting the context |
| 349 | _fix_exception_context(new_exc_details[1], exc_details[1]) |
Nick Coghlan | 1a33b2f | 2013-10-01 23:24:56 +1000 | [diff] [blame] | 350 | pending_raise = True |
Nick Coghlan | a5bd2a1 | 2012-06-01 00:00:38 +1000 | [diff] [blame] | 351 | exc_details = new_exc_details |
Nick Coghlan | 1a33b2f | 2013-10-01 23:24:56 +1000 | [diff] [blame] | 352 | if pending_raise: |
| 353 | try: |
| 354 | # bare "raise exc_details[1]" replaces our carefully |
| 355 | # set-up context |
| 356 | fixed_ctx = exc_details[1].__context__ |
| 357 | raise exc_details[1] |
| 358 | except BaseException: |
| 359 | exc_details[1].__context__ = fixed_ctx |
| 360 | raise |
| 361 | return received_exc and suppressed_exc |