Mercurial > cpython-lru_cache-weakref
annotate Lib/contextlib.py @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 1423834b72d8 |
| children | 787b969d37f0 260c8ed3f5df |
| rev | line source |
|---|---|
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Utilities for with-statement contexts. See PEP 343.""" |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 import sys |
|
46612
1423834b72d8
#1748: use functools.wraps instead of rolling own metadata update.
Georg Brandl <georg@python.org>
parents:
43749
diff
changeset
|
4 from functools import wraps |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
6 __all__ = ["contextmanager", "nested", "closing"] |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
8 class GeneratorContextManager(object): |
|
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
9 """Helper for @contextmanager decorator.""" |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 def __init__(self, gen): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 self.gen = gen |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 def __enter__(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 return self.gen.next() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 raise RuntimeError("generator didn't yield") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 def __exit__(self, type, value, traceback): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 if type is None: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 self.gen.next() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 return |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 raise RuntimeError("generator didn't stop") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 else: |
|
43749
c4624dc0d41b
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
Nick Coghlan <ncoghlan@gmail.com>
parents:
42737
diff
changeset
|
29 if value is None: |
|
c4624dc0d41b
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
Nick Coghlan <ncoghlan@gmail.com>
parents:
42737
diff
changeset
|
30 # Need to force instantiation so we can reliably |
|
c4624dc0d41b
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
Nick Coghlan <ncoghlan@gmail.com>
parents:
42737
diff
changeset
|
31 # tell if we get the same exception back |
|
c4624dc0d41b
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
Nick Coghlan <ncoghlan@gmail.com>
parents:
42737
diff
changeset
|
32 value = type() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 self.gen.throw(type, value, traceback) |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36926
diff
changeset
|
35 raise RuntimeError("generator didn't stop after throw()") |
|
37565
8094cc0c98d7
Minor clarity edit to contextlib per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37449
diff
changeset
|
36 except StopIteration, exc: |
|
37449
5aa3cef4a8a7
Fix typos; enhance comments on patch for SF #1462485.
Phillip J. Eby <pje@telecommunity.com>
parents:
37446
diff
changeset
|
37 # Suppress the exception *unless* it's the same exception that |
|
5aa3cef4a8a7
Fix typos; enhance comments on patch for SF #1462485.
Phillip J. Eby <pje@telecommunity.com>
parents:
37446
diff
changeset
|
38 # was passed to throw(). This prevents a StopIteration |
|
5aa3cef4a8a7
Fix typos; enhance comments on patch for SF #1462485.
Phillip J. Eby <pje@telecommunity.com>
parents:
37446
diff
changeset
|
39 # raised inside the "with" statement from being suppressed |
|
37565
8094cc0c98d7
Minor clarity edit to contextlib per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37449
diff
changeset
|
40 return exc is not value |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36926
diff
changeset
|
41 except: |
|
37206
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
42 # only re-raise if it's *not* the exception that was |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
43 # passed to throw(), because __exit__() must not raise |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
44 # an exception unless __exit__() itself failed. But throw() |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
45 # has to raise the exception to signal propagation, so this |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
46 # fixes the impedance mismatch between the throw() protocol |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
47 # and the __exit__() protocol. |
|
51df994885e9
More extensive comment on __exit__ handling, per Guido's request.
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
48 # |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36926
diff
changeset
|
49 if sys.exc_info()[1] is not value: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36926
diff
changeset
|
50 raise |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
53 def contextmanager(func): |
|
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
54 """@contextmanager decorator. |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 Typical usage: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
58 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 def some_generator(<arguments>): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 yield <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 This makes this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 with some_generator(<arguments>) as <variable>: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 <variable> = <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 """ |
|
46612
1423834b72d8
#1748: use functools.wraps instead of rolling own metadata update.
Georg Brandl <georg@python.org>
parents:
43749
diff
changeset
|
81 @wraps(func) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 def helper(*args, **kwds): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
83 return GeneratorContextManager(func(*args, **kwds)) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 return helper |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
87 @contextmanager |
|
38084
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
38045
diff
changeset
|
88 def nested(*managers): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 """Support multiple context managers in a single with-statement. |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 Code like this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 with nested(A, B, C) as (X, Y, Z): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 is equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 with A as X: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 with B as Y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 with C as Z: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 """ |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 exits = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 vars = [] |
|
36926
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
106 exc = (None, None, None) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 try: |
|
42737
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
108 for mgr in managers: |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
109 exit = mgr.__exit__ |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
110 enter = mgr.__enter__ |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
111 vars.append(enter()) |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
112 exits.append(exit) |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
113 yield vars |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
114 except: |
|
adb83f4ac4cb
Use try-except-finally in contextlib.
Georg Brandl <georg@python.org>
parents:
38101
diff
changeset
|
115 exc = sys.exc_info() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 while exits: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 exit = exits.pop() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 try: |
|
36926
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
120 if exit(*exc): |
|
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
121 exc = (None, None, None) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 except: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 exc = sys.exc_info() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 if exc != (None, None, None): |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37565
diff
changeset
|
125 # Don't rely on sys.exc_info() still containing |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37565
diff
changeset
|
126 # the right information. Another exception may |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37565
diff
changeset
|
127 # have been raised and caught by an exit method |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37565
diff
changeset
|
128 raise exc[0], exc[1], exc[2] |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
37971
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
131 class closing(object): |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
132 """Context to automatically close something at the end of a block. |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 Code like this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 with closing(<module>.open(<arguments>)) as f: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 <block> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 is equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 f = <module>.open(<arguments>) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 <block> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 f.close() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 """ |
|
37971
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
148 def __init__(self, thing): |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
149 self.thing = thing |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
150 def __enter__(self): |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
151 return self.thing |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
152 def __exit__(self, *exc_info): |
|
af44db2a9523
Move the PEP 343 documentation and implementation closer to the
Nick Coghlan <ncoghlan@gmail.com>
parents:
37955
diff
changeset
|
153 self.thing.close() |
