gpt4 book ai didi

python - pdb 中是否可能存在嵌套断点——如果不能,是什么阻止了它们?

转载 作者:行者123 更新时间:2023-11-28 19:13:44 29 4
gpt4 key购买 nike

关注 tutorial on Python's debugger ,我使用了 pdb.set_trace() 来中断示例代码。它有效,但如果您在交互式提示符下并想要另一个嵌套断点怎么办?

(Pdb) def test(): pdb.set_trace(); print "don't print this yet"
(Pdb) test()
don't print this yet

它并没有停止。调试器从根本上说是“一个深度”吗?例如这是 Python 的钩子(Hook)的限制,还是 pdb 没有选择做的事情?

最佳答案

is this is a limitation of Python's hooks, or just something pdb doesn't choose to do?

这似乎是钩子(Hook)的限制。

我做了一个测试,看看什么被调用了,什么没有调用(将打印语句放在 /usr/lib/python2.7/bdb.py 中)

快速检查在 pdb.py 中找到 set_trace :

def set_trace():
Pdb().set_trace(sys._getframe().f_back)

bdb.py 中调用 set_trace

def set_trace(self, frame=None):
"""Start debugging from `frame`.
If frame is not specified, debugging starts from caller's frame.
"""
if frame is None:
frame = sys._getframe().f_back
self.reset()
while frame:
frame.f_trace = self.trace_dispatch
self.botframe = frame
frame = frame.f_back
self.set_step()
sys.settrace(self.trace_dispatch)

这会设置一个回调到 trace_dispatch,also in bdb.py . sys.settrace 代码本身可能在 threading.py 中:

def settrace(func):
global _trace_hook
_trace_hook = func

GitHub 搜索找不到更多关于 _trace_hook 的引用,所以大概是在某处的 C 代码中神奇地提取了它。

当调用 test() 时,结果是调用了 sys.settrace()...但是随后并没有调用 trace_dispatch()。

关于python - pdb 中是否可能存在嵌套断点——如果不能,是什么阻止了它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270745/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com