Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +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 | |
Neal Norwitz | d03b073 | 2006-03-28 05:51:02 +0000 | [diff] [blame] | 7 | \versionadded{2.5} |
| 8 | |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 9 | This module provides utilities for common tasks involving the |
| 10 | \keyword{with} statement. |
| 11 | |
| 12 | Functions provided: |
| 13 | |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 14 | \begin{funcdesc}{contextmanager}{func} |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 15 | This function is a decorator that can be used to define a factory |
| 16 | function for \keyword{with} statement context objects, without |
| 17 | needing to create a class or separate \method{__enter__()} and |
| 18 | \method{__exit__()} methods. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 19 | |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 20 | A simple example (this is not recommended as a real way of |
| 21 | generating HTML!): |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 22 | |
| 23 | \begin{verbatim} |
| 24 | from __future__ import with_statement |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 25 | from contextlib import contextmanager |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 26 | |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 27 | @contextmanager |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 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 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +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. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +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 |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +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 \keyword{with} statement will |
| 56 | treat the exception as having been handled, and resume execution with |
| 57 | the statement immediately following the \keyword{with} statement. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 60 | \begin{funcdesc}{nested}{mgr1\optional{, mgr2\optional{, ...}}} |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 61 | Combine multiple context managers into a single nested context manager. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 62 | |
| 63 | Code like this: |
| 64 | |
| 65 | \begin{verbatim} |
| 66 | from contextlib import nested |
| 67 | |
| 68 | with nested(A, B, C) as (X, Y, Z): |
| 69 | do_something() |
| 70 | \end{verbatim} |
| 71 | |
| 72 | is equivalent to this: |
| 73 | |
| 74 | \begin{verbatim} |
| 75 | with A as X: |
| 76 | with B as Y: |
| 77 | with C as Z: |
| 78 | do_something() |
| 79 | \end{verbatim} |
| 80 | |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 81 | Note that if the \method{__exit__()} method of one of the nested |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 82 | context managers indicates an exception should be suppressed, no |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 83 | exception information will be passed to any remaining outer context |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 84 | objects. Similarly, if the \method{__exit__()} method of one of the |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 85 | nested context managers raises an exception, any previous exception |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 86 | state will be lost; the new exception will be passed to the |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 87 | \method{__exit__()} methods of any remaining outer context managers. |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 88 | In general, \method{__exit__()} methods should avoid raising |
| 89 | exceptions, and in particular they should not re-raise a |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 90 | passed-in exception. |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \label{context-closing} |
| 94 | \begin{funcdesc}{closing}{thing} |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 95 | Return a context manager that closes \var{thing} upon completion of |
| 96 | the block. This is basically equivalent to: |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 97 | |
| 98 | \begin{verbatim} |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 99 | from contextlib import contextmanager |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 100 | |
Nick Coghlan | afd5e63 | 2006-05-03 13:02:47 +0000 | [diff] [blame^] | 101 | @contextmanager |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 102 | def closing(thing): |
| 103 | try: |
| 104 | yield thing |
| 105 | finally: |
| 106 | thing.close() |
| 107 | \end{verbatim} |
| 108 | |
| 109 | And lets you write code like this: |
| 110 | \begin{verbatim} |
Phillip J. Eby | bdfd693 | 2006-03-28 00:08:22 +0000 | [diff] [blame] | 111 | from __future__ import with_statement |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 112 | from contextlib import closing |
Phillip J. Eby | bdfd693 | 2006-03-28 00:08:22 +0000 | [diff] [blame] | 113 | import codecs |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 114 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 115 | with closing(urllib.urlopen('http://www.python.org')) as page: |
| 116 | for line in page: |
Nick Coghlan | 5ef9d9f | 2006-04-23 15:14:37 +0000 | [diff] [blame] | 117 | print line |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 118 | \end{verbatim} |
| 119 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 120 | without needing to explicitly close \code{page}. Even if an error |
| 121 | occurs, \code{page.close()} will be called when the \keyword{with} |
| 122 | block is exited. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{seealso} |
| 126 | \seepep{0343}{The "with" statement} |
| 127 | {The specification, background, and examples for the |
| 128 | Python \keyword{with} statement.} |
| 129 | \end{seealso} |