how to modify code while debugging it without having to stop and then restart debugger

Neil Hodgson nyamatongwe+thunder at gmail.com
Wed Nov 9 17:53:53 EST 2005


Steven D'Aprano:

> You write a function:
> 
> def myfunct(s):
>     # input arg s is a string
>     foo = s*3
>     bar = s.upper() + foo   # LINE 2
>     blob = foo.lower() + bar
>     return blob
> 
> You enter the debugger and single-step to the marked line LINE 2. Then you
> edit the code to this:
> 
> def myfunct(n):
>     # input arg n is an int
>     foo = n + 1
>     bar = foo*2   # LINE 2
>     blob = foo**bar
>     return blob
> 
> What should Python do when you step the debugger, 

    Python should throw the power switch and sulk for at least an hour.

 > and why is it useful?

    Teaches the user who's boss.

    Debug time code modification is useful to me in C++ for two reasons. 
The first is where there is a simple bug: step, step, step, aah!, 
fiddle, step, works! The second is where you want to perturb the code to 
produce some unusual situation within a test run: how would the calling 
code cope if this code allowed a duplicate element through? fiddle, 
step, step, crash! Mmm, that looks like the fault report, maybe there is 
another way that duplicates are possible. The first type of change 
become permanent parts of the code while the second type are ephemeral.

    There are limitations to the technology: if you add too much code it 
won't fit in the allocation (which has been padded a bit for debugging 
but not much) or the function is being reentered. I'm not sure about all 
the limitations and they change between releases but it probably fails 
about one time in ten for me. This doesn't stop the session, just leaves 
your change unapplied.

    Neil



More information about the Python-list mailing list