Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`contextlib` --- Utilities for :keyword:`with`\ -statement contexts. |
| 3 | ========================================================================= |
| 4 | |
| 5 | .. module:: contextlib |
| 6 | :synopsis: Utilities for with-statement contexts. |
| 7 | |
| 8 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | This module provides utilities for common tasks involving the :keyword:`with` |
| 10 | statement. For more information see also :ref:`typecontextmanager` and |
| 11 | :ref:`context-managers`. |
| 12 | |
| 13 | Functions provided: |
| 14 | |
| 15 | |
| 16 | .. function:: contextmanager(func) |
| 17 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 18 | This function is a :term:`decorator` that can be used to define a factory |
| 19 | function for :keyword:`with` statement context managers, without needing to |
| 20 | create a class or separate :meth:`__enter__` and :meth:`__exit__` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| 22 | A simple example (this is not recommended as a real way of generating HTML!):: |
| 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | from contextlib import contextmanager |
| 25 | |
| 26 | @contextmanager |
| 27 | def tag(name): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 28 | print("<%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | yield |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 30 | print("</%s>" % name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | >>> with tag("h1"): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 33 | ... print("foo") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | ... |
| 35 | <h1> |
| 36 | foo |
| 37 | </h1> |
| 38 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 39 | The function being decorated must return a :term:`generator`-iterator when |
| 40 | called. This iterator must yield exactly one value, which will be bound to |
| 41 | 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] | 42 | |
| 43 | At the point where the generator yields, the block nested in the :keyword:`with` |
| 44 | statement is executed. The generator is then resumed after the block is exited. |
| 45 | If an unhandled exception occurs in the block, it is reraised inside the |
| 46 | generator at the point where the yield occurred. Thus, you can use a |
| 47 | :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` statement to trap |
| 48 | the error (if any), or ensure that some cleanup takes place. If an exception is |
| 49 | trapped merely in order to log it or to perform some action (rather than to |
| 50 | suppress it entirely), the generator must reraise that exception. Otherwise the |
| 51 | generator context manager will indicate to the :keyword:`with` statement that |
| 52 | the exception has been handled, and execution will resume with the statement |
| 53 | immediately following the :keyword:`with` statement. |
| 54 | |
| 55 | |
| 56 | .. function:: nested(mgr1[, mgr2[, ...]]) |
| 57 | |
| 58 | Combine multiple context managers into a single nested context manager. |
| 59 | |
| 60 | Code like this:: |
| 61 | |
| 62 | from contextlib import nested |
| 63 | |
| 64 | with nested(A, B, C) as (X, Y, Z): |
| 65 | do_something() |
| 66 | |
| 67 | is equivalent to this:: |
| 68 | |
| 69 | with A as X: |
| 70 | with B as Y: |
| 71 | with C as Z: |
| 72 | do_something() |
| 73 | |
| 74 | Note that if the :meth:`__exit__` method of one of the nested context managers |
| 75 | indicates an exception should be suppressed, no exception information will be |
| 76 | passed to any remaining outer context managers. Similarly, if the |
| 77 | :meth:`__exit__` method of one of the nested managers raises an exception, any |
| 78 | previous exception state will be lost; the new exception will be passed to the |
| 79 | :meth:`__exit__` methods of any remaining outer context managers. In general, |
| 80 | :meth:`__exit__` methods should avoid raising exceptions, and in particular they |
| 81 | should not re-raise a passed-in exception. |
| 82 | |
| 83 | |
| 84 | .. function:: closing(thing) |
| 85 | |
| 86 | Return a context manager that closes *thing* upon completion of the block. This |
| 87 | is basically equivalent to:: |
| 88 | |
| 89 | from contextlib import contextmanager |
| 90 | |
| 91 | @contextmanager |
| 92 | def closing(thing): |
| 93 | try: |
| 94 | yield thing |
| 95 | finally: |
| 96 | thing.close() |
| 97 | |
| 98 | And lets you write code like this:: |
| 99 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | from contextlib import closing |
| 101 | import urllib |
| 102 | |
| 103 | with closing(urllib.urlopen('http://www.python.org')) as page: |
| 104 | for line in page: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 105 | print(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
| 107 | without needing to explicitly close ``page``. Even if an error occurs, |
| 108 | ``page.close()`` will be called when the :keyword:`with` block is exited. |
| 109 | |
| 110 | |
| 111 | .. seealso:: |
| 112 | |
| 113 | :pep:`0343` - The "with" statement |
| 114 | The specification, background, and examples for the Python :keyword:`with` |
| 115 | statement. |
| 116 | |