Context manager that does no additional processing. Used as a stand-in for a normal context manager, when a particular block of code is only sometimes used with a normal context manager: cm = optional_cm if condition else nullcontext() with cm: # Perform operation, using op
| 773 | |
| 774 | |
| 775 | class nullcontext(AbstractContextManager, AbstractAsyncContextManager): |
| 776 | """Context manager that does no additional processing. |
| 777 | |
| 778 | Used as a stand-in for a normal context manager, when a particular |
| 779 | block of code is only sometimes used with a normal context manager: |
| 780 | |
| 781 | cm = optional_cm if condition else nullcontext() |
| 782 | with cm: |
| 783 | # Perform operation, using optional_cm if condition is True |
| 784 | """ |
| 785 | |
| 786 | def __init__(self, enter_result=None): |
| 787 | self.enter_result = enter_result |
| 788 | |
| 789 | def __enter__(self): |
| 790 | return self.enter_result |
| 791 | |
| 792 | def __exit__(self, *excinfo): |
| 793 | pass |
| 794 | |
| 795 | async def __aenter__(self): |
| 796 | return self.enter_result |
| 797 | |
| 798 | async def __aexit__(self, *excinfo): |
| 799 | pass |
| 800 | |
| 801 | |
| 802 | class chdir(AbstractContextManager): |
no outgoing calls