Skip to content

Commit c7ab581

Browse files
pablogsal1st1
authored andcommitted
bpo-32650 Add support for async generators and more test for coroutines in pdb (python#5403)
1 parent 4687702 commit c7ab581

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

Lib/bdb.py

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

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

10+
GENERATOR_AND_COROUTINE_FLAGS = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR
11+
1012

1113
class BdbQuit(Exception):
1214
"""Exception to give up completely."""
@@ -127,7 +129,7 @@ def dispatch_call(self, frame, arg):
127129
# No need to trace this function
128130
return # None
129131
# Ignore call events in generator except when stepping.
130-
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
132+
if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
131133
return self.trace_dispatch
132134
self.user_call(frame, arg)
133135
if self.quitting: raise BdbQuit
@@ -142,7 +144,7 @@ def dispatch_return(self, frame, arg):
142144
"""
143145
if self.stop_here(frame) or frame == self.returnframe:
144146
# Ignore return events in generator except when stepping.
145-
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
147+
if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
146148
return self.trace_dispatch
147149
try:
148150
self.frame_returning = frame
@@ -166,7 +168,7 @@ def dispatch_exception(self, frame, arg):
166168
# When stepping with next/until/return in a generator frame, skip
167169
# the internal StopIteration exception (with no traceback)
168170
# triggered by a subiterator run with the 'yield from' statement.
169-
if not (frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
171+
if not (frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS
170172
and arg[0] is StopIteration and arg[2] is None):
171173
self.user_exception(frame, arg)
172174
if self.quitting: raise BdbQuit
@@ -175,7 +177,7 @@ def dispatch_exception(self, frame, arg):
175177
# next/until command at the last statement in the generator before the
176178
# exception.
177179
elif (self.stopframe and frame is not self.stopframe
178-
and self.stopframe.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
180+
and self.stopframe.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS
179181
and arg[0] in (StopIteration, GeneratorExit)):
180182
self.user_exception(frame, arg)
181183
if self.quitting: raise BdbQuit
@@ -309,7 +311,7 @@ def set_next(self, frame):
309311

310312
def set_return(self, frame):
311313
"""Stop when returning from the given frame."""
312-
if frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
314+
if frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
313315
self._set_stopinfo(frame, None, -1)
314316
else:
315317
self._set_stopinfo(frame.f_back, frame)

Lib/test/test_pdb.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def test_pdb_next_command_for_coroutine():
742742
... await test_coro()
743743
744744
>>> def test_function():
745-
... loop = asyncio.get_event_loop()
745+
... loop = asyncio.new_event_loop()
746746
... loop.run_until_complete(test_main())
747747
... loop.close()
748748
... print("finished")
@@ -837,6 +837,47 @@ def test_pdb_return_command_for_generator():
837837
finished
838838
"""
839839

840+
def test_pdb_return_command_for_coroutine():
841+
"""Testing no unwindng stack on yield for coroutines for "return" command
842+
843+
>>> import asyncio
844+
845+
>>> async def test_coro():
846+
... await asyncio.sleep(0)
847+
... await asyncio.sleep(0)
848+
... await asyncio.sleep(0)
849+
850+
>>> async def test_main():
851+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
852+
... await test_coro()
853+
854+
>>> def test_function():
855+
... loop = asyncio.new_event_loop()
856+
... loop.run_until_complete(test_main())
857+
... loop.close()
858+
... print("finished")
859+
860+
>>> with PdbTestInput(['step',
861+
... 'step',
862+
... 'next',
863+
... 'continue']):
864+
... test_function()
865+
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main()
866+
-> await test_coro()
867+
(Pdb) step
868+
--Call--
869+
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro()
870+
-> async def test_coro():
871+
(Pdb) step
872+
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro()
873+
-> await asyncio.sleep(0)
874+
(Pdb) next
875+
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro()
876+
-> await asyncio.sleep(0)
877+
(Pdb) continue
878+
finished
879+
"""
880+
840881
def test_pdb_until_command_for_generator():
841882
"""Testing no unwindng stack on yield for generators
842883
for "until" command if target breakpoing is not reached

0 commit comments

Comments
 (0)