Skip to content

Commit 3cfb84c

Browse files
authored
[3.6] bpo-32650 Add support for async generators and more test for coroutines in pdb (GH-5403). (#5411)
(cherry picked from commit c7ab581)
1 parent 543ec00 commit 3cfb84c

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

‎Lib/bdb.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
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+
12+
1013
class BdbQuit(Exception):
1114
"""Exception to give up completely."""
1215

@@ -77,7 +80,7 @@ def dispatch_call(self, frame, arg):
7780
# No need to trace this function
7881
return # None
7982
# Ignore call events in generator except when stepping.
80-
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
83+
if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
8184
return self.trace_dispatch
8285
self.user_call(frame, arg)
8386
if self.quitting: raise BdbQuit
@@ -86,7 +89,7 @@ def dispatch_call(self, frame, arg):
8689
def dispatch_return(self, frame, arg):
8790
if self.stop_here(frame) or frame == self.returnframe:
8891
# Ignore return events in generator except when stepping.
89-
if self.stopframe and frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
92+
if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
9093
return self.trace_dispatch
9194
try:
9295
self.frame_returning = frame
@@ -104,7 +107,7 @@ def dispatch_exception(self, frame, arg):
104107
# When stepping with next/until/return in a generator frame, skip
105108
# the internal StopIteration exception (with no traceback)
106109
# triggered by a subiterator run with the 'yield from' statement.
107-
if not (frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
110+
if not (frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS
108111
and arg[0] is StopIteration and arg[2] is None):
109112
self.user_exception(frame, arg)
110113
if self.quitting: raise BdbQuit
@@ -113,7 +116,7 @@ def dispatch_exception(self, frame, arg):
113116
# next/until command at the last statement in the generator before the
114117
# exception.
115118
elif (self.stopframe and frame is not self.stopframe
116-
and self.stopframe.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE)
119+
and self.stopframe.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS
117120
and arg[0] in (StopIteration, GeneratorExit)):
118121
self.user_exception(frame, arg)
119122
if self.quitting: raise BdbQuit
@@ -230,7 +233,7 @@ def set_next(self, frame):
230233

231234
def set_return(self, frame):
232235
"""Stop when returning from the given frame."""
233-
if frame.f_code.co_flags & (CO_GENERATOR | CO_COROUTINE):
236+
if frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
234237
self._set_stopinfo(frame, None, -1)
235238
else:
236239
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
@@ -739,7 +739,7 @@ def test_pdb_next_command_for_coroutine():
739739
... await test_coro()
740740
741741
>>> def test_function():
742-
... loop = asyncio.get_event_loop()
742+
... loop = asyncio.new_event_loop()
743743
... loop.run_until_complete(test_main())
744744
... loop.close()
745745
... print("finished")
@@ -834,6 +834,47 @@ def test_pdb_return_command_for_generator():
834834
finished
835835
"""
836836

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

0 commit comments

Comments
 (0)