Skip to content

Commit a117167

Browse files
tomgrin10asvetlov
andauthored
bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
1 parent 97e8b1e commit a117167

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Doc/library/contextlib.rst

+18
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,26 @@ Functions and classes provided:
243243
with cm as file:
244244
# Perform processing on the file
245245

246+
It can also be used as a stand-in for
247+
:ref:`asynchronous context managers <async-context-managers>`::
248+
249+
async def send_http(session=None):
250+
if not session:
251+
# If no http session, create it with aiohttp
252+
cm = aiohttp.ClientSession()
253+
else:
254+
# Caller is responsible for closing the session
255+
cm = nullcontext(session)
256+
257+
async with cm as session:
258+
# Send http requests with session
259+
246260
.. versionadded:: 3.7
247261

262+
.. versionchanged:: 3.10
263+
:term:`asynchronous context manager` support was added.
264+
265+
248266

249267
.. function:: suppress(*exceptions)
250268

Lib/contextlib.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def _fix_exception_context(new_exc, old_exc):
704704
return received_exc and suppressed_exc
705705

706706

707-
class nullcontext(AbstractContextManager):
707+
class nullcontext(AbstractContextManager, AbstractAsyncContextManager):
708708
"""Context manager that does no additional processing.
709709
710710
Used as a stand-in for a normal context manager, when a particular
@@ -723,3 +723,9 @@ def __enter__(self):
723723

724724
def __exit__(self, *excinfo):
725725
pass
726+
727+
async def __aenter__(self):
728+
return self.enter_result
729+
730+
async def __aexit__(self, *excinfo):
731+
pass

Lib/test/test_contextlib_async.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
2-
from contextlib import aclosing, asynccontextmanager, AbstractAsyncContextManager, AsyncExitStack
2+
from contextlib import (
3+
asynccontextmanager, AbstractAsyncContextManager,
4+
AsyncExitStack, nullcontext, aclosing)
35
import functools
46
from test import support
57
import unittest
@@ -537,5 +539,15 @@ async def suppress_exc(*exc_details):
537539
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
538540

539541

542+
class TestAsyncNullcontext(unittest.TestCase):
543+
@_async_test
544+
async def test_async_nullcontext(self):
545+
class C:
546+
pass
547+
c = C()
548+
async with nullcontext(c) as c_in:
549+
self.assertIs(c_in, c)
550+
551+
540552
if __name__ == '__main__':
541553
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add async context manager support for contextlib.nullcontext.

0 commit comments

Comments
 (0)