Georg Brandl | 0eaab97 | 2009-06-08 08:00:22 +0000 | [diff] [blame] | 1 | :mod:`contextlib` --- Utilities for :keyword:`with`\ -statement contexts |
| 2 | ======================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: contextlib |
| 5 | :synopsis: Utilities for with-statement contexts. |
| 6 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/contextlib.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 9 | -------------- |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | This module provides utilities for common tasks involving the :keyword:`with` |
| 12 | statement. For more information see also :ref:`typecontextmanager` and |
| 13 | :ref:`context-managers`. |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 16 | Utilities |
| 17 | --------- |
| 18 | |
| 19 | Functions and classes provided: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
Brett Cannon | 9e080e0 | 2016-04-08 12:15:27 -0700 | [diff] [blame] | 21 | .. class:: AbstractContextManager |
| 22 | |
Brett Cannon | 516f546 | 2016-06-09 15:55:52 -0700 | [diff] [blame] | 23 | An :term:`abstract base class` for classes that implement |
Brett Cannon | 9e080e0 | 2016-04-08 12:15:27 -0700 | [diff] [blame] | 24 | :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 Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 33 | .. decorator:: contextmanager |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 35 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | |
| 39 | A simple example (this is not recommended as a real way of generating HTML!):: |
| 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | from contextlib import contextmanager |
| 42 | |
| 43 | @contextmanager |
| 44 | def tag(name): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 45 | print("<%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | yield |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 47 | print("</%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | >>> with tag("h1"): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 50 | ... print("foo") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | ... |
| 52 | <h1> |
| 53 | foo |
| 54 | </h1> |
| 55 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 56 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 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 Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 72 | :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 Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 78 | |
| 79 | .. versionchanged:: 3.2 |
| 80 | Use of :class:`ContextDecorator`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 82 | |
Jelle Zijlstra | 2e62469 | 2017-04-30 18:25:58 -0700 | [diff] [blame] | 83 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | from contextlib import closing |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 130 | from urllib.request import urlopen |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 132 | with closing(urlopen('http://www.python.org')) as page: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | for line in page: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 134 | print(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 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 Brandl | a7c17e5 | 2013-10-13 22:25:10 +0200 | [diff] [blame] | 139 | |
Jesse-Bakker | 0784a2e | 2017-11-23 01:23:28 +0100 | [diff] [blame] | 140 | .. _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 Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 162 | .. function:: suppress(*exceptions) |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 163 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 164 | 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 Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 167 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 168 | 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 Coghlan | b4534ae | 2013-10-13 23:23:08 +1000 | [diff] [blame] | 172 | |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 173 | For example:: |
| 174 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 175 | from contextlib import suppress |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 176 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 177 | with suppress(FileNotFoundError): |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 178 | os.remove('somefile.tmp') |
| 179 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 180 | with suppress(FileNotFoundError): |
| 181 | os.remove('someotherfile.tmp') |
| 182 | |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 183 | This code is equivalent to:: |
| 184 | |
| 185 | try: |
| 186 | os.remove('somefile.tmp') |
Nick Coghlan | b4534ae | 2013-10-13 23:23:08 +1000 | [diff] [blame] | 187 | except FileNotFoundError: |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 188 | pass |
| 189 | |
Nick Coghlan | 240f86d | 2013-10-17 23:40:57 +1000 | [diff] [blame] | 190 | try: |
| 191 | os.remove('someotherfile.tmp') |
| 192 | except FileNotFoundError: |
| 193 | pass |
| 194 | |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 195 | This context manager is :ref:`reentrant <reentrant-cms>`. |
| 196 | |
Raymond Hettinger | e318a88 | 2013-03-10 22:26:51 -0700 | [diff] [blame] | 197 | .. versionadded:: 3.4 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
Nick Coghlan | b4534ae | 2013-10-13 23:23:08 +1000 | [diff] [blame] | 199 | |
Raymond Hettinger | 088cbf2 | 2013-10-10 00:46:57 -0700 | [diff] [blame] | 200 | .. 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 Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 209 | You can capture that output in a string by redirecting the output to an |
Raymond Hettinger | 088cbf2 | 2013-10-10 00:46:57 -0700 | [diff] [blame] | 210 | :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 Coghlan | b4534ae | 2013-10-13 23:23:08 +1000 | [diff] [blame] | 229 | 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 Coghlan | 36d8ef9 | 2014-10-12 10:25:00 +1000 | [diff] [blame] | 234 | This context manager is :ref:`reentrant <reentrant-cms>`. |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 235 | |
Raymond Hettinger | 088cbf2 | 2013-10-10 00:46:57 -0700 | [diff] [blame] | 236 | .. versionadded:: 3.4 |
| 237 | |
Georg Brandl | a7c17e5 | 2013-10-13 22:25:10 +0200 | [diff] [blame] | 238 | |
Berker Peksag | bb44fe0 | 2014-11-28 23:28:06 +0200 | [diff] [blame] | 239 | .. 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 Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 249 | .. 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 Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 257 | ``ContextDecorator`` is used by :func:`contextmanager`, so you get this |
| 258 | functionality automatically. |
| 259 | |
| 260 | Example of ``ContextDecorator``:: |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 261 | |
| 262 | from contextlib import ContextDecorator |
| 263 | |
| 264 | class mycontext(ContextDecorator): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 265 | def __enter__(self): |
| 266 | print('Starting') |
| 267 | return self |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 268 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 269 | def __exit__(self, *exc): |
| 270 | print('Finishing') |
| 271 | return False |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 272 | |
| 273 | >>> @mycontext() |
| 274 | ... def function(): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 275 | ... print('The bit in the middle') |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 276 | ... |
| 277 | >>> function() |
| 278 | Starting |
| 279 | The bit in the middle |
| 280 | Finishing |
| 281 | |
| 282 | >>> with mycontext(): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 283 | ... print('The bit in the middle') |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 284 | ... |
| 285 | Starting |
| 286 | The bit in the middle |
| 287 | Finishing |
| 288 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 289 | 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 Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 304 | 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 Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 310 | def __enter__(self): |
| 311 | return self |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 312 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 313 | def __exit__(self, *exc): |
| 314 | return False |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 315 | |
Nick Coghlan | 0ded3e3 | 2011-05-05 23:49:25 +1000 | [diff] [blame] | 316 | .. 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 Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 322 | .. versionadded:: 3.2 |
| 323 | |
| 324 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 325 | .. 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 Svetlov | 5b89840 | 2012-12-18 21:26:36 +0200 | [diff] [blame] | 338 | # in the list raise an exception |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 339 | |
| 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 342 | 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 Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 344 | |
| 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 350 | registration, this ends up behaving as if multiple nested :keyword:`with` |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 351 | 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 Coghlan | a497b44 | 2012-05-22 23:02:00 +1000 | [diff] [blame] | 361 | .. versionadded:: 3.3 |
| 362 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 363 | .. 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 370 | would if used directly as part of a :keyword:`with` statement. |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 371 | |
| 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 388 | method to be used as a function decorator. |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 389 | |
| 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 399 | method to be used as a function decorator. |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 400 | |
| 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 Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 406 | explicitly or implicitly at the end of a :keyword:`with` statement). |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 407 | |
| 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 Warsaw | d8f870d | 2013-05-10 11:35:38 -0400 | [diff] [blame] | 413 | # Hold onto the close method, but don't call it yet. |
| 414 | close_files = stack.pop_all().close |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 415 | # 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 Warsaw | d8f870d | 2013-05-10 11:35:38 -0400 | [diff] [blame] | 418 | # close_files() can then be invoked explicitly to close them all. |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 419 | |
| 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 Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 427 | |
| 428 | Examples and Recipes |
| 429 | -------------------- |
| 430 | |
| 431 | This section describes some examples and recipes for making effective use of |
| 432 | the tools provided by :mod:`contextlib`. |
| 433 | |
| 434 | |
Nick Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 435 | Supporting a variable number of context managers |
| 436 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 437 | |
| 438 | The primary use case for :class:`ExitStack` is the one given in the class |
| 439 | documentation: supporting a variable number of context managers and other |
| 440 | cleanup operations in a single :keyword:`with` statement. The variability |
| 441 | may come from the number of context managers needed being driven by user |
| 442 | input (such as opening a user specified collection of files), or from |
| 443 | some of the context managers being optional:: |
| 444 | |
| 445 | with ExitStack() as stack: |
| 446 | for resource in resources: |
| 447 | stack.enter_context(resource) |
Raymond Hettinger | e8e2df3 | 2014-05-25 18:06:04 -0700 | [diff] [blame] | 448 | if need_special_resource(): |
Nick Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 449 | special = acquire_special_resource() |
| 450 | stack.callback(release_special_resource, special) |
| 451 | # Perform operations that use the acquired resources |
| 452 | |
| 453 | As shown, :class:`ExitStack` also makes it quite easy to use :keyword:`with` |
| 454 | statements to manage arbitrary resources that don't natively support the |
| 455 | context management protocol. |
| 456 | |
| 457 | |
Nick Coghlan | 2722827 | 2012-05-31 22:17:08 +1000 | [diff] [blame] | 458 | Catching exceptions from ``__enter__`` methods |
| 459 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 460 | |
| 461 | It is occasionally desirable to catch exceptions from an ``__enter__`` |
| 462 | method implementation, *without* inadvertently catching exceptions from |
| 463 | the :keyword:`with` statement body or the context manager's ``__exit__`` |
| 464 | method. By using :class:`ExitStack` the steps in the context management |
| 465 | protocol 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 | |
| 476 | Actually needing to do this is likely to indicate that the underlying API |
| 477 | should be providing a direct resource management interface for use with |
| 478 | :keyword:`try`/:keyword:`except`/:keyword:`finally` statements, but not |
| 479 | all APIs are well designed in that regard. When a context manager is the |
| 480 | only resource management API provided, then :class:`ExitStack` can make it |
| 481 | easier to handle various situations that can't be handled directly in a |
| 482 | :keyword:`with` statement. |
| 483 | |
| 484 | |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 485 | Cleaning up in an ``__enter__`` implementation |
| 486 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 487 | |
| 488 | As noted in the documentation of :meth:`ExitStack.push`, this |
| 489 | method can be useful in cleaning up an already allocated resource if later |
| 490 | steps in the :meth:`__enter__` implementation fail. |
| 491 | |
| 492 | Here's an example of doing this for a context manager that accepts resource |
| 493 | acquisition and release functions, along with an optional validation function, |
| 494 | and maps them to the context management protocol:: |
| 495 | |
Brett Cannon | 9e080e0 | 2016-04-08 12:15:27 -0700 | [diff] [blame] | 496 | from contextlib import contextmanager, AbstractContextManager, ExitStack |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 497 | |
Brett Cannon | 9e080e0 | 2016-04-08 12:15:27 -0700 | [diff] [blame] | 498 | class ResourceManager(AbstractContextManager): |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 499 | |
| 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 | |
| 531 | Replacing any use of ``try-finally`` and flag variables |
| 532 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 533 | |
| 534 | A pattern you will sometimes see is a ``try-finally`` statement with a flag |
| 535 | variable to indicate whether or not the body of the ``finally`` clause should |
| 536 | be executed. In its simplest form (that can't already be handled just by |
| 537 | using 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 | |
| 548 | As with any ``try`` statement based code, this can cause problems for |
| 549 | development and review, because the setup code and the cleanup code can end |
| 550 | up being separated by arbitrarily long sections of code. |
| 551 | |
| 552 | :class:`ExitStack` makes it possible to instead register a callback for |
| 553 | execution at the end of a ``with`` statement, and then later decide to skip |
| 554 | executing 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 | |
| 564 | This allows the intended cleanup up behaviour to be made explicit up front, |
| 565 | rather than requiring a separate flag variable. |
| 566 | |
| 567 | If a particular application uses this pattern a lot, it can be simplified |
| 568 | even 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 | |
| 585 | If the resource cleanup isn't already neatly bundled into a standalone |
| 586 | function, then it is still possible to use the decorator form of |
| 587 | :meth:`ExitStack.callback` to declare the resource cleanup in |
| 588 | advance:: |
| 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 | |
| 600 | Due to the way the decorator protocol works, a callback function |
| 601 | declared this way cannot take any parameters. Instead, any resources to |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 602 | be released must be accessed as closure variables. |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 603 | |
| 604 | |
| 605 | Using a context manager as a function decorator |
| 606 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 607 | |
| 608 | :class:`ContextDecorator` makes it possible to use a context manager in |
| 609 | both an ordinary ``with`` statement and also as a function decorator. |
| 610 | |
| 611 | For example, it is sometimes useful to wrap functions or groups of statements |
| 612 | with a logger that can track the time of entry and time of exit. Rather than |
| 613 | writing both a function decorator and a context manager for the task, |
| 614 | inheriting from :class:`ContextDecorator` provides both capabilities in a |
| 615 | single 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 Sajip | dd917f8 | 2016-08-31 08:22:29 +0100 | [diff] [blame] | 627 | logging.info('Entering: %s', self.name) |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 628 | |
| 629 | def __exit__(self, exc_type, exc, exc_tb): |
Vinay Sajip | dd917f8 | 2016-08-31 08:22:29 +0100 | [diff] [blame] | 630 | logging.info('Exiting: %s', self.name) |
Nick Coghlan | 3267a30 | 2012-05-21 22:54:43 +1000 | [diff] [blame] | 631 | |
| 632 | Instances 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 | |
| 638 | And 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 | |
| 645 | Note that there is one additional limitation when using context managers |
| 646 | as 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 |
| 648 | an explicit ``with`` statement. |
| 649 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 | .. seealso:: |
| 651 | |
Serhiy Storchaka | e4ba872 | 2016-03-31 15:30:54 +0300 | [diff] [blame] | 652 | :pep:`343` - The "with" statement |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 653 | The specification, background, and examples for the Python :keyword:`with` |
| 654 | statement. |
| 655 | |
Nick Coghlan | 0acceb7 | 2013-10-20 13:22:21 +1000 | [diff] [blame] | 656 | .. _single-use-reusable-and-reentrant-cms: |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 657 | |
Nick Coghlan | 0acceb7 | 2013-10-20 13:22:21 +1000 | [diff] [blame] | 658 | Single use, reusable and reentrant context managers |
| 659 | --------------------------------------------------- |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 660 | |
| 661 | Most context managers are written in a way that means they can only be |
| 662 | used effectively in a :keyword:`with` statement once. These single use |
| 663 | context managers must be created afresh each time they're used - |
| 664 | attempting to use them a second time will trigger an exception or |
| 665 | otherwise not work correctly. |
| 666 | |
| 667 | This common limitation means that it is generally advisable to create |
| 668 | context managers directly in the header of the :keyword:`with` statement |
| 669 | where they are used (as shown in all of the usage examples above). |
| 670 | |
| 671 | Files are an example of effectively single use context managers, since |
| 672 | the first :keyword:`with` statement will close the file, preventing any |
| 673 | further IO operations using that file object. |
| 674 | |
| 675 | Context managers created using :func:`contextmanager` are also single use |
| 676 | context managers, and will complain about the underlying generator failing |
| 677 | to 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 Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 693 | ... pass |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 694 | ... |
| 695 | Traceback (most recent call last): |
| 696 | ... |
| 697 | RuntimeError: generator didn't yield |
| 698 | |
| 699 | |
| 700 | .. _reentrant-cms: |
| 701 | |
| 702 | Reentrant context managers |
| 703 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 704 | |
| 705 | More sophisticated context managers may be "reentrant". These context |
| 706 | managers can not only be used in multiple :keyword:`with` statements, |
| 707 | but may also be used *inside* a :keyword:`with` statement that is already |
| 708 | using the same context manager. |
| 709 | |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 710 | :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 |
| 712 | reentrant use:: |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 713 | |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 714 | >>> 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 Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 722 | ... |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 723 | >>> 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 | |
| 729 | Real world examples of reentrancy are more likely to involve multiple |
| 730 | functions calling each other and hence be far more complicated than this |
| 731 | example. |
| 732 | |
| 733 | Note 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 |
| 735 | makes a global modification to the system state by binding :data:`sys.stdout` |
| 736 | to a different stream. |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 737 | |
| 738 | |
| 739 | .. _reusable-cms: |
| 740 | |
| 741 | Reusable context managers |
| 742 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 743 | |
| 744 | Distinct from both single use and reentrant context managers are "reusable" |
| 745 | context managers (or, to be completely explicit, "reusable, but not |
| 746 | reentrant" context managers, since reentrant context managers are also |
| 747 | reusable). These context managers support being used multiple times, but |
| 748 | will fail (or otherwise not work correctly) if the specific context manager |
| 749 | instance has already been used in a containing with statement. |
| 750 | |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 751 | :class:`threading.Lock` is an example of a reusable, but not reentrant, |
| 752 | context manager (for a reentrant lock, it is necessary to use |
| 753 | :class:`threading.RLock` instead). |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 754 | |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 755 | Another example of a reusable, but not reentrant, context manager is |
| 756 | :class:`ExitStack`, as it invokes *all* currently registered callbacks |
| 757 | when leaving any with statement, regardless of where those callbacks |
| 758 | were added:: |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 759 | |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 760 | >>> from contextlib import ExitStack |
| 761 | >>> stack = ExitStack() |
| 762 | >>> with stack: |
| 763 | ... stack.callback(print, "Callback: from first context") |
| 764 | ... print("Leaving first context") |
Nick Coghlan | 8608d26 | 2013-10-20 00:30:51 +1000 | [diff] [blame] | 765 | ... |
Nick Coghlan | 8e113b4 | 2013-11-03 17:00:51 +1000 | [diff] [blame] | 766 | 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 | |
| 786 | As the output from the example shows, reusing a single stack object across |
| 787 | multiple with statements works correctly, but attempting to nest them |
| 788 | will cause the stack to be cleared at the end of the innermost with |
| 789 | statement, which is unlikely to be desirable behaviour. |
| 790 | |
| 791 | Using separate :class:`ExitStack` instances instead of reusing a single |
| 792 | instance 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 |