]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)
authorTom Gringauz <tomgrin10@gmail.com>
Mon, 9 Nov 2020 12:34:07 +0000 (14:34 +0200)
committerGitHub <noreply@github.com>
Mon, 9 Nov 2020 12:34:07 +0000 (14:34 +0200)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Doc/library/contextlib.rst
Lib/contextlib.py
Lib/test/test_contextlib_async.py
Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst [new file with mode: 0644]

index d5a1068a734fd69323417aa7aef3681f0a98cd67..91edbba72834720e69287020a88367992e2525a8 100644 (file)
@@ -243,8 +243,26 @@ Functions and classes provided:
           with cm as file:
               # Perform processing on the file
 
+   It can also be used as a stand-in for
+   :ref:`asynchronous context managers <async-context-managers>`::
+
+       async def send_http(session=None):
+          if not session:
+              # If no http session, create it with aiohttp
+              cm = aiohttp.ClientSession()
+          else:
+              # Caller is responsible for closing the session
+              cm = nullcontext(session)
+
+          async with cm as session:
+              # Send http requests with session
+
    .. versionadded:: 3.7
 
+   .. versionchanged:: 3.10
+      :term:`asynchronous context manager` support was added.
+
+
 
 .. function:: suppress(*exceptions)
 
index 56b4968118bdb39e43ddf14311b570c7c31f043b..a0b523c96fb285417e4c062ea0c4669fa9445362 100644 (file)
@@ -704,7 +704,7 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
         return received_exc and suppressed_exc
 
 
-class nullcontext(AbstractContextManager):
+class nullcontext(AbstractContextManager, AbstractAsyncContextManager):
     """Context manager that does no additional processing.
 
     Used as a stand-in for a normal context manager, when a particular
@@ -723,3 +723,9 @@ class nullcontext(AbstractContextManager):
 
     def __exit__(self, *excinfo):
         pass
+
+    async def __aenter__(self):
+        return self.enter_result
+
+    async def __aexit__(self, *excinfo):
+        pass
index 109807d633d565497510fa026cda37bc95c2907a..290ef05b82a801203361463480b121ac7f2d52ca 100644 (file)
@@ -1,5 +1,7 @@
 import asyncio
-from contextlib import aclosing, asynccontextmanager, AbstractAsyncContextManager, AsyncExitStack
+from contextlib import (
+    asynccontextmanager, AbstractAsyncContextManager,
+    AsyncExitStack, nullcontext, aclosing)
 import functools
 from test import support
 import unittest
@@ -537,5 +539,15 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
         self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
 
 
+class TestAsyncNullcontext(unittest.TestCase):
+    @_async_test
+    async def test_async_nullcontext(self):
+        class C:
+            pass
+        c = C()
+        async with nullcontext(c) as c_in:
+            self.assertIs(c_in, c)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst b/Misc/NEWS.d/next/Library/2020-08-14-00-39-04.bpo-41543.RpcRjb.rst
new file mode 100644 (file)
index 0000000..753dc76
--- /dev/null
@@ -0,0 +1 @@
+Add async context manager support for contextlib.nullcontext.