Home > Net >  python pdb: conditional breakpoint
python pdb: conditional breakpoint

Time:09-10

I want to set breakpoints in different places of my program. However they are fired if I pass a specific argument to my program, I called it STOP_LEVEL

STOP_LEVEL = 0
def mbp(sl=-1):
    if sl == STOP_LEVEL:
        # I would like to print which function called this
        mlog.info("break point at %s, %s", ...)
        breakpoint()

Then I can create a breakpoint like

   mbp(2)

and if I set stop_level to 2, it's fired.

First, I would like to know are there other standard methods for such functionality? Second, I would like to know where from my mbp function was called, I mean the caller function, filename and line number. How can I include them in my function as logging information?

CodePudding user response:

There isn't built in functionality like that.

The only way to find where your application was called from would be to examine the stack or to pop from your function. After popping the stack (returning from your function) you'll be at the location in your code it was called from.

To examine the stack you can use inspect.stack()

In pdb you can run the command where

CodePudding user response:

I ended up to this function:

import sys
STOP_LEVEL = 0
def mbp(sl=-1):
    if sl == STOP_LEVEL:
        fname = sys._getframe().f_back.f_code.co_name
        line = sys._getframe().f_back.f_lineno
        mlog.info("break point at %s line %s",fname, line)
        breakpoint()
  • Related