Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing Python code in the debugger

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.

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.

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.

like image 861
max Avatar asked Jan 28 '11 19:01

max


People also ask

How do I run a Python code in debugging?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

Which Python function allows us to easily swap out the debugger?

Python breakpoint() - Change Debugger Module We can use PYTHONBREAKPOINT environment variable to provide the debugger method to be called by breakpoint() function. This is very helpful because we can change the debugger module easily without making any code change.

How do I run a Python script in debug mode in Terminal?

Directly use command python pdg-debug.py without -m pdb to run the code. The program will automatically break at the position of pdb. set_trace() and enter the pdb debugging environment. You can use the command p variable to view the variables or use the command c to continue to run.

Is there a debugger for Python?

Python has a built-in debugger called pdb . It's a simple utility with a command line interface that does the main job. It has all the debugger features you'll need, but if you're looking to pimp it up a little, you can extend it using ipdb, which will provide the debugger with features from IPython.


2 Answers

Yes, pdb can do this. 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.)

like image 36
Lennart Regebro Avatar answered Sep 30 '22 07:09

Lennart Regebro


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.

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.

like image 117
9000 Avatar answered Sep 30 '22 07:09

9000