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