[Python-checkins] cpython (merge 3.6 -> default): Merge 3.6 (issue #28372)

yury.selivanov python-checkins at python.org
Wed Oct 5 19:33:58 EDT 2016


https://hg.python.org/cpython/rev/8bc3e9754b3d
changeset:   104313:8bc3e9754b3d
parent:      104310:893f65369fea
parent:      104312:f7550d535878
user:        Yury Selivanov <yury at magic.io>
date:        Wed Oct 05 19:33:52 2016 -0400
summary:
  Merge 3.6 (issue #28372)

files:
  Lib/asyncio/coroutines.py            |  19 ++++++++
  Lib/test/test_asyncio/test_events.py |  34 ++++++++++++++++
  Misc/NEWS                            |   2 +
  3 files changed, 55 insertions(+), 0 deletions(-)


diff --git a/Lib/asyncio/coroutines.py b/Lib/asyncio/coroutines.py
--- a/Lib/asyncio/coroutines.py
+++ b/Lib/asyncio/coroutines.py
@@ -261,6 +261,25 @@
 def _format_coroutine(coro):
     assert iscoroutine(coro)
 
+    if not hasattr(coro, 'cr_code') and not hasattr(coro, 'gi_code'):
+        # Most likely a Cython coroutine.
+        coro_name = getattr(coro, '__qualname__', coro.__name__)
+        coro_name = '{}()'.format(coro_name)
+
+        running = False
+        try:
+            running = coro.cr_running
+        except AttributeError:
+            try:
+                running = coro.gi_running
+            except AttributeError:
+                pass
+
+        if running:
+            return '{} running'.format(coro_name)
+        else:
+            return coro_name
+
     coro_name = None
     if isinstance(coro, CoroWrapper):
         func = coro.func
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -1,5 +1,6 @@
 """Tests for events.py."""
 
+import collections.abc
 import functools
 import gc
 import io
@@ -25,6 +26,7 @@
     import tty
 
 import asyncio
+from asyncio import coroutines
 from asyncio import proactor_events
 from asyncio import selector_events
 from asyncio import sslproto
@@ -2380,6 +2382,38 @@
         h = loop.call_later(0, noop)
         check_source_traceback(h)
 
+    @unittest.skipUnless(hasattr(collections.abc, 'Coroutine'),
+                         'No collections.abc.Coroutine')
+    def test_coroutine_like_object_debug_formatting(self):
+        # Test that asyncio can format coroutines that are instances of
+        # collections.abc.Coroutine, but lack cr_core or gi_code attributes
+        # (such as ones compiled with Cython).
+
+        class Coro:
+            __name__ = 'AAA'
+
+            def send(self, v):
+                pass
+
+            def throw(self, *exc):
+                pass
+
+            def close(self):
+                pass
+
+            def __await__(self):
+                pass
+
+        coro = Coro()
+        self.assertTrue(asyncio.iscoroutine(coro))
+        self.assertEqual(coroutines._format_coroutine(coro), 'AAA()')
+
+        coro.__qualname__ = 'BBB'
+        self.assertEqual(coroutines._format_coroutine(coro), 'BBB()')
+
+        coro.cr_running = True
+        self.assertEqual(coroutines._format_coroutine(coro), 'BBB() running')
+
 
 class TimerTests(unittest.TestCase):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -186,6 +186,8 @@
 
 - Issue #28371: Deprecate passing asyncio.Handles to run_in_executor.
 
+- Issue #28372: Fix asyncio to support formatting of non-python coroutines.
+
 Windows
 -------
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list