Modifying running code? [HACK ALERT]

Hans Nowak ivnowa at hvision.nl
Mon Dec 11 09:48:11 EST 2000


Bjoern Giesler wrote:
> 
> Hi,
> 
> I'm writing a kind of mini runtime environment that lets me debug / correct
> buggy Python code "on the fly". This is supposed to work as follows:
> 
> 1) an exception gets thrown by the buggy code and is caught by a toplevel
> exception handler
> 
> 2) the toplevel handler figures out the function that caused the exception
> 
> 3) the function source is loaded into an editor and can be corrected by the
> user
> 
> 4) if the code has been saved, it is recompiled and inserted instead of the
> original function
> 
> 5) the program is then resumed at the point where the original function had
> been called
> 
> Can this be done in Python at all? Can it be done in regular Python or only
> in Stackless Python (which supports continuations and would therefore, I
> believe, make step 5 easy)? Is the whole thing complete bull doodoo? Any
> hints or suggestions?
> 
> Thanks in advance for any input,
>                 --Björn

This sure sounds like an interesting task... I'm not sure if it's
possible... the reference manual says,

"Python uses the ``termination'' model of error handling: an exception
handler can find out what happened and continue execution at an outer
level, but it cannot repair the cause of the error and retry the failing
operation (except by re-entering the offending piece of code from the
top)."

I think/hope there are ways around this, like wrapping every function
that can be debugged in a wrapper class that catches the exception, and
can recall the wrapped function... not very convenient if you have a lot
of functions like this... if it's possible at all. Editing the code will
be a problem too; you will need to point the program to a string or text
file that holds the function code, unless you're planning to edit
bytecode. :(

--Hans Nowak



More information about the Python-list mailing list