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 | |
| 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | This module provides utilities for common tasks involving the :keyword:`with` |
| 9 | statement. For more information see also :ref:`typecontextmanager` and |
| 10 | :ref:`context-managers`. |
| 11 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 12 | .. seealso:: |
| 13 | |
| 14 | Latest version of the :source:`contextlib Python source code |
| 15 | <Lib/contextlib.py>` |
| 16 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | Functions provided: |
| 18 | |
| 19 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 20 | .. decorator:: contextmanager |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 22 | This function is a :term:`decorator` that can be used to define a factory |
| 23 | function for :keyword:`with` statement context managers, without needing to |
| 24 | create a class or separate :meth:`__enter__` and :meth:`__exit__` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | A simple example (this is not recommended as a real way of generating HTML!):: |
| 27 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | from contextlib import contextmanager |
| 29 | |
| 30 | @contextmanager |
| 31 | def tag(name): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 32 | print("<%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | yield |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 34 | print("</%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
| 36 | >>> with tag("h1"): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 37 | ... print("foo") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | ... |
| 39 | <h1> |
| 40 | foo |
| 41 | </h1> |
| 42 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 43 | The function being decorated must return a :term:`generator`-iterator when |
| 44 | called. This iterator must yield exactly one value, which will be bound to |
| 45 | 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] | 46 | |
| 47 | At the point where the generator yields, the block nested in the :keyword:`with` |
| 48 | statement is executed. The generator is then resumed after the block is exited. |
| 49 | If an unhandled exception occurs in the block, it is reraised inside the |
| 50 | generator at the point where the yield occurred. Thus, you can use a |
| 51 | :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` statement to trap |
| 52 | the error (if any), or ensure that some cleanup takes place. If an exception is |
| 53 | trapped merely in order to log it or to perform some action (rather than to |
| 54 | suppress it entirely), the generator must reraise that exception. Otherwise the |
| 55 | generator context manager will indicate to the :keyword:`with` statement that |
| 56 | the exception has been handled, and execution will resume with the statement |
| 57 | immediately following the :keyword:`with` statement. |
| 58 | |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 59 | contextmanager uses :class:`ContextDecorator` so the context managers it |
| 60 | creates can be used as decorators as well as in :keyword:`with` statements. |
| 61 | |
| 62 | .. versionchanged:: 3.2 |
| 63 | Use of :class:`ContextDecorator`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | .. function:: closing(thing) |
| 67 | |
| 68 | Return a context manager that closes *thing* upon completion of the block. This |
| 69 | is basically equivalent to:: |
| 70 | |
| 71 | from contextlib import contextmanager |
| 72 | |
| 73 | @contextmanager |
| 74 | def closing(thing): |
| 75 | try: |
| 76 | yield thing |
| 77 | finally: |
| 78 | thing.close() |
| 79 | |
| 80 | And lets you write code like this:: |
| 81 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | from contextlib import closing |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 83 | from urllib.request import urlopen |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 85 | with closing(urlopen('http://www.python.org')) as page: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | for line in page: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 87 | print(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
| 89 | without needing to explicitly close ``page``. Even if an error occurs, |
| 90 | ``page.close()`` will be called when the :keyword:`with` block is exited. |
| 91 | |
| 92 | |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 93 | .. class:: ContextDecorator() |
| 94 | |
| 95 | A base class that enables a context manager to also be used as a decorator. |
| 96 | |
| 97 | Context managers inheriting from ``ContextDecorator`` have to implement |
| 98 | ``__enter__`` and ``__exit__`` as normal. ``__exit__`` retains its optional |
| 99 | exception handling even when used as a decorator. |
| 100 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 101 | ``ContextDecorator`` is used by :func:`contextmanager`, so you get this |
| 102 | functionality automatically. |
| 103 | |
| 104 | Example of ``ContextDecorator``:: |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 105 | |
| 106 | from contextlib import ContextDecorator |
| 107 | |
| 108 | class mycontext(ContextDecorator): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 109 | def __enter__(self): |
| 110 | print('Starting') |
| 111 | return self |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 113 | def __exit__(self, *exc): |
| 114 | print('Finishing') |
| 115 | return False |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 116 | |
| 117 | >>> @mycontext() |
| 118 | ... def function(): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 119 | ... print('The bit in the middle') |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 120 | ... |
| 121 | >>> function() |
| 122 | Starting |
| 123 | The bit in the middle |
| 124 | Finishing |
| 125 | |
| 126 | >>> with mycontext(): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 127 | ... print('The bit in the middle') |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 128 | ... |
| 129 | Starting |
| 130 | The bit in the middle |
| 131 | Finishing |
| 132 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 133 | This change is just syntactic sugar for any construct of the following form:: |
| 134 | |
| 135 | def f(): |
| 136 | with cm(): |
| 137 | # Do stuff |
| 138 | |
| 139 | ``ContextDecorator`` lets you instead write:: |
| 140 | |
| 141 | @cm() |
| 142 | def f(): |
| 143 | # Do stuff |
| 144 | |
| 145 | It makes it clear that the ``cm`` applies to the whole function, rather than |
| 146 | just a piece of it (and saving an indentation level is nice, too). |
| 147 | |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 148 | Existing context managers that already have a base class can be extended by |
| 149 | using ``ContextDecorator`` as a mixin class:: |
| 150 | |
| 151 | from contextlib import ContextDecorator |
| 152 | |
| 153 | class mycontext(ContextBaseClass, ContextDecorator): |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 154 | def __enter__(self): |
| 155 | return self |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 156 | |
Georg Brandl | 86e78d1 | 2010-07-18 13:43:32 +0000 | [diff] [blame] | 157 | def __exit__(self, *exc): |
| 158 | return False |
Michael Foord | b3a8984 | 2010-06-30 12:17:50 +0000 | [diff] [blame] | 159 | |
| 160 | .. versionadded:: 3.2 |
| 161 | |
| 162 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | .. seealso:: |
| 164 | |
| 165 | :pep:`0343` - The "with" statement |
| 166 | The specification, background, and examples for the Python :keyword:`with` |
| 167 | statement. |
| 168 | |