Mercurial > cpython-lru_cache-weakref
annotate Lib/contextlib.py @ 66978:aed2bbafa550 3.1
Blocked revisions 87324 via svnmerge
........
r87324 | r.david.murray | 2010-12-16 14:08:51 -0500 (Thu, 16 Dec 2010) | 9 lines
#10719: restore messages generated on invalid compileall args
Before the introduction of filename arguments to compileall it gave semi useful
messages about not being able to 'list' names that weren't valid directories.
This fix restores that behavior. In addition to the test for this case, the
patch also adds a test for the default behavior of compileall when no arguments
are provided, and fixes a bug in one of the previously added tests.
........
| author | R. David Murray <rdmurray@bitdance.com> |
|---|---|
| date | Thu, 16 Dec 2010 19:10:16 +0000 |
| parents | 45506be44514 |
| children | e3fa2ddbb3a3 |
| 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 |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43756
diff
changeset
|
4 from functools import wraps |
|
54404
3eb522dc3337
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46664
diff
changeset
|
5 from warnings import warn |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 __all__ = ["contextmanager", "nested", "closing"] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 class GeneratorContextManager(object): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 """Helper for @contextmanager decorator.""" |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 def __init__(self, gen): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 self.gen = gen |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 def __enter__(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
17 return next(self.gen) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 raise RuntimeError("generator didn't yield") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 def __exit__(self, type, value, traceback): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 if type is None: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
24 next(self.gen) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 return |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 raise RuntimeError("generator didn't stop") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 else: |
|
43756
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
30 if value is None: |
|
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
31 # Need to force instantiation so we can reliably |
|
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
32 # tell if we get the same exception back |
|
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
33 value = type() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 self.gen.throw(type, value, traceback) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
36 raise RuntimeError("generator didn't stop after throw()") |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
38453
diff
changeset
|
37 except StopIteration as exc: |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
38 # Suppress the exception *unless* it's the same exception that |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
39 # was passed to throw(). This prevents a StopIteration |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
40 # raised inside the "with" statement from being suppressed |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
41 return exc is not value |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
42 except: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
43 # only re-raise if it's *not* the exception that was |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
44 # passed to throw(), because __exit__() must not raise |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
45 # an exception unless __exit__() itself failed. But throw() |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
46 # has to raise the exception to signal propagation, so this |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
47 # fixes the impedance mismatch between the throw() protocol |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
48 # and the __exit__() protocol. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
49 # |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
50 if sys.exc_info()[1] is not value: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
51 raise |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 def contextmanager(func): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 """@contextmanager decorator. |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 Typical usage: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 @contextmanager |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 def some_generator(<arguments>): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 yield <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 This makes this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 with some_generator(<arguments>) as <variable>: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 <variable> = <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 """ |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43756
diff
changeset
|
82 @wraps(func) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 def helper(*args, **kwds): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 return GeneratorContextManager(func(*args, **kwds)) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 return helper |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 @contextmanager |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
89 def nested(*managers): |
|
54823
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
90 """Combine multiple context managers into a single nested context manager. |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
54823
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
92 This function has been deprecated in favour of the multiple manager form |
|
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
93 of the with statement. |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
54823
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
95 The one advantage of this function over the multiple manager form of the |
|
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
96 with statement is that argument unpacking allows it to be |
|
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
97 used with a variable number of context managers as follows: |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
54823
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
99 with nested(*managers): |
|
45506be44514
Merged revisions 73518-73519 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
54660
diff
changeset
|
100 do_something() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 """ |
|
54404
3eb522dc3337
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46664
diff
changeset
|
103 warn("With-statements now directly support multiple context managers", |
|
54660
13a9874d8f47
Issue 6256: Fix stacklevel in warning message.
Raymond Hettinger <python@rcn.com>
parents:
54404
diff
changeset
|
104 DeprecationWarning, 3) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 exits = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 vars = [] |
|
36926
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
107 exc = (None, None, None) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 try: |
|
42775
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
109 for mgr in managers: |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
110 exit = mgr.__exit__ |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
111 enter = mgr.__enter__ |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
112 vars.append(enter()) |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
113 exits.append(exit) |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
114 yield vars |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
115 except: |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
116 exc = sys.exc_info() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 while exits: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 exit = exits.pop() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 try: |
|
36926
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
121 if exit(*exc): |
|
fc10ff322df1
Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents:
36770
diff
changeset
|
122 exc = (None, None, None) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 except: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 exc = sys.exc_info() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 if exc != (None, None, None): |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
126 # Don't rely on sys.exc_info() still containing |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
127 # the right information. Another exception may |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
128 # have been raised and caught by an exit method |
|
43189
d442984f290b
Fix a poorly-translated raise statement in contextlib.
Collin Winter <collinw@gmail.com>
parents:
43102
diff
changeset
|
129 # exc[1] already has the __traceback__ attribute populated |
|
d442984f290b
Fix a poorly-translated raise statement in contextlib.
Collin Winter <collinw@gmail.com>
parents:
43102
diff
changeset
|
130 raise exc[1] |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
133 class closing(object): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
134 """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
|
135 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 Code like this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 with closing(<module>.open(<arguments>)) as f: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 <block> |
|
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 is equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 f = <module>.open(<arguments>) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 <block> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 f.close() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 """ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
150 def __init__(self, thing): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
151 self.thing = thing |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
152 def __enter__(self): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
153 return self.thing |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
154 def __exit__(self, *exc_info): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
155 self.thing.close() |
