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