如何使pdb识别两次运行之间的源已更改? [英] How to make pdb recognize that the source has changed between runs?

查看:83
本文介绍了如何使pdb识别两次运行之间的源已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,pdb无法识别源代码在运行之间是否已更改。也就是说,如果我正在调试,请注意一个错误,修复该错误,然后在pdb中重新运行该程序(即不退出pdb),pdb将不会重新编译代码。即使pdb列出了新的源代码,我仍将调试旧版本的代码。



因此,当源更改时,pdb不会更新已编译的代码吗? ?如果没有,有没有办法做到这一点?我希望能够停留在单个pdb会话中,以保持断点之类。



FWIW,gdb会在调试程序的下面更改时通知您它,尽管仅在该程序重新启动时才可以。这是我要在pdb中复制的行为。

解决方案

在pdb中重新运行程序是什么意思? 如果您导入了模块,则Python不会重新读取它,除非您明确要求这样做,即使用 reload(module)。但是, reload 远非防弹(请参阅

Python代码重新加载有很多陷阱。为了更稳健地解决问题,您可以使用一个类包装pdb,该类将断点信息记录到磁盘上的文件中,例如,然后在命令中播放它们。



(对不起,请忽略此答案的第一个版本;该时间太早了,我没有足够仔细地阅读您的问题。)


From what I can tell, pdb does not recognize when the source code has changed between "runs". That is, if I'm debugging, notice a bug, fix that bug, and rerun the program in pdb (i.e. without exiting pdb), pdb will not recompile the code. I'll still be debugging the old version of the code, even if pdb lists the new source code.

So, does pdb not update the compiled code as the source changes? If not, is there a way to make it do so? I'd like to be able to stay in a single pdb session in order to keep my breakpoints and such.

FWIW, gdb will notice when the program it's debugging changes underneath it, though only on a restart of that program. This is the behavior I'm trying to replicate in pdb.

解决方案

What do you mean by "rerun the program in pdb?" If you've imported a module, Python won't reread it unless you explicitly ask to do so, i.e. with reload(module). However, reload is far from bulletproof (see xreload for another strategy).

There are plenty of pitfalls in Python code reloading. To more robustly solve your problem, you could wrap pdb with a class that records your breakpoint info to a file on disk, for example, and plays them back on command.

(Sorry, ignore the first version of this answer; it's early and I didn't read your question carefully enough.)

这篇关于如何使pdb识别两次运行之间的源已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆