From ef9384d16131c851ffca67e4d8c0a34620765983 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Mon, 15 Nov 2021 15:18:20 +0000 Subject: [PATCH] utils.misc: Make nullcontext work with asyncio Implement __aenter__ and __aexit__ on nullcontext so it can be used as an asynchronous context manager. --- devlib/utils/misc.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py index de4944b..eb9dfd3 100644 --- a/devlib/utils/misc.py +++ b/devlib/utils/misc.py @@ -748,8 +748,7 @@ def batch_contextmanager(f, kwargs_list): yield -@contextmanager -def nullcontext(enter_result=None): +class nullcontext: """ Backport of Python 3.7 ``contextlib.nullcontext`` @@ -761,7 +760,20 @@ def nullcontext(enter_result=None): statement, or `None` if nothing is specified. :type enter_result: object """ - yield enter_result + def __init__(self, enter_result=None): + self.enter_result = enter_result + + def __enter__(self): + return self.enter_result + + async def __aenter__(self): + return self.enter_result + + def __exit__(*_): + return + + async def __aexit__(*_): + return class tls_property: