blob: a564943d87a85937f4c7f29963a941d0b214c8ef [file] [log] [blame]
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00001"""Utilities for with-statement contexts. See PEP 343."""
2
3import sys
Nick Coghlan3267a302012-05-21 22:54:43 +10004from collections import deque
Christian Heimes81ee3ef2008-05-04 22:42:01 +00005from functools import wraps
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00006
Raymond Hettinger088cbf22013-10-10 00:46:57 -07007__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack",
Nick Coghlan240f86d2013-10-17 23:40:57 +10008 "redirect_stdout", "suppress"]
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00009
Michael Foordb3a89842010-06-30 12:17:50 +000010
11class ContextDecorator(object):
12 "A base class or mixin that enables context managers to work as decorators."
Nick Coghlan0ded3e32011-05-05 23:49:25 +100013
14 def _recreate_cm(self):
15 """Return a recreated instance of self.
Nick Coghlanfdc2c552011-05-06 00:02:12 +100016
Nick Coghlan3267a302012-05-21 22:54:43 +100017 Allows an otherwise one-shot context manager like
Nick Coghlan0ded3e32011-05-05 23:49:25 +100018 _GeneratorContextManager to support use as
Nick Coghlan3267a302012-05-21 22:54:43 +100019 a decorator via implicit recreation.
Nick Coghlanfdc2c552011-05-06 00:02:12 +100020
Nick Coghlan3267a302012-05-21 22:54:43 +100021 This is a private interface just for _GeneratorContextManager.
22 See issue #11647 for details.
Nick Coghlan0ded3e32011-05-05 23:49:25 +100023 """
24 return self
25
Michael Foordb3a89842010-06-30 12:17:50 +000026 def __call__(self, func):
27 @wraps(func)
28 def inner(*args, **kwds):
Nick Coghlan0ded3e32011-05-05 23:49:25 +100029 with self._recreate_cm():
Michael Foordb3a89842010-06-30 12:17:50 +000030 return func(*args, **kwds)
31 return inner
32
33
Antoine Pitrou67b212e2011-01-08 09:55:31 +000034class _GeneratorContextManager(ContextDecorator):
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000035 """Helper for @contextmanager decorator."""
36
Nick Coghlan0ded3e32011-05-05 23:49:25 +100037 def __init__(self, func, *args, **kwds):
38 self.gen = func(*args, **kwds)
39 self.func, self.args, self.kwds = func, args, kwds
40
41 def _recreate_cm(self):
42 # _GCM instances are one-shot context managers, so the
43 # CM must be recreated each time a decorated function is
44 # called
45 return self.__class__(self.func, *self.args, **self.kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000046
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000047 def __enter__(self):
48 try:
Georg Brandla18af4e2007-04-21 15:47:16 +000049 return next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000050 except StopIteration:
Nick Coghlan8608d262013-10-20 00:30:51 +100051 raise RuntimeError("generator didn't yield") from None
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000052
53 def __exit__(self, type, value, traceback):
54 if type is None:
55 try:
Georg Brandla18af4e2007-04-21 15:47:16 +000056 next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000057 except StopIteration:
58 return
59 else:
60 raise RuntimeError("generator didn't stop")
61 else:
Guido van Rossum2cc30da2007-11-02 23:46:40 +000062 if value is None:
63 # Need to force instantiation so we can reliably
64 # tell if we get the same exception back
65 value = type()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000066 try:
67 self.gen.throw(type, value, traceback)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 raise RuntimeError("generator didn't stop after throw()")
Guido van Rossumb940e112007-01-10 16:19:56 +000069 except StopIteration as exc:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 # Suppress the exception *unless* it's the same exception that
71 # was passed to throw(). This prevents a StopIteration
72 # raised inside the "with" statement from being suppressed
73 return exc is not value
74 except:
75 # only re-raise if it's *not* the exception that was
76 # passed to throw(), because __exit__() must not raise
77 # an exception unless __exit__() itself failed. But throw()
78 # has to raise the exception to signal propagation, so this
79 # fixes the impedance mismatch between the throw() protocol
80 # and the __exit__() protocol.
81 #
82 if sys.exc_info()[1] is not value:
83 raise
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000084
85
86def contextmanager(func):
87 """@contextmanager decorator.
88
89 Typical usage:
90
91 @contextmanager
92 def some_generator(<arguments>):
93 <setup>
94 try:
95 yield <value>
96 finally:
97 <cleanup>
98
99 This makes this:
100
101 with some_generator(<arguments>) as <variable>:
102 <body>
103
104 equivalent to this:
105
106 <setup>
107 try:
108 <variable> = <value>
109 <body>
110 finally:
111 <cleanup>
112
113 """
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000114 @wraps(func)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000115 def helper(*args, **kwds):
Nick Coghlan0ded3e32011-05-05 23:49:25 +1000116 return _GeneratorContextManager(func, *args, **kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000117 return helper
118
119
Nick Coghlan8608d262013-10-20 00:30:51 +1000120# Unfortunately, this was originally published as a class, so
121# backwards compatibility prevents the use of the wrapper function
122# approach used for the other classes
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123class closing(object):
124 """Context to automatically close something at the end of a block.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000125
126 Code like this:
127
128 with closing(<module>.open(<arguments>)) as f:
129 <block>
130
131 is equivalent to this:
132
133 f = <module>.open(<arguments>)
134 try:
135 <block>
136 finally:
137 f.close()
138
139 """
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140 def __init__(self, thing):
141 self.thing = thing
142 def __enter__(self):
143 return self.thing
144 def __exit__(self, *exc_info):
145 self.thing.close()
Nick Coghlan3267a302012-05-21 22:54:43 +1000146
Nick Coghlan8608d262013-10-20 00:30:51 +1000147class _RedirectStdout:
148 """Helper for redirect_stdout."""
149
150 def __init__(self, new_target):
151 self._new_target = new_target
152 self._old_target = self._sentinel = object()
153
154 def __enter__(self):
155 if self._old_target is not self._sentinel:
156 raise RuntimeError("Cannot reenter {!r}".format(self))
157 self._old_target = sys.stdout
158 sys.stdout = self._new_target
159 return self._new_target
160
161 def __exit__(self, exctype, excinst, exctb):
162 restore_stdout = self._old_target
163 self._old_target = self._sentinel
164 sys.stdout = restore_stdout
165
166# Use a wrapper function since we don't care about supporting inheritance
167# and a function gives much cleaner output in help()
168def redirect_stdout(target):
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700169 """Context manager for temporarily redirecting stdout to another file
170
171 # How to send help() to stderr
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700172 with redirect_stdout(sys.stderr):
173 help(dir)
174
175 # How to write help() to a file
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700176 with open('help.txt', 'w') as f:
177 with redirect_stdout(f):
178 help(pow)
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700179 """
Nick Coghlan8608d262013-10-20 00:30:51 +1000180 return _RedirectStdout(target)
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700181
Nick Coghlan8608d262013-10-20 00:30:51 +1000182
183class _SuppressExceptions:
184 """Helper for suppress."""
185 def __init__(self, *exceptions):
186 self._exceptions = exceptions
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700187
188 def __enter__(self):
Nick Coghlan8608d262013-10-20 00:30:51 +1000189 pass
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700190
191 def __exit__(self, exctype, excinst, exctb):
Nick Coghlan8608d262013-10-20 00:30:51 +1000192 # Unlike isinstance and issubclass, exception handling only
193 # looks at the concrete type heirarchy (ignoring the instance
194 # and subclass checking hooks). However, all exceptions are
195 # also required to be concrete subclasses of BaseException, so
196 # if there's a discrepancy in behaviour, we currently consider it
197 # the fault of the strange way the exception has been defined rather
198 # than the fact that issubclass can be customised while the
199 # exception checks can't.
200 # See http://bugs.python.org/issue12029 for more details
201 return exctype is not None and issubclass(exctype, self._exceptions)
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700202
Nick Coghlan8608d262013-10-20 00:30:51 +1000203# Use a wrapper function since we don't care about supporting inheritance
204# and a function gives much cleaner output in help()
Nick Coghlan240f86d2013-10-17 23:40:57 +1000205def suppress(*exceptions):
206 """Context manager to suppress specified exceptions
Raymond Hettingere318a882013-03-10 22:26:51 -0700207
Nick Coghlan8608d262013-10-20 00:30:51 +1000208 After the exception is suppressed, execution proceeds with the next
209 statement following the with statement.
Raymond Hettingere318a882013-03-10 22:26:51 -0700210
Nick Coghlan8608d262013-10-20 00:30:51 +1000211 with suppress(FileNotFoundError):
212 os.remove(somefile)
213 # Execution still resumes here if the file was already removed
Raymond Hettingere318a882013-03-10 22:26:51 -0700214 """
Nick Coghlan8608d262013-10-20 00:30:51 +1000215 return _SuppressExceptions(*exceptions)
Nick Coghlan3267a302012-05-21 22:54:43 +1000216
217# Inspired by discussions on http://bugs.python.org/issue13585
218class ExitStack(object):
219 """Context manager for dynamic management of a stack of exit callbacks
220
221 For example:
222
223 with ExitStack() as stack:
224 files = [stack.enter_context(open(fname)) for fname in filenames]
225 # All opened files will automatically be closed at the end of
226 # the with statement, even if attempts to open files later
Andrew Svetlov5b898402012-12-18 21:26:36 +0200227 # in the list raise an exception
Nick Coghlan3267a302012-05-21 22:54:43 +1000228
229 """
230 def __init__(self):
231 self._exit_callbacks = deque()
232
233 def pop_all(self):
234 """Preserve the context stack by transferring it to a new instance"""
235 new_stack = type(self)()
236 new_stack._exit_callbacks = self._exit_callbacks
237 self._exit_callbacks = deque()
238 return new_stack
239
240 def _push_cm_exit(self, cm, cm_exit):
241 """Helper to correctly register callbacks to __exit__ methods"""
242 def _exit_wrapper(*exc_details):
243 return cm_exit(cm, *exc_details)
244 _exit_wrapper.__self__ = cm
245 self.push(_exit_wrapper)
246
247 def push(self, exit):
248 """Registers a callback with the standard __exit__ method signature
249
250 Can suppress exceptions the same way __exit__ methods can.
251
252 Also accepts any object with an __exit__ method (registering a call
253 to the method instead of the object itself)
254 """
255 # We use an unbound method rather than a bound method to follow
256 # the standard lookup behaviour for special methods
257 _cb_type = type(exit)
258 try:
259 exit_method = _cb_type.__exit__
260 except AttributeError:
261 # Not a context manager, so assume its a callable
262 self._exit_callbacks.append(exit)
263 else:
264 self._push_cm_exit(exit, exit_method)
265 return exit # Allow use as a decorator
266
267 def callback(self, callback, *args, **kwds):
268 """Registers an arbitrary callback and arguments.
269
270 Cannot suppress exceptions.
271 """
272 def _exit_wrapper(exc_type, exc, tb):
273 callback(*args, **kwds)
274 # We changed the signature, so using @wraps is not appropriate, but
275 # setting __wrapped__ may still help with introspection
276 _exit_wrapper.__wrapped__ = callback
277 self.push(_exit_wrapper)
278 return callback # Allow use as a decorator
279
280 def enter_context(self, cm):
281 """Enters the supplied context manager
282
283 If successful, also pushes its __exit__ method as a callback and
284 returns the result of the __enter__ method.
285 """
286 # We look up the special methods on the type to match the with statement
287 _cm_type = type(cm)
288 _exit = _cm_type.__exit__
289 result = _cm_type.__enter__(cm)
290 self._push_cm_exit(cm, _exit)
291 return result
292
293 def close(self):
294 """Immediately unwind the context stack"""
295 self.__exit__(None, None, None)
296
297 def __enter__(self):
298 return self
299
300 def __exit__(self, *exc_details):
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000301 received_exc = exc_details[0] is not None
302
Nick Coghlan77452fc2012-06-01 22:48:32 +1000303 # We manipulate the exception state so it behaves as though
304 # we were actually nesting multiple with statements
305 frame_exc = sys.exc_info()[1]
306 def _fix_exception_context(new_exc, old_exc):
307 while 1:
308 exc_context = new_exc.__context__
309 if exc_context in (None, frame_exc):
310 break
311 new_exc = exc_context
312 new_exc.__context__ = old_exc
313
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000314 # Callbacks are invoked in LIFO order to match the behaviour of
315 # nested context managers
316 suppressed_exc = False
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000317 pending_raise = False
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000318 while self._exit_callbacks:
319 cb = self._exit_callbacks.pop()
Nick Coghlan3267a302012-05-21 22:54:43 +1000320 try:
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000321 if cb(*exc_details):
322 suppressed_exc = True
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000323 pending_raise = False
Nick Coghlan3267a302012-05-21 22:54:43 +1000324 exc_details = (None, None, None)
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000325 except:
326 new_exc_details = sys.exc_info()
Nick Coghlan77452fc2012-06-01 22:48:32 +1000327 # simulate the stack of exceptions by setting the context
328 _fix_exception_context(new_exc_details[1], exc_details[1])
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000329 pending_raise = True
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000330 exc_details = new_exc_details
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000331 if pending_raise:
332 try:
333 # bare "raise exc_details[1]" replaces our carefully
334 # set-up context
335 fixed_ctx = exc_details[1].__context__
336 raise exc_details[1]
337 except BaseException:
338 exc_details[1].__context__ = fixed_ctx
339 raise
340 return received_exc and suppressed_exc