1 | n/a | """Unit tests for the with statement specified in PEP 343.""" |
---|
2 | n/a | |
---|
3 | n/a | |
---|
4 | n/a | __author__ = "Mike Bland" |
---|
5 | n/a | __email__ = "mbland at acm dot org" |
---|
6 | n/a | |
---|
7 | n/a | import sys |
---|
8 | n/a | import unittest |
---|
9 | n/a | from collections import deque |
---|
10 | n/a | from contextlib import _GeneratorContextManager, contextmanager |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class MockContextManager(_GeneratorContextManager): |
---|
14 | n/a | def __init__(self, *args): |
---|
15 | n/a | super().__init__(*args) |
---|
16 | n/a | self.enter_called = False |
---|
17 | n/a | self.exit_called = False |
---|
18 | n/a | self.exit_args = None |
---|
19 | n/a | |
---|
20 | n/a | def __enter__(self): |
---|
21 | n/a | self.enter_called = True |
---|
22 | n/a | return _GeneratorContextManager.__enter__(self) |
---|
23 | n/a | |
---|
24 | n/a | def __exit__(self, type, value, traceback): |
---|
25 | n/a | self.exit_called = True |
---|
26 | n/a | self.exit_args = (type, value, traceback) |
---|
27 | n/a | return _GeneratorContextManager.__exit__(self, type, |
---|
28 | n/a | value, traceback) |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | def mock_contextmanager(func): |
---|
32 | n/a | def helper(*args, **kwds): |
---|
33 | n/a | return MockContextManager(func, args, kwds) |
---|
34 | n/a | return helper |
---|
35 | n/a | |
---|
36 | n/a | |
---|
37 | n/a | class MockResource(object): |
---|
38 | n/a | def __init__(self): |
---|
39 | n/a | self.yielded = False |
---|
40 | n/a | self.stopped = False |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | @mock_contextmanager |
---|
44 | n/a | def mock_contextmanager_generator(): |
---|
45 | n/a | mock = MockResource() |
---|
46 | n/a | try: |
---|
47 | n/a | mock.yielded = True |
---|
48 | n/a | yield mock |
---|
49 | n/a | finally: |
---|
50 | n/a | mock.stopped = True |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | class Nested(object): |
---|
54 | n/a | |
---|
55 | n/a | def __init__(self, *managers): |
---|
56 | n/a | self.managers = managers |
---|
57 | n/a | self.entered = None |
---|
58 | n/a | |
---|
59 | n/a | def __enter__(self): |
---|
60 | n/a | if self.entered is not None: |
---|
61 | n/a | raise RuntimeError("Context is not reentrant") |
---|
62 | n/a | self.entered = deque() |
---|
63 | n/a | vars = [] |
---|
64 | n/a | try: |
---|
65 | n/a | for mgr in self.managers: |
---|
66 | n/a | vars.append(mgr.__enter__()) |
---|
67 | n/a | self.entered.appendleft(mgr) |
---|
68 | n/a | except: |
---|
69 | n/a | if not self.__exit__(*sys.exc_info()): |
---|
70 | n/a | raise |
---|
71 | n/a | return vars |
---|
72 | n/a | |
---|
73 | n/a | def __exit__(self, *exc_info): |
---|
74 | n/a | # Behave like nested with statements |
---|
75 | n/a | # first in, last out |
---|
76 | n/a | # New exceptions override old ones |
---|
77 | n/a | ex = exc_info |
---|
78 | n/a | for mgr in self.entered: |
---|
79 | n/a | try: |
---|
80 | n/a | if mgr.__exit__(*ex): |
---|
81 | n/a | ex = (None, None, None) |
---|
82 | n/a | except: |
---|
83 | n/a | ex = sys.exc_info() |
---|
84 | n/a | self.entered = None |
---|
85 | n/a | if ex is not exc_info: |
---|
86 | n/a | raise ex[0](ex[1]).with_traceback(ex[2]) |
---|
87 | n/a | |
---|
88 | n/a | |
---|
89 | n/a | class MockNested(Nested): |
---|
90 | n/a | def __init__(self, *managers): |
---|
91 | n/a | Nested.__init__(self, *managers) |
---|
92 | n/a | self.enter_called = False |
---|
93 | n/a | self.exit_called = False |
---|
94 | n/a | self.exit_args = None |
---|
95 | n/a | |
---|
96 | n/a | def __enter__(self): |
---|
97 | n/a | self.enter_called = True |
---|
98 | n/a | return Nested.__enter__(self) |
---|
99 | n/a | |
---|
100 | n/a | def __exit__(self, *exc_info): |
---|
101 | n/a | self.exit_called = True |
---|
102 | n/a | self.exit_args = exc_info |
---|
103 | n/a | return Nested.__exit__(self, *exc_info) |
---|
104 | n/a | |
---|
105 | n/a | |
---|
106 | n/a | class FailureTestCase(unittest.TestCase): |
---|
107 | n/a | def testNameError(self): |
---|
108 | n/a | def fooNotDeclared(): |
---|
109 | n/a | with foo: pass |
---|
110 | n/a | self.assertRaises(NameError, fooNotDeclared) |
---|
111 | n/a | |
---|
112 | n/a | def testEnterAttributeError1(self): |
---|
113 | n/a | class LacksEnter(object): |
---|
114 | n/a | def __exit__(self, type, value, traceback): |
---|
115 | n/a | pass |
---|
116 | n/a | |
---|
117 | n/a | def fooLacksEnter(): |
---|
118 | n/a | foo = LacksEnter() |
---|
119 | n/a | with foo: pass |
---|
120 | n/a | self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnter) |
---|
121 | n/a | |
---|
122 | n/a | def testEnterAttributeError2(self): |
---|
123 | n/a | class LacksEnterAndExit(object): |
---|
124 | n/a | pass |
---|
125 | n/a | |
---|
126 | n/a | def fooLacksEnterAndExit(): |
---|
127 | n/a | foo = LacksEnterAndExit() |
---|
128 | n/a | with foo: pass |
---|
129 | n/a | self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnterAndExit) |
---|
130 | n/a | |
---|
131 | n/a | def testExitAttributeError(self): |
---|
132 | n/a | class LacksExit(object): |
---|
133 | n/a | def __enter__(self): |
---|
134 | n/a | pass |
---|
135 | n/a | |
---|
136 | n/a | def fooLacksExit(): |
---|
137 | n/a | foo = LacksExit() |
---|
138 | n/a | with foo: pass |
---|
139 | n/a | self.assertRaisesRegex(AttributeError, '__exit__', fooLacksExit) |
---|
140 | n/a | |
---|
141 | n/a | def assertRaisesSyntaxError(self, codestr): |
---|
142 | n/a | def shouldRaiseSyntaxError(s): |
---|
143 | n/a | compile(s, '', 'single') |
---|
144 | n/a | self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr) |
---|
145 | n/a | |
---|
146 | n/a | def testAssignmentToNoneError(self): |
---|
147 | n/a | self.assertRaisesSyntaxError('with mock as None:\n pass') |
---|
148 | n/a | self.assertRaisesSyntaxError( |
---|
149 | n/a | 'with mock as (None):\n' |
---|
150 | n/a | ' pass') |
---|
151 | n/a | |
---|
152 | n/a | def testAssignmentToTupleOnlyContainingNoneError(self): |
---|
153 | n/a | self.assertRaisesSyntaxError('with mock as None,:\n pass') |
---|
154 | n/a | self.assertRaisesSyntaxError( |
---|
155 | n/a | 'with mock as (None,):\n' |
---|
156 | n/a | ' pass') |
---|
157 | n/a | |
---|
158 | n/a | def testAssignmentToTupleContainingNoneError(self): |
---|
159 | n/a | self.assertRaisesSyntaxError( |
---|
160 | n/a | 'with mock as (foo, None, bar):\n' |
---|
161 | n/a | ' pass') |
---|
162 | n/a | |
---|
163 | n/a | def testEnterThrows(self): |
---|
164 | n/a | class EnterThrows(object): |
---|
165 | n/a | def __enter__(self): |
---|
166 | n/a | raise RuntimeError("Enter threw") |
---|
167 | n/a | def __exit__(self, *args): |
---|
168 | n/a | pass |
---|
169 | n/a | |
---|
170 | n/a | def shouldThrow(): |
---|
171 | n/a | ct = EnterThrows() |
---|
172 | n/a | self.foo = None |
---|
173 | n/a | with ct as self.foo: |
---|
174 | n/a | pass |
---|
175 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
176 | n/a | self.assertEqual(self.foo, None) |
---|
177 | n/a | |
---|
178 | n/a | def testExitThrows(self): |
---|
179 | n/a | class ExitThrows(object): |
---|
180 | n/a | def __enter__(self): |
---|
181 | n/a | return |
---|
182 | n/a | def __exit__(self, *args): |
---|
183 | n/a | raise RuntimeError(42) |
---|
184 | n/a | def shouldThrow(): |
---|
185 | n/a | with ExitThrows(): |
---|
186 | n/a | pass |
---|
187 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
188 | n/a | |
---|
189 | n/a | class ContextmanagerAssertionMixin(object): |
---|
190 | n/a | |
---|
191 | n/a | def setUp(self): |
---|
192 | n/a | self.TEST_EXCEPTION = RuntimeError("test exception") |
---|
193 | n/a | |
---|
194 | n/a | def assertInWithManagerInvariants(self, mock_manager): |
---|
195 | n/a | self.assertTrue(mock_manager.enter_called) |
---|
196 | n/a | self.assertFalse(mock_manager.exit_called) |
---|
197 | n/a | self.assertEqual(mock_manager.exit_args, None) |
---|
198 | n/a | |
---|
199 | n/a | def assertAfterWithManagerInvariants(self, mock_manager, exit_args): |
---|
200 | n/a | self.assertTrue(mock_manager.enter_called) |
---|
201 | n/a | self.assertTrue(mock_manager.exit_called) |
---|
202 | n/a | self.assertEqual(mock_manager.exit_args, exit_args) |
---|
203 | n/a | |
---|
204 | n/a | def assertAfterWithManagerInvariantsNoError(self, mock_manager): |
---|
205 | n/a | self.assertAfterWithManagerInvariants(mock_manager, |
---|
206 | n/a | (None, None, None)) |
---|
207 | n/a | |
---|
208 | n/a | def assertInWithGeneratorInvariants(self, mock_generator): |
---|
209 | n/a | self.assertTrue(mock_generator.yielded) |
---|
210 | n/a | self.assertFalse(mock_generator.stopped) |
---|
211 | n/a | |
---|
212 | n/a | def assertAfterWithGeneratorInvariantsNoError(self, mock_generator): |
---|
213 | n/a | self.assertTrue(mock_generator.yielded) |
---|
214 | n/a | self.assertTrue(mock_generator.stopped) |
---|
215 | n/a | |
---|
216 | n/a | def raiseTestException(self): |
---|
217 | n/a | raise self.TEST_EXCEPTION |
---|
218 | n/a | |
---|
219 | n/a | def assertAfterWithManagerInvariantsWithError(self, mock_manager, |
---|
220 | n/a | exc_type=None): |
---|
221 | n/a | self.assertTrue(mock_manager.enter_called) |
---|
222 | n/a | self.assertTrue(mock_manager.exit_called) |
---|
223 | n/a | if exc_type is None: |
---|
224 | n/a | self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION) |
---|
225 | n/a | exc_type = type(self.TEST_EXCEPTION) |
---|
226 | n/a | self.assertEqual(mock_manager.exit_args[0], exc_type) |
---|
227 | n/a | # Test the __exit__ arguments. Issue #7853 |
---|
228 | n/a | self.assertIsInstance(mock_manager.exit_args[1], exc_type) |
---|
229 | n/a | self.assertIsNot(mock_manager.exit_args[2], None) |
---|
230 | n/a | |
---|
231 | n/a | def assertAfterWithGeneratorInvariantsWithError(self, mock_generator): |
---|
232 | n/a | self.assertTrue(mock_generator.yielded) |
---|
233 | n/a | self.assertTrue(mock_generator.stopped) |
---|
234 | n/a | |
---|
235 | n/a | |
---|
236 | n/a | class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): |
---|
237 | n/a | def testInlineGeneratorSyntax(self): |
---|
238 | n/a | with mock_contextmanager_generator(): |
---|
239 | n/a | pass |
---|
240 | n/a | |
---|
241 | n/a | def testUnboundGenerator(self): |
---|
242 | n/a | mock = mock_contextmanager_generator() |
---|
243 | n/a | with mock: |
---|
244 | n/a | pass |
---|
245 | n/a | self.assertAfterWithManagerInvariantsNoError(mock) |
---|
246 | n/a | |
---|
247 | n/a | def testInlineGeneratorBoundSyntax(self): |
---|
248 | n/a | with mock_contextmanager_generator() as foo: |
---|
249 | n/a | self.assertInWithGeneratorInvariants(foo) |
---|
250 | n/a | # FIXME: In the future, we'll try to keep the bound names from leaking |
---|
251 | n/a | self.assertAfterWithGeneratorInvariantsNoError(foo) |
---|
252 | n/a | |
---|
253 | n/a | def testInlineGeneratorBoundToExistingVariable(self): |
---|
254 | n/a | foo = None |
---|
255 | n/a | with mock_contextmanager_generator() as foo: |
---|
256 | n/a | self.assertInWithGeneratorInvariants(foo) |
---|
257 | n/a | self.assertAfterWithGeneratorInvariantsNoError(foo) |
---|
258 | n/a | |
---|
259 | n/a | def testInlineGeneratorBoundToDottedVariable(self): |
---|
260 | n/a | with mock_contextmanager_generator() as self.foo: |
---|
261 | n/a | self.assertInWithGeneratorInvariants(self.foo) |
---|
262 | n/a | self.assertAfterWithGeneratorInvariantsNoError(self.foo) |
---|
263 | n/a | |
---|
264 | n/a | def testBoundGenerator(self): |
---|
265 | n/a | mock = mock_contextmanager_generator() |
---|
266 | n/a | with mock as foo: |
---|
267 | n/a | self.assertInWithGeneratorInvariants(foo) |
---|
268 | n/a | self.assertInWithManagerInvariants(mock) |
---|
269 | n/a | self.assertAfterWithGeneratorInvariantsNoError(foo) |
---|
270 | n/a | self.assertAfterWithManagerInvariantsNoError(mock) |
---|
271 | n/a | |
---|
272 | n/a | def testNestedSingleStatements(self): |
---|
273 | n/a | mock_a = mock_contextmanager_generator() |
---|
274 | n/a | with mock_a as foo: |
---|
275 | n/a | mock_b = mock_contextmanager_generator() |
---|
276 | n/a | with mock_b as bar: |
---|
277 | n/a | self.assertInWithManagerInvariants(mock_a) |
---|
278 | n/a | self.assertInWithManagerInvariants(mock_b) |
---|
279 | n/a | self.assertInWithGeneratorInvariants(foo) |
---|
280 | n/a | self.assertInWithGeneratorInvariants(bar) |
---|
281 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_b) |
---|
282 | n/a | self.assertAfterWithGeneratorInvariantsNoError(bar) |
---|
283 | n/a | self.assertInWithManagerInvariants(mock_a) |
---|
284 | n/a | self.assertInWithGeneratorInvariants(foo) |
---|
285 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_a) |
---|
286 | n/a | self.assertAfterWithGeneratorInvariantsNoError(foo) |
---|
287 | n/a | |
---|
288 | n/a | |
---|
289 | n/a | class NestedNonexceptionalTestCase(unittest.TestCase, |
---|
290 | n/a | ContextmanagerAssertionMixin): |
---|
291 | n/a | def testSingleArgInlineGeneratorSyntax(self): |
---|
292 | n/a | with Nested(mock_contextmanager_generator()): |
---|
293 | n/a | pass |
---|
294 | n/a | |
---|
295 | n/a | def testSingleArgBoundToNonTuple(self): |
---|
296 | n/a | m = mock_contextmanager_generator() |
---|
297 | n/a | # This will bind all the arguments to nested() into a single list |
---|
298 | n/a | # assigned to foo. |
---|
299 | n/a | with Nested(m) as foo: |
---|
300 | n/a | self.assertInWithManagerInvariants(m) |
---|
301 | n/a | self.assertAfterWithManagerInvariantsNoError(m) |
---|
302 | n/a | |
---|
303 | n/a | def testSingleArgBoundToSingleElementParenthesizedList(self): |
---|
304 | n/a | m = mock_contextmanager_generator() |
---|
305 | n/a | # This will bind all the arguments to nested() into a single list |
---|
306 | n/a | # assigned to foo. |
---|
307 | n/a | with Nested(m) as (foo): |
---|
308 | n/a | self.assertInWithManagerInvariants(m) |
---|
309 | n/a | self.assertAfterWithManagerInvariantsNoError(m) |
---|
310 | n/a | |
---|
311 | n/a | def testSingleArgBoundToMultipleElementTupleError(self): |
---|
312 | n/a | def shouldThrowValueError(): |
---|
313 | n/a | with Nested(mock_contextmanager_generator()) as (foo, bar): |
---|
314 | n/a | pass |
---|
315 | n/a | self.assertRaises(ValueError, shouldThrowValueError) |
---|
316 | n/a | |
---|
317 | n/a | def testSingleArgUnbound(self): |
---|
318 | n/a | mock_contextmanager = mock_contextmanager_generator() |
---|
319 | n/a | mock_nested = MockNested(mock_contextmanager) |
---|
320 | n/a | with mock_nested: |
---|
321 | n/a | self.assertInWithManagerInvariants(mock_contextmanager) |
---|
322 | n/a | self.assertInWithManagerInvariants(mock_nested) |
---|
323 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_contextmanager) |
---|
324 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
---|
325 | n/a | |
---|
326 | n/a | def testMultipleArgUnbound(self): |
---|
327 | n/a | m = mock_contextmanager_generator() |
---|
328 | n/a | n = mock_contextmanager_generator() |
---|
329 | n/a | o = mock_contextmanager_generator() |
---|
330 | n/a | mock_nested = MockNested(m, n, o) |
---|
331 | n/a | with mock_nested: |
---|
332 | n/a | self.assertInWithManagerInvariants(m) |
---|
333 | n/a | self.assertInWithManagerInvariants(n) |
---|
334 | n/a | self.assertInWithManagerInvariants(o) |
---|
335 | n/a | self.assertInWithManagerInvariants(mock_nested) |
---|
336 | n/a | self.assertAfterWithManagerInvariantsNoError(m) |
---|
337 | n/a | self.assertAfterWithManagerInvariantsNoError(n) |
---|
338 | n/a | self.assertAfterWithManagerInvariantsNoError(o) |
---|
339 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
---|
340 | n/a | |
---|
341 | n/a | def testMultipleArgBound(self): |
---|
342 | n/a | mock_nested = MockNested(mock_contextmanager_generator(), |
---|
343 | n/a | mock_contextmanager_generator(), mock_contextmanager_generator()) |
---|
344 | n/a | with mock_nested as (m, n, o): |
---|
345 | n/a | self.assertInWithGeneratorInvariants(m) |
---|
346 | n/a | self.assertInWithGeneratorInvariants(n) |
---|
347 | n/a | self.assertInWithGeneratorInvariants(o) |
---|
348 | n/a | self.assertInWithManagerInvariants(mock_nested) |
---|
349 | n/a | self.assertAfterWithGeneratorInvariantsNoError(m) |
---|
350 | n/a | self.assertAfterWithGeneratorInvariantsNoError(n) |
---|
351 | n/a | self.assertAfterWithGeneratorInvariantsNoError(o) |
---|
352 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
---|
353 | n/a | |
---|
354 | n/a | |
---|
355 | n/a | class ExceptionalTestCase(ContextmanagerAssertionMixin, unittest.TestCase): |
---|
356 | n/a | def testSingleResource(self): |
---|
357 | n/a | cm = mock_contextmanager_generator() |
---|
358 | n/a | def shouldThrow(): |
---|
359 | n/a | with cm as self.resource: |
---|
360 | n/a | self.assertInWithManagerInvariants(cm) |
---|
361 | n/a | self.assertInWithGeneratorInvariants(self.resource) |
---|
362 | n/a | self.raiseTestException() |
---|
363 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
364 | n/a | self.assertAfterWithManagerInvariantsWithError(cm) |
---|
365 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.resource) |
---|
366 | n/a | |
---|
367 | n/a | def testExceptionNormalized(self): |
---|
368 | n/a | cm = mock_contextmanager_generator() |
---|
369 | n/a | def shouldThrow(): |
---|
370 | n/a | with cm as self.resource: |
---|
371 | n/a | # Note this relies on the fact that 1 // 0 produces an exception |
---|
372 | n/a | # that is not normalized immediately. |
---|
373 | n/a | 1 // 0 |
---|
374 | n/a | self.assertRaises(ZeroDivisionError, shouldThrow) |
---|
375 | n/a | self.assertAfterWithManagerInvariantsWithError(cm, ZeroDivisionError) |
---|
376 | n/a | |
---|
377 | n/a | def testNestedSingleStatements(self): |
---|
378 | n/a | mock_a = mock_contextmanager_generator() |
---|
379 | n/a | mock_b = mock_contextmanager_generator() |
---|
380 | n/a | def shouldThrow(): |
---|
381 | n/a | with mock_a as self.foo: |
---|
382 | n/a | with mock_b as self.bar: |
---|
383 | n/a | self.assertInWithManagerInvariants(mock_a) |
---|
384 | n/a | self.assertInWithManagerInvariants(mock_b) |
---|
385 | n/a | self.assertInWithGeneratorInvariants(self.foo) |
---|
386 | n/a | self.assertInWithGeneratorInvariants(self.bar) |
---|
387 | n/a | self.raiseTestException() |
---|
388 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
389 | n/a | self.assertAfterWithManagerInvariantsWithError(mock_a) |
---|
390 | n/a | self.assertAfterWithManagerInvariantsWithError(mock_b) |
---|
391 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
---|
392 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.bar) |
---|
393 | n/a | |
---|
394 | n/a | def testMultipleResourcesInSingleStatement(self): |
---|
395 | n/a | cm_a = mock_contextmanager_generator() |
---|
396 | n/a | cm_b = mock_contextmanager_generator() |
---|
397 | n/a | mock_nested = MockNested(cm_a, cm_b) |
---|
398 | n/a | def shouldThrow(): |
---|
399 | n/a | with mock_nested as (self.resource_a, self.resource_b): |
---|
400 | n/a | self.assertInWithManagerInvariants(cm_a) |
---|
401 | n/a | self.assertInWithManagerInvariants(cm_b) |
---|
402 | n/a | self.assertInWithManagerInvariants(mock_nested) |
---|
403 | n/a | self.assertInWithGeneratorInvariants(self.resource_a) |
---|
404 | n/a | self.assertInWithGeneratorInvariants(self.resource_b) |
---|
405 | n/a | self.raiseTestException() |
---|
406 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
407 | n/a | self.assertAfterWithManagerInvariantsWithError(cm_a) |
---|
408 | n/a | self.assertAfterWithManagerInvariantsWithError(cm_b) |
---|
409 | n/a | self.assertAfterWithManagerInvariantsWithError(mock_nested) |
---|
410 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.resource_a) |
---|
411 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.resource_b) |
---|
412 | n/a | |
---|
413 | n/a | def testNestedExceptionBeforeInnerStatement(self): |
---|
414 | n/a | mock_a = mock_contextmanager_generator() |
---|
415 | n/a | mock_b = mock_contextmanager_generator() |
---|
416 | n/a | self.bar = None |
---|
417 | n/a | def shouldThrow(): |
---|
418 | n/a | with mock_a as self.foo: |
---|
419 | n/a | self.assertInWithManagerInvariants(mock_a) |
---|
420 | n/a | self.assertInWithGeneratorInvariants(self.foo) |
---|
421 | n/a | self.raiseTestException() |
---|
422 | n/a | with mock_b as self.bar: |
---|
423 | n/a | pass |
---|
424 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
425 | n/a | self.assertAfterWithManagerInvariantsWithError(mock_a) |
---|
426 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
---|
427 | n/a | |
---|
428 | n/a | # The inner statement stuff should never have been touched |
---|
429 | n/a | self.assertEqual(self.bar, None) |
---|
430 | n/a | self.assertFalse(mock_b.enter_called) |
---|
431 | n/a | self.assertFalse(mock_b.exit_called) |
---|
432 | n/a | self.assertEqual(mock_b.exit_args, None) |
---|
433 | n/a | |
---|
434 | n/a | def testNestedExceptionAfterInnerStatement(self): |
---|
435 | n/a | mock_a = mock_contextmanager_generator() |
---|
436 | n/a | mock_b = mock_contextmanager_generator() |
---|
437 | n/a | def shouldThrow(): |
---|
438 | n/a | with mock_a as self.foo: |
---|
439 | n/a | with mock_b as self.bar: |
---|
440 | n/a | self.assertInWithManagerInvariants(mock_a) |
---|
441 | n/a | self.assertInWithManagerInvariants(mock_b) |
---|
442 | n/a | self.assertInWithGeneratorInvariants(self.foo) |
---|
443 | n/a | self.assertInWithGeneratorInvariants(self.bar) |
---|
444 | n/a | self.raiseTestException() |
---|
445 | n/a | self.assertRaises(RuntimeError, shouldThrow) |
---|
446 | n/a | self.assertAfterWithManagerInvariantsWithError(mock_a) |
---|
447 | n/a | self.assertAfterWithManagerInvariantsNoError(mock_b) |
---|
448 | n/a | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
---|
449 | n/a | self.assertAfterWithGeneratorInvariantsNoError(self.bar) |
---|
450 | n/a | |
---|
451 | n/a | def testRaisedStopIteration1(self): |
---|
452 | n/a | # From bug 1462485 |
---|
453 | n/a | @contextmanager |
---|
454 | n/a | def cm(): |
---|
455 | n/a | yield |
---|
456 | n/a | |
---|
457 | n/a | def shouldThrow(): |
---|
458 | n/a | with cm(): |
---|
459 | n/a | raise StopIteration("from with") |
---|
460 | n/a | |
---|
461 | n/a | with self.assertWarnsRegex(DeprecationWarning, "StopIteration"): |
---|
462 | n/a | self.assertRaises(StopIteration, shouldThrow) |
---|
463 | n/a | |
---|
464 | n/a | def testRaisedStopIteration2(self): |
---|
465 | n/a | # From bug 1462485 |
---|
466 | n/a | class cm(object): |
---|
467 | n/a | def __enter__(self): |
---|
468 | n/a | pass |
---|
469 | n/a | def __exit__(self, type, value, traceback): |
---|
470 | n/a | pass |
---|
471 | n/a | |
---|
472 | n/a | def shouldThrow(): |
---|
473 | n/a | with cm(): |
---|
474 | n/a | raise StopIteration("from with") |
---|
475 | n/a | |
---|
476 | n/a | self.assertRaises(StopIteration, shouldThrow) |
---|
477 | n/a | |
---|
478 | n/a | def testRaisedStopIteration3(self): |
---|
479 | n/a | # Another variant where the exception hasn't been instantiated |
---|
480 | n/a | # From bug 1705170 |
---|
481 | n/a | @contextmanager |
---|
482 | n/a | def cm(): |
---|
483 | n/a | yield |
---|
484 | n/a | |
---|
485 | n/a | def shouldThrow(): |
---|
486 | n/a | with cm(): |
---|
487 | n/a | raise next(iter([])) |
---|
488 | n/a | |
---|
489 | n/a | with self.assertWarnsRegex(DeprecationWarning, "StopIteration"): |
---|
490 | n/a | self.assertRaises(StopIteration, shouldThrow) |
---|
491 | n/a | |
---|
492 | n/a | def testRaisedGeneratorExit1(self): |
---|
493 | n/a | # From bug 1462485 |
---|
494 | n/a | @contextmanager |
---|
495 | n/a | def cm(): |
---|
496 | n/a | yield |
---|
497 | n/a | |
---|
498 | n/a | def shouldThrow(): |
---|
499 | n/a | with cm(): |
---|
500 | n/a | raise GeneratorExit("from with") |
---|
501 | n/a | |
---|
502 | n/a | self.assertRaises(GeneratorExit, shouldThrow) |
---|
503 | n/a | |
---|
504 | n/a | def testRaisedGeneratorExit2(self): |
---|
505 | n/a | # From bug 1462485 |
---|
506 | n/a | class cm (object): |
---|
507 | n/a | def __enter__(self): |
---|
508 | n/a | pass |
---|
509 | n/a | def __exit__(self, type, value, traceback): |
---|
510 | n/a | pass |
---|
511 | n/a | |
---|
512 | n/a | def shouldThrow(): |
---|
513 | n/a | with cm(): |
---|
514 | n/a | raise GeneratorExit("from with") |
---|
515 | n/a | |
---|
516 | n/a | self.assertRaises(GeneratorExit, shouldThrow) |
---|
517 | n/a | |
---|
518 | n/a | def testErrorsInBool(self): |
---|
519 | n/a | # issue4589: __exit__ return code may raise an exception |
---|
520 | n/a | # when looking at its truth value. |
---|
521 | n/a | |
---|
522 | n/a | class cm(object): |
---|
523 | n/a | def __init__(self, bool_conversion): |
---|
524 | n/a | class Bool: |
---|
525 | n/a | def __bool__(self): |
---|
526 | n/a | return bool_conversion() |
---|
527 | n/a | self.exit_result = Bool() |
---|
528 | n/a | def __enter__(self): |
---|
529 | n/a | return 3 |
---|
530 | n/a | def __exit__(self, a, b, c): |
---|
531 | n/a | return self.exit_result |
---|
532 | n/a | |
---|
533 | n/a | def trueAsBool(): |
---|
534 | n/a | with cm(lambda: True): |
---|
535 | n/a | self.fail("Should NOT see this") |
---|
536 | n/a | trueAsBool() |
---|
537 | n/a | |
---|
538 | n/a | def falseAsBool(): |
---|
539 | n/a | with cm(lambda: False): |
---|
540 | n/a | self.fail("Should raise") |
---|
541 | n/a | self.assertRaises(AssertionError, falseAsBool) |
---|
542 | n/a | |
---|
543 | n/a | def failAsBool(): |
---|
544 | n/a | with cm(lambda: 1//0): |
---|
545 | n/a | self.fail("Should NOT see this") |
---|
546 | n/a | self.assertRaises(ZeroDivisionError, failAsBool) |
---|
547 | n/a | |
---|
548 | n/a | |
---|
549 | n/a | class NonLocalFlowControlTestCase(unittest.TestCase): |
---|
550 | n/a | |
---|
551 | n/a | def testWithBreak(self): |
---|
552 | n/a | counter = 0 |
---|
553 | n/a | while True: |
---|
554 | n/a | counter += 1 |
---|
555 | n/a | with mock_contextmanager_generator(): |
---|
556 | n/a | counter += 10 |
---|
557 | n/a | break |
---|
558 | n/a | counter += 100 # Not reached |
---|
559 | n/a | self.assertEqual(counter, 11) |
---|
560 | n/a | |
---|
561 | n/a | def testWithContinue(self): |
---|
562 | n/a | counter = 0 |
---|
563 | n/a | while True: |
---|
564 | n/a | counter += 1 |
---|
565 | n/a | if counter > 2: |
---|
566 | n/a | break |
---|
567 | n/a | with mock_contextmanager_generator(): |
---|
568 | n/a | counter += 10 |
---|
569 | n/a | continue |
---|
570 | n/a | counter += 100 # Not reached |
---|
571 | n/a | self.assertEqual(counter, 12) |
---|
572 | n/a | |
---|
573 | n/a | def testWithReturn(self): |
---|
574 | n/a | def foo(): |
---|
575 | n/a | counter = 0 |
---|
576 | n/a | while True: |
---|
577 | n/a | counter += 1 |
---|
578 | n/a | with mock_contextmanager_generator(): |
---|
579 | n/a | counter += 10 |
---|
580 | n/a | return counter |
---|
581 | n/a | counter += 100 # Not reached |
---|
582 | n/a | self.assertEqual(foo(), 11) |
---|
583 | n/a | |
---|
584 | n/a | def testWithYield(self): |
---|
585 | n/a | def gen(): |
---|
586 | n/a | with mock_contextmanager_generator(): |
---|
587 | n/a | yield 12 |
---|
588 | n/a | yield 13 |
---|
589 | n/a | x = list(gen()) |
---|
590 | n/a | self.assertEqual(x, [12, 13]) |
---|
591 | n/a | |
---|
592 | n/a | def testWithRaise(self): |
---|
593 | n/a | counter = 0 |
---|
594 | n/a | try: |
---|
595 | n/a | counter += 1 |
---|
596 | n/a | with mock_contextmanager_generator(): |
---|
597 | n/a | counter += 10 |
---|
598 | n/a | raise RuntimeError |
---|
599 | n/a | counter += 100 # Not reached |
---|
600 | n/a | except RuntimeError: |
---|
601 | n/a | self.assertEqual(counter, 11) |
---|
602 | n/a | else: |
---|
603 | n/a | self.fail("Didn't raise RuntimeError") |
---|
604 | n/a | |
---|
605 | n/a | |
---|
606 | n/a | class AssignmentTargetTestCase(unittest.TestCase): |
---|
607 | n/a | |
---|
608 | n/a | def testSingleComplexTarget(self): |
---|
609 | n/a | targets = {1: [0, 1, 2]} |
---|
610 | n/a | with mock_contextmanager_generator() as targets[1][0]: |
---|
611 | n/a | self.assertEqual(list(targets.keys()), [1]) |
---|
612 | n/a | self.assertEqual(targets[1][0].__class__, MockResource) |
---|
613 | n/a | with mock_contextmanager_generator() as list(targets.values())[0][1]: |
---|
614 | n/a | self.assertEqual(list(targets.keys()), [1]) |
---|
615 | n/a | self.assertEqual(targets[1][1].__class__, MockResource) |
---|
616 | n/a | with mock_contextmanager_generator() as targets[2]: |
---|
617 | n/a | keys = list(targets.keys()) |
---|
618 | n/a | keys.sort() |
---|
619 | n/a | self.assertEqual(keys, [1, 2]) |
---|
620 | n/a | class C: pass |
---|
621 | n/a | blah = C() |
---|
622 | n/a | with mock_contextmanager_generator() as blah.foo: |
---|
623 | n/a | self.assertEqual(hasattr(blah, "foo"), True) |
---|
624 | n/a | |
---|
625 | n/a | def testMultipleComplexTargets(self): |
---|
626 | n/a | class C: |
---|
627 | n/a | def __enter__(self): return 1, 2, 3 |
---|
628 | n/a | def __exit__(self, t, v, tb): pass |
---|
629 | n/a | targets = {1: [0, 1, 2]} |
---|
630 | n/a | with C() as (targets[1][0], targets[1][1], targets[1][2]): |
---|
631 | n/a | self.assertEqual(targets, {1: [1, 2, 3]}) |
---|
632 | n/a | with C() as (list(targets.values())[0][2], list(targets.values())[0][1], list(targets.values())[0][0]): |
---|
633 | n/a | self.assertEqual(targets, {1: [3, 2, 1]}) |
---|
634 | n/a | with C() as (targets[1], targets[2], targets[3]): |
---|
635 | n/a | self.assertEqual(targets, {1: 1, 2: 2, 3: 3}) |
---|
636 | n/a | class B: pass |
---|
637 | n/a | blah = B() |
---|
638 | n/a | with C() as (blah.one, blah.two, blah.three): |
---|
639 | n/a | self.assertEqual(blah.one, 1) |
---|
640 | n/a | self.assertEqual(blah.two, 2) |
---|
641 | n/a | self.assertEqual(blah.three, 3) |
---|
642 | n/a | |
---|
643 | n/a | |
---|
644 | n/a | class ExitSwallowsExceptionTestCase(unittest.TestCase): |
---|
645 | n/a | |
---|
646 | n/a | def testExitTrueSwallowsException(self): |
---|
647 | n/a | class AfricanSwallow: |
---|
648 | n/a | def __enter__(self): pass |
---|
649 | n/a | def __exit__(self, t, v, tb): return True |
---|
650 | n/a | try: |
---|
651 | n/a | with AfricanSwallow(): |
---|
652 | n/a | 1/0 |
---|
653 | n/a | except ZeroDivisionError: |
---|
654 | n/a | self.fail("ZeroDivisionError should have been swallowed") |
---|
655 | n/a | |
---|
656 | n/a | def testExitFalseDoesntSwallowException(self): |
---|
657 | n/a | class EuropeanSwallow: |
---|
658 | n/a | def __enter__(self): pass |
---|
659 | n/a | def __exit__(self, t, v, tb): return False |
---|
660 | n/a | try: |
---|
661 | n/a | with EuropeanSwallow(): |
---|
662 | n/a | 1/0 |
---|
663 | n/a | except ZeroDivisionError: |
---|
664 | n/a | pass |
---|
665 | n/a | else: |
---|
666 | n/a | self.fail("ZeroDivisionError should have been raised") |
---|
667 | n/a | |
---|
668 | n/a | |
---|
669 | n/a | class NestedWith(unittest.TestCase): |
---|
670 | n/a | |
---|
671 | n/a | class Dummy(object): |
---|
672 | n/a | def __init__(self, value=None, gobble=False): |
---|
673 | n/a | if value is None: |
---|
674 | n/a | value = self |
---|
675 | n/a | self.value = value |
---|
676 | n/a | self.gobble = gobble |
---|
677 | n/a | self.enter_called = False |
---|
678 | n/a | self.exit_called = False |
---|
679 | n/a | |
---|
680 | n/a | def __enter__(self): |
---|
681 | n/a | self.enter_called = True |
---|
682 | n/a | return self.value |
---|
683 | n/a | |
---|
684 | n/a | def __exit__(self, *exc_info): |
---|
685 | n/a | self.exit_called = True |
---|
686 | n/a | self.exc_info = exc_info |
---|
687 | n/a | if self.gobble: |
---|
688 | n/a | return True |
---|
689 | n/a | |
---|
690 | n/a | class InitRaises(object): |
---|
691 | n/a | def __init__(self): raise RuntimeError() |
---|
692 | n/a | |
---|
693 | n/a | class EnterRaises(object): |
---|
694 | n/a | def __enter__(self): raise RuntimeError() |
---|
695 | n/a | def __exit__(self, *exc_info): pass |
---|
696 | n/a | |
---|
697 | n/a | class ExitRaises(object): |
---|
698 | n/a | def __enter__(self): pass |
---|
699 | n/a | def __exit__(self, *exc_info): raise RuntimeError() |
---|
700 | n/a | |
---|
701 | n/a | def testNoExceptions(self): |
---|
702 | n/a | with self.Dummy() as a, self.Dummy() as b: |
---|
703 | n/a | self.assertTrue(a.enter_called) |
---|
704 | n/a | self.assertTrue(b.enter_called) |
---|
705 | n/a | self.assertTrue(a.exit_called) |
---|
706 | n/a | self.assertTrue(b.exit_called) |
---|
707 | n/a | |
---|
708 | n/a | def testExceptionInExprList(self): |
---|
709 | n/a | try: |
---|
710 | n/a | with self.Dummy() as a, self.InitRaises(): |
---|
711 | n/a | pass |
---|
712 | n/a | except: |
---|
713 | n/a | pass |
---|
714 | n/a | self.assertTrue(a.enter_called) |
---|
715 | n/a | self.assertTrue(a.exit_called) |
---|
716 | n/a | |
---|
717 | n/a | def testExceptionInEnter(self): |
---|
718 | n/a | try: |
---|
719 | n/a | with self.Dummy() as a, self.EnterRaises(): |
---|
720 | n/a | self.fail('body of bad with executed') |
---|
721 | n/a | except RuntimeError: |
---|
722 | n/a | pass |
---|
723 | n/a | else: |
---|
724 | n/a | self.fail('RuntimeError not reraised') |
---|
725 | n/a | self.assertTrue(a.enter_called) |
---|
726 | n/a | self.assertTrue(a.exit_called) |
---|
727 | n/a | |
---|
728 | n/a | def testExceptionInExit(self): |
---|
729 | n/a | body_executed = False |
---|
730 | n/a | with self.Dummy(gobble=True) as a, self.ExitRaises(): |
---|
731 | n/a | body_executed = True |
---|
732 | n/a | self.assertTrue(a.enter_called) |
---|
733 | n/a | self.assertTrue(a.exit_called) |
---|
734 | n/a | self.assertTrue(body_executed) |
---|
735 | n/a | self.assertNotEqual(a.exc_info[0], None) |
---|
736 | n/a | |
---|
737 | n/a | def testEnterReturnsTuple(self): |
---|
738 | n/a | with self.Dummy(value=(1,2)) as (a1, a2), \ |
---|
739 | n/a | self.Dummy(value=(10, 20)) as (b1, b2): |
---|
740 | n/a | self.assertEqual(1, a1) |
---|
741 | n/a | self.assertEqual(2, a2) |
---|
742 | n/a | self.assertEqual(10, b1) |
---|
743 | n/a | self.assertEqual(20, b2) |
---|
744 | n/a | |
---|
745 | n/a | if __name__ == '__main__': |
---|
746 | n/a | unittest.main() |
---|