1"""Utilities for with-statement contexts. See PEP 343.""" 2import abc 3import sys 4import _collections_abc 5from collections import deque 6from functools import wraps 7 8__all__ = ["asynccontextmanager", "contextmanager", "closing", "nullcontext", 9 "AbstractContextManager", "AbstractAsyncContextManager", 10 "AsyncExitStack", "ContextDecorator", "ExitStack", 11 "redirect_stdout", "redirect_stderr", "suppress"] 12 13 14class AbstractContextManager(abc.ABC): 15 16 """An abstract base class for context managers.""" 17 18 def __enter__(self): 19 """Return `self` upon entering the runtime context.""" 20 return self 21 22 @abc.abstractmethod 23 def __exit__(self, exc_type, exc_value, traceback): 24 """Raise any exception triggered within the runtime context.""" 25 return None 26 27 @classmethod 28 def __subclasshook__(cls, C): 29 if cls is AbstractContextManager: 30 return _collections_abc._check_methods(C, "__enter__", "__exit__") 31 return NotImplemented 32 33 34class AbstractAsyncContextManager(abc.ABC): 35 36 """An abstract base class for asynchronous context managers.""" 37 38 async def __aenter__(self): 39 """Return `self` upon entering the runtime context.""" 40 return self 41 42 @abc.abstractmethod 43 async def __aexit__(self, exc_type, exc_value, traceback): 44 """Raise any exception triggered within the runtime context.""" 45 return None 46 47 @classmethod 48 def __subclasshook__(cls, C): 49 if cls is AbstractAsyncContextManager: 50 return _collections_abc._check_methods(C, "__aenter__", 51 "__aexit__") 52 return NotImplemented 53 54 55class ContextDecorator(object): 56 "A base class or mixin that enables context managers to work as decorators." 57 58 def _recreate_cm(self): 59 """Return a recreated instance of self. 60 61 Allows an otherwise one-shot context manager like 62 _GeneratorContextManager to support use as 63 a decorator via implicit recreation. 64 65 This is a private interface just for _GeneratorContextManager. 66 See issue #11647 for details. 67 """ 68 return self 69 70 def __call__(self, func): 71 @wraps(func) 72 def inner(*args, **kwds): 73 with self._recreate_cm(): 74 return func(*args, **kwds) 75 return inner 76 77 78class _GeneratorContextManagerBase: 79 """Shared functionality for @contextmanager and @asynccontextmanager.""" 80 81 def __init__(self, func, args, kwds): 82 self.gen = func(*args, **kwds) 83 self.func, self.args, self.kwds = func, args, kwds 84 # Issue 19330: ensure context manager instances have good docstrings 85 doc = getattr(func, "__doc__", None) 86 if doc is None: 87 doc = type(self).__doc__ 88 self.__doc__ = doc 89 # Unfortunately, this still doesn't provide good help output when 90 # inspecting the created context manager instances, since pydoc 91 # currently bypasses the instance docstring and shows the docstring 92 # for the class instead. 93 # See http://bugs.python.org/issue19404 for more details. 94 95 96class _GeneratorContextManager(_GeneratorContextManagerBase, 97 AbstractContextManager, 98 ContextDecorator): 99 """Helper for @contextmanager decorator.""" 100 101 def _recreate_cm(self): 102 # _GCM instances are one-shot context managers, so the 103 # CM must be recreated each time a decorated function is 104 # called 105 return self.__class__(self.func, self.args, self.kwds) 106 107 def __enter__(self): 108 # do not keep args and kwds alive unnecessarily 109 # they are only needed for recreation, which is not possible anymore 110 del self.args, self.kwds, self.func 111 try: 112 return next(self.gen) 113 except StopIteration: 114 raise RuntimeError("generator didn't yield") from None 115 116 def __exit__(self, type, value, traceback): 117 if type is None: 118 try: 119 next(self.gen) 120 except StopIteration: 121 return False 122 else: 123 raise RuntimeError("generator didn't stop") 124 else: 125 if value is None: 126 # Need to force instantiation so we can reliably 127 # tell if we get the same exception back 128 value = type() 129 try: 130 self.gen.throw(type, value, traceback) 131 except StopIteration as exc: 132 # Suppress StopIteration *unless* it's the same exception that 133 # was passed to throw(). This prevents a StopIteration 134 # raised inside the "with" statement from being suppressed. 135 return exc is not value 136 except RuntimeError as exc: 137 # Don't re-raise the passed in exception. (issue27122) 138 if exc is value: 139 return False 140 # Likewise, avoid suppressing if a StopIteration exception 141 # was passed to throw() and later wrapped into a RuntimeError 142 # (see PEP 479). 143 if type is StopIteration and exc.__cause__ is value: 144 return False 145 raise 146 except: 147 # only re-raise if it's *not* the exception that was 148 # passed to throw(), because __exit__() must not raise 149 # an exception unless __exit__() itself failed. But throw() 150 # has to raise the exception to signal propagation, so this 151 # fixes the impedance mismatch between the throw() protocol 152 # and the __exit__() protocol. 153 # 154 # This cannot use 'except BaseException as exc' (as in the 155 # async implementation) to maintain compatibility with 156 # Python 2, where old-style class exceptions are not caught 157 # by 'except BaseException'. 158 if sys.exc_info()[1] is value: 159 return False 160 raise 161 raise RuntimeError("generator didn't stop after throw()") 162 163 164class _AsyncGeneratorContextManager(_GeneratorContextManagerBase, 165 AbstractAsyncContextManager): 166 """Helper for @asynccontextmanager.""" 167 168 async def __aenter__(self): 169 try: 170 return await self.gen.__anext__() 171 except StopAsyncIteration: 172 raise RuntimeError("generator didn't yield") from None 173 174 async def __aexit__(self, typ, value, traceback): 175 if typ is None: 176 try: 177 await self.gen.__anext__() 178 except StopAsyncIteration: 179 return 180 else: 181 raise RuntimeError("generator didn't stop") 182 else: 183 if value is None: 184 value = typ() 185 # See _GeneratorContextManager.__exit__ for comments on subtleties 186 # in this implementation 187 try: 188 await self.gen.athrow(typ, value, traceback) 189 raise RuntimeError("generator didn't stop after throw()") 190 except StopAsyncIteration as exc: 191 return exc is not value 192 except RuntimeError as exc: 193 if exc is value: 194 return False 195 # Avoid suppressing if a StopIteration exception 196 # was passed to throw() and later wrapped into a RuntimeError 197 # (see PEP 479 for sync generators; async generators also 198 # have this behavior). But do this only if the exception wrapped 199 # by the RuntimeError is actully Stop(Async)Iteration (see 200 # issue29692). 201 if isinstance(value, (StopIteration, StopAsyncIteration)): 202 if exc.__cause__ is value: 203 return False 204 raise 205 except BaseException as exc: 206 if exc is not value: 207 raise 208 209 210def contextmanager(func): 211 """@contextmanager decorator. 212 213 Typical usage: 214 215 @contextmanager 216 def some_generator(<arguments>): 217 <setup> 218 try: 219 yield <value> 220 finally: 221 <cleanup> 222 223 This makes this: 224 225 with some_generator(<arguments>) as <variable>: 226 <body> 227 228 equivalent to this: 229 230 <setup> 231 try: 232 <variable> = <value> 233 <body> 234 finally: 235 <cleanup> 236 """ 237 @wraps(func) 238 def helper(*args, **kwds): 239 return _GeneratorContextManager(func, args, kwds) 240 return helper 241 242 243def asynccontextmanager(func): 244 """@asynccontextmanager decorator. 245 246 Typical usage: 247 248 @asynccontextmanager 249 async def some_async_generator(<arguments>): 250 <setup> 251 try: 252 yield <value> 253 finally: 254 <cleanup> 255 256 This makes this: 257 258 async with some_async_generator(<arguments>) as <variable>: 259 <body> 260 261 equivalent to this: 262 263 <setup> 264 try: 265 <variable> = <value> 266 <body> 267 finally: 268 <cleanup> 269 """ 270 @wraps(func) 271 def helper(*args, **kwds): 272 return _AsyncGeneratorContextManager(func, args, kwds) 273 return helper 274 275 276class closing(AbstractContextManager): 277 """Context to automatically close something at the end of a block. 278 279 Code like this: 280 281 with closing(<module>.open(<arguments>)) as f: 282 <block> 283 284 is equivalent to this: 285 286 f = <module>.open(<arguments>) 287 try: 288 <block> 289 finally: 290 f.close() 291 292 """ 293 def __init__(self, thing): 294 self.thing = thing 295 def __enter__(self): 296 return self.thing 297 def __exit__(self, *exc_info): 298 self.thing.close() 299 300 301class _RedirectStream(AbstractContextManager): 302 303 _stream = None 304 305 def __init__(self, new_target): 306 self._new_target = new_target 307 # We use a list of old targets to make this CM re-entrant 308 self._old_targets = [] 309 310 def __enter__(self): 311 self._old_targets.append(getattr(sys, self._stream)) 312 setattr(sys, self._stream, self._new_target) 313 return self._new_target 314 315 def __exit__(self, exctype, excinst, exctb): 316 setattr(sys, self._stream, self._old_targets.pop()) 317 318 319class redirect_stdout(_RedirectStream): 320 """Context manager for temporarily redirecting stdout to another file. 321 322 # How to send help() to stderr 323 with redirect_stdout(sys.stderr): 324 help(dir) 325 326 # How to write help() to a file 327 with open('help.txt', 'w') as f: 328 with redirect_stdout(f): 329 help(pow) 330 """ 331 332 _stream = "stdout" 333 334 335class redirect_stderr(_RedirectStream): 336 """Context manager for temporarily redirecting stderr to another file.""" 337 338 _stream = "stderr" 339 340 341class suppress(AbstractContextManager): 342 """Context manager to suppress specified exceptions 343 344 After the exception is suppressed, execution proceeds with the next 345 statement following the with statement. 346 347 with suppress(FileNotFoundError): 348 os.remove(somefile) 349 # Execution still resumes here if the file was already removed 350 """ 351 352 def __init__(self, *exceptions): 353 self._exceptions = exceptions 354 355 def __enter__(self): 356 pass 357 358 def __exit__(self, exctype, excinst, exctb): 359 # Unlike isinstance and issubclass, CPython exception handling 360 # currently only looks at the concrete type hierarchy (ignoring 361 # the instance and subclass checking hooks). While Guido considers 362 # that a bug rather than a feature, it's a fairly hard one to fix 363 # due to various internal implementation details. suppress provides 364 # the simpler issubclass based semantics, rather than trying to 365 # exactly reproduce the limitations of the CPython interpreter. 366 # 367 # See http://bugs.python.org/issue12029 for more details 368 return exctype is not None and issubclass(exctype, self._exceptions) 369 370 371class _BaseExitStack: 372 """A base class for ExitStack and AsyncExitStack.""" 373 374 @staticmethod 375 def _create_exit_wrapper(cm, cm_exit): 376 def _exit_wrapper(exc_type, exc, tb): 377 return cm_exit(cm, exc_type, exc, tb) 378 return _exit_wrapper 379 380 @staticmethod 381 def _create_cb_wrapper(callback, *args, **kwds): 382 def _exit_wrapper(exc_type, exc, tb): 383 callback(*args, **kwds) 384 return _exit_wrapper 385 386 def __init__(self): 387 self._exit_callbacks = deque() 388 389 def pop_all(self): 390 """Preserve the context stack by transferring it to a new instance.""" 391 new_stack = type(self)() 392 new_stack._exit_callbacks = self._exit_callbacks 393 self._exit_callbacks = deque() 394 return new_stack 395 396 def push(self, exit): 397 """Registers a callback with the standard __exit__ method signature. 398 399 Can suppress exceptions the same way __exit__ method can. 400 Also accepts any object with an __exit__ method (registering a call 401 to the method instead of the object itself). 402 """ 403 # We use an unbound method rather than a bound method to follow 404 # the standard lookup behaviour for special methods. 405 _cb_type = type(exit) 406 407 try: 408 exit_method = _cb_type.__exit__ 409 except AttributeError: 410 # Not a context manager, so assume it's a callable. 411 self._push_exit_callback(exit) 412 else: 413 self._push_cm_exit(exit, exit_method) 414 return exit # Allow use as a decorator. 415 416 def enter_context(self, cm): 417 """Enters the supplied context manager. 418 419 If successful, also pushes its __exit__ method as a callback and 420 returns the result of the __enter__ method. 421 """ 422 # We look up the special methods on the type to match the with 423 # statement. 424 _cm_type = type(cm) 425 _exit = _cm_type.__exit__ 426 result = _cm_type.__enter__(cm) 427 self._push_cm_exit(cm, _exit) 428 return result 429 430 def callback(self, callback, *args, **kwds): 431 """Registers an arbitrary callback and arguments. 432 433 Cannot suppress exceptions. 434 """ 435 _exit_wrapper = self._create_cb_wrapper(callback, *args, **kwds) 436 437 # We changed the signature, so using @wraps is not appropriate, but 438 # setting __wrapped__ may still help with introspection. 439 _exit_wrapper.__wrapped__ = callback 440 self._push_exit_callback(_exit_wrapper) 441 return callback # Allow use as a decorator 442 443 def _push_cm_exit(self, cm, cm_exit): 444 """Helper to correctly register callbacks to __exit__ methods.""" 445 _exit_wrapper = self._create_exit_wrapper(cm, cm_exit) 446 _exit_wrapper.__self__ = cm 447 self._push_exit_callback(_exit_wrapper, True) 448 449 def _push_exit_callback(self, callback, is_sync=True): 450 self._exit_callbacks.append((is_sync, callback)) 451 452 453# Inspired by discussions on http://bugs.python.org/issue13585 454class ExitStack(_BaseExitStack, AbstractContextManager): 455 """Context manager for dynamic management of a stack of exit callbacks. 456 457 For example: 458 with ExitStack() as stack: 459 files = [stack.enter_context(open(fname)) for fname in filenames] 460 # All opened files will automatically be closed at the end of 461 # the with statement, even if attempts to open files later 462 # in the list raise an exception. 463 """ 464 465 def __enter__(self): 466 return self 467 468 def __exit__(self, *exc_details): 469 received_exc = exc_details[0] is not None 470 471 # We manipulate the exception state so it behaves as though 472 # we were actually nesting multiple with statements 473 frame_exc = sys.exc_info()[1] 474 def _fix_exception_context(new_exc, old_exc): 475 # Context may not be correct, so find the end of the chain 476 while 1: 477 exc_context = new_exc.__context__ 478 if exc_context is old_exc: 479 # Context is already set correctly (see issue 20317) 480 return 481 if exc_context is None or exc_context is frame_exc: 482 break 483 new_exc = exc_context 484 # Change the end of the chain to point to the exception 485 # we expect it to reference 486 new_exc.__context__ = old_exc 487 488 # Callbacks are invoked in LIFO order to match the behaviour of 489 # nested context managers 490 suppressed_exc = False 491 pending_raise = False 492 while self._exit_callbacks: 493 is_sync, cb = self._exit_callbacks.pop() 494 assert is_sync 495 try: 496 if cb(*exc_details): 497 suppressed_exc = True 498 pending_raise = False 499 exc_details = (None, None, None) 500 except: 501 new_exc_details = sys.exc_info() 502 # simulate the stack of exceptions by setting the context 503 _fix_exception_context(new_exc_details[1], exc_details[1]) 504 pending_raise = True 505 exc_details = new_exc_details 506 if pending_raise: 507 try: 508 # bare "raise exc_details[1]" replaces our carefully 509 # set-up context 510 fixed_ctx = exc_details[1].__context__ 511 raise exc_details[1] 512 except BaseException: 513 exc_details[1].__context__ = fixed_ctx 514 raise 515 return received_exc and suppressed_exc 516 517 def close(self): 518 """Immediately unwind the context stack.""" 519 self.__exit__(None, None, None) 520 521 522# Inspired by discussions on https://bugs.python.org/issue29302 523class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager): 524 """Async context manager for dynamic management of a stack of exit 525 callbacks. 526 527 For example: 528 async with AsyncExitStack() as stack: 529 connections = [await stack.enter_async_context(get_connection()) 530 for i in range(5)] 531 # All opened connections will automatically be released at the 532 # end of the async with statement, even if attempts to open a 533 # connection later in the list raise an exception. 534 """ 535 536 @staticmethod 537 def _create_async_exit_wrapper(cm, cm_exit): 538 async def _exit_wrapper(exc_type, exc, tb): 539 return await cm_exit(cm, exc_type, exc, tb) 540 return _exit_wrapper 541 542 @staticmethod 543 def _create_async_cb_wrapper(callback, *args, **kwds): 544 async def _exit_wrapper(exc_type, exc, tb): 545 await callback(*args, **kwds) 546 return _exit_wrapper 547 548 async def enter_async_context(self, cm): 549 """Enters the supplied async context manager. 550 551 If successful, also pushes its __aexit__ method as a callback and 552 returns the result of the __aenter__ method. 553 """ 554 _cm_type = type(cm) 555 _exit = _cm_type.__aexit__ 556 result = await _cm_type.__aenter__(cm) 557 self._push_async_cm_exit(cm, _exit) 558 return result 559 560 def push_async_exit(self, exit): 561 """Registers a coroutine function with the standard __aexit__ method 562 signature. 563 564 Can suppress exceptions the same way __aexit__ method can. 565 Also accepts any object with an __aexit__ method (registering a call 566 to the method instead of the object itself). 567 """ 568 _cb_type = type(exit) 569 try: 570 exit_method = _cb_type.__aexit__ 571 except AttributeError: 572 # Not an async context manager, so assume it's a coroutine function 573 self._push_exit_callback(exit, False) 574 else: 575 self._push_async_cm_exit(exit, exit_method) 576 return exit # Allow use as a decorator 577 578 def push_async_callback(self, callback, *args, **kwds): 579 """Registers an arbitrary coroutine function and arguments. 580 581 Cannot suppress exceptions. 582 """ 583 _exit_wrapper = self._create_async_cb_wrapper(callback, *args, **kwds) 584 585 # We changed the signature, so using @wraps is not appropriate, but 586 # setting __wrapped__ may still help with introspection. 587 _exit_wrapper.__wrapped__ = callback 588 self._push_exit_callback(_exit_wrapper, False) 589 return callback # Allow use as a decorator 590 591 async def aclose(self): 592 """Immediately unwind the context stack.""" 593 await self.__aexit__(None, None, None) 594 595 def _push_async_cm_exit(self, cm, cm_exit): 596 """Helper to correctly register coroutine function to __aexit__ 597 method.""" 598 _exit_wrapper = self._create_async_exit_wrapper(cm, cm_exit) 599 _exit_wrapper.__self__ = cm 600 self._push_exit_callback(_exit_wrapper, False) 601 602 async def __aenter__(self): 603 return self 604 605 async def __aexit__(self, *exc_details): 606 received_exc = exc_details[0] is not None 607 608 # We manipulate the exception state so it behaves as though 609 # we were actually nesting multiple with statements 610 frame_exc = sys.exc_info()[1] 611 def _fix_exception_context(new_exc, old_exc): 612 # Context may not be correct, so find the end of the chain 613 while 1: 614 exc_context = new_exc.__context__ 615 if exc_context is old_exc: 616 # Context is already set correctly (see issue 20317) 617 return 618 if exc_context is None or exc_context is frame_exc: 619 break 620 new_exc = exc_context 621 # Change the end of the chain to point to the exception 622 # we expect it to reference 623 new_exc.__context__ = old_exc 624 625 # Callbacks are invoked in LIFO order to match the behaviour of 626 # nested context managers 627 suppressed_exc = False 628 pending_raise = False 629 while self._exit_callbacks: 630 is_sync, cb = self._exit_callbacks.pop() 631 try: 632 if is_sync: 633 cb_suppress = cb(*exc_details) 634 else: 635 cb_suppress = await cb(*exc_details) 636 637 if cb_suppress: 638 suppressed_exc = True 639 pending_raise = False 640 exc_details = (None, None, None) 641 except: 642 new_exc_details = sys.exc_info() 643 # simulate the stack of exceptions by setting the context 644 _fix_exception_context(new_exc_details[1], exc_details[1]) 645 pending_raise = True 646 exc_details = new_exc_details 647 if pending_raise: 648 try: 649 # bare "raise exc_details[1]" replaces our carefully 650 # set-up context 651 fixed_ctx = exc_details[1].__context__ 652 raise exc_details[1] 653 except BaseException: 654 exc_details[1].__context__ = fixed_ctx 655 raise 656 return received_exc and suppressed_exc 657 658 659class nullcontext(AbstractContextManager): 660 """Context manager that does no additional processing. 661 662 Used as a stand-in for a normal context manager, when a particular 663 block of code is only sometimes used with a normal context manager: 664 665 cm = optional_cm if condition else nullcontext() 666 with cm: 667 # Perform operation, using optional_cm if condition is True 668 """ 669 670 def __init__(self, enter_result=None): 671 self.enter_result = enter_result 672 673 def __enter__(self): 674 return self.enter_result 675 676 def __exit__(self, *excinfo): 677 pass 678