| 1 |
import functools |
| 2 |
from contextlib import ( |
| 3 |
asynccontextmanager, AbstractAsyncContextManager, |
| 4 |
AsyncExitStack, nullcontext, aclosing, contextmanager) |
| 5 |
from test import support |
| 6 |
from test.support import run_no_yield_async_fn as _run_async_fn |
| 7 |
import unittest |
| 8 |
import traceback |
| 9 |
|
| 10 |
from test.test_contextlib import TestBaseExitStack |
| 11 |
|
| 12 |
|
| 13 |
def _async_test(async_fn): |
| 14 |
"""Decorator to turn an async function into a synchronous function""" |
| 15 |
@functools.wraps(async_fn) |
| 16 |
def wrapper(*args, **kwargs): |
| 17 |
return _run_async_fn(async_fn, *args, **kwargs) |
| 18 |
|
| 19 |
return wrapper |
| 20 |
|
| 21 |
|
| 22 |
class TestAbstractAsyncContextManager(unittest.TestCase): |
| 23 |
|
| 24 |
@_async_test |
| 25 |
async def test_enter(self): |
| 26 |
class DefaultEnter(AbstractAsyncContextManager): |
| 27 |
async def __aexit__(self, *args): |
| 28 |
await super().__aexit__(*args) |
| 29 |
|
| 30 |
manager = DefaultEnter() |
| 31 |
self.assertIs(await manager.__aenter__(), manager) |
| 32 |
|
| 33 |
async with manager as context: |
| 34 |
self.assertIs(manager, context) |
| 35 |
|
| 36 |
@_async_test |
| 37 |
async def test_slots(self): |
| 38 |
class DefaultAsyncContextManager(AbstractAsyncContextManager): |
| 39 |
__slots__ = () |
| 40 |
|
| 41 |
async def __aexit__(self, *args): |
| 42 |
await super().__aexit__(*args) |
| 43 |
|
| 44 |
with self.assertRaises(AttributeError): |
| 45 |
manager = DefaultAsyncContextManager() |
| 46 |
manager.var = 42 |
| 47 |
|
| 48 |
@_async_test |
| 49 |
async def test_async_gen_propagates_generator_exit(self): |
| 50 |
# A regression test for https://bugs.python.org/issue33786. |
| 51 |
|
| 52 |
@asynccontextmanager |
| 53 |
async def ctx(): |
| 54 |
yield |
| 55 |
|
| 56 |
async def gen(): |
| 57 |
async with ctx(): |
| 58 |
yield 11 |
| 59 |
|
| 60 |
g = gen() |
| 61 |
async for val in g: |
| 62 |
self.assertEqual(val, 11) |
| 63 |
break |
| 64 |
await g.aclose() |
| 65 |
|
| 66 |
def test_exit_is_abstract(self): |
| 67 |
class MissingAexit(AbstractAsyncContextManager): |
| 68 |
pass |
| 69 |
|
| 70 |
with self.assertRaises(TypeError): |
| 71 |
MissingAexit() |
| 72 |
|
| 73 |
def test_structural_subclassing(self): |
| 74 |
class ManagerFromScratch: |
| 75 |
async def __aenter__(self): |
| 76 |
return self |
| 77 |
async def __aexit__(self, exc_type, exc_value, traceback): |
| 78 |
return None |
| 79 |
|
| 80 |
self.assertIsSubclass(ManagerFromScratch, AbstractAsyncContextManager) |
| 81 |
|
| 82 |
class DefaultEnter(AbstractAsyncContextManager): |
| 83 |
async def __aexit__(self, *args): |
| 84 |
await super().__aexit__(*args) |
| 85 |
|
| 86 |
self.assertIsSubclass(DefaultEnter, AbstractAsyncContextManager) |
| 87 |
|
| 88 |
class NoneAenter(ManagerFromScratch): |
| 89 |
__aenter__ = None |
| 90 |
|
| 91 |
self.assertNotIsSubclass(NoneAenter, AbstractAsyncContextManager) |
| 92 |
|
| 93 |
class NoneAexit(ManagerFromScratch): |
| 94 |
__aexit__ = None |
| 95 |
|
| 96 |
self.assertNotIsSubclass(NoneAexit, AbstractAsyncContextManager) |
| 97 |
|
| 98 |
|
| 99 |
class AsyncContextManagerTestCase(unittest.TestCase): |
| 100 |
|
| 101 |
@_async_test |
| 102 |
async def test_contextmanager_plain(self): |
| 103 |
state = [] |
| 104 |
@asynccontextmanager |
| 105 |
async def woohoo(): |
| 106 |
state.append(1) |
| 107 |
yield 42 |
| 108 |
state.append(999) |
| 109 |
async with woohoo() as x: |
| 110 |
self.assertEqual(state, [1]) |
| 111 |
self.assertEqual(x, 42) |
| 112 |
state.append(x) |
| 113 |
self.assertEqual(state, [1, 42, 999]) |
| 114 |
|
| 115 |
@_async_test |
| 116 |
async def test_contextmanager_finally(self): |
| 117 |
state = [] |
| 118 |
@asynccontextmanager |
| 119 |
async def woohoo(): |
| 120 |
state.append(1) |
| 121 |
try: |
| 122 |
yield 42 |
| 123 |
finally: |
| 124 |
state.append(999) |
| 125 |
with self.assertRaises(ZeroDivisionError): |
| 126 |
async with woohoo() as x: |
| 127 |
self.assertEqual(state, [1]) |
| 128 |
self.assertEqual(x, 42) |
| 129 |
state.append(x) |
| 130 |
raise ZeroDivisionError() |
| 131 |
self.assertEqual(state, [1, 42, 999]) |
| 132 |
|
| 133 |
@_async_test |
| 134 |
async def test_contextmanager_traceback(self): |
| 135 |
@asynccontextmanager |
| 136 |
async def f(): |
| 137 |
yield |
| 138 |
|
| 139 |
try: |
| 140 |
async with f(): |
| 141 |
1/0 |
| 142 |
except ZeroDivisionError as e: |
| 143 |
frames = traceback.extract_tb(e.__traceback__) |
| 144 |
|
| 145 |
self.assertEqual(len(frames), 1) |
| 146 |
self.assertEqual(frames[0].name, 'test_contextmanager_traceback') |
| 147 |
self.assertEqual(frames[0].line, '1/0') |
| 148 |
|
| 149 |
# Repeat with RuntimeError (which goes through a different code path) |
| 150 |
class RuntimeErrorSubclass(RuntimeError): |
| 151 |
pass |
| 152 |
|
| 153 |
try: |
| 154 |
async with f(): |
| 155 |
raise RuntimeErrorSubclass(42) |
| 156 |
except RuntimeErrorSubclass as e: |
| 157 |
frames = traceback.extract_tb(e.__traceback__) |
| 158 |
|
| 159 |
self.assertEqual(len(frames), 1) |
| 160 |
self.assertEqual(frames[0].name, 'test_contextmanager_traceback') |
| 161 |
self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)') |
| 162 |
|
| 163 |
class StopIterationSubclass(StopIteration): |
| 164 |
pass |
| 165 |
|
| 166 |
class StopAsyncIterationSubclass(StopAsyncIteration): |
| 167 |
pass |
| 168 |
|
| 169 |
for stop_exc in ( |
| 170 |
StopIteration('spam'), |
| 171 |
StopAsyncIteration('ham'), |
| 172 |
StopIterationSubclass('spam'), |
| 173 |
StopAsyncIterationSubclass('spam') |
| 174 |
): |
| 175 |
with self.subTest(type=type(stop_exc)): |
| 176 |
try: |
| 177 |
async with f(): |
| 178 |
raise stop_exc |
| 179 |
except type(stop_exc) as e: |
| 180 |
self.assertIs(e, stop_exc) |
| 181 |
frames = traceback.extract_tb(e.__traceback__) |
| 182 |
else: |
| 183 |
self.fail(f'{stop_exc} was suppressed') |
| 184 |
|
| 185 |
self.assertEqual(len(frames), 1) |
| 186 |
self.assertEqual(frames[0].name, 'test_contextmanager_traceback') |
| 187 |
self.assertEqual(frames[0].line, 'raise stop_exc') |
| 188 |
|
| 189 |
@_async_test |
| 190 |
async def test_contextmanager_no_reraise(self): |
| 191 |
@asynccontextmanager |
| 192 |
async def whee(): |
| 193 |
yield |
| 194 |
ctx = whee() |
| 195 |
await ctx.__aenter__() |
| 196 |
# Calling __aexit__ should not result in an exception |
| 197 |
self.assertFalse(await ctx.__aexit__(TypeError, TypeError("foo"), None)) |
| 198 |
|
| 199 |
@_async_test |
| 200 |
async def test_contextmanager_trap_yield_after_throw(self): |
| 201 |
@asynccontextmanager |
| 202 |
async def whoo(): |
| 203 |
try: |
| 204 |
yield |
| 205 |
except: |
| 206 |
yield |
| 207 |
ctx = whoo() |
| 208 |
await ctx.__aenter__() |
| 209 |
with self.assertRaises(RuntimeError): |
| 210 |
await ctx.__aexit__(TypeError, TypeError('foo'), None) |
| 211 |
if support.check_impl_detail(cpython=True): |
| 212 |
# The "gen" attribute is an implementation detail. |
| 213 |
self.assertFalse(ctx.gen.ag_suspended) |
| 214 |
|
| 215 |
@_async_test |
| 216 |
async def test_contextmanager_trap_no_yield(self): |
| 217 |
@asynccontextmanager |
| 218 |
async def whoo(): |
| 219 |
if False: |
| 220 |
yield |
| 221 |
ctx = whoo() |
| 222 |
with self.assertRaises(RuntimeError): |
| 223 |
await ctx.__aenter__() |
| 224 |
|
| 225 |
@_async_test |
| 226 |
async def test_contextmanager_trap_second_yield(self): |
| 227 |
@asynccontextmanager |
| 228 |
async def whoo(): |
| 229 |
yield |
| 230 |
yield |
| 231 |
ctx = whoo() |
| 232 |
await ctx.__aenter__() |
| 233 |
with self.assertRaises(RuntimeError): |
| 234 |
await ctx.__aexit__(None, None, None) |
| 235 |
if support.check_impl_detail(cpython=True): |
| 236 |
# The "gen" attribute is an implementation detail. |
| 237 |
self.assertFalse(ctx.gen.ag_suspended) |
| 238 |
|
| 239 |
@_async_test |
| 240 |
async def test_contextmanager_non_normalised(self): |
| 241 |
@asynccontextmanager |
| 242 |
async def whoo(): |
| 243 |
try: |
| 244 |
yield |
| 245 |
except RuntimeError: |
| 246 |
raise SyntaxError |
| 247 |
|
| 248 |
ctx = whoo() |
| 249 |
await ctx.__aenter__() |
| 250 |
with self.assertRaises(SyntaxError): |
| 251 |
await ctx.__aexit__(RuntimeError, None, None) |
| 252 |
|
| 253 |
@_async_test |
| 254 |
async def test_contextmanager_except(self): |
| 255 |
state = [] |
| 256 |
@asynccontextmanager |
| 257 |
async def woohoo(): |
| 258 |
state.append(1) |
| 259 |
try: |
| 260 |
yield 42 |
| 261 |
except ZeroDivisionError as e: |
| 262 |
state.append(e.args[0]) |
| 263 |
self.assertEqual(state, [1, 42, 999]) |
| 264 |
async with woohoo() as x: |
| 265 |
self.assertEqual(state, [1]) |
| 266 |
self.assertEqual(x, 42) |
| 267 |
state.append(x) |
| 268 |
raise ZeroDivisionError(999) |
| 269 |
self.assertEqual(state, [1, 42, 999]) |
| 270 |
|
| 271 |
@_async_test |
| 272 |
async def test_contextmanager_except_stopiter(self): |
| 273 |
@asynccontextmanager |
| 274 |
async def woohoo(): |
| 275 |
yield |
| 276 |
|
| 277 |
class StopIterationSubclass(StopIteration): |
| 278 |
pass |
| 279 |
|
| 280 |
class StopAsyncIterationSubclass(StopAsyncIteration): |
| 281 |
pass |
| 282 |
|
| 283 |
for stop_exc in ( |
| 284 |
StopIteration('spam'), |
| 285 |
StopAsyncIteration('ham'), |
| 286 |
StopIterationSubclass('spam'), |
| 287 |
StopAsyncIterationSubclass('spam') |
| 288 |
): |
| 289 |
with self.subTest(type=type(stop_exc)): |
| 290 |
try: |
| 291 |
async with woohoo(): |
| 292 |
raise stop_exc |
| 293 |
except Exception as ex: |
| 294 |
self.assertIs(ex, stop_exc) |
| 295 |
else: |
| 296 |
self.fail(f'{stop_exc} was suppressed') |
| 297 |
|
| 298 |
@_async_test |
| 299 |
async def test_contextmanager_wrap_runtimeerror(self): |
| 300 |
@asynccontextmanager |
| 301 |
async def woohoo(): |
| 302 |
try: |
| 303 |
yield |
| 304 |
except Exception as exc: |
| 305 |
raise RuntimeError(f'caught {exc}') from exc |
| 306 |
|
| 307 |
with self.assertRaises(RuntimeError): |
| 308 |
async with woohoo(): |
| 309 |
1 / 0 |
| 310 |
|
| 311 |
# If the context manager wrapped StopAsyncIteration in a RuntimeError, |
| 312 |
# we also unwrap it, because we can't tell whether the wrapping was |
| 313 |
# done by the generator machinery or by the generator itself. |
| 314 |
with self.assertRaises(StopAsyncIteration): |
| 315 |
async with woohoo(): |
| 316 |
raise StopAsyncIteration |
| 317 |
|
| 318 |
def _create_contextmanager_attribs(self): |
| 319 |
def attribs(**kw): |
| 320 |
def decorate(func): |
| 321 |
for k,v in kw.items(): |
| 322 |
setattr(func,k,v) |
| 323 |
return func |
| 324 |
return decorate |
| 325 |
@asynccontextmanager |
| 326 |
@attribs(foo='bar') |
| 327 |
async def baz(spam): |
| 328 |
"""Whee!""" |
| 329 |
yield |
| 330 |
return baz |
| 331 |
|
| 332 |
def test_contextmanager_attribs(self): |
| 333 |
baz = self._create_contextmanager_attribs() |
| 334 |
self.assertEqual(baz.__name__,'baz') |
| 335 |
self.assertEqual(baz.foo, 'bar') |
| 336 |
|
| 337 |
@support.requires_docstrings |
| 338 |
def test_contextmanager_doc_attrib(self): |
| 339 |
baz = self._create_contextmanager_attribs() |
| 340 |
self.assertEqual(baz.__doc__, "Whee!") |
| 341 |
|
| 342 |
@support.requires_docstrings |
| 343 |
@_async_test |
| 344 |
async def test_instance_docstring_given_cm_docstring(self): |
| 345 |
baz = self._create_contextmanager_attribs()(None) |
| 346 |
self.assertEqual(baz.__doc__, "Whee!") |
| 347 |
async with baz: |
| 348 |
pass # suppress warning |
| 349 |
|
| 350 |
@_async_test |
| 351 |
async def test_keywords(self): |
| 352 |
# Ensure no keyword arguments are inhibited |
| 353 |
@asynccontextmanager |
| 354 |
async def woohoo(self, func, args, kwds): |
| 355 |
yield (self, func, args, kwds) |
| 356 |
async with woohoo(self=11, func=22, args=33, kwds=44) as target: |
| 357 |
self.assertEqual(target, (11, 22, 33, 44)) |
| 358 |
|
| 359 |
@_async_test |
| 360 |
async def test_recursive(self): |
| 361 |
depth = 0 |
| 362 |
ncols = 0 |
| 363 |
|
| 364 |
@asynccontextmanager |
| 365 |
async def woohoo(): |
| 366 |
nonlocal ncols |
| 367 |
ncols += 1 |
| 368 |
|
| 369 |
nonlocal depth |
| 370 |
before = depth |
| 371 |
depth += 1 |
| 372 |
yield |
| 373 |
depth -= 1 |
| 374 |
self.assertEqual(depth, before) |
| 375 |
|
| 376 |
@woohoo() |
| 377 |
async def recursive(): |
| 378 |
if depth < 10: |
| 379 |
await recursive() |
| 380 |
|
| 381 |
await recursive() |
| 382 |
|
| 383 |
self.assertEqual(ncols, 10) |
| 384 |
self.assertEqual(depth, 0) |
| 385 |
|
| 386 |
@_async_test |
| 387 |
async def test_decorator(self): |
| 388 |
entered = False |
| 389 |
|
| 390 |
@asynccontextmanager |
| 391 |
async def context(): |
| 392 |
nonlocal entered |
| 393 |
entered = True |
| 394 |
yield |
| 395 |
entered = False |
| 396 |
|
| 397 |
@context() |
| 398 |
async def test(): |
| 399 |
self.assertTrue(entered) |
| 400 |
|
| 401 |
self.assertFalse(entered) |
| 402 |
await test() |
| 403 |
self.assertFalse(entered) |
| 404 |
|
| 405 |
@_async_test |
| 406 |
async def test_decorator_with_exception(self): |
| 407 |
entered = False |
| 408 |
|
| 409 |
@asynccontextmanager |
| 410 |
async def context(): |
| 411 |
nonlocal entered |
| 412 |
try: |
| 413 |
entered = True |
| 414 |
yield |
| 415 |
finally: |
| 416 |
entered = False |
| 417 |
|
| 418 |
@context() |
| 419 |
async def test(): |
| 420 |
self.assertTrue(entered) |
| 421 |
raise NameError('foo') |
| 422 |
|
| 423 |
self.assertFalse(entered) |
| 424 |
with self.assertRaisesRegex(NameError, 'foo'): |
| 425 |
await test() |
| 426 |
self.assertFalse(entered) |
| 427 |
|
| 428 |
@_async_test |
| 429 |
async def test_decorating_method(self): |
| 430 |
|
| 431 |
@asynccontextmanager |
| 432 |
async def context(): |
| 433 |
yield |
| 434 |
|
| 435 |
|
| 436 |
class Test(object): |
| 437 |
|
| 438 |
@context() |
| 439 |
async def method(self, a, b, c=None): |
| 440 |
self.a = a |
| 441 |
self.b = b |
| 442 |
self.c = c |
| 443 |
|
| 444 |
# these tests are for argument passing when used as a decorator |
| 445 |
test = Test() |
| 446 |
await test.method(1, 2) |
| 447 |
self.assertEqual(test.a, 1) |
| 448 |
self.assertEqual(test.b, 2) |
| 449 |
self.assertEqual(test.c, None) |
| 450 |
|
| 451 |
test = Test() |
| 452 |
await test.method('a', 'b', 'c') |
| 453 |
self.assertEqual(test.a, 'a') |
| 454 |
self.assertEqual(test.b, 'b') |
| 455 |
self.assertEqual(test.c, 'c') |
| 456 |
|
| 457 |
test = Test() |
| 458 |
await test.method(a=1, b=2) |
| 459 |
self.assertEqual(test.a, 1) |
| 460 |
self.assertEqual(test.b, 2) |
| 461 |
|
| 462 |
|
| 463 |
class AclosingTestCase(unittest.TestCase): |
| 464 |
|
| 465 |
@support.requires_docstrings |
| 466 |
def test_instance_docs(self): |
| 467 |
cm_docstring = aclosing.__doc__ |
| 468 |
obj = aclosing(None) |
| 469 |
self.assertEqual(obj.__doc__, cm_docstring) |
| 470 |
|
| 471 |
@_async_test |
| 472 |
async def test_aclosing(self): |
| 473 |
state = [] |
| 474 |
class C: |
| 475 |
async def aclose(self): |
| 476 |
state.append(1) |
| 477 |
x = C() |
| 478 |
self.assertEqual(state, []) |
| 479 |
async with aclosing(x) as y: |
| 480 |
self.assertEqual(x, y) |
| 481 |
self.assertEqual(state, [1]) |
| 482 |
|
| 483 |
@_async_test |
| 484 |
async def test_aclosing_error(self): |
| 485 |
state = [] |
| 486 |
class C: |
| 487 |
async def aclose(self): |
| 488 |
state.append(1) |
| 489 |
x = C() |
| 490 |
self.assertEqual(state, []) |
| 491 |
with self.assertRaises(ZeroDivisionError): |
| 492 |
async with aclosing(x) as y: |
| 493 |
self.assertEqual(x, y) |
| 494 |
1 / 0 |
| 495 |
self.assertEqual(state, [1]) |
| 496 |
|
| 497 |
@_async_test |
| 498 |
async def test_aclosing_bpo41229(self): |
| 499 |
state = [] |
| 500 |
|
| 501 |
@contextmanager |
| 502 |
def sync_resource(): |
| 503 |
try: |
| 504 |
yield |
| 505 |
finally: |
| 506 |
state.append(1) |
| 507 |
|
| 508 |
async def agenfunc(): |
| 509 |
with sync_resource(): |
| 510 |
yield -1 |
| 511 |
yield -2 |
| 512 |
|
| 513 |
x = agenfunc() |
| 514 |
self.assertEqual(state, []) |
| 515 |
with self.assertRaises(ZeroDivisionError): |
| 516 |
async with aclosing(x) as y: |
| 517 |
self.assertEqual(x, y) |
| 518 |
self.assertEqual(-1, await x.__anext__()) |
| 519 |
1 / 0 |
| 520 |
self.assertEqual(state, [1]) |
| 521 |
|
| 522 |
|
| 523 |
class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase): |
| 524 |
class SyncAsyncExitStack(AsyncExitStack): |
| 525 |
|
| 526 |
def close(self): |
| 527 |
return _run_async_fn(self.aclose) |
| 528 |
|
| 529 |
def __enter__(self): |
| 530 |
return _run_async_fn(self.__aenter__) |
| 531 |
|
| 532 |
def __exit__(self, *exc_details): |
| 533 |
return _run_async_fn(self.__aexit__, *exc_details) |
| 534 |
|
| 535 |
exit_stack = SyncAsyncExitStack |
| 536 |
callback_error_internal_frames = [ |
| 537 |
('__exit__', 'return _run_async_fn(self.__aexit__, *exc_details)'), |
| 538 |
('run_no_yield_async_fn', 'coro.send(None)'), |
| 539 |
('__aexit__', 'raise exc'), |
| 540 |
('__aexit__', 'cb_suppress = cb(*exc_details)'), |
| 541 |
] |
| 542 |
|
| 543 |
@_async_test |
| 544 |
async def test_async_callback(self): |
| 545 |
expected = [ |
| 546 |
((), {}), |
| 547 |
((1,), {}), |
| 548 |
((1,2), {}), |
| 549 |
((), dict(example=1)), |
| 550 |
((1,), dict(example=1)), |
| 551 |
((1,2), dict(example=1)), |
| 552 |
] |
| 553 |
result = [] |
| 554 |
async def _exit(*args, **kwds): |
| 555 |
"""Test metadata propagation""" |
| 556 |
result.append((args, kwds)) |
| 557 |
|
| 558 |
async with AsyncExitStack() as stack: |
| 559 |
for args, kwds in reversed(expected): |
| 560 |
if args and kwds: |
| 561 |
f = stack.push_async_callback(_exit, *args, **kwds) |
| 562 |
elif args: |
| 563 |
f = stack.push_async_callback(_exit, *args) |
| 564 |
elif kwds: |
| 565 |
f = stack.push_async_callback(_exit, **kwds) |
| 566 |
else: |
| 567 |
f = stack.push_async_callback(_exit) |
| 568 |
self.assertIs(f, _exit) |
| 569 |
for wrapper in stack._exit_callbacks: |
| 570 |
self.assertIs(wrapper[1].__wrapped__, _exit) |
| 571 |
self.assertNotEqual(wrapper[1].__name__, _exit.__name__) |
| 572 |
self.assertIsNone(wrapper[1].__doc__, _exit.__doc__) |
| 573 |
|
| 574 |
self.assertEqual(result, expected) |
| 575 |
|
| 576 |
result = [] |
| 577 |
async with AsyncExitStack() as stack: |
| 578 |
with self.assertRaises(TypeError): |
| 579 |
stack.push_async_callback(arg=1) |
| 580 |
with self.assertRaises(TypeError): |
| 581 |
self.exit_stack.push_async_callback(arg=2) |
| 582 |
with self.assertRaises(TypeError): |
| 583 |
stack.push_async_callback(callback=_exit, arg=3) |
| 584 |
self.assertEqual(result, []) |
| 585 |
|
| 586 |
@_async_test |
| 587 |
async def test_async_push(self): |
| 588 |
exc_raised = ZeroDivisionError |
| 589 |
async def _expect_exc(exc_type, exc, exc_tb): |
| 590 |
self.assertIs(exc_type, exc_raised) |
| 591 |
async def _suppress_exc(*exc_details): |
| 592 |
return True |
| 593 |
async def _expect_ok(exc_type, exc, exc_tb): |
| 594 |
self.assertIsNone(exc_type) |
| 595 |
self.assertIsNone(exc) |
| 596 |
self.assertIsNone(exc_tb) |
| 597 |
class ExitCM(object): |
| 598 |
def __init__(self, check_exc): |
| 599 |
self.check_exc = check_exc |
| 600 |
async def __aenter__(self): |
| 601 |
self.fail("Should not be called!") |
| 602 |
async def __aexit__(self, *exc_details): |
| 603 |
await self.check_exc(*exc_details) |
| 604 |
|
| 605 |
async with self.exit_stack() as stack: |
| 606 |
stack.push_async_exit(_expect_ok) |
| 607 |
self.assertIs(stack._exit_callbacks[-1][1], _expect_ok) |
| 608 |
cm = ExitCM(_expect_ok) |
| 609 |
stack.push_async_exit(cm) |
| 610 |
self.assertIs(stack._exit_callbacks[-1][1].__self__, cm) |
| 611 |
stack.push_async_exit(_suppress_exc) |
| 612 |
self.assertIs(stack._exit_callbacks[-1][1], _suppress_exc) |
| 613 |
cm = ExitCM(_expect_exc) |
| 614 |
stack.push_async_exit(cm) |
| 615 |
self.assertIs(stack._exit_callbacks[-1][1].__self__, cm) |
| 616 |
stack.push_async_exit(_expect_exc) |
| 617 |
self.assertIs(stack._exit_callbacks[-1][1], _expect_exc) |
| 618 |
stack.push_async_exit(_expect_exc) |
| 619 |
self.assertIs(stack._exit_callbacks[-1][1], _expect_exc) |
| 620 |
1/0 |
| 621 |
|
| 622 |
@_async_test |
| 623 |
async def test_enter_async_context(self): |
| 624 |
class TestCM(object): |
| 625 |
async def __aenter__(self): |
| 626 |
result.append(1) |
| 627 |
async def __aexit__(self, *exc_details): |
| 628 |
result.append(3) |
| 629 |
|
| 630 |
result = [] |
| 631 |
cm = TestCM() |
| 632 |
|
| 633 |
async with AsyncExitStack() as stack: |
| 634 |
@stack.push_async_callback # Registered first => cleaned up last |
| 635 |
async def _exit(): |
| 636 |
result.append(4) |
| 637 |
self.assertIsNotNone(_exit) |
| 638 |
await stack.enter_async_context(cm) |
| 639 |
self.assertIs(stack._exit_callbacks[-1][1].__self__, cm) |
| 640 |
result.append(2) |
| 641 |
|
| 642 |
self.assertEqual(result, [1, 2, 3, 4]) |
| 643 |
|
| 644 |
@_async_test |
| 645 |
async def test_enter_async_context_errors(self): |
| 646 |
class LacksEnterAndExit: |
| 647 |
pass |
| 648 |
class LacksEnter: |
| 649 |
async def __aexit__(self, *exc_info): |
| 650 |
pass |
| 651 |
class LacksExit: |
| 652 |
async def __aenter__(self): |
| 653 |
pass |
| 654 |
|
| 655 |
async with self.exit_stack() as stack: |
| 656 |
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): |
| 657 |
await stack.enter_async_context(LacksEnterAndExit()) |
| 658 |
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): |
| 659 |
await stack.enter_async_context(LacksEnter()) |
| 660 |
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): |
| 661 |
await stack.enter_async_context(LacksExit()) |
| 662 |
self.assertFalse(stack._exit_callbacks) |
| 663 |
|
| 664 |
@_async_test |
| 665 |
async def test_async_exit_exception_chaining(self): |
| 666 |
# Ensure exception chaining matches the reference behaviour |
| 667 |
async def raise_exc(exc): |
| 668 |
raise exc |
| 669 |
|
| 670 |
saved_details = None |
| 671 |
async def suppress_exc(*exc_details): |
| 672 |
nonlocal saved_details |
| 673 |
saved_details = exc_details |
| 674 |
return True |
| 675 |
|
| 676 |
try: |
| 677 |
async with self.exit_stack() as stack: |
| 678 |
stack.push_async_callback(raise_exc, IndexError) |
| 679 |
stack.push_async_callback(raise_exc, KeyError) |
| 680 |
stack.push_async_callback(raise_exc, AttributeError) |
| 681 |
stack.push_async_exit(suppress_exc) |
| 682 |
stack.push_async_callback(raise_exc, ValueError) |
| 683 |
1 / 0 |
| 684 |
except IndexError as exc: |
| 685 |
self.assertIsInstance(exc.__context__, KeyError) |
| 686 |
self.assertIsInstance(exc.__context__.__context__, AttributeError) |
| 687 |
# Inner exceptions were suppressed |
| 688 |
self.assertIsNone(exc.__context__.__context__.__context__) |
| 689 |
else: |
| 690 |
self.fail("Expected IndexError, but no exception was raised") |
| 691 |
# Check the inner exceptions |
| 692 |
inner_exc = saved_details[1] |
| 693 |
self.assertIsInstance(inner_exc, ValueError) |
| 694 |
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError) |
| 695 |
|
| 696 |
@_async_test |
| 697 |
async def test_async_exit_exception_explicit_none_context(self): |
| 698 |
# Ensure AsyncExitStack chaining matches actual nested `with` statements |
| 699 |
# regarding explicit __context__ = None. |
| 700 |
|
| 701 |
class MyException(Exception): |
| 702 |
pass |
| 703 |
|
| 704 |
@asynccontextmanager |
| 705 |
async def my_cm(): |
| 706 |
try: |
| 707 |
yield |
| 708 |
except BaseException: |
| 709 |
exc = MyException() |
| 710 |
try: |
| 711 |
raise exc |
| 712 |
finally: |
| 713 |
exc.__context__ = None |
| 714 |
|
| 715 |
@asynccontextmanager |
| 716 |
async def my_cm_with_exit_stack(): |
| 717 |
async with self.exit_stack() as stack: |
| 718 |
await stack.enter_async_context(my_cm()) |
| 719 |
yield stack |
| 720 |
|
| 721 |
for cm in (my_cm, my_cm_with_exit_stack): |
| 722 |
with self.subTest(): |
| 723 |
try: |
| 724 |
async with cm(): |
| 725 |
raise IndexError() |
| 726 |
except MyException as exc: |
| 727 |
self.assertIsNone(exc.__context__) |
| 728 |
else: |
| 729 |
self.fail("Expected IndexError, but no exception was raised") |
| 730 |
|
| 731 |
@_async_test |
| 732 |
async def test_instance_bypass_async(self): |
| 733 |
class Example(object): pass |
| 734 |
cm = Example() |
| 735 |
cm.__aenter__ = object() |
| 736 |
cm.__aexit__ = object() |
| 737 |
stack = self.exit_stack() |
| 738 |
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): |
| 739 |
await stack.enter_async_context(cm) |
| 740 |
stack.push_async_exit(cm) |
| 741 |
self.assertIs(stack._exit_callbacks[-1][1], cm) |
| 742 |
|
| 743 |
|
| 744 |
class TestAsyncNullcontext(unittest.TestCase): |
| 745 |
@_async_test |
| 746 |
async def test_async_nullcontext(self): |
| 747 |
class C: |
| 748 |
pass |
| 749 |
c = C() |
| 750 |
async with nullcontext(c) as c_in: |
| 751 |
self.assertIs(c_in, c) |
| 752 |
|
| 753 |
|
| 754 |
if __name__ == '__main__': |
| 755 |
unittest.main() |