Skip to content

Commit 543ec00

Browse files
miss-islingtonasvetlov
authored andcommitted
bpo-32650: Add native coroutine support to bdb when stepping over line (GH-5400) (#5402)
(cherry picked from commit 4687702)
1 parent 9105879 commit 543ec00

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Lib/bdb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import fnmatch
44
import sys
55
import os
6-
from inspect import CO_GENERATOR
6+
from inspect import CO_GENERATOR, CO_COROUTINE
77

88
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
99

@@ -77,7 +77,7 @@ def dispatch_call(self, frame, arg):
7777
# No need to trace this function
7878
return # None
7979
# Ignore call events in generator except when stepping.
80-
if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
80+
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
8181
return self.trace_dispatch
8282
self.user_call(frame, arg)
8383
if self.quitting: raise BdbQuit
@@ -86,7 +86,7 @@ def dispatch_call(self, frame, arg):
8686
def dispatch_return(self, frame, arg):
8787
if self.stop_here(frame) or frame == self.returnframe:
8888
# Ignore return events in generator except when stepping.
89-
if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
89+
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
9090
return self.trace_dispatch
9191
try:
9292
self.frame_returning = frame
@@ -104,7 +104,7 @@ def dispatch_exception(self, frame, arg):
104104
# When stepping with next/until/return in a generator frame, skip
105105
# the internal StopIteration exception (with no traceback)
106106
# triggered by a subiterator run with the 'yield from' statement.
107-
if not (frame.f_code.co_flags & CO_GENERATOR
107+
if not (frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
108108
and arg[0] is StopIteration and arg[2] is None):
109109
self.user_exception(frame, arg)
110110
if self.quitting: raise BdbQuit
@@ -113,7 +113,7 @@ def dispatch_exception(self, frame, arg):
113113
# next/until command at the last statement in the generator before the
114114
# exception.
115115
elif (self.stopframe and frame is not self.stopframe
116-
and self.stopframe.f_code.co_flags & CO_GENERATOR
116+
and self.stopframe.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
117117
and arg[0] in (StopIteration, GeneratorExit)):
118118
self.user_exception(frame, arg)
119119
if self.quitting: raise BdbQuit
@@ -230,7 +230,7 @@ def set_next(self, frame):
230230

231231
def set_return(self, frame):
232232
"""Stop when returning from the given frame."""
233-
if frame.f_code.co_flags & CO_GENERATOR:
233+
if frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
234234
self._set_stopinfo(frame, None, -1)
235235
else:
236236
self._set_stopinfo(frame.f_back, frame)

Lib/test/test_pdb.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,61 @@ def test_pdb_next_command_for_generator():
724724
finished
725725
"""
726726

727+
def test_pdb_next_command_for_coroutine():
728+
"""Testing skip unwindng stack on yield for coroutines for "next" command
729+
730+
>>> import asyncio
731+
732+
>>> async def test_coro():
733+
... await asyncio.sleep(0)
734+
... await asyncio.sleep(0)
735+
... await asyncio.sleep(0)
736+
737+
>>> async def test_main():
738+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
739+
... await test_coro()
740+
741+
>>> def test_function():
742+
... loop = asyncio.get_event_loop()
743+
... loop.run_until_complete(test_main())
744+
... loop.close()
745+
... print("finished")
746+
747+
>>> with PdbTestInput(['step',
748+
... 'step',
749+
... 'next',
750+
... 'next',
751+
... 'next',
752+
... 'step',
753+
... 'continue']):
754+
... test_function()
755+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
756+
-> await test_coro()
757+
(Pdb) step
758+
--Call--
759+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
760+
-> async def test_coro():
761+
(Pdb) step
762+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
763+
-> await asyncio.sleep(0)
764+
(Pdb) next
765+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
766+
-> await asyncio.sleep(0)
767+
(Pdb) next
768+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
769+
-> await asyncio.sleep(0)
770+
(Pdb) next
771+
Internal StopIteration
772+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
773+
-> await test_coro()
774+
(Pdb) step
775+
--Return--
776+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
777+
-> await test_coro()
778+
(Pdb) continue
779+
finished
780+
"""
781+
727782
def test_pdb_return_command_for_generator():
728783
"""Testing no unwindng stack on yield for generators
729784
for "return" command
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Pdb and other debuggers dependent on bdb.py will correctly step over (next
2+
command) native coroutines. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)