Mercurial > cpython-lru_cache-weakref
annotate Lib/contextlib.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | 8c17e898e0e8 |
| children | 8ef66c73b1e1 |
| 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 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
62652
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
6 __all__ = ["contextmanager", "closing", "ContextDecorator"] |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
7 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 |
|
62652
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
9 class ContextDecorator(object): |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
10 "A base class or mixin that enables context managers to work as decorators." |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
11 |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
12 def _recreate_cm(self): |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
13 """Return a recreated instance of self. |
|
69851
9b76edfddc54
Remove trailing whitespace
Nick Coghlan <ncoghlan@gmail.com>
parents:
69849
diff
changeset
|
14 |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
15 Allows otherwise one-shot context managers like |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
16 _GeneratorContextManager to support use as |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
17 decorators via implicit recreation. |
|
69851
9b76edfddc54
Remove trailing whitespace
Nick Coghlan <ncoghlan@gmail.com>
parents:
69849
diff
changeset
|
18 |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
19 Note: this is a private interface just for _GCM in 3.2 but will be |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
20 renamed and documented for third party use in 3.3 |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
21 """ |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
22 return self |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
23 |
|
62652
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
24 def __call__(self, func): |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
25 @wraps(func) |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
26 def inner(*args, **kwds): |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
27 with self._recreate_cm(): |
|
62652
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
28 return func(*args, **kwds) |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
29 return inner |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
30 |
|
30e1d8d08105
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
55020
diff
changeset
|
31 |
|
67463
c03f64c71b05
Issue #10859: Make `contextlib.GeneratorContextManager` officially
Antoine Pitrou <solipsis@pitrou.net>
parents:
62652
diff
changeset
|
32 class _GeneratorContextManager(ContextDecorator): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 """Helper for @contextmanager decorator.""" |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
35 def __init__(self, func, *args, **kwds): |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
36 self.gen = func(*args, **kwds) |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
37 self.func, self.args, self.kwds = func, args, kwds |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
38 |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
39 def _recreate_cm(self): |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
40 # _GCM instances are one-shot context managers, so the |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
41 # CM must be recreated each time a decorated function is |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
42 # called |
|
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
43 return self.__class__(self.func, *self.args, **self.kwds) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 def __enter__(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
47 return next(self.gen) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 raise RuntimeError("generator didn't yield") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 def __exit__(self, type, value, traceback): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 if type is None: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40587
diff
changeset
|
54 next(self.gen) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 except StopIteration: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 return |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 raise RuntimeError("generator didn't stop") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 else: |
|
43756
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
60 if value is None: |
|
317ba53cd45c
Merged revisions 58742-58816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43189
diff
changeset
|
61 # 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
|
62 # 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
|
63 value = type() |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 # 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
|
69 # 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
|
70 # 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
|
71 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
|
72 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
|
73 # 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
|
74 # 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
|
75 # 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
|
76 # 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
|
77 # 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
|
78 # 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
|
79 # |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36926
diff
changeset
|
80 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
|
81 raise |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 def contextmanager(func): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 """@contextmanager decorator. |
|
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 Typical usage: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 @contextmanager |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 def some_generator(<arguments>): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 yield <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 This makes this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 with some_generator(<arguments>) as <variable>: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 <body> |
|
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 equivalent to this: |
|
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 <setup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 <variable> = <value> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 <body> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 <cleanup> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 """ |
|
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
|
112 @wraps(func) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 def helper(*args, **kwds): |
|
69849
e4ba097123f6
Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents:
67463
diff
changeset
|
114 return _GeneratorContextManager(func, *args, **kwds) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 return helper |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
118 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
|
119 """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
|
120 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 Code like this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 with closing(<module>.open(<arguments>)) as f: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 <block> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 is equivalent to this: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 f = <module>.open(<arguments>) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 <block> |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 f.close() |
|
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 """ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 self.thing.close() |
