Jumping the Stacks Shark
Another non-flattering progress report for me today. I didn't realize that "breakpoint()" automatically reports its line number and activates Python's built-in debugger, PDB.
So instead, I dove into the world of the "inspect" module and of the "sys" module; both of which allow you to get the "frame" info of the code you are currently in, or one frame back in the stack, or even two frames back, or more if you need. That indeed constitutes jumping the shark.
So here is some of the jumper code I added to my teach-myself program:
import inspect
import sys
import os
def brk(mssg):
print(f'\n(X): We hit a debug brk(mssg) point at {gotten_line_numb_2back()}')
print(f'(Y): PDB debugger commands: l=LIST, c=CONTINUE, pp var= PrettyPrint the specific var')
print(f'(Z): Additional message: {mssg}\n')
breakpoint()
And next below is the function that produces the "gotten" line number, two frames back:def gotten_line_numb_2back(): #-- inspect 2 back !!!!
frame_current = inspect.currentframe()
frame_info_2priors_back = sys._getframe(2) #--get frame info from caller's caller
frame_info_current = inspect.getframeinfo(frame_current)
return f'{frame_info_2priors_back.f_lineno}' #--Google "python inspect two frames back"
brk('used sys.getframe(2) and (dot)f_lineno to corectly point to line of brk() !!!!')
For completeness, here is my code if you want to get the frame info just one frame back:def gotten_line_numb_1back():
frame_current = inspect.currentframe()
frame_info_prior = inspect.getframeinfo(frame_current.f_back) #--don't get from execution of this func, get from caller
frame_info_current = inspect.getframeinfo(frame_current)
return f'{frame_info_prior.lineno}'
print(f'\n(d): The gotten_line_numb func returned the following line number: \t{gotten_line_numb_1back()}')
So here, I probably dove too deep into the swamp waters. This is all part of the learning process.More to Explore
Python breakpoint() --YT search results
Python breakpoint() --YT search results

Comments
Post a Comment