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, u
| 750 | |
| 751 | |
| 752 | class nullcontext(AbstractContextManager, AbstractAsyncContextManager): |
| 753 | """Context manager that does no additional processing. |
| 754 | |
| 755 | Used as a stand-in for a normal context manager, when a particular |
| 756 | block of code is only sometimes used with a normal context manager: |
| 757 | |
| 758 | cm = optional_cm if condition else nullcontext() |
| 759 | with cm: |
| 760 | # Perform operation, using optional_cm if condition is True |
| 761 | """ |
| 762 | |
| 763 | def __init__(self, enter_result=None): |
| 764 | self.enter_result = enter_result |
| 765 | |
| 766 | def __enter__(self): |
| 767 | return self.enter_result |
| 768 | |
| 769 | def __exit__(self, *excinfo): |
| 770 | pass |
| 771 | |
| 772 | async def __aenter__(self): |
| 773 | return self.enter_result |
| 774 | |
| 775 | async def __aexit__(self, *excinfo): |
| 776 | pass |
| 777 | |
| 778 | |
| 779 | class chdir(AbstractContextManager): |