1
1
"""Debugger basics"""
2
2
3
+ # This is originally from cpython 3.10: https://raw.githubusercontent.com/python/cpython/3.10/Lib/bdb.py
4
+ # Patches for micropython have been commented as such.
5
+
3
6
import fnmatch
4
7
import sys
5
8
import os
6
- from inspect import CO_GENERATOR , CO_COROUTINE , CO_ASYNC_GENERATOR
9
+
10
+ ## MPY: no inspect module avaialble
11
+ # from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR
7
12
8
13
__all__ = ["BdbQuit" , "Bdb" , "Breakpoint" ]
9
14
10
- GENERATOR_AND_COROUTINE_FLAGS = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR
15
+ ## MPY: These flags currently don't exist
16
+ # GENERATOR_AND_COROUTINE_FLAGS = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR
11
17
12
18
13
19
class BdbQuit (Exception ):
@@ -115,6 +121,12 @@ def dispatch_line(self, frame):
115
121
if self .quitting : raise BdbQuit
116
122
return self .trace_dispatch
117
123
124
+ def is_coroutine (self , frame ):
125
+ ## MPY: co_flags attrib not available, compatible method of detecting coroutine TBD
126
+ # return frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS
127
+ return False
128
+
129
+
118
130
def dispatch_call (self , frame , arg ):
119
131
"""Invoke user function and return trace function for call event.
120
132
@@ -131,7 +143,7 @@ def dispatch_call(self, frame, arg):
131
143
# No need to trace this function
132
144
return # None
133
145
# Ignore call events in generator except when stepping.
134
- if self .stopframe and frame . f_code . co_flags & GENERATOR_AND_COROUTINE_FLAGS :
146
+ if self .stopframe and self . is_coroutine ( frame ) :
135
147
return self .trace_dispatch
136
148
self .user_call (frame , arg )
137
149
if self .quitting : raise BdbQuit
@@ -146,7 +158,7 @@ def dispatch_return(self, frame, arg):
146
158
"""
147
159
if self .stop_here (frame ) or frame == self .returnframe :
148
160
# Ignore return events in generator except when stepping.
149
- if self .stopframe and frame . f_code . co_flags & GENERATOR_AND_COROUTINE_FLAGS :
161
+ if self .stopframe and self . is_coroutine ( frame ) :
150
162
return self .trace_dispatch
151
163
try :
152
164
self .frame_returning = frame
@@ -170,7 +182,7 @@ def dispatch_exception(self, frame, arg):
170
182
# When stepping with next/until/return in a generator frame, skip
171
183
# the internal StopIteration exception (with no traceback)
172
184
# triggered by a subiterator run with the 'yield from' statement.
173
- if not (frame . f_code . co_flags & GENERATOR_AND_COROUTINE_FLAGS
185
+ if not (self . is_coroutine ( frame )
174
186
and arg [0 ] is StopIteration and arg [2 ] is None ):
175
187
self .user_exception (frame , arg )
176
188
if self .quitting : raise BdbQuit
@@ -179,7 +191,7 @@ def dispatch_exception(self, frame, arg):
179
191
# next/until command at the last statement in the generator before the
180
192
# exception.
181
193
elif (self .stopframe and frame is not self .stopframe
182
- and self .stopframe . f_code . co_flags & GENERATOR_AND_COROUTINE_FLAGS
194
+ and self .is_coroutine ( self . stopframe )
183
195
and arg [0 ] in (StopIteration , GeneratorExit )):
184
196
self .user_exception (frame , arg )
185
197
if self .quitting : raise BdbQuit
@@ -315,7 +327,7 @@ def set_next(self, frame):
315
327
316
328
def set_return (self , frame ):
317
329
"""Stop when returning from the given frame."""
318
- if frame . f_code . co_flags & GENERATOR_AND_COROUTINE_FLAGS :
330
+ if self . is_coroutine ( frame ) :
319
331
self ._set_stopinfo (frame , None , - 1 )
320
332
else :
321
333
self ._set_stopinfo (frame .f_back , frame )
@@ -557,7 +569,8 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
557
569
line of code (if it exists).
558
570
559
571
"""
560
- import linecache , reprlib
572
+ ## MPY: reprlib not yet available
573
+ import linecache #, reprlib
561
574
frame , lineno = frame_lineno
562
575
filename = self .canonic (frame .f_code .co_filename )
563
576
s = '%s(%r)' % (filename , lineno )
@@ -569,7 +582,7 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
569
582
if '__return__' in frame .f_locals :
570
583
rv = frame .f_locals ['__return__' ]
571
584
s += '->'
572
- s += reprlib . repr (rv )
585
+ s += repr (rv )
573
586
line = linecache .getline (filename , lineno , frame .f_globals )
574
587
if line :
575
588
s += lprefix + line .strip ()
@@ -628,7 +641,7 @@ def runctx(self, cmd, globals, locals):
628
641
629
642
# This method is more useful to debug a single function call.
630
643
631
- def runcall (self , func , / , * args , ** kwds ):
644
+ def runcall (self , func , * args , ** kwds ):
632
645
"""Debug a single function call.
633
646
634
647
Return the result of the function call.
0 commit comments