I asked about bdb in particular because IDLE has a visual debugger based on a subclass Idb of bdb.Bdb. I realize now that Ibd could be rewritten to use this PEP without touching bdb.
From the PEP: " To insert a breakpoint at a given line, the matching instruction offsets should be found from
code.co_lines()."
`co_lines is not indexed and is not mentioned in the code object doc. From experiment, co_lines yields (start, stop, lineno) tuples, where start and stop are byte offsets. I presume the needed instruction offsets for each breakpoint line is the index of the first tuple with that line. Is the following correct?
def co_offsets(code):
line = 1
lines = [None] # Dummy since lines are 1 based.
for codeoffset, (start, stop, lineno) in enumerate(code.co_lines()):
if lineno == line:
lines.append(codeoffset)
line += 1
return lines
def f(x):
x += 3
return x
lines = co_offsets(f.__code__)
# lines = [None, 0, 1, 5] where 1, 5 are the offsets of initial load x instructions for lines 2 and 3.
# lines[3] (=5) would be instruction offset for breakpoint on the return line (line 3).