annotate Lib/test/test_contextlib.py @ 84007:dbfbdf2b5c19 3.3

Fix #17967: For ftp urls CWD to target instead of hopping to each directory towards target. This fixes a bug where target is accessible, but parent directories are restricted.
author Senthil Kumaran <senthil@uthcode.com>
date Sat, 01 Jun 2013 08:27:06 -0700
parents c108bc96aec6
children 423736775f6b 406b47c64480
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
134 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
152 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
153 self.assertTrue(f.closed)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 finally:
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
168 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
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
77095
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
373 class TestExitStack(unittest.TestCase):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
374
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
375 def test_no_resources(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
376 with ExitStack():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
377 pass
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
378
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
379 def test_callback(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
380 expected = [
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
381 ((), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
382 ((1,), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
383 ((1,2), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
384 ((), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
385 ((1,), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
386 ((1,2), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
387 ]
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
388 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
389 def _exit(*args, **kwds):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
390 """Test metadata propagation"""
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
391 result.append((args, kwds))
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
392 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
393 for args, kwds in reversed(expected):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
394 if args and kwds:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
395 f = stack.callback(_exit, *args, **kwds)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
396 elif args:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
397 f = stack.callback(_exit, *args)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
398 elif kwds:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
399 f = stack.callback(_exit, **kwds)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
400 else:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
401 f = stack.callback(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
402 self.assertIs(f, _exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
403 for wrapper in stack._exit_callbacks:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
404 self.assertIs(wrapper.__wrapped__, _exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
405 self.assertNotEqual(wrapper.__name__, _exit.__name__)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
406 self.assertIsNone(wrapper.__doc__, _exit.__doc__)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
407 self.assertEqual(result, expected)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
408
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
409 def test_push(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
410 exc_raised = ZeroDivisionError
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
411 def _expect_exc(exc_type, exc, exc_tb):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
412 self.assertIs(exc_type, exc_raised)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
413 def _suppress_exc(*exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
414 return True
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
415 def _expect_ok(exc_type, exc, exc_tb):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
416 self.assertIsNone(exc_type)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
417 self.assertIsNone(exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
418 self.assertIsNone(exc_tb)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
419 class ExitCM(object):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
420 def __init__(self, check_exc):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
421 self.check_exc = check_exc
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
422 def __enter__(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
423 self.fail("Should not be called!")
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
424 def __exit__(self, *exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
425 self.check_exc(*exc_details)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
426 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
427 stack.push(_expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
428 self.assertIs(stack._exit_callbacks[-1], _expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
429 cm = ExitCM(_expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
430 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
431 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
432 stack.push(_suppress_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
433 self.assertIs(stack._exit_callbacks[-1], _suppress_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
434 cm = ExitCM(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
435 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
436 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
437 stack.push(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
438 self.assertIs(stack._exit_callbacks[-1], _expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
439 stack.push(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
440 self.assertIs(stack._exit_callbacks[-1], _expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
441 1/0
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
442
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
443 def test_enter_context(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
444 class TestCM(object):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
445 def __enter__(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
446 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
447 def __exit__(self, *exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
448 result.append(3)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
449
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
450 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
451 cm = TestCM()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
452 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
453 @stack.callback # Registered first => cleaned up last
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
454 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
455 result.append(4)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
456 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
457 stack.enter_context(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
458 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
459 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
460 self.assertEqual(result, [1, 2, 3, 4])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
461
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
462 def test_close(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
463 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
464 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
465 @stack.callback
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
466 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
467 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
468 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
469 stack.close()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
470 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
471 self.assertEqual(result, [1, 2])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
472
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
473 def test_pop_all(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
474 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
475 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
476 @stack.callback
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
477 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
478 result.append(3)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
479 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
480 new_stack = stack.pop_all()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
481 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
482 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
483 new_stack.close()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
484 self.assertEqual(result, [1, 2, 3])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
485
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
486 def test_exit_raise(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
487 with self.assertRaises(ZeroDivisionError):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
488 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
489 stack.push(lambda *exc: False)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
490 1/0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
491
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
492 def test_exit_suppress(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
493 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
494 stack.push(lambda *exc: True)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
495 1/0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
496
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
497 def test_exit_exception_chaining_reference(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
498 # Sanity check to make sure that ExitStack chaining matches
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
499 # actual nested with statements
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
500 class RaiseExc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
501 def __init__(self, exc):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
502 self.exc = exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
503 def __enter__(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
504 return self
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
505 def __exit__(self, *exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
506 raise self.exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
507
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
508 class RaiseExcWithContext:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
509 def __init__(self, outer, inner):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
510 self.outer = outer
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
511 self.inner = inner
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
512 def __enter__(self):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
513 return self
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
514 def __exit__(self, *exc_details):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
515 try:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
516 raise self.inner
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
517 except:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
518 raise self.outer
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
519
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
520 class SuppressExc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
521 def __enter__(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
522 return self
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
523 def __exit__(self, *exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
524 type(self).saved_details = exc_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
525 return True
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
526
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
527 try:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
528 with RaiseExc(IndexError):
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
529 with RaiseExcWithContext(KeyError, AttributeError):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
530 with SuppressExc():
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
531 with RaiseExc(ValueError):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
532 1 / 0
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
533 except IndexError as exc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
534 self.assertIsInstance(exc.__context__, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
535 self.assertIsInstance(exc.__context__.__context__, AttributeError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
536 # Inner exceptions were suppressed
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
537 self.assertIsNone(exc.__context__.__context__.__context__)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
538 else:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
539 self.fail("Expected IndexError, but no exception was raised")
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
540 # Check the inner exceptions
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
541 inner_exc = SuppressExc.saved_details[1]
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
542 self.assertIsInstance(inner_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
543 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
544
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
545 def test_exit_exception_chaining(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
546 # Ensure exception chaining matches the reference behaviour
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
547 def raise_exc(exc):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
548 raise exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
549
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
550 saved_details = None
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
551 def suppress_exc(*exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
552 nonlocal saved_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
553 saved_details = exc_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
554 return True
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
555
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
556 try:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
557 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
558 stack.callback(raise_exc, IndexError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
559 stack.callback(raise_exc, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
560 stack.callback(raise_exc, AttributeError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
561 stack.push(suppress_exc)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
562 stack.callback(raise_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
563 1 / 0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
564 except IndexError as exc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
565 self.assertIsInstance(exc.__context__, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
566 self.assertIsInstance(exc.__context__.__context__, AttributeError)
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
567 # Inner exceptions were suppressed
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
568 self.assertIsNone(exc.__context__.__context__.__context__)
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
569 else:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
570 self.fail("Expected IndexError, but no exception was raised")
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
571 # Check the inner exceptions
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
572 inner_exc = saved_details[1]
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
573 self.assertIsInstance(inner_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
574 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
575
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
576 def test_exit_exception_chaining_suppress(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
577 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
578 stack.push(lambda *exc: True)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
579 stack.push(lambda *exc: 1/0)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
580 stack.push(lambda *exc: {}[1])
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
581
77260
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
582 def test_excessive_nesting(self):
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
583 # The original implementation would die with RecursionError here
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
584 with ExitStack() as stack:
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
585 for i in range(10000):
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
586 stack.callback(int)
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
587
77095
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
588 def test_instance_bypass(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
589 class Example(object): pass
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
590 cm = Example()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
591 cm.__exit__ = object()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
592 stack = ExitStack()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
593 self.assertRaises(AttributeError, stack.enter_context, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
594 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
595 self.assertIs(stack._exit_callbacks[-1], cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
596
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
597
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
598 # 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
599 def test_main():
55021
b168dccee602 remove warnings supression
Benjamin Peterson <benjamin@python.org>
parents: 55018
diff changeset
600 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
601
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 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
603 test_main()