Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4687702

Browse files
pablogsalasvetlov
authored andcommitted
bpo-32650: Add native coroutine support to bdb when stepping over line (GH-5400)
1 parent 7c99e93 commit 4687702

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

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

@@ -127,7 +127,7 @@ def dispatch_call(self, frame, arg):
127127
# No need to trace this function
128128
return # None
129129
# Ignore call events in generator except when stepping.
130-
if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
130+
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
131131
return self.trace_dispatch
132132
self.user_call(frame, arg)
133133
if self.quitting: raise BdbQuit
@@ -142,7 +142,7 @@ def dispatch_return(self, frame, arg):
142142
"""
143143
if self.stop_here(frame) or frame == self.returnframe:
144144
# Ignore return events in generator except when stepping.
145-
if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
145+
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
146146
return self.trace_dispatch
147147
try:
148148
self.frame_returning = frame
@@ -166,7 +166,7 @@ def dispatch_exception(self, frame, arg):
166166
# When stepping with next/until/return in a generator frame, skip
167167
# the internal StopIteration exception (with no traceback)
168168
# triggered by a subiterator run with the 'yield from' statement.
169-
if not (frame.f_code.co_flags & CO_GENERATOR
169+
if not (frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
170170
and arg[0] is StopIteration and arg[2] is None):
171171
self.user_exception(frame, arg)
172172
if self.quitting: raise BdbQuit
@@ -175,7 +175,7 @@ def dispatch_exception(self, frame, arg):
175175
# next/until command at the last statement in the generator before the
176176
# exception.
177177
elif (self.stopframe and frame is not self.stopframe
178-
and self.stopframe.f_code.co_flags & CO_GENERATOR
178+
and self.stopframe.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
179179
and arg[0] in (StopIteration, GeneratorExit)):
180180
self.user_exception(frame, arg)
181181
if self.quitting: raise BdbQuit
@@ -309,7 +309,7 @@ def set_next(self, frame):
309309

310310
def set_return(self, frame):
311311
"""Stop when returning from the given frame."""
312-
if frame.f_code.co_flags & CO_GENERATOR:
312+
if frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
313313
self._set_stopinfo(frame, None, -1)
314314
else:
315315
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
@@ -727,6 +727,61 @@ def test_pdb_next_command_for_generator():
727727
finished
728728
"""
729729

730+
def test_pdb_next_command_for_coroutine():
731+
"""Testing skip unwindng stack on yield for coroutines for "next" command
732+
733+
>>> import asyncio
734+
735+
>>> async def test_coro():
736+
... await asyncio.sleep(0)
737+
... await asyncio.sleep(0)
738+
... await asyncio.sleep(0)
739+
740+
>>> async def test_main():
741+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
742+
... await test_coro()
743+
744+
>>> def test_function():
745+
... loop = asyncio.get_event_loop()
746+
... loop.run_until_complete(test_main())
747+
... loop.close()
748+
... print("finished")
749+
750+
>>> with PdbTestInput(['step',
751+
... 'step',
752+
... 'next',
753+
... 'next',
754+
... 'next',
755+
... 'step',
756+
... 'continue']):
757+
... test_function()
758+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
759+
-> await test_coro()
760+
(Pdb) step
761+
--Call--
762+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
763+
-> async def test_coro():
764+
(Pdb) step
765+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
766+
-> await asyncio.sleep(0)
767+
(Pdb) next
768+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
769+
-> await asyncio.sleep(0)
770+
(Pdb) next
771+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
772+
-> await asyncio.sleep(0)
773+
(Pdb) next
774+
Internal StopIteration
775+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
776+
-> await test_coro()
777+
(Pdb) step
778+
--Return--
779+
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
780+
-> await test_coro()
781+
(Pdb) continue
782+
finished
783+
"""
784+
730785
def test_pdb_return_command_for_generator():
731786
"""Testing no unwindng stack on yield for generators
732787
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)