Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_contextlib.py @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | e4ba097123f6 |
| children | 8ef66c73b1e1 |
| rev | line source |
|---|---|
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Unit tests for contextlib.py, and other context managers.""" |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
3 import sys |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 import tempfile |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import unittest |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 from contextlib import * # Tests __all__ |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46664
diff
changeset
|
7 from test import support |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60174
diff
changeset
|
8 try: |
|
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60174
diff
changeset
|
9 import threading |
|
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60174
diff
changeset
|
10 except ImportError: |
|
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60174
diff
changeset
|
11 threading = None |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
| 60174 | 13 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 class ContextManagerTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 def test_contextmanager_plain(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 @contextmanager |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 state.append(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 def test_contextmanager_finally(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 @contextmanager |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 state.append(1) |
|
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 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 state.append(999) |
| 60174 | 38 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 raise ZeroDivisionError() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
46 def test_contextmanager_no_reraise(self): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
47 @contextmanager |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
48 def whee(): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
49 yield |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
50 ctx = whee() |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
51 ctx.__enter__() |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
52 # Calling __exit__ should not result in an exception |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
53 self.assertFalse(ctx.__exit__(TypeError, TypeError("foo"), None)) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
54 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
55 def test_contextmanager_trap_yield_after_throw(self): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
56 @contextmanager |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
57 def whoo(): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
58 try: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
59 yield |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
60 except: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
61 yield |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
62 ctx = whoo() |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
63 ctx.__enter__() |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
64 self.assertRaises( |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
65 RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
66 ) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
67 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 def test_contextmanager_except(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 @contextmanager |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 yield 42 |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
75 except ZeroDivisionError as e: |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 state.append(e.args[0]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 raise ZeroDivisionError(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
85 def _create_contextmanager_attribs(self): |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
86 def attribs(**kw): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
87 def decorate(func): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
88 for k,v in kw.items(): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
89 setattr(func,k,v) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
90 return func |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
91 return decorate |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
92 @contextmanager |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
93 @attribs(foo='bar') |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
94 def baz(spam): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
95 """Whee!""" |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
96 return baz |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
97 |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
98 def test_contextmanager_attribs(self): |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
99 baz = self._create_contextmanager_attribs() |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
100 self.assertEqual(baz.__name__,'baz') |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
101 self.assertEqual(baz.foo, 'bar') |
|
59091
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
102 |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
103 @unittest.skipIf(sys.flags.optimize >= 2, |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
104 "Docstrings are omitted with -O2 and above") |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
105 def test_contextmanager_doc_attrib(self): |
|
8d3ff2fbbe4b
Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
55021
diff
changeset
|
106 baz = self._create_contextmanager_attribs() |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
107 self.assertEqual(baz.__doc__, "Whee!") |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
108 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 class ClosingTestCase(unittest.TestCase): |
|
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 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 def test_closing(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 def test_closing_error(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 self.assertEqual(state, []) |
| 60174 | 131 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 self.assertEqual(x, y) |
| 60174 | 134 1 / 0 |
| 135 self.assertEqual(state, [1]) | |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 class FileContextTestCase(unittest.TestCase): |
|
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 def testWithOpen(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 tfn = tempfile.mktemp() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 with open(tfn, "w") as f: |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
144 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 f.write("Booh\n") |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
146 self.assertTrue(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 f = None |
| 60174 | 148 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 with open(tfn, "r") as f: |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
150 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 self.assertEqual(f.read(), "Booh\n") |
| 60174 | 152 1 / 0 |
| 153 self.assertTrue(f.closed) | |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 finally: |
| 60174 | 155 support.unlink(tfn) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60174
diff
changeset
|
157 @unittest.skipUnless(threading, 'Threading required for this test.') |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 class LockContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 def boilerPlate(self, lock, locked): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
161 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 with lock: |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
163 self.assertTrue(locked()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
164 self.assertFalse(locked()) |
| 60174 | 165 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 with lock: |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54406
diff
changeset
|
167 self.assertTrue(locked()) |
| 60174 | 168 1 / 0 |
| 169 self.assertFalse(locked()) | |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 def testWithLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 lock = threading.Lock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 self.boilerPlate(lock, lock.locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 def testWithRLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 lock = threading.RLock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 self.boilerPlate(lock, lock._is_owned) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 def testWithCondition(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 lock = threading.Condition() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 return lock._is_owned() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 def testWithSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 lock = threading.Semaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 def testWithBoundedSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 lock = threading.BoundedSemaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
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:
61073
diff
changeset
|
205 |
|
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:
61073
diff
changeset
|
206 class mycontext(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:
61073
diff
changeset
|
207 started = False |
|
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:
61073
diff
changeset
|
208 exc = None |
|
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:
61073
diff
changeset
|
209 catch = False |
|
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:
61073
diff
changeset
|
210 |
|
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:
61073
diff
changeset
|
211 def __enter__(self): |
|
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:
61073
diff
changeset
|
212 self.started = True |
|
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:
61073
diff
changeset
|
213 return self |
|
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:
61073
diff
changeset
|
214 |
|
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:
61073
diff
changeset
|
215 def __exit__(self, *exc): |
|
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:
61073
diff
changeset
|
216 self.exc = exc |
|
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:
61073
diff
changeset
|
217 return self.catch |
|
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:
61073
diff
changeset
|
218 |
|
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:
61073
diff
changeset
|
219 |
|
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:
61073
diff
changeset
|
220 class TestContextDecorator(unittest.TestCase): |
|
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:
61073
diff
changeset
|
221 |
|
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:
61073
diff
changeset
|
222 def test_contextdecorator(self): |
|
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:
61073
diff
changeset
|
223 context = mycontext() |
|
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:
61073
diff
changeset
|
224 with context as result: |
|
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:
61073
diff
changeset
|
225 self.assertIs(result, context) |
|
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:
61073
diff
changeset
|
226 self.assertTrue(context.started) |
|
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:
61073
diff
changeset
|
227 |
|
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:
61073
diff
changeset
|
228 self.assertEqual(context.exc, (None, None, None)) |
|
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:
61073
diff
changeset
|
229 |
|
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:
61073
diff
changeset
|
230 |
|
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:
61073
diff
changeset
|
231 def test_contextdecorator_with_exception(self): |
|
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:
61073
diff
changeset
|
232 context = mycontext() |
|
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:
61073
diff
changeset
|
233 |
|
66581
38bec90fd8ab
#10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62652
diff
changeset
|
234 with self.assertRaisesRegex(NameError, 'foo'): |
|
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:
61073
diff
changeset
|
235 with context: |
|
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:
61073
diff
changeset
|
236 raise NameError('foo') |
|
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:
61073
diff
changeset
|
237 self.assertIsNotNone(context.exc) |
|
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:
61073
diff
changeset
|
238 self.assertIs(context.exc[0], NameError) |
|
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:
61073
diff
changeset
|
239 |
|
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:
61073
diff
changeset
|
240 context = mycontext() |
|
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:
61073
diff
changeset
|
241 context.catch = True |
|
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:
61073
diff
changeset
|
242 with context: |
|
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:
61073
diff
changeset
|
243 raise NameError('foo') |
|
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:
61073
diff
changeset
|
244 self.assertIsNotNone(context.exc) |
|
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:
61073
diff
changeset
|
245 self.assertIs(context.exc[0], NameError) |
|
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:
61073
diff
changeset
|
246 |
|
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:
61073
diff
changeset
|
247 |
|
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:
61073
diff
changeset
|
248 def test_decorator(self): |
|
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:
61073
diff
changeset
|
249 context = mycontext() |
|
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:
61073
diff
changeset
|
250 |
|
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:
61073
diff
changeset
|
251 @context |
|
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:
61073
diff
changeset
|
252 def test(): |
|
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:
61073
diff
changeset
|
253 self.assertIsNone(context.exc) |
|
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:
61073
diff
changeset
|
254 self.assertTrue(context.started) |
|
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:
61073
diff
changeset
|
255 test() |
|
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:
61073
diff
changeset
|
256 self.assertEqual(context.exc, (None, None, None)) |
|
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:
61073
diff
changeset
|
257 |
|
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:
61073
diff
changeset
|
258 |
|
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:
61073
diff
changeset
|
259 def test_decorator_with_exception(self): |
|
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:
61073
diff
changeset
|
260 context = mycontext() |
|
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:
61073
diff
changeset
|
261 |
|
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:
61073
diff
changeset
|
262 @context |
|
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:
61073
diff
changeset
|
263 def test(): |
|
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:
61073
diff
changeset
|
264 self.assertIsNone(context.exc) |
|
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:
61073
diff
changeset
|
265 self.assertTrue(context.started) |
|
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:
61073
diff
changeset
|
266 raise NameError('foo') |
|
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:
61073
diff
changeset
|
267 |
|
66581
38bec90fd8ab
#10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62652
diff
changeset
|
268 with self.assertRaisesRegex(NameError, 'foo'): |
|
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:
61073
diff
changeset
|
269 test() |
|
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:
61073
diff
changeset
|
270 self.assertIsNotNone(context.exc) |
|
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:
61073
diff
changeset
|
271 self.assertIs(context.exc[0], NameError) |
|
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:
61073
diff
changeset
|
272 |
|
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:
61073
diff
changeset
|
273 |
|
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:
61073
diff
changeset
|
274 def test_decorating_method(self): |
|
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:
61073
diff
changeset
|
275 context = mycontext() |
|
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:
61073
diff
changeset
|
276 |
|
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:
61073
diff
changeset
|
277 class Test(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:
61073
diff
changeset
|
278 |
|
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:
61073
diff
changeset
|
279 @context |
|
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:
61073
diff
changeset
|
280 def method(self, a, b, c=None): |
|
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:
61073
diff
changeset
|
281 self.a = a |
|
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:
61073
diff
changeset
|
282 self.b = b |
|
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:
61073
diff
changeset
|
283 self.c = c |
|
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:
61073
diff
changeset
|
284 |
|
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:
61073
diff
changeset
|
285 # these tests are for argument passing when used as a decorator |
|
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:
61073
diff
changeset
|
286 test = Test() |
|
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:
61073
diff
changeset
|
287 test.method(1, 2) |
|
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:
61073
diff
changeset
|
288 self.assertEqual(test.a, 1) |
|
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:
61073
diff
changeset
|
289 self.assertEqual(test.b, 2) |
|
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:
61073
diff
changeset
|
290 self.assertEqual(test.c, None) |
|
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:
61073
diff
changeset
|
291 |
|
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:
61073
diff
changeset
|
292 test = Test() |
|
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:
61073
diff
changeset
|
293 test.method('a', 'b', 'c') |
|
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:
61073
diff
changeset
|
294 self.assertEqual(test.a, 'a') |
|
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:
61073
diff
changeset
|
295 self.assertEqual(test.b, 'b') |
|
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:
61073
diff
changeset
|
296 self.assertEqual(test.c, 'c') |
|
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:
61073
diff
changeset
|
297 |
|
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:
61073
diff
changeset
|
298 test = Test() |
|
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:
61073
diff
changeset
|
299 test.method(a=1, b=2) |
|
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:
61073
diff
changeset
|
300 self.assertEqual(test.a, 1) |
|
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:
61073
diff
changeset
|
301 self.assertEqual(test.b, 2) |
|
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:
61073
diff
changeset
|
302 |
|
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:
61073
diff
changeset
|
303 |
|
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:
61073
diff
changeset
|
304 def test_typo_enter(self): |
|
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:
61073
diff
changeset
|
305 class mycontext(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:
61073
diff
changeset
|
306 def __unter__(self): |
|
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:
61073
diff
changeset
|
307 pass |
|
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:
61073
diff
changeset
|
308 def __exit__(self, *exc): |
|
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:
61073
diff
changeset
|
309 pass |
|
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:
61073
diff
changeset
|
310 |
|
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:
61073
diff
changeset
|
311 with self.assertRaises(AttributeError): |
|
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:
61073
diff
changeset
|
312 with mycontext(): |
|
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:
61073
diff
changeset
|
313 pass |
|
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:
61073
diff
changeset
|
314 |
|
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:
61073
diff
changeset
|
315 |
|
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:
61073
diff
changeset
|
316 def test_typo_exit(self): |
|
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:
61073
diff
changeset
|
317 class mycontext(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:
61073
diff
changeset
|
318 def __enter__(self): |
|
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:
61073
diff
changeset
|
319 pass |
|
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:
61073
diff
changeset
|
320 def __uxit__(self, *exc): |
|
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:
61073
diff
changeset
|
321 pass |
|
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:
61073
diff
changeset
|
322 |
|
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:
61073
diff
changeset
|
323 with self.assertRaises(AttributeError): |
|
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:
61073
diff
changeset
|
324 with mycontext(): |
|
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:
61073
diff
changeset
|
325 pass |
|
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:
61073
diff
changeset
|
326 |
|
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:
61073
diff
changeset
|
327 |
|
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:
61073
diff
changeset
|
328 def test_contextdecorator_as_mixin(self): |
|
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:
61073
diff
changeset
|
329 class somecontext(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:
61073
diff
changeset
|
330 started = False |
|
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:
61073
diff
changeset
|
331 exc = None |
|
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:
61073
diff
changeset
|
332 |
|
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:
61073
diff
changeset
|
333 def __enter__(self): |
|
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:
61073
diff
changeset
|
334 self.started = True |
|
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:
61073
diff
changeset
|
335 return self |
|
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:
61073
diff
changeset
|
336 |
|
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:
61073
diff
changeset
|
337 def __exit__(self, *exc): |
|
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:
61073
diff
changeset
|
338 self.exc = exc |
|
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:
61073
diff
changeset
|
339 |
|
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:
61073
diff
changeset
|
340 class mycontext(somecontext, 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:
61073
diff
changeset
|
341 pass |
|
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:
61073
diff
changeset
|
342 |
|
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:
61073
diff
changeset
|
343 context = mycontext() |
|
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:
61073
diff
changeset
|
344 @context |
|
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:
61073
diff
changeset
|
345 def test(): |
|
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:
61073
diff
changeset
|
346 self.assertIsNone(context.exc) |
|
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:
61073
diff
changeset
|
347 self.assertTrue(context.started) |
|
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:
61073
diff
changeset
|
348 test() |
|
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:
61073
diff
changeset
|
349 self.assertEqual(context.exc, (None, None, None)) |
|
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:
61073
diff
changeset
|
350 |
|
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:
61073
diff
changeset
|
351 |
|
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:
61073
diff
changeset
|
352 def test_contextmanager_as_decorator(self): |
|
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:
61073
diff
changeset
|
353 @contextmanager |
|
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:
61073
diff
changeset
|
354 def woohoo(y): |
|
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:
61073
diff
changeset
|
355 state.append(y) |
|
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:
61073
diff
changeset
|
356 yield |
|
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:
61073
diff
changeset
|
357 state.append(999) |
|
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:
61073
diff
changeset
|
358 |
|
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:
66581
diff
changeset
|
359 state = [] |
|
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:
61073
diff
changeset
|
360 @woohoo(1) |
|
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:
61073
diff
changeset
|
361 def test(x): |
|
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:
61073
diff
changeset
|
362 self.assertEqual(state, [1]) |
|
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:
61073
diff
changeset
|
363 state.append(x) |
|
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:
61073
diff
changeset
|
364 test('something') |
|
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:
61073
diff
changeset
|
365 self.assertEqual(state, [1, 'something', 999]) |
|
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:
61073
diff
changeset
|
366 |
|
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:
66581
diff
changeset
|
367 # Issue #11647: Ensure the decorated function is 'reusable' |
|
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:
66581
diff
changeset
|
368 state = [] |
|
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:
66581
diff
changeset
|
369 test('something else') |
|
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:
66581
diff
changeset
|
370 self.assertEqual(state, [1, 'something else', 999]) |
|
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:
66581
diff
changeset
|
371 |
|
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:
61073
diff
changeset
|
372 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
373 # This is needed to make the test actually run under regrtest.py! |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
374 def test_main(): |
|
55021
b168dccee602
remove warnings supression
Benjamin Peterson <benjamin@python.org>
parents:
55018
diff
changeset
|
375 support.run_unittest(__name__) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
376 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 if __name__ == "__main__": |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36770
diff
changeset
|
378 test_main() |
