Dannid
Dannid

Reputation: 1697

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

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')

Breakpoint:

b set.update
b ss.update

Errors:

The specified object 'ss.update' is not a function
or was not found along sys.path.

The specified object 'set.update' is not a function
or was not found along sys.path.

(Note, I also tried with the parentheses at the end, e.g., b set.update(), but still got the error. I didn't print all the permutations of errors.)

Upvotes: 5

Views: 3212

Answers (1)

Dannid
Dannid

Reputation: 1697

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.

Upvotes: 2

Related Questions