Mercurial > cpython-lru_cache-weakref
annotate Doc/library/contextlib.rst @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b5674229d0f9 |
| children |
| rev | line source |
|---|---|
|
56814
127d623396ab
Merged revisions 73286,73294,73296,73459,73462-73463,73544,73576-73577,73595-73596,73693-73694,73704-73705,73707,73713,73937-73940,73945,73951,73979 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52909
diff
changeset
|
1 :mod:`contextlib` --- Utilities for :keyword:`with`\ -statement contexts |
|
127d623396ab
Merged revisions 73286,73294,73296,73459,73462-73463,73544,73576-73577,73595-73596,73693-73694,73704-73705,73707,73713,73937-73940,73945,73951,73979 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52909
diff
changeset
|
2 ======================================================================== |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 .. module:: contextlib |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 :synopsis: Utilities for with-statement contexts. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 .. versionadded:: 2.5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 |
|
71952
b5674229d0f9
Backport source links from 3.x.
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
10 **Source code:** :source:`Lib/contextlib.py` |
|
b5674229d0f9
Backport source links from 3.x.
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
11 |
|
b5674229d0f9
Backport source links from 3.x.
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
12 -------------- |
|
b5674229d0f9
Backport source links from 3.x.
Éric Araujo <merwok@netwok.org>
parents:
68256
diff
changeset
|
13 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 This module provides utilities for common tasks involving the :keyword:`with` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 statement. For more information see also :ref:`typecontextmanager` and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 :ref:`context-managers`. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 Functions provided: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 .. function:: contextmanager(func) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 |
|
44103
1e266015cf5b
Add more entries to the glossary.
Georg Brandl <georg@python.org>
parents:
43629
diff
changeset
|
23 This function is a :term:`decorator` that can be used to define a factory |
|
1e266015cf5b
Add more entries to the glossary.
Georg Brandl <georg@python.org>
parents:
43629
diff
changeset
|
24 function for :keyword:`with` statement context managers, without needing to |
|
1e266015cf5b
Add more entries to the glossary.
Georg Brandl <georg@python.org>
parents:
43629
diff
changeset
|
25 create a class or separate :meth:`__enter__` and :meth:`__exit__` methods. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 A simple example (this is not recommended as a real way of generating HTML!):: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 from contextlib import contextmanager |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 @contextmanager |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 def tag(name): |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 print "<%s>" % name |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 yield |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
35 print "</%s>" % name |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 >>> with tag("h1"): |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 ... print "foo" |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 <h1> |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 foo |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 </h1> |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 |
|
43629
4f662ba09f1f
Add :term: for generators.
Georg Brandl <georg@python.org>
parents:
42604
diff
changeset
|
44 The function being decorated must return a :term:`generator`-iterator when |
|
4f662ba09f1f
Add :term: for generators.
Georg Brandl <georg@python.org>
parents:
42604
diff
changeset
|
45 called. This iterator must yield exactly one value, which will be bound to |
|
4f662ba09f1f
Add :term: for generators.
Georg Brandl <georg@python.org>
parents:
42604
diff
changeset
|
46 the targets in the :keyword:`with` statement's :keyword:`as` clause, if any. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
48 At the point where the generator yields, the block nested in the :keyword:`with` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 statement is executed. The generator is then resumed after the block is exited. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 If an unhandled exception occurs in the block, it is reraised inside the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 generator at the point where the yield occurred. Thus, you can use a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` statement to trap |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
53 the error (if any), or ensure that some cleanup takes place. If an exception is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 trapped merely in order to log it or to perform some action (rather than to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 suppress it entirely), the generator must reraise that exception. Otherwise the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 generator context manager will indicate to the :keyword:`with` statement that |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 the exception has been handled, and execution will resume with the statement |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 immediately following the :keyword:`with` statement. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 .. function:: nested(mgr1[, mgr2[, ...]]) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
63 Combine multiple context managers into a single nested context manager. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 |
|
54785
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
65 This function has been deprecated in favour of the multiple manager form |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
66 of the :keyword:`with` statement. |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
67 |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
68 The one advantage of this function over the multiple manager form of the |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
69 :keyword:`with` statement is that argument unpacking allows it to be |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
70 used with a variable number of context managers as follows:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
71 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 from contextlib import nested |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
73 |
|
54785
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
74 with nested(*managers): |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 do_something() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 Note that if the :meth:`__exit__` method of one of the nested context managers |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 indicates an exception should be suppressed, no exception information will be |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 passed to any remaining outer context managers. Similarly, if the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
80 :meth:`__exit__` method of one of the nested managers raises an exception, any |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 previous exception state will be lost; the new exception will be passed to the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
82 :meth:`__exit__` methods of any remaining outer context managers. In general, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 :meth:`__exit__` methods should avoid raising exceptions, and in particular they |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
84 should not re-raise a passed-in exception. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 |
|
54785
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
86 This function has two major quirks that have led to it being deprecated. Firstly, |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
87 as the context managers are all constructed before the function is invoked, the |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
88 :meth:`__new__` and :meth:`__init__` methods of the inner context managers are |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
89 not actually covered by the scope of the outer context managers. That means, for |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
90 example, that using :func:`nested` to open two files is a programming error as the |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
91 first file will not be closed promptly if an exception is thrown when opening |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
92 the second file. |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
93 |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
94 Secondly, if the :meth:`__enter__` method of one of the inner context managers |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
95 raises an exception that is caught and suppressed by the :meth:`__exit__` method |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
96 of one of the outer context managers, this construct will raise |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
97 :exc:`RuntimeError` rather than skipping the body of the :keyword:`with` |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
98 statement. |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
99 |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
100 Developers that need to support nesting of a variable number of context managers |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
101 can either use the :mod:`warnings` module to suppress the DeprecationWarning |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
102 raised by this function or else use this function as a model for an application |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
103 specific implementation. |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
104 |
|
54411
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
52909
diff
changeset
|
105 .. deprecated:: 2.7 |
|
54785
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
106 The with-statement now supports this functionality directly (without the |
|
33489117da9c
Issue 6288: update the contextlib.nested() docs to explain why it has been deprecated and should generally be avoided
Nick Coghlan <ncoghlan@gmail.com>
parents:
54618
diff
changeset
|
107 confusing error prone quirks). |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
108 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 .. function:: closing(thing) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 Return a context manager that closes *thing* upon completion of the block. This |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 is basically equivalent to:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 from contextlib import contextmanager |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 @contextmanager |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 def closing(thing): |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 try: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 yield thing |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 finally: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 thing.close() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 And lets you write code like this:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 from contextlib import closing |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 import urllib |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 with closing(urllib.urlopen('http://www.python.org')) as page: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
129 for line in page: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
130 print line |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 without needing to explicitly close ``page``. Even if an error occurs, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 ``page.close()`` will be called when the :keyword:`with` block is exited. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
136 .. seealso:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
137 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
138 :pep:`0343` - The "with" statement |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 The specification, background, and examples for the Python :keyword:`with` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 statement. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 |
