[Python-checkins] bpo-41696: Fix handling of debug mode in asyncio.run (#22069)

Shantanu webhook-mailer at python.org
Thu Sep 3 00:54:55 EDT 2020


https://github.com/python/cpython/commit/0770ad948cb6d9f7f6c4002efd83e27c27069808
commit: 0770ad948cb6d9f7f6c4002efd83e27c27069808
branch: master
author: Shantanu <12621235+hauntsaninja at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-09-02T21:54:46-07:00
summary:

bpo-41696: Fix handling of debug mode in asyncio.run (#22069)

* bpo-41696: Fix handling of debug mode in asyncio.run

This allows PYTHONASYNCIODEBUG or -X dev to enable asyncio debug mode
when using asyncio.run

* 📜🤖 Added by blurb_it.

Co-authored-by: hauntsaninja <>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2020-09-03-01-35-32.bpo-41696.zkYGre.rst
M Lib/asyncio/runners.py
M Lib/test/test_asyncio/test_runners.py

diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py
index 03ce33300eba8..268635d68fb0c 100644
--- a/Lib/asyncio/runners.py
+++ b/Lib/asyncio/runners.py
@@ -5,7 +5,7 @@
 from . import tasks
 
 
-def run(main, *, debug=False):
+def run(main, *, debug=None):
     """Execute the coroutine and return the result.
 
     This function runs the passed coroutine, taking care of
@@ -39,7 +39,8 @@ async def main():
     loop = events.new_event_loop()
     try:
         events.set_event_loop(loop)
-        loop.set_debug(debug)
+        if debug is not None:
+            loop.set_debug(debug)
         return loop.run_until_complete(main)
     finally:
         try:
diff --git a/Lib/test/test_asyncio/test_runners.py b/Lib/test/test_asyncio/test_runners.py
index 3b58ddee443ad..b9ae02dc3c04e 100644
--- a/Lib/test/test_asyncio/test_runners.py
+++ b/Lib/test/test_asyncio/test_runners.py
@@ -87,6 +87,9 @@ async def main(expected):
 
         asyncio.run(main(False))
         asyncio.run(main(True), debug=True)
+        with mock.patch('asyncio.coroutines._is_debug_mode', lambda: True):
+            asyncio.run(main(True))
+            asyncio.run(main(False), debug=False)
 
     def test_asyncio_run_from_running_loop(self):
         async def main():
diff --git a/Misc/NEWS.d/next/Library/2020-09-03-01-35-32.bpo-41696.zkYGre.rst b/Misc/NEWS.d/next/Library/2020-09-03-01-35-32.bpo-41696.zkYGre.rst
new file mode 100644
index 0000000000000..67bbbb857f18c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-09-03-01-35-32.bpo-41696.zkYGre.rst
@@ -0,0 +1 @@
+Fix handling of debug mode in :func:`asyncio.run`. This allows setting ``PYTHONASYNCIODEBUG`` or ``-X dev`` to enable asyncio debug mode when using :func:`asyncio.run`.
\ No newline at end of file



More information about the Python-checkins mailing list