在调试器中更改Python代码 [英] changing Python code in the debugger

查看:138
本文介绍了在调试器中更改Python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何调试器允许在调试时更改Python代码?

Is there any debugger that allows Python code to be changed while debugging?

换句话说:发生运行时异常,调试器停止,我可以更改代码我喜欢的方式,并告诉程序继续。

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.

另一方面,我也对是否从理论上讲,可以允许对Python代码进行更改而不会遇到这些问题:即,以某种方式更新对已更改对象的所有引用等。我几乎可以确定第二个问题的答案是否定的,但是如果我错了,我想知道。

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: i.e., 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.

编辑:如果我的目标(发生异常时以交互方式更改代码,然后继续执行)是可以实现的,而无需调试器-也会很好。我不需要使用调试器。

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.

推荐答案

由于您可以按照需要的方式更改常规类的内容时间,无需更新对对象的引用:只需使用新方法和其他属性更新类的 __ dict __

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.

问题出在函数引用上:您不能在不更改其标识的情况下更新函数。您可以使用始终按名称查找实际功能的代理功能,并且随时可以更改实际功能。否则,您需要对代码进行计划,以使其不会长时间存储函数引用;一旦函数更新,它将很快按名称查找,但是传递到某处的旧引用将继续执行更长的时间。

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.

这种修补在如果您想在长时间运行的系统上进行升级而又不会造成严重的停机,请执行以下操作:将其暂停片刻,以一致且不间断地升级几个类和功能。 AFAIK Erlang以类似的方式进行即时更新。

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.

这篇关于在调试器中更改Python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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