blob: 2fbc90cdc8cad0cd7bea4770d9d4dc0e95399421 [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",
Berker Peksagbb44fe02014-11-28 23:28:06 +02008 "redirect_stdout", "redirect_stderr", "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
Nick Coghlan059def52013-10-26 18:08:15 +100040 # 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 Coghlan0ded3e32011-05-05 23:49:25 +100050
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
55 return self.__class__(self.func, *self.args, **self.kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000056
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000057 def __enter__(self):
58 try:
Georg Brandla18af4e2007-04-21 15:47:16 +000059 return next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000060 except StopIteration:
Nick Coghlan8608d262013-10-20 00:30:51 +100061 raise RuntimeError("generator didn't yield") from None
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000062
63 def __exit__(self, type, value, traceback):
64 if type is None:
65 try:
Georg Brandla18af4e2007-04-21 15:47:16 +000066 next(self.gen)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000067 except StopIteration:
68 return
69 else:
70 raise RuntimeError("generator didn't stop")
71 else:
Guido van Rossum2cc30da2007-11-02 23:46:40 +000072 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 Rossum1a5e21e2006-02-28 21:57:43 +000076 try:
77 self.gen.throw(type, value, traceback)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 raise RuntimeError("generator didn't stop after throw()")
Guido van Rossumb940e112007-01-10 16:19:56 +000079 except StopIteration as exc:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080 # Suppress the exception *unless* it's the same exception that
81 # was passed to throw(). This prevents a StopIteration
82 # raised inside the "with" statement from being suppressed
83 return exc is not value
84 except:
85 # only re-raise if it's *not* the exception that was
86 # passed to throw(), because __exit__() must not raise
87 # an exception unless __exit__() itself failed. But throw()
88 # has to raise the exception to signal propagation, so this
89 # fixes the impedance mismatch between the throw() protocol
90 # and the __exit__() protocol.
91 #
92 if sys.exc_info()[1] is not value:
93 raise
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000094
95
96def contextmanager(func):
97 """@contextmanager decorator.
98
99 Typical usage:
100
101 @contextmanager
102 def some_generator(<arguments>):
103 <setup>
104 try:
105 yield <value>
106 finally:
107 <cleanup>
108
109 This makes this:
110
111 with some_generator(<arguments>) as <variable>:
112 <body>
113
114 equivalent to this:
115
116 <setup>
117 try:
118 <variable> = <value>
119 <body>
120 finally:
121 <cleanup>
122
123 """
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000124 @wraps(func)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000125 def helper(*args, **kwds):
Nick Coghlan0ded3e32011-05-05 23:49:25 +1000126 return _GeneratorContextManager(func, *args, **kwds)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000127 return helper
128
129
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130class closing(object):
131 """Context to automatically close something at the end of a block.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000132
133 Code like this:
134
135 with closing(<module>.open(<arguments>)) as f:
136 <block>
137
138 is equivalent to this:
139
140 f = <module>.open(<arguments>)
141 try:
142 <block>
143 finally:
144 f.close()
145
146 """
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 def __init__(self, thing):
148 self.thing = thing
149 def __enter__(self):
150 return self.thing
151 def __exit__(self, *exc_info):
152 self.thing.close()
Nick Coghlan3267a302012-05-21 22:54:43 +1000153
Berker Peksagbb44fe02014-11-28 23:28:06 +0200154
155class _RedirectStream:
156
157 _stream = None
158
159 def __init__(self, new_target):
160 self._new_target = new_target
161 # We use a list of old targets to make this CM re-entrant
162 self._old_targets = []
163
164 def __enter__(self):
165 self._old_targets.append(getattr(sys, self._stream))
166 setattr(sys, self._stream, self._new_target)
167 return self._new_target
168
169 def __exit__(self, exctype, excinst, exctb):
170 setattr(sys, self._stream, self._old_targets.pop())
171
172
173class redirect_stdout(_RedirectStream):
174 """Context manager for temporarily redirecting stdout to another file.
Nick Coghlan059def52013-10-26 18:08:15 +1000175
176 # How to send help() to stderr
177 with redirect_stdout(sys.stderr):
178 help(dir)
179
180 # How to write help() to a file
181 with open('help.txt', 'w') as f:
182 with redirect_stdout(f):
183 help(pow)
184 """
Nick Coghlan8608d262013-10-20 00:30:51 +1000185
Berker Peksagbb44fe02014-11-28 23:28:06 +0200186 _stream = "stdout"
Nick Coghlan8608d262013-10-20 00:30:51 +1000187
Nick Coghlan8608d262013-10-20 00:30:51 +1000188
Berker Peksagbb44fe02014-11-28 23:28:06 +0200189class redirect_stderr(_RedirectStream):
190 """Context manager for temporarily redirecting stderr to another file."""
191
192 _stream = "stderr"
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700193
Nick Coghlan8608d262013-10-20 00:30:51 +1000194
Nick Coghlan059def52013-10-26 18:08:15 +1000195class suppress:
Nick Coghlan240f86d2013-10-17 23:40:57 +1000196 """Context manager to suppress specified exceptions
Raymond Hettingere318a882013-03-10 22:26:51 -0700197
Nick Coghlan8608d262013-10-20 00:30:51 +1000198 After the exception is suppressed, execution proceeds with the next
199 statement following the with statement.
Raymond Hettingere318a882013-03-10 22:26:51 -0700200
Nick Coghlan8608d262013-10-20 00:30:51 +1000201 with suppress(FileNotFoundError):
202 os.remove(somefile)
203 # Execution still resumes here if the file was already removed
Raymond Hettingere318a882013-03-10 22:26:51 -0700204 """
Nick Coghlan059def52013-10-26 18:08:15 +1000205
206 def __init__(self, *exceptions):
207 self._exceptions = exceptions
208
209 def __enter__(self):
210 pass
211
212 def __exit__(self, exctype, excinst, exctb):
213 # Unlike isinstance and issubclass, CPython exception handling
214 # currently only looks at the concrete type hierarchy (ignoring
215 # the instance and subclass checking hooks). While Guido considers
216 # that a bug rather than a feature, it's a fairly hard one to fix
217 # due to various internal implementation details. suppress provides
218 # the simpler issubclass based semantics, rather than trying to
219 # exactly reproduce the limitations of the CPython interpreter.
220 #
221 # See http://bugs.python.org/issue12029 for more details
222 return exctype is not None and issubclass(exctype, self._exceptions)
223
Nick Coghlan3267a302012-05-21 22:54:43 +1000224
225# Inspired by discussions on http://bugs.python.org/issue13585
226class ExitStack(object):
227 """Context manager for dynamic management of a stack of exit callbacks
228
229 For example:
230
231 with ExitStack() as stack:
232 files = [stack.enter_context(open(fname)) for fname in filenames]
233 # All opened files will automatically be closed at the end of
234 # the with statement, even if attempts to open files later
Andrew Svetlov5b898402012-12-18 21:26:36 +0200235 # in the list raise an exception
Nick Coghlan3267a302012-05-21 22:54:43 +1000236
237 """
238 def __init__(self):
239 self._exit_callbacks = deque()
240
241 def pop_all(self):
242 """Preserve the context stack by transferring it to a new instance"""
243 new_stack = type(self)()
244 new_stack._exit_callbacks = self._exit_callbacks
245 self._exit_callbacks = deque()
246 return new_stack
247
248 def _push_cm_exit(self, cm, cm_exit):
249 """Helper to correctly register callbacks to __exit__ methods"""
250 def _exit_wrapper(*exc_details):
251 return cm_exit(cm, *exc_details)
252 _exit_wrapper.__self__ = cm
253 self.push(_exit_wrapper)
254
255 def push(self, exit):
256 """Registers a callback with the standard __exit__ method signature
257
258 Can suppress exceptions the same way __exit__ methods can.
259
260 Also accepts any object with an __exit__ method (registering a call
261 to the method instead of the object itself)
262 """
263 # We use an unbound method rather than a bound method to follow
264 # the standard lookup behaviour for special methods
265 _cb_type = type(exit)
266 try:
267 exit_method = _cb_type.__exit__
268 except AttributeError:
269 # Not a context manager, so assume its a callable
270 self._exit_callbacks.append(exit)
271 else:
272 self._push_cm_exit(exit, exit_method)
273 return exit # Allow use as a decorator
274
275 def callback(self, callback, *args, **kwds):
276 """Registers an arbitrary callback and arguments.
277
278 Cannot suppress exceptions.
279 """
280 def _exit_wrapper(exc_type, exc, tb):
281 callback(*args, **kwds)
282 # We changed the signature, so using @wraps is not appropriate, but
283 # setting __wrapped__ may still help with introspection
284 _exit_wrapper.__wrapped__ = callback
285 self.push(_exit_wrapper)
286 return callback # Allow use as a decorator
287
288 def enter_context(self, cm):
289 """Enters the supplied context manager
290
291 If successful, also pushes its __exit__ method as a callback and
292 returns the result of the __enter__ method.
293 """
294 # We look up the special methods on the type to match the with statement
295 _cm_type = type(cm)
296 _exit = _cm_type.__exit__
297 result = _cm_type.__enter__(cm)
298 self._push_cm_exit(cm, _exit)
299 return result
300
301 def close(self):
302 """Immediately unwind the context stack"""
303 self.__exit__(None, None, None)
304
305 def __enter__(self):
306 return self
307
308 def __exit__(self, *exc_details):
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000309 received_exc = exc_details[0] is not None
310
Nick Coghlan77452fc2012-06-01 22:48:32 +1000311 # We manipulate the exception state so it behaves as though
312 # we were actually nesting multiple with statements
313 frame_exc = sys.exc_info()[1]
314 def _fix_exception_context(new_exc, old_exc):
Nick Coghlanadd94c92014-01-24 23:05:45 +1000315 # Context may not be correct, so find the end of the chain
Nick Coghlan77452fc2012-06-01 22:48:32 +1000316 while 1:
317 exc_context = new_exc.__context__
Nick Coghlan09761e72014-01-22 22:24:46 +1000318 if exc_context is old_exc:
319 # Context is already set correctly (see issue 20317)
320 return
321 if exc_context is None or exc_context is frame_exc:
Nick Coghlan77452fc2012-06-01 22:48:32 +1000322 break
323 new_exc = exc_context
Nick Coghlan09761e72014-01-22 22:24:46 +1000324 # Change the end of the chain to point to the exception
325 # we expect it to reference
Nick Coghlan77452fc2012-06-01 22:48:32 +1000326 new_exc.__context__ = old_exc
327
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000328 # Callbacks are invoked in LIFO order to match the behaviour of
329 # nested context managers
330 suppressed_exc = False
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000331 pending_raise = False
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000332 while self._exit_callbacks:
333 cb = self._exit_callbacks.pop()
Nick Coghlan3267a302012-05-21 22:54:43 +1000334 try:
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000335 if cb(*exc_details):
336 suppressed_exc = True
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000337 pending_raise = False
Nick Coghlan3267a302012-05-21 22:54:43 +1000338 exc_details = (None, None, None)
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000339 except:
340 new_exc_details = sys.exc_info()
Nick Coghlan77452fc2012-06-01 22:48:32 +1000341 # simulate the stack of exceptions by setting the context
342 _fix_exception_context(new_exc_details[1], exc_details[1])
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000343 pending_raise = True
Nick Coghlana5bd2a12012-06-01 00:00:38 +1000344 exc_details = new_exc_details
Nick Coghlan1a33b2f2013-10-01 23:24:56 +1000345 if pending_raise:
346 try:
347 # bare "raise exc_details[1]" replaces our carefully
348 # set-up context
349 fixed_ctx = exc_details[1].__context__
350 raise exc_details[1]
351 except BaseException:
352 exc_details[1].__context__ = fixed_ctx
353 raise
354 return received_exc and suppressed_exc