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