changing local variable values in python debugger

Richie Hindle richie at entrian.com
Fri Sep 17 09:59:15 EDT 2004


[Richie]
> Setting the value of a local variable works, but *examining* the value resets
> it to whatever it was when you arrived at the current pdb prompt.  It's a bug,
> either in pdb or in Python's core debugger support depending on how you look
> at it.  I've been meaning to try to fix this for ages, but haven't found the
> time to figure out how to do so.

For those interested in the details, here's what I can remember from the last
time I looked at it: when you access frame.f_locals, Python calls
PyFrame_FastToLocals to go from the locals tuple to a dictionary.  When you
leave the context of a trace function, it calls PyFrame_LocalsToFast to write
back any changes to the dictionary into the tuple.  But the act of accessing a
value via frame.f_locals within a call to the trace function will overwrite
any changes by calling PyFrame_FastToLocals a second time, repopulating the
potentially-modified f_locals dictionary from the original locals tuple.

Either pdb needs to take a copy of f_locals at the start of a trace function
and never access frame.f_locals again, ensuring that PyFrame_FastToLocals only
ever gets called once, or Python itself needs to make sure it doesn't call
PyFrame_FastToLocals more than once per call to the trace function.  The
former is easier, but only fixes pdb.  I suspect the latter is hard, but it
would fix all the Python debuggers in one go.

All of this is wrapped in a big caveat: it's all dredged out of years-old
memory and could be completely wrong, either because my memory is poor (and
believe me, it is 8-) or because things have changed since I last looked at
all this.

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list