blob: 48ca0da6b95f3ab494207f104eed69f03291631e [file] [log] [blame]
Georg Brandl0eaab972009-06-08 08:00:22 +00001:mod:`contextlib` --- Utilities for :keyword:`with`\ -statement contexts
2========================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: contextlib
5 :synopsis: Utilities for with-statement contexts.
6
Raymond Hettinger10480942011-01-10 03:26:08 +00007**Source code:** :source:`Lib/contextlib.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger4f707fd2011-01-10 19:54:11 +00009--------------
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module provides utilities for common tasks involving the :keyword:`with`
12statement. For more information see also :ref:`typecontextmanager` and
13:ref:`context-managers`.
14
Georg Brandl116aa622007-08-15 14:28:22 +000015
Nick Coghlan3267a302012-05-21 22:54:43 +100016Utilities
17---------
18
19Functions and classes provided:
Georg Brandl116aa622007-08-15 14:28:22 +000020
Brett Cannon9e080e02016-04-08 12:15:27 -070021.. class:: AbstractContextManager
22
Brett Cannon516f5462016-06-09 15:55:52 -070023 An :term:`abstract base class` for classes that implement
Brett Cannon9e080e02016-04-08 12:15:27 -070024 :meth:`object.__enter__` and :meth:`object.__exit__`. A default
25 implementation for :meth:`object.__enter__` is provided which returns
26 ``self`` while :meth:`object.__exit__` is an abstract method which by default
27 returns ``None``. See also the definition of :ref:`typecontextmanager`.
28
29 .. versionadded:: 3.6
30
31
32
Georg Brandl8a1caa22010-07-29 16:01:11 +000033.. decorator:: contextmanager
Georg Brandl116aa622007-08-15 14:28:22 +000034
Christian Heimesd8654cf2007-12-02 15:22:16 +000035 This function is a :term:`decorator` that can be used to define a factory
36 function for :keyword:`with` statement context managers, without needing to
37 create a class or separate :meth:`__enter__` and :meth:`__exit__` methods.
Georg Brandl116aa622007-08-15 14:28:22 +000038
39 A simple example (this is not recommended as a real way of generating HTML!)::
40
Georg Brandl116aa622007-08-15 14:28:22 +000041 from contextlib import contextmanager
42
43 @contextmanager
44 def tag(name):
Georg Brandl6911e3c2007-09-04 07:15:32 +000045 print("<%s>" % name)
Georg Brandl116aa622007-08-15 14:28:22 +000046 yield
Georg Brandl6911e3c2007-09-04 07:15:32 +000047 print("</%s>" % name)
Georg Brandl116aa622007-08-15 14:28:22 +000048
49 >>> with tag("h1"):
Georg Brandl6911e3c2007-09-04 07:15:32 +000050 ... print("foo")
Georg Brandl116aa622007-08-15 14:28:22 +000051 ...
52 <h1>
53 foo
54 </h1>
55
Georg Brandl9afde1c2007-11-01 20:32:30 +000056 The function being decorated must return a :term:`generator`-iterator when
57 called. This iterator must yield exactly one value, which will be bound to
58 the targets in the :keyword:`with` statement's :keyword:`as` clause, if any.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60 At the point where the generator yields, the block nested in the :keyword:`with`
61 statement is executed. The generator is then resumed after the block is exited.
62 If an unhandled exception occurs in the block, it is reraised inside the
63 generator at the point where the yield occurred. Thus, you can use a
64 :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` statement to trap
65 the error (if any), or ensure that some cleanup takes place. If an exception is
66 trapped merely in order to log it or to perform some action (rather than to
67 suppress it entirely), the generator must reraise that exception. Otherwise the
68 generator context manager will indicate to the :keyword:`with` statement that
69 the exception has been handled, and execution will resume with the statement
70 immediately following the :keyword:`with` statement.
71
Nick Coghlan0ded3e32011-05-05 23:49:25 +100072 :func:`contextmanager` uses :class:`ContextDecorator` so the context managers
73 it creates can be used as decorators as well as in :keyword:`with` statements.
74 When used as a decorator, a new generator instance is implicitly created on
75 each function call (this allows the otherwise "one-shot" context managers
76 created by :func:`contextmanager` to meet the requirement that context
77 managers support multiple invocations in order to be used as decorators).
Michael Foordb3a89842010-06-30 12:17:50 +000078
79 .. versionchanged:: 3.2
80 Use of :class:`ContextDecorator`.
Georg Brandl116aa622007-08-15 14:28:22 +000081
Georg Brandl86e78d12010-07-18 13:43:32 +000082
Jelle Zijlstra2e624692017-04-30 18:25:58 -070083.. decorator:: asynccontextmanager
84
85 Similar to :func:`~contextlib.contextmanager`, but creates an
86 :ref:`asynchronous context manager <async-context-managers>`.
87
88 This function is a :term:`decorator` that can be used to define a factory
89 function for :keyword:`async with` statement asynchronous context managers,
90 without needing to create a class or separate :meth:`__aenter__` and
91 :meth:`__aexit__` methods. It must be applied to an :term:`asynchronous
92 generator` function.
93
94 A simple example::
95
96 from contextlib import asynccontextmanager
97
98 @asynccontextmanager
99 async def get_connection():
100 conn = await acquire_db_connection()
101 try:
102 yield
103 finally:
104 await release_db_connection(conn)
105
106 async def get_all_users():
107 async with get_connection() as conn:
108 return conn.query('SELECT ...')
109
110 .. versionadded:: 3.7
111
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113.. function:: closing(thing)
114
115 Return a context manager that closes *thing* upon completion of the block. This
116 is basically equivalent to::
117
118 from contextlib import contextmanager
119
120 @contextmanager
121 def closing(thing):
122 try:
123 yield thing
124 finally:
125 thing.close()
126
127 And lets you write code like this::
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129 from contextlib import closing
Georg Brandl0f7ede42008-06-23 11:23:31 +0000130 from urllib.request import urlopen
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Georg Brandl0f7ede42008-06-23 11:23:31 +0000132 with closing(urlopen('http://www.python.org')) as page:
Georg Brandl116aa622007-08-15 14:28:22 +0000133 for line in page:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000134 print(line)
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136 without needing to explicitly close ``page``. Even if an error occurs,
137 ``page.close()`` will be called when the :keyword:`with` block is exited.
138
Georg Brandla7c17e52013-10-13 22:25:10 +0200139
Jesse-Bakker0784a2e2017-11-23 01:23:28 +0100140.. _simplifying-support-for-single-optional-context-managers:
141
142.. function:: nullcontext(enter_result=None)
143
144 Return a context manager that returns enter_result from ``__enter__``, but
145 otherwise does nothing. It is intended to be used as a stand-in for an
146 optional context manager, for example::
147
148 def process_file(file_or_path):
149 if isinstance(file_or_path, str):
150 # If string, open file
151 cm = open(file_or_path)
152 else:
153 # Caller is responsible for closing file
154 cm = nullcontext(file_or_path)
155
156 with cm as file:
157 # Perform processing on the file
158
159 .. versionadded:: 3.7
160
161
Nick Coghlan240f86d2013-10-17 23:40:57 +1000162.. function:: suppress(*exceptions)
Raymond Hettingere318a882013-03-10 22:26:51 -0700163
Nick Coghlan240f86d2013-10-17 23:40:57 +1000164 Return a context manager that suppresses any of the specified exceptions
165 if they occur in the body of a with statement and then resumes execution
166 with the first statement following the end of the with statement.
Raymond Hettingere318a882013-03-10 22:26:51 -0700167
Nick Coghlan240f86d2013-10-17 23:40:57 +1000168 As with any other mechanism that completely suppresses exceptions, this
169 context manager should be used only to cover very specific errors where
170 silently continuing with program execution is known to be the right
171 thing to do.
Nick Coghlanb4534ae2013-10-13 23:23:08 +1000172
Raymond Hettingere318a882013-03-10 22:26:51 -0700173 For example::
174
Nick Coghlan240f86d2013-10-17 23:40:57 +1000175 from contextlib import suppress
Raymond Hettingere318a882013-03-10 22:26:51 -0700176
Nick Coghlan240f86d2013-10-17 23:40:57 +1000177 with suppress(FileNotFoundError):
Raymond Hettingere318a882013-03-10 22:26:51 -0700178 os.remove('somefile.tmp')
179
Nick Coghlan240f86d2013-10-17 23:40:57 +1000180 with suppress(FileNotFoundError):
181 os.remove('someotherfile.tmp')
182
Raymond Hettingere318a882013-03-10 22:26:51 -0700183 This code is equivalent to::
184
185 try:
186 os.remove('somefile.tmp')
Nick Coghlanb4534ae2013-10-13 23:23:08 +1000187 except FileNotFoundError:
Raymond Hettingere318a882013-03-10 22:26:51 -0700188 pass
189
Nick Coghlan240f86d2013-10-17 23:40:57 +1000190 try:
191 os.remove('someotherfile.tmp')
192 except FileNotFoundError:
193 pass
194
Nick Coghlan8608d262013-10-20 00:30:51 +1000195 This context manager is :ref:`reentrant <reentrant-cms>`.
196
Raymond Hettingere318a882013-03-10 22:26:51 -0700197 .. versionadded:: 3.4
Georg Brandl116aa622007-08-15 14:28:22 +0000198
Nick Coghlanb4534ae2013-10-13 23:23:08 +1000199
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700200.. function:: redirect_stdout(new_target)
201
202 Context manager for temporarily redirecting :data:`sys.stdout` to
203 another file or file-like object.
204
205 This tool adds flexibility to existing functions or classes whose output
206 is hardwired to stdout.
207
208 For example, the output of :func:`help` normally is sent to *sys.stdout*.
Serhiy Storchakad65c9492015-11-02 14:10:23 +0200209 You can capture that output in a string by redirecting the output to an
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700210 :class:`io.StringIO` object::
211
212 f = io.StringIO()
213 with redirect_stdout(f):
214 help(pow)
215 s = f.getvalue()
216
217 To send the output of :func:`help` to a file on disk, redirect the output
218 to a regular file::
219
220 with open('help.txt', 'w') as f:
221 with redirect_stdout(f):
222 help(pow)
223
224 To send the output of :func:`help` to *sys.stderr*::
225
226 with redirect_stdout(sys.stderr):
227 help(pow)
228
Nick Coghlanb4534ae2013-10-13 23:23:08 +1000229 Note that the global side effect on :data:`sys.stdout` means that this
230 context manager is not suitable for use in library code and most threaded
231 applications. It also has no effect on the output of subprocesses.
232 However, it is still a useful approach for many utility scripts.
233
Nick Coghlan36d8ef92014-10-12 10:25:00 +1000234 This context manager is :ref:`reentrant <reentrant-cms>`.
Nick Coghlan8608d262013-10-20 00:30:51 +1000235
Raymond Hettinger088cbf22013-10-10 00:46:57 -0700236 .. versionadded:: 3.4
237
Georg Brandla7c17e52013-10-13 22:25:10 +0200238
Berker Peksagbb44fe02014-11-28 23:28:06 +0200239.. function:: redirect_stderr(new_target)
240
241 Similar to :func:`~contextlib.redirect_stdout` but redirecting
242 :data:`sys.stderr` to another file or file-like object.
243
244 This context manager is :ref:`reentrant <reentrant-cms>`.
245
246 .. versionadded:: 3.5
247
248
Michael Foordb3a89842010-06-30 12:17:50 +0000249.. class:: ContextDecorator()
250
251 A base class that enables a context manager to also be used as a decorator.
252
253 Context managers inheriting from ``ContextDecorator`` have to implement
254 ``__enter__`` and ``__exit__`` as normal. ``__exit__`` retains its optional
255 exception handling even when used as a decorator.
256
Georg Brandl86e78d12010-07-18 13:43:32 +0000257 ``ContextDecorator`` is used by :func:`contextmanager`, so you get this
258 functionality automatically.
259
260 Example of ``ContextDecorator``::
Michael Foordb3a89842010-06-30 12:17:50 +0000261
262 from contextlib import ContextDecorator
263
264 class mycontext(ContextDecorator):
Georg Brandl86e78d12010-07-18 13:43:32 +0000265 def __enter__(self):
266 print('Starting')
267 return self
Michael Foordb3a89842010-06-30 12:17:50 +0000268
Georg Brandl86e78d12010-07-18 13:43:32 +0000269 def __exit__(self, *exc):
270 print('Finishing')
271 return False
Michael Foordb3a89842010-06-30 12:17:50 +0000272
273 >>> @mycontext()
274 ... def function():
Georg Brandl86e78d12010-07-18 13:43:32 +0000275 ... print('The bit in the middle')
Michael Foordb3a89842010-06-30 12:17:50 +0000276 ...
277 >>> function()
278 Starting
279 The bit in the middle
280 Finishing
281
282 >>> with mycontext():
Georg Brandl86e78d12010-07-18 13:43:32 +0000283 ... print('The bit in the middle')
Michael Foordb3a89842010-06-30 12:17:50 +0000284 ...
285 Starting
286 The bit in the middle
287 Finishing
288
Georg Brandl86e78d12010-07-18 13:43:32 +0000289 This change is just syntactic sugar for any construct of the following form::
290
291 def f():
292 with cm():
293 # Do stuff
294
295 ``ContextDecorator`` lets you instead write::
296
297 @cm()
298 def f():
299 # Do stuff
300
301 It makes it clear that the ``cm`` applies to the whole function, rather than
302 just a piece of it (and saving an indentation level is nice, too).
303
Michael Foordb3a89842010-06-30 12:17:50 +0000304 Existing context managers that already have a base class can be extended by
305 using ``ContextDecorator`` as a mixin class::
306
307 from contextlib import ContextDecorator
308
309 class mycontext(ContextBaseClass, ContextDecorator):
Georg Brandl86e78d12010-07-18 13:43:32 +0000310 def __enter__(self):
311 return self
Michael Foordb3a89842010-06-30 12:17:50 +0000312
Georg Brandl86e78d12010-07-18 13:43:32 +0000313 def __exit__(self, *exc):
314 return False
Michael Foordb3a89842010-06-30 12:17:50 +0000315
Nick Coghlan0ded3e32011-05-05 23:49:25 +1000316 .. note::
317 As the decorated function must be able to be called multiple times, the
318 underlying context manager must support use in multiple :keyword:`with`
319 statements. If this is not the case, then the original construct with the
320 explicit :keyword:`with` statement inside the function should be used.
321
Michael Foordb3a89842010-06-30 12:17:50 +0000322 .. versionadded:: 3.2
323
324
Nick Coghlan3267a302012-05-21 22:54:43 +1000325.. class:: ExitStack()
326
327 A context manager that is designed to make it easy to programmatically
328 combine other context managers and cleanup functions, especially those
329 that are optional or otherwise driven by input data.
330
331 For example, a set of files may easily be handled in a single with
332 statement as follows::
333
334 with ExitStack() as stack:
335 files = [stack.enter_context(open(fname)) for fname in filenames]
336 # All opened files will automatically be closed at the end of
337 # the with statement, even if attempts to open files later
Andrew Svetlov5b898402012-12-18 21:26:36 +0200338 # in the list raise an exception
Nick Coghlan3267a302012-05-21 22:54:43 +1000339
340 Each instance maintains a stack of registered callbacks that are called in
341 reverse order when the instance is closed (either explicitly or implicitly
Nick Coghlan27228272012-05-31 22:17:08 +1000342 at the end of a :keyword:`with` statement). Note that callbacks are *not*
343 invoked implicitly when the context stack instance is garbage collected.
Nick Coghlan3267a302012-05-21 22:54:43 +1000344
345 This stack model is used so that context managers that acquire their
346 resources in their ``__init__`` method (such as file objects) can be
347 handled correctly.
348
349 Since registered callbacks are invoked in the reverse order of
Nick Coghlan27228272012-05-31 22:17:08 +1000350 registration, this ends up behaving as if multiple nested :keyword:`with`
Nick Coghlan3267a302012-05-21 22:54:43 +1000351 statements had been used with the registered set of callbacks. This even
352 extends to exception handling - if an inner callback suppresses or replaces
353 an exception, then outer callbacks will be passed arguments based on that
354 updated state.
355
356 This is a relatively low level API that takes care of the details of
357 correctly unwinding the stack of exit callbacks. It provides a suitable
358 foundation for higher level context managers that manipulate the exit
359 stack in application specific ways.
360
Nick Coghlana497b442012-05-22 23:02:00 +1000361 .. versionadded:: 3.3
362
Nick Coghlan3267a302012-05-21 22:54:43 +1000363 .. method:: enter_context(cm)
364
365 Enters a new context manager and adds its :meth:`__exit__` method to
366 the callback stack. The return value is the result of the context
367 manager's own :meth:`__enter__` method.
368
369 These context managers may suppress exceptions just as they normally
Nick Coghlan27228272012-05-31 22:17:08 +1000370 would if used directly as part of a :keyword:`with` statement.
Nick Coghlan3267a302012-05-21 22:54:43 +1000371
372 .. method:: push(exit)
373
374 Adds a context manager's :meth:`__exit__` method to the callback stack.
375
376 As ``__enter__`` is *not* invoked, this method can be used to cover
377 part of an :meth:`__enter__` implementation with a context manager's own
378 :meth:`__exit__` method.
379
380 If passed an object that is not a context manager, this method assumes
381 it is a callback with the same signature as a context manager's
382 :meth:`__exit__` method and adds it directly to the callback stack.
383
384 By returning true values, these callbacks can suppress exceptions the
385 same way context manager :meth:`__exit__` methods can.
386
387 The passed in object is returned from the function, allowing this
Nick Coghlan27228272012-05-31 22:17:08 +1000388 method to be used as a function decorator.
Nick Coghlan3267a302012-05-21 22:54:43 +1000389
390 .. method:: callback(callback, *args, **kwds)
391
392 Accepts an arbitrary callback function and arguments and adds it to
393 the callback stack.
394
395 Unlike the other methods, callbacks added this way cannot suppress
396 exceptions (as they are never passed the exception details).
397
398 The passed in callback is returned from the function, allowing this
Nick Coghlan27228272012-05-31 22:17:08 +1000399 method to be used as a function decorator.
Nick Coghlan3267a302012-05-21 22:54:43 +1000400
401 .. method:: pop_all()
402
403 Transfers the callback stack to a fresh :class:`ExitStack` instance
404 and returns it. No callbacks are invoked by this operation - instead,
405 they will now be invoked when the new stack is closed (either
Nick Coghlan27228272012-05-31 22:17:08 +1000406 explicitly or implicitly at the end of a :keyword:`with` statement).
Nick Coghlan3267a302012-05-21 22:54:43 +1000407
408 For example, a group of files can be opened as an "all or nothing"
409 operation as follows::
410
411 with ExitStack() as stack:
412 files = [stack.enter_context(open(fname)) for fname in filenames]
Barry Warsawd8f870d2013-05-10 11:35:38 -0400413 # Hold onto the close method, but don't call it yet.
414 close_files = stack.pop_all().close
Nick Coghlan3267a302012-05-21 22:54:43 +1000415 # If opening any file fails, all previously opened files will be
416 # closed automatically. If all files are opened successfully,
417 # they will remain open even after the with statement ends.
Barry Warsawd8f870d2013-05-10 11:35:38 -0400418 # close_files() can then be invoked explicitly to close them all.
Nick Coghlan3267a302012-05-21 22:54:43 +1000419
420 .. method:: close()
421
422 Immediately unwinds the callback stack, invoking callbacks in the
423 reverse order of registration. For any context managers and exit
424 callbacks registered, the arguments passed in will indicate that no
425 exception occurred.
426
Nick Coghlan3267a302012-05-21 22:54:43 +1000427
428Examples and Recipes
429--------------------
430
431This section describes some examples and recipes for making effective use of
432the tools provided by :mod:`contextlib`.
433
434
Nick Coghlan27228272012-05-31 22:17:08 +1000435Supporting a variable number of context managers
436^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
437
438The primary use case for :class:`ExitStack` is the one given in the class
439documentation: supporting a variable number of context managers and other
440cleanup operations in a single :keyword:`with` statement. The variability
441may come from the number of context managers needed being driven by user
442input (such as opening a user specified collection of files), or from
443some of the context managers being optional::
444
445 with ExitStack() as stack:
446 for resource in resources:
447 stack.enter_context(resource)
Raymond Hettingere8e2df32014-05-25 18:06:04 -0700448 if need_special_resource():
Nick Coghlan27228272012-05-31 22:17:08 +1000449 special = acquire_special_resource()
450 stack.callback(release_special_resource, special)
451 # Perform operations that use the acquired resources
452
453As shown, :class:`ExitStack` also makes it quite easy to use :keyword:`with`
454statements to manage arbitrary resources that don't natively support the
455context management protocol.
456
457
Nick Coghlan27228272012-05-31 22:17:08 +1000458Catching exceptions from ``__enter__`` methods
459^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
460
461It is occasionally desirable to catch exceptions from an ``__enter__``
462method implementation, *without* inadvertently catching exceptions from
463the :keyword:`with` statement body or the context manager's ``__exit__``
464method. By using :class:`ExitStack` the steps in the context management
465protocol can be separated slightly in order to allow this::
466
467 stack = ExitStack()
468 try:
469 x = stack.enter_context(cm)
470 except Exception:
471 # handle __enter__ exception
472 else:
473 with stack:
474 # Handle normal case
475
476Actually needing to do this is likely to indicate that the underlying API
477should be providing a direct resource management interface for use with
478:keyword:`try`/:keyword:`except`/:keyword:`finally` statements, but not
479all APIs are well designed in that regard. When a context manager is the
480only resource management API provided, then :class:`ExitStack` can make it
481easier to handle various situations that can't be handled directly in a
482:keyword:`with` statement.
483
484
Nick Coghlan3267a302012-05-21 22:54:43 +1000485Cleaning up in an ``__enter__`` implementation
486^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
487
488As noted in the documentation of :meth:`ExitStack.push`, this
489method can be useful in cleaning up an already allocated resource if later
490steps in the :meth:`__enter__` implementation fail.
491
492Here's an example of doing this for a context manager that accepts resource
493acquisition and release functions, along with an optional validation function,
494and maps them to the context management protocol::
495
Brett Cannon9e080e02016-04-08 12:15:27 -0700496 from contextlib import contextmanager, AbstractContextManager, ExitStack
Nick Coghlan3267a302012-05-21 22:54:43 +1000497
Brett Cannon9e080e02016-04-08 12:15:27 -0700498 class ResourceManager(AbstractContextManager):
Nick Coghlan3267a302012-05-21 22:54:43 +1000499
500 def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
501 self.acquire_resource = acquire_resource
502 self.release_resource = release_resource
503 if check_resource_ok is None:
504 def check_resource_ok(resource):
505 return True
506 self.check_resource_ok = check_resource_ok
507
508 @contextmanager
509 def _cleanup_on_error(self):
510 with ExitStack() as stack:
511 stack.push(self)
512 yield
513 # The validation check passed and didn't raise an exception
514 # Accordingly, we want to keep the resource, and pass it
515 # back to our caller
516 stack.pop_all()
517
518 def __enter__(self):
519 resource = self.acquire_resource()
520 with self._cleanup_on_error():
521 if not self.check_resource_ok(resource):
522 msg = "Failed validation for {!r}"
523 raise RuntimeError(msg.format(resource))
524 return resource
525
526 def __exit__(self, *exc_details):
527 # We don't need to duplicate any of our resource release logic
528 self.release_resource()
529
530
531Replacing any use of ``try-finally`` and flag variables
532^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
533
534A pattern you will sometimes see is a ``try-finally`` statement with a flag
535variable to indicate whether or not the body of the ``finally`` clause should
536be executed. In its simplest form (that can't already be handled just by
537using an ``except`` clause instead), it looks something like this::
538
539 cleanup_needed = True
540 try:
541 result = perform_operation()
542 if result:
543 cleanup_needed = False
544 finally:
545 if cleanup_needed:
546 cleanup_resources()
547
548As with any ``try`` statement based code, this can cause problems for
549development and review, because the setup code and the cleanup code can end
550up being separated by arbitrarily long sections of code.
551
552:class:`ExitStack` makes it possible to instead register a callback for
553execution at the end of a ``with`` statement, and then later decide to skip
554executing that callback::
555
556 from contextlib import ExitStack
557
558 with ExitStack() as stack:
559 stack.callback(cleanup_resources)
560 result = perform_operation()
561 if result:
562 stack.pop_all()
563
564This allows the intended cleanup up behaviour to be made explicit up front,
565rather than requiring a separate flag variable.
566
567If a particular application uses this pattern a lot, it can be simplified
568even further by means of a small helper class::
569
570 from contextlib import ExitStack
571
572 class Callback(ExitStack):
573 def __init__(self, callback, *args, **kwds):
574 super(Callback, self).__init__()
575 self.callback(callback, *args, **kwds)
576
577 def cancel(self):
578 self.pop_all()
579
580 with Callback(cleanup_resources) as cb:
581 result = perform_operation()
582 if result:
583 cb.cancel()
584
585If the resource cleanup isn't already neatly bundled into a standalone
586function, then it is still possible to use the decorator form of
587:meth:`ExitStack.callback` to declare the resource cleanup in
588advance::
589
590 from contextlib import ExitStack
591
592 with ExitStack() as stack:
593 @stack.callback
594 def cleanup_resources():
595 ...
596 result = perform_operation()
597 if result:
598 stack.pop_all()
599
600Due to the way the decorator protocol works, a callback function
601declared this way cannot take any parameters. Instead, any resources to
Martin Panterd21e0b52015-10-10 10:36:22 +0000602be released must be accessed as closure variables.
Nick Coghlan3267a302012-05-21 22:54:43 +1000603
604
605Using a context manager as a function decorator
606^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
607
608:class:`ContextDecorator` makes it possible to use a context manager in
609both an ordinary ``with`` statement and also as a function decorator.
610
611For example, it is sometimes useful to wrap functions or groups of statements
612with a logger that can track the time of entry and time of exit. Rather than
613writing both a function decorator and a context manager for the task,
614inheriting from :class:`ContextDecorator` provides both capabilities in a
615single definition::
616
617 from contextlib import ContextDecorator
618 import logging
619
620 logging.basicConfig(level=logging.INFO)
621
622 class track_entry_and_exit(ContextDecorator):
623 def __init__(self, name):
624 self.name = name
625
626 def __enter__(self):
Vinay Sajipdd917f82016-08-31 08:22:29 +0100627 logging.info('Entering: %s', self.name)
Nick Coghlan3267a302012-05-21 22:54:43 +1000628
629 def __exit__(self, exc_type, exc, exc_tb):
Vinay Sajipdd917f82016-08-31 08:22:29 +0100630 logging.info('Exiting: %s', self.name)
Nick Coghlan3267a302012-05-21 22:54:43 +1000631
632Instances of this class can be used as both a context manager::
633
634 with track_entry_and_exit('widget loader'):
635 print('Some time consuming activity goes here')
636 load_widget()
637
638And also as a function decorator::
639
640 @track_entry_and_exit('widget loader')
641 def activity():
642 print('Some time consuming activity goes here')
643 load_widget()
644
645Note that there is one additional limitation when using context managers
646as function decorators: there's no way to access the return value of
647:meth:`__enter__`. If that value is needed, then it is still necessary to use
648an explicit ``with`` statement.
649
Georg Brandl116aa622007-08-15 14:28:22 +0000650.. seealso::
651
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300652 :pep:`343` - The "with" statement
Georg Brandl116aa622007-08-15 14:28:22 +0000653 The specification, background, and examples for the Python :keyword:`with`
654 statement.
655
Nick Coghlan0acceb72013-10-20 13:22:21 +1000656.. _single-use-reusable-and-reentrant-cms:
Nick Coghlan8608d262013-10-20 00:30:51 +1000657
Nick Coghlan0acceb72013-10-20 13:22:21 +1000658Single use, reusable and reentrant context managers
659---------------------------------------------------
Nick Coghlan8608d262013-10-20 00:30:51 +1000660
661Most context managers are written in a way that means they can only be
662used effectively in a :keyword:`with` statement once. These single use
663context managers must be created afresh each time they're used -
664attempting to use them a second time will trigger an exception or
665otherwise not work correctly.
666
667This common limitation means that it is generally advisable to create
668context managers directly in the header of the :keyword:`with` statement
669where they are used (as shown in all of the usage examples above).
670
671Files are an example of effectively single use context managers, since
672the first :keyword:`with` statement will close the file, preventing any
673further IO operations using that file object.
674
675Context managers created using :func:`contextmanager` are also single use
676context managers, and will complain about the underlying generator failing
677to yield if an attempt is made to use them a second time::
678
679 >>> from contextlib import contextmanager
680 >>> @contextmanager
681 ... def singleuse():
682 ... print("Before")
683 ... yield
684 ... print("After")
685 ...
686 >>> cm = singleuse()
687 >>> with cm:
688 ... pass
689 ...
690 Before
691 After
692 >>> with cm:
Serhiy Storchakadba90392016-05-10 12:01:23 +0300693 ... pass
Nick Coghlan8608d262013-10-20 00:30:51 +1000694 ...
695 Traceback (most recent call last):
696 ...
697 RuntimeError: generator didn't yield
698
699
700.. _reentrant-cms:
701
702Reentrant context managers
703^^^^^^^^^^^^^^^^^^^^^^^^^^
704
705More sophisticated context managers may be "reentrant". These context
706managers can not only be used in multiple :keyword:`with` statements,
707but may also be used *inside* a :keyword:`with` statement that is already
708using the same context manager.
709
Nick Coghlan8e113b42013-11-03 17:00:51 +1000710:class:`threading.RLock` is an example of a reentrant context manager, as are
711:func:`suppress` and :func:`redirect_stdout`. Here's a very simple example of
712reentrant use::
Nick Coghlan8608d262013-10-20 00:30:51 +1000713
Nick Coghlan8e113b42013-11-03 17:00:51 +1000714 >>> from contextlib import redirect_stdout
715 >>> from io import StringIO
716 >>> stream = StringIO()
717 >>> write_to_stream = redirect_stdout(stream)
718 >>> with write_to_stream:
719 ... print("This is written to the stream rather than stdout")
720 ... with write_to_stream:
721 ... print("This is also written to the stream")
Nick Coghlan8608d262013-10-20 00:30:51 +1000722 ...
Nick Coghlan8e113b42013-11-03 17:00:51 +1000723 >>> print("This is written directly to stdout")
724 This is written directly to stdout
725 >>> print(stream.getvalue())
726 This is written to the stream rather than stdout
727 This is also written to the stream
728
729Real world examples of reentrancy are more likely to involve multiple
730functions calling each other and hence be far more complicated than this
731example.
732
733Note also that being reentrant is *not* the same thing as being thread safe.
734:func:`redirect_stdout`, for example, is definitely not thread safe, as it
735makes a global modification to the system state by binding :data:`sys.stdout`
736to a different stream.
Nick Coghlan8608d262013-10-20 00:30:51 +1000737
738
739.. _reusable-cms:
740
741Reusable context managers
742^^^^^^^^^^^^^^^^^^^^^^^^^
743
744Distinct from both single use and reentrant context managers are "reusable"
745context managers (or, to be completely explicit, "reusable, but not
746reentrant" context managers, since reentrant context managers are also
747reusable). These context managers support being used multiple times, but
748will fail (or otherwise not work correctly) if the specific context manager
749instance has already been used in a containing with statement.
750
Nick Coghlan8e113b42013-11-03 17:00:51 +1000751:class:`threading.Lock` is an example of a reusable, but not reentrant,
752context manager (for a reentrant lock, it is necessary to use
753:class:`threading.RLock` instead).
Nick Coghlan8608d262013-10-20 00:30:51 +1000754
Nick Coghlan8e113b42013-11-03 17:00:51 +1000755Another example of a reusable, but not reentrant, context manager is
756:class:`ExitStack`, as it invokes *all* currently registered callbacks
757when leaving any with statement, regardless of where those callbacks
758were added::
Nick Coghlan8608d262013-10-20 00:30:51 +1000759
Nick Coghlan8e113b42013-11-03 17:00:51 +1000760 >>> from contextlib import ExitStack
761 >>> stack = ExitStack()
762 >>> with stack:
763 ... stack.callback(print, "Callback: from first context")
764 ... print("Leaving first context")
Nick Coghlan8608d262013-10-20 00:30:51 +1000765 ...
Nick Coghlan8e113b42013-11-03 17:00:51 +1000766 Leaving first context
767 Callback: from first context
768 >>> with stack:
769 ... stack.callback(print, "Callback: from second context")
770 ... print("Leaving second context")
771 ...
772 Leaving second context
773 Callback: from second context
774 >>> with stack:
775 ... stack.callback(print, "Callback: from outer context")
776 ... with stack:
777 ... stack.callback(print, "Callback: from inner context")
778 ... print("Leaving inner context")
779 ... print("Leaving outer context")
780 ...
781 Leaving inner context
782 Callback: from inner context
783 Callback: from outer context
784 Leaving outer context
785
786As the output from the example shows, reusing a single stack object across
787multiple with statements works correctly, but attempting to nest them
788will cause the stack to be cleared at the end of the innermost with
789statement, which is unlikely to be desirable behaviour.
790
791Using separate :class:`ExitStack` instances instead of reusing a single
792instance avoids that problem::
793
794 >>> from contextlib import ExitStack
795 >>> with ExitStack() as outer_stack:
796 ... outer_stack.callback(print, "Callback: from outer context")
797 ... with ExitStack() as inner_stack:
798 ... inner_stack.callback(print, "Callback: from inner context")
799 ... print("Leaving inner context")
800 ... print("Leaving outer context")
801 ...
802 Leaving inner context
803 Callback: from inner context
804 Leaving outer context
805 Callback: from outer context