[Python-checkins] bpo-32650: Add native coroutine support to bdb when stepping over line (GH-5400) (#5402)

Andrew Svetlov webhook-mailer at python.org
Mon Jan 29 00:57:10 EST 2018


https://github.com/python/cpython/commit/543ec005a4c83fcc0d14ac1e4c0f1a36bf84640b
commit: 543ec005a4c83fcc0d14ac1e4c0f1a36bf84640b
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Andrew Svetlov <andrew.svetlov at gmail.com>
date: 2018-01-29T07:57:06+02:00
summary:

bpo-32650: Add native coroutine support to bdb when stepping over line (GH-5400) (#5402)

(cherry picked from commit 46877024423e98d1b872bf308dacacd583327207)

files:
A Misc/NEWS.d/next/Core and Builtins/2018-01-28-23-01-39.bpo-32650.Bbi7ek.rst
M Lib/bdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/bdb.py b/Lib/bdb.py
index 67a08463fa1b..fe6af903b732 100644
--- a/Lib/bdb.py
+++ b/Lib/bdb.py
@@ -3,7 +3,7 @@
 import fnmatch
 import sys
 import os
-from inspect import CO_GENERATOR
+from inspect import CO_GENERATOR, CO_COROUTINE
 
 __all__ = ["BdbQuit", "Bdb", "Breakpoint"]
 
@@ -77,7 +77,7 @@ def dispatch_call(self, frame, arg):
             # No need to trace this function
             return # None
         # Ignore call events in generator except when stepping.
-        if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
+        if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
             return self.trace_dispatch
         self.user_call(frame, arg)
         if self.quitting: raise BdbQuit
@@ -86,7 +86,7 @@ def dispatch_call(self, frame, arg):
     def dispatch_return(self, frame, arg):
         if self.stop_here(frame) or frame == self.returnframe:
             # Ignore return events in generator except when stepping.
-            if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
+            if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
                 return self.trace_dispatch
             try:
                 self.frame_returning = frame
@@ -104,7 +104,7 @@ def dispatch_exception(self, frame, arg):
             # When stepping with next/until/return in a generator frame, skip
             # the internal StopIteration exception (with no traceback)
             # triggered by a subiterator run with the 'yield from' statement.
-            if not (frame.f_code.co_flags & CO_GENERATOR
+            if not (frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
                     and arg[0] is StopIteration and arg[2] is None):
                 self.user_exception(frame, arg)
                 if self.quitting: raise BdbQuit
@@ -113,7 +113,7 @@ def dispatch_exception(self, frame, arg):
         # next/until command at the last statement in the generator before the
         # exception.
         elif (self.stopframe and frame is not self.stopframe
-                and self.stopframe.f_code.co_flags & CO_GENERATOR
+                and self.stopframe.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
                 and arg[0] in (StopIteration, GeneratorExit)):
             self.user_exception(frame, arg)
             if self.quitting: raise BdbQuit
@@ -230,7 +230,7 @@ def set_next(self, frame):
 
     def set_return(self, frame):
         """Stop when returning from the given frame."""
-        if frame.f_code.co_flags & CO_GENERATOR:
+        if frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
             self._set_stopinfo(frame, None, -1)
         else:
             self._set_stopinfo(frame.f_back, frame)
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 0ea2af554175..a7656196371b 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -724,6 +724,61 @@ def test_pdb_next_command_for_generator():
     finished
     """
 
+def test_pdb_next_command_for_coroutine():
+    """Testing skip unwindng stack on yield for coroutines for "next" command
+
+    >>> import asyncio
+
+    >>> async def test_coro():
+    ...     await asyncio.sleep(0)
+    ...     await asyncio.sleep(0)
+    ...     await asyncio.sleep(0)
+
+    >>> async def test_main():
+    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     await test_coro()
+
+    >>> def test_function():
+    ...     loop = asyncio.get_event_loop()
+    ...     loop.run_until_complete(test_main())
+    ...     loop.close()
+    ...     print("finished")
+
+    >>> with PdbTestInput(['step',
+    ...                    'step',
+    ...                    'next',
+    ...                    'next',
+    ...                    'next',
+    ...                    'step',
+    ...                    'continue']):
+    ...     test_function()
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
+    -> await test_coro()
+    (Pdb) step
+    --Call--
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
+    -> async def test_coro():
+    (Pdb) step
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
+    -> await asyncio.sleep(0)
+    (Pdb) next
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
+    -> await asyncio.sleep(0)
+    (Pdb) next
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
+    -> await asyncio.sleep(0)
+    (Pdb) next
+    Internal StopIteration
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
+    -> await test_coro()
+    (Pdb) step
+    --Return--
+    > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
+    -> await test_coro()
+    (Pdb) continue
+    finished
+    """
+
 def test_pdb_return_command_for_generator():
     """Testing no unwindng stack on yield for generators
        for "return" command
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-01-28-23-01-39.bpo-32650.Bbi7ek.rst b/Misc/NEWS.d/next/Core and Builtins/2018-01-28-23-01-39.bpo-32650.Bbi7ek.rst
new file mode 100644
index 000000000000..f0b9e3ff459d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-01-28-23-01-39.bpo-32650.Bbi7ek.rst	
@@ -0,0 +1,2 @@
+Pdb and other debuggers dependent on bdb.py will correctly step over (next
+command) native coroutines. Patch by Pablo Galindo.



More information about the Python-checkins mailing list