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

半城伤御伤魂 提交于 2019-12-01 22:08:39

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13225913/in-pdb-python-debugger-can-i-set-a-breakpoint-on-a-builtin-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!