In pdb (python debugger), can I set a breakpoint on a builtin function?

后端 未结 1 1442
野的像风
野的像风 2021-01-21 03:32

I want to set a breakpoint on the set.update() function, but when I try, I get an error message.

Example:

ss= set()
ss.update(\'a\')

Br

相关标签:
1条回答
  • 2021-01-21 03:55

    Thanks! Using @avasal's answer and Doug Hellmann's pdb webpage, I came up with this:

    Since I was trying to catch set.update, I had to edit the sets.py file, but that wasn't enough, since python was using the builtin set class rather than the one I edited. So I overwrote the builtin sets class:

    import sets
    locals()['__builtins__'].set=sets.Set
    

    Then I could set conditional break points in the debugger:

    b set.update, iterable=='a' #successful
    b set.update, iterable=='b' #won't stop for ss.update('a')
    

    My entire example file looks like this:

    import pdb
    import sets
    locals()['__builtins__'].set=sets.Set
    
    pdb.set_trace()
    ss = set()
    ss.update('a')
    
    print "goodbye cruel world"
    

    Then at the debugger prompt, enter this:

    b set.update, iterable=='a'
    

    Hope this helps others too.

    0 讨论(0)
提交回复
热议问题