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 | e708cf5 | 2006-04-25 11:05:56 +0000 | [diff] [blame] | 14 | \begin{funcdesc}{context}{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 | |
| 20 | A simple example: |
| 21 | |
| 22 | \begin{verbatim} |
| 23 | from __future__ import with_statement |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 24 | from contextlib import contextfactory |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 25 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 26 | @contextfactory |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 27 | def tag(name): |
| 28 | print "<%s>" % name |
| 29 | yield |
| 30 | print "</%s>" % name |
| 31 | |
| 32 | >>> with tag("h1"): |
| 33 | ... print "foo" |
| 34 | ... |
| 35 | <h1> |
| 36 | foo |
| 37 | </h1> |
| 38 | \end{verbatim} |
| 39 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 40 | The function being decorated must return a generator-iterator when |
| 41 | called. This iterator must yield exactly one value, which will be |
| 42 | bound to the targets in the \keyword{with} statement's \keyword{as} |
| 43 | clause, if any. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 44 | |
| 45 | At the point where the generator yields, the block nested in the |
| 46 | \keyword{with} statement is executed. The generator is then resumed |
| 47 | after the block is exited. If an unhandled exception occurs in the |
| 48 | block, it is reraised inside the generator at the point where the yield |
| 49 | occurred. Thus, you can use a |
| 50 | \keyword{try}...\keyword{except}...\keyword{finally} statement to trap |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 51 | the error (if any), or ensure that some cleanup takes place. If an |
| 52 | exception is trapped merely in order to log it or to perform some |
| 53 | action (rather than to suppress it entirely), the generator must |
| 54 | reraise that exception. Otherwise the \keyword{with} statement will |
| 55 | treat the exception as having been handled, and resume execution with |
| 56 | the statement immediately following the \keyword{with} statement. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{nested}{ctx1\optional{, ctx2\optional{, ...}}} |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 60 | Combine multiple context managers into a single nested context manager. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 61 | |
| 62 | Code like this: |
| 63 | |
| 64 | \begin{verbatim} |
| 65 | from contextlib import nested |
| 66 | |
| 67 | with nested(A, B, C) as (X, Y, Z): |
| 68 | do_something() |
| 69 | \end{verbatim} |
| 70 | |
| 71 | is equivalent to this: |
| 72 | |
| 73 | \begin{verbatim} |
| 74 | with A as X: |
| 75 | with B as Y: |
| 76 | with C as Z: |
| 77 | do_something() |
| 78 | \end{verbatim} |
| 79 | |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 80 | Note that if the \method{__exit__()} method of one of the nested |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 81 | context objects indicates an exception should be suppressed, no |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 82 | exception information will be passed to any remaining outer context |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 83 | objects. Similarly, if the \method{__exit__()} method of one of the |
| 84 | nested context objects raises an exception, any previous exception |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 85 | state will be lost; the new exception will be passed to the |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 86 | \method{__exit__()} methods of any remaining outer context objects. |
Nick Coghlan | 877cf23 | 2006-04-24 04:17:02 +0000 | [diff] [blame] | 87 | In general, \method{__exit__()} methods should avoid raising |
| 88 | exceptions, and in particular they should not re-raise a |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 89 | passed-in exception. |
| 90 | \end{funcdesc} |
| 91 | |
| 92 | \label{context-closing} |
| 93 | \begin{funcdesc}{closing}{thing} |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 94 | Return a context that closes \var{thing} upon completion of the |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 95 | block. This is basically equivalent to: |
| 96 | |
| 97 | \begin{verbatim} |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 98 | from contextlib import contextfactory |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 99 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 100 | @contextfactory |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 101 | def closing(thing): |
| 102 | try: |
| 103 | yield thing |
| 104 | finally: |
| 105 | thing.close() |
| 106 | \end{verbatim} |
| 107 | |
| 108 | And lets you write code like this: |
| 109 | \begin{verbatim} |
Phillip J. Eby | bdfd693 | 2006-03-28 00:08:22 +0000 | [diff] [blame] | 110 | from __future__ import with_statement |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 111 | from contextlib import closing |
Phillip J. Eby | bdfd693 | 2006-03-28 00:08:22 +0000 | [diff] [blame] | 112 | import codecs |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 113 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 114 | with closing(urllib.urlopen('http://www.python.org')) as page: |
| 115 | for line in page: |
Nick Coghlan | 5ef9d9f | 2006-04-23 15:14:37 +0000 | [diff] [blame] | 116 | print line |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 117 | \end{verbatim} |
| 118 | |
Nick Coghlan | a7e820a | 2006-04-25 10:56:51 +0000 | [diff] [blame] | 119 | without needing to explicitly close \code{page}. Even if an error |
| 120 | occurs, \code{page.close()} will be called when the \keyword{with} |
| 121 | block is exited. |
Phillip J. Eby | d207b4f | 2006-03-27 23:58:46 +0000 | [diff] [blame] | 122 | \end{funcdesc} |
| 123 | |
| 124 | \begin{seealso} |
| 125 | \seepep{0343}{The "with" statement} |
| 126 | {The specification, background, and examples for the |
| 127 | Python \keyword{with} statement.} |
| 128 | \end{seealso} |