Lines Matching refs:self
14 def __init__(self, *args):
16 self.enter_called = False
17 self.exit_called = False
18 self.exit_args = None
20 def __enter__(self):
21 self.enter_called = True
22 return _GeneratorContextManager.__enter__(self)
24 def __exit__(self, type, value, traceback):
25 self.exit_called = True
26 self.exit_args = (type, value, traceback)
27 return _GeneratorContextManager.__exit__(self, type,
38 def __init__(self):
39 self.yielded = False
40 self.stopped = False
55 def __init__(self, *managers):
56 self.managers = managers
57 self.entered = None
59 def __enter__(self):
60 if self.entered is not None:
62 self.entered = deque()
65 for mgr in self.managers:
67 self.entered.appendleft(mgr)
69 if not self.__exit__(*sys.exc_info()):
73 def __exit__(self, *exc_info):
78 for mgr in self.entered:
84 self.entered = None
90 def __init__(self, *managers):
91 Nested.__init__(self, *managers)
92 self.enter_called = False
93 self.exit_called = False
94 self.exit_args = None
96 def __enter__(self):
97 self.enter_called = True
98 return Nested.__enter__(self)
100 def __exit__(self, *exc_info):
101 self.exit_called = True
102 self.exit_args = exc_info
103 return Nested.__exit__(self, *exc_info)
107 def testNameError(self):
110 self.assertRaises(NameError, fooNotDeclared)
112 def testEnterAttributeError1(self):
114 def __exit__(self, type, value, traceback):
120 self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnter)
122 def testEnterAttributeError2(self):
129 self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnterAndExit)
131 def testExitAttributeError(self):
133 def __enter__(self):
139 self.assertRaisesRegex(TypeError, 'the context manager.*__exit__', fooLacksExit)
141 def assertRaisesSyntaxError(self, codestr):
144 self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)
146 def testAssignmentToNoneError(self):
147 self.assertRaisesSyntaxError('with mock as None:\n pass')
148 self.assertRaisesSyntaxError(
152 def testAssignmentToTupleOnlyContainingNoneError(self):
153 self.assertRaisesSyntaxError('with mock as None,:\n pass')
154 self.assertRaisesSyntaxError(
158 def testAssignmentToTupleContainingNoneError(self):
159 self.assertRaisesSyntaxError(
163 def testEnterThrows(self):
165 def __enter__(self):
167 def __exit__(self, *args):
172 self.foo = None
173 with ct as self.foo:
175 self.assertRaises(RuntimeError, shouldThrow)
176 self.assertEqual(self.foo, None)
178 def testExitThrows(self):
180 def __enter__(self):
182 def __exit__(self, *args):
187 self.assertRaises(RuntimeError, shouldThrow)
191 def setUp(self):
192 self.TEST_EXCEPTION = RuntimeError("test exception")
194 def assertInWithManagerInvariants(self, mock_manager):
195 self.assertTrue(mock_manager.enter_called)
196 self.assertFalse(mock_manager.exit_called)
197 self.assertEqual(mock_manager.exit_args, None)
199 def assertAfterWithManagerInvariants(self, mock_manager, exit_args):
200 self.assertTrue(mock_manager.enter_called)
201 self.assertTrue(mock_manager.exit_called)
202 self.assertEqual(mock_manager.exit_args, exit_args)
204 def assertAfterWithManagerInvariantsNoError(self, mock_manager):
205 self.assertAfterWithManagerInvariants(mock_manager,
208 def assertInWithGeneratorInvariants(self, mock_generator):
209 self.assertTrue(mock_generator.yielded)
210 self.assertFalse(mock_generator.stopped)
212 def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):
213 self.assertTrue(mock_generator.yielded)
214 self.assertTrue(mock_generator.stopped)
216 def raiseTestException(self):
217 raise self.TEST_EXCEPTION
219 def assertAfterWithManagerInvariantsWithError(self, mock_manager,
221 self.assertTrue(mock_manager.enter_called)
222 self.assertTrue(mock_manager.exit_called)
224 self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
225 exc_type = type(self.TEST_EXCEPTION)
226 self.assertEqual(mock_manager.exit_args[0], exc_type)
228 self.assertIsInstance(mock_manager.exit_args[1], exc_type)
229 self.assertIsNot(mock_manager.exit_args[2], None)
231 def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
232 self.assertTrue(mock_generator.yielded)
233 self.assertTrue(mock_generator.stopped)
237 def testInlineGeneratorSyntax(self):
241 def testUnboundGenerator(self):
245 self.assertAfterWithManagerInvariantsNoError(mock)
247 def testInlineGeneratorBoundSyntax(self):
249 self.assertInWithGeneratorInvariants(foo)
251 self.assertAfterWithGeneratorInvariantsNoError(foo)
253 def testInlineGeneratorBoundToExistingVariable(self):
256 self.assertInWithGeneratorInvariants(foo)
257 self.assertAfterWithGeneratorInvariantsNoError(foo)
259 def testInlineGeneratorBoundToDottedVariable(self):
260 with mock_contextmanager_generator() as self.foo:
261 self.assertInWithGeneratorInvariants(self.foo)
262 self.assertAfterWithGeneratorInvariantsNoError(self.foo)
264 def testBoundGenerator(self):
267 self.assertInWithGeneratorInvariants(foo)
268 self.assertInWithManagerInvariants(mock)
269 self.assertAfterWithGeneratorInvariantsNoError(foo)
270 self.assertAfterWithManagerInvariantsNoError(mock)
272 def testNestedSingleStatements(self):
277 self.assertInWithManagerInvariants(mock_a)
278 self.assertInWithManagerInvariants(mock_b)
279 self.assertInWithGeneratorInvariants(foo)
280 self.assertInWithGeneratorInvariants(bar)
281 self.assertAfterWithManagerInvariantsNoError(mock_b)
282 self.assertAfterWithGeneratorInvariantsNoError(bar)
283 self.assertInWithManagerInvariants(mock_a)
284 self.assertInWithGeneratorInvariants(foo)
285 self.assertAfterWithManagerInvariantsNoError(mock_a)
286 self.assertAfterWithGeneratorInvariantsNoError(foo)
291 def testSingleArgInlineGeneratorSyntax(self):
295 def testSingleArgBoundToNonTuple(self):
300 self.assertInWithManagerInvariants(m)
301 self.assertAfterWithManagerInvariantsNoError(m)
303 def testSingleArgBoundToSingleElementParenthesizedList(self):
308 self.assertInWithManagerInvariants(m)
309 self.assertAfterWithManagerInvariantsNoError(m)
311 def testSingleArgBoundToMultipleElementTupleError(self):
315 self.assertRaises(ValueError, shouldThrowValueError)
317 def testSingleArgUnbound(self):
321 self.assertInWithManagerInvariants(mock_contextmanager)
322 self.assertInWithManagerInvariants(mock_nested)
323 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
324 self.assertAfterWithManagerInvariantsNoError(mock_nested)
326 def testMultipleArgUnbound(self):
332 self.assertInWithManagerInvariants(m)
333 self.assertInWithManagerInvariants(n)
334 self.assertInWithManagerInvariants(o)
335 self.assertInWithManagerInvariants(mock_nested)
336 self.assertAfterWithManagerInvariantsNoError(m)
337 self.assertAfterWithManagerInvariantsNoError(n)
338 self.assertAfterWithManagerInvariantsNoError(o)
339 self.assertAfterWithManagerInvariantsNoError(mock_nested)
341 def testMultipleArgBound(self):
345 self.assertInWithGeneratorInvariants(m)
346 self.assertInWithGeneratorInvariants(n)
347 self.assertInWithGeneratorInvariants(o)
348 self.assertInWithManagerInvariants(mock_nested)
349 self.assertAfterWithGeneratorInvariantsNoError(m)
350 self.assertAfterWithGeneratorInvariantsNoError(n)
351 self.assertAfterWithGeneratorInvariantsNoError(o)
352 self.assertAfterWithManagerInvariantsNoError(mock_nested)
356 def testSingleResource(self):
359 with cm as self.resource:
360 self.assertInWithManagerInvariants(cm)
361 self.assertInWithGeneratorInvariants(self.resource)
362 self.raiseTestException()
363 self.assertRaises(RuntimeError, shouldThrow)
364 self.assertAfterWithManagerInvariantsWithError(cm)
365 self.assertAfterWithGeneratorInvariantsWithError(self.resource)
367 def testExceptionNormalized(self):
370 with cm as self.resource:
374 self.assertRaises(ZeroDivisionError, shouldThrow)
375 self.assertAfterWithManagerInvariantsWithError(cm, ZeroDivisionError)
377 def testNestedSingleStatements(self):
381 with mock_a as self.foo:
382 with mock_b as self.bar:
383 self.assertInWithManagerInvariants(mock_a)
384 self.assertInWithManagerInvariants(mock_b)
385 self.assertInWithGeneratorInvariants(self.foo)
386 self.assertInWithGeneratorInvariants(self.bar)
387 self.raiseTestException()
388 self.assertRaises(RuntimeError, shouldThrow)
389 self.assertAfterWithManagerInvariantsWithError(mock_a)
390 self.assertAfterWithManagerInvariantsWithError(mock_b)
391 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
392 self.assertAfterWithGeneratorInvariantsWithError(self.bar)
394 def testMultipleResourcesInSingleStatement(self):
399 with mock_nested as (self.resource_a, self.resource_b):
400 self.assertInWithManagerInvariants(cm_a)
401 self.assertInWithManagerInvariants(cm_b)
402 self.assertInWithManagerInvariants(mock_nested)
403 self.assertInWithGeneratorInvariants(self.resource_a)
404 self.assertInWithGeneratorInvariants(self.resource_b)
405 self.raiseTestException()
406 self.assertRaises(RuntimeError, shouldThrow)
407 self.assertAfterWithManagerInvariantsWithError(cm_a)
408 self.assertAfterWithManagerInvariantsWithError(cm_b)
409 self.assertAfterWithManagerInvariantsWithError(mock_nested)
410 self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)
411 self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)
413 def testNestedExceptionBeforeInnerStatement(self):
416 self.bar = None
418 with mock_a as self.foo:
419 self.assertInWithManagerInvariants(mock_a)
420 self.assertInWithGeneratorInvariants(self.foo)
421 self.raiseTestException()
422 with mock_b as self.bar:
424 self.assertRaises(RuntimeError, shouldThrow)
425 self.assertAfterWithManagerInvariantsWithError(mock_a)
426 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
429 self.assertEqual(self.bar, None)
430 self.assertFalse(mock_b.enter_called)
431 self.assertFalse(mock_b.exit_called)
432 self.assertEqual(mock_b.exit_args, None)
434 def testNestedExceptionAfterInnerStatement(self):
438 with mock_a as self.foo:
439 with mock_b as self.bar:
440 self.assertInWithManagerInvariants(mock_a)
441 self.assertInWithManagerInvariants(mock_b)
442 self.assertInWithGeneratorInvariants(self.foo)
443 self.assertInWithGeneratorInvariants(self.bar)
444 self.raiseTestException()
445 self.assertRaises(RuntimeError, shouldThrow)
446 self.assertAfterWithManagerInvariantsWithError(mock_a)
447 self.assertAfterWithManagerInvariantsNoError(mock_b)
448 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
449 self.assertAfterWithGeneratorInvariantsNoError(self.bar)
451 def testRaisedStopIteration1(self):
461 with self.assertRaisesRegex(StopIteration, 'from with'):
464 def testRaisedStopIteration2(self):
467 def __enter__(self):
469 def __exit__(self, type, value, traceback):
476 with self.assertRaisesRegex(StopIteration, 'from with'):
479 def testRaisedStopIteration3(self):
490 with self.assertRaises(StopIteration):
493 def testRaisedGeneratorExit1(self):
503 self.assertRaises(GeneratorExit, shouldThrow)
505 def testRaisedGeneratorExit2(self):
508 def __enter__(self):
510 def __exit__(self, type, value, traceback):
517 self.assertRaises(GeneratorExit, shouldThrow)
519 def testErrorsInBool(self):
524 def __init__(self, bool_conversion):
526 def __bool__(self):
528 self.exit_result = Bool()
529 def __enter__(self):
531 def __exit__(self, a, b, c):
532 return self.exit_result
536 self.fail("Should NOT see this")
541 self.fail("Should raise")
542 self.assertRaises(AssertionError, falseAsBool)
546 self.fail("Should NOT see this")
547 self.assertRaises(ZeroDivisionError, failAsBool)
552 def testWithBreak(self):
560 self.assertEqual(counter, 11)
562 def testWithContinue(self):
572 self.assertEqual(counter, 12)
574 def testWithReturn(self):
583 self.assertEqual(foo(), 11)
585 def testWithYield(self):
591 self.assertEqual(x, [12, 13])
593 def testWithRaise(self):
602 self.assertEqual(counter, 11)
604 self.fail("Didn't raise RuntimeError")
609 def testSingleComplexTarget(self):
612 self.assertEqual(list(targets.keys()), [1])
613 self.assertEqual(targets[1][0].__class__, MockResource)
615 self.assertEqual(list(targets.keys()), [1])
616 self.assertEqual(targets[1][1].__class__, MockResource)
620 self.assertEqual(keys, [1, 2])
624 self.assertEqual(hasattr(blah, "foo"), True)
626 def testMultipleComplexTargets(self):
628 def __enter__(self): return 1, 2, 3
629 def __exit__(self, t, v, tb): pass
632 self.assertEqual(targets, {1: [1, 2, 3]})
634 self.assertEqual(targets, {1: [3, 2, 1]})
636 self.assertEqual(targets, {1: 1, 2: 2, 3: 3})
640 self.assertEqual(blah.one, 1)
641 self.assertEqual(blah.two, 2)
642 self.assertEqual(blah.three, 3)
644 def testWithExtendedTargets(self):
646 self.assertEqual(a, 1)
647 self.assertEqual(b, [2, 3])
648 self.assertEqual(c, 4)
653 def testExitTrueSwallowsException(self):
655 def __enter__(self): pass
656 def __exit__(self, t, v, tb): return True
661 self.fail("ZeroDivisionError should have been swallowed")
663 def testExitFalseDoesntSwallowException(self):
665 def __enter__(self): pass
666 def __exit__(self, t, v, tb): return False
673 self.fail("ZeroDivisionError should have been raised")
679 def __init__(self, value=None, gobble=False):
681 value = self
682 self.value = value
683 self.gobble = gobble
684 self.enter_called = False
685 self.exit_called = False
687 def __enter__(self):
688 self.enter_called = True
689 return self.value
691 def __exit__(self, *exc_info):
692 self.exit_called = True
693 self.exc_info = exc_info
694 if self.gobble:
698 def __init__(self): raise RuntimeError()
701 def __enter__(self): raise RuntimeError()
702 def __exit__(self, *exc_info): pass
705 def __enter__(self): pass
706 def __exit__(self, *exc_info): raise RuntimeError()
708 def testNoExceptions(self):
709 with self.Dummy() as a, self.Dummy() as b:
710 self.assertTrue(a.enter_called)
711 self.assertTrue(b.enter_called)
712 self.assertTrue(a.exit_called)
713 self.assertTrue(b.exit_called)
715 def testExceptionInExprList(self):
717 with self.Dummy() as a, self.InitRaises():
721 self.assertTrue(a.enter_called)
722 self.assertTrue(a.exit_called)
724 def testExceptionInEnter(self):
726 with self.Dummy() as a, self.EnterRaises():
727 self.fail('body of bad with executed')
731 self.fail('RuntimeError not reraised')
732 self.assertTrue(a.enter_called)
733 self.assertTrue(a.exit_called)
735 def testExceptionInExit(self):
737 with self.Dummy(gobble=True) as a, self.ExitRaises():
739 self.assertTrue(a.enter_called)
740 self.assertTrue(a.exit_called)
741 self.assertTrue(body_executed)
742 self.assertNotEqual(a.exc_info[0], None)
744 def testEnterReturnsTuple(self):
745 with self.Dummy(value=(1,2)) as (a1, a2), \
746 self.Dummy(value=(10, 20)) as (b1, b2):
747 self.assertEqual(1, a1)
748 self.assertEqual(2, a2)
749 self.assertEqual(10, b1)
750 self.assertEqual(20, b2)