简体   繁体   English

在调试器中更改 Python 代码

[英]changing Python code in the debugger

Is there any debugger that allows Python code to be changed while debugging?是否有任何调试器允许在调试时更改 Python 代码?

In other words: run-time exception occurs, debugger stops, I change the code any way I like, and tell the program to continue.换句话说:运行时异常发生,调试器停止,我以任何我喜欢的方式更改代码,并告诉程序继续。

I am aware of the problems with this approach, such that references to functions would still point to the old definitions if I redefine the function on the fly, and so on.我知道这种方法的问题,如果我动态重新定义函数,对函数的引用仍然指向旧定义,依此类推。 I am ok with that, since I just want to be able to make small fixes in very simple circumstances.我对此没有意见,因为我只想能够在非常简单的情况下进行小的修复。

On the other hand, I'm also interested in whether it's theoretically possible to allow changes to Python code without running into these problems: ie, somehow update all the references to the objects that changed, etc. I'm nearly sure the answer to the second question is no, but if I'm wrong, I'd like to know.另一方面,我也对理论上是否可以允许更改 Python 代码而不会遇到这些问题感兴趣:即,以某种方式更新对已更改对象的所有引用等。我几乎可以肯定答案是第二个问题是否定的,但如果我错了,我想知道。

EDIT: If my goal (changing the code interactively when an exception occurred, and then continuing execution), is achievable without a debugger - that would be good as well.编辑:如果我的目标(在发生异常时以交互方式更改代码,然后继续执行)可以在没有调试器的情况下实现 - 那也很好。 I don't need to use the debugger.我不需要使用调试器。

Since you can change the contents of regular classes the way you want at any time, there's no need to update references to objects: you just update class's __dict__ with new methods and other attributes.由于您可以随时以您想要的方式更改常规类的内容,因此无需更新对对象的引用:您只需使用新方法和其他属性更新类的__dict__

The problem is with references to functions: you can't update a function without changing its identity.问题在于对函数的引用:您无法在不更改函数标识的情况下更新函数。 You can use proxy functions that always look up real functions by name, and you change real functions at any moment.您可以使用始终按名称查找实际函数的代理函数,并且您可以随时更改实际函数。 Else, you plan your code so that it doesn't store function references for long;否则,您计划您的代码,使其不会长时间存储函数引用; once a function is updated, it will soon be looked up by name, but old references passed somewhere will continue to execute for a bit longer.一旦一个函数被更新,它很快就会被名称查找,但是在某处传递的旧引用将继续执行一段时间。

Such patching would make sense on a long-running system when you want to upgrade it without serious downtime: you pause it for a moment to upgrade several classes and functions consistently and un-pause.当您希望在不严重停机的情况下升级系统时,这种修补对于长期运行的系统是有意义的:您暂停它一会儿以一致地升级多个类和函数并取消暂停。 AFAIK Erlang does its on-the-fly updates in a similar way. AFAIK Erlang 以类似的方式进行即时更新。

Yes, pdb can do this.是的,pdb 可以做到这一点。 Although you have to do the editing in another editor, and the changes will be ignored until you restart.尽管您必须在另一个编辑器中进行编辑,并且更改将被忽略,直到您重新启动。

But since all you want to do is small changes, this is not a problem.但是由于您要做的只是小的更改,因此这不是问题。 But you can't change the running code (except with reload, see below), as changing the code would mean the code and the state is out of sync.但是你不能改变正在运行的代码(除了重新加载,见下文),因为改变代码意味着代码和状态不同步。

What you can do with a debugger is test the change you want to do.您可以使用调试器做的是测试您想要做的更改。 You can paste in the code as you want to change it, and thus test that it is correct without rerunning the whole program.您可以根据需要粘贴代码以进行更改,从而无需重新运行整个程序即可测试它是否正确。 But in that case you don't edit the file.但在这种情况下,您不要编辑文件。

(In some specific cases you might be able to get away with not restarting by careful use of "reload()", but that's probably not worth the effort.) (在某些特定情况下,您可能可以通过谨慎使用“reload()”来避免重新启动,但这可能不值得付出努力。)

You can update the code in a running python process with xreload:您可以使用 xreload 更新正在运行的 Python 进程中的代码:

http://svn.python.org/projects/sandbox/trunk/xreload/xreload.py http://svn.python.org/projects/sandbox/trunk/xreload/xreload.py

There are many limitations, they are listed at the top of the file.有很多限制,它们列在文件的顶部。 I'm not sure if this handles the case you want though - do you want to actually prevent the exception from propagating?我不确定这是否能处理你想要的情况 - 你真的想阻止异常传播吗? That requires more than updating the running program.这需要的不仅仅是更新正在运行的程序。

whether it's theoretically possible to allow changes to Python code理论上是否可以允许更改 Python 代码

Yes.是的。 It's possible to update a code object "on the fly".可以“即时”更新代码对象。 Nothing in the Python VM seems to prevent this. Python VM 中的任何内容似乎都无法阻止这一点。 There's not an ongoing verification of the checksums or anything.没有对校验和或任何东西的持续验证。

somehow update all the references to the objects that changed,以某种方式更新对已更改对象的所有引用,

This doesn't even make sense.这甚至没有意义。 Once the class-level method definition is changed, "updating" all the references isn't even necessary or sensible.一旦更改了类级方法定义,“更新”所有引用甚至没有必要或明智。 The code is changed in the one and only place it exists.代码在唯一的地方发生了变化。

This is why debuggers are bad.这就是调试器不好的原因。 They seem to lead to murky, unclear thinking.它们似乎会导致模糊不清的想法。 Thinking too much about the debugger means thinking too little about the real problem at hand.对调试器考虑太多意味着对手头的真正问题考虑太少。

TDD is a far, far better investment. TDD 是一项远好得多的投资。 Small, manageable unit tests run quickly and provide enduring evidence that things work.小型、易于管理的单元测试运行速度很快,并提供持久的证据证明事情有效。

http://en.wikipedia.org/wiki/Test-driven_development http://en.wikipedia.org/wiki/Test-driven_development

name = (input("Enter Name: ")) name = (input("请输入姓名:"))

if name == "e": print ("This word has a letter e") if name == "e": print("这个词有一个字母e")

else : print("This word has no letter e") else : print("这个词没有字母e")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM