[Python-Dev] Changing a value in a frame (for a debugger)

Fabio Zadrozny fabiofz at gmail.com
Wed Feb 7 11:11:36 CET 2007


On 2/7/07, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>
> Fabio Zadrozny wrote:
>
> > frame = findFrame(thread_id, frame_id)
> > exec '%s=%s' % (attr, expression) in frame.f_globals, frame.f_locals
>
> The locals of a function are actually stored in an array.
> When you access them as a dict using locals(), all you
> get is a dict containing a copy of their current values.
> Modifying that dict doesn't affect the underlying array.
>
> It seems that reading the f_locals of a frame does the
> same thing. To modify the locals, you would need to poke
> values into the original array -- but it doesn't seem
> to be exposed to Python.
>
> So it looks like you're out of luck.
>

Would it be ok to add a feature request for that? I initially thought it was
completely read-only, but I find it strange that it affects the topmost
frame correctly (so, it seems that even though I get a copy, when I alter
that copy on the topmost frame it affects it correctly)... anyone has a clue
why that happens?

It seems to affect pdb too...

Consider the code:
if __name__ == '__main__':

    def call1():
        v_on_frame1 = 10
        print 'v_on_frame1', v_on_frame1

    def call0():
        import pdb;pdb.set_trace()
        v_on_frame0 = 10
        call1()
        print 'v_on_frame0', v_on_frame0

    call0()


#when modifying in the current frame
> x:\scbr15\source\python\tests_base\empty_test.py(9)call0()
-> v_on_frame0 = 10
(Pdb) n
> x:\scbr15\source\python\tests_base\empty_test.py(10)call0()
-> call1()
(Pdb) v_on_frame0 = 40
(Pdb) c
v_on_frame1 10
v_on_frame0 40



#when modifying an upper frame it does not work
> x:\scbr15\source\python\tests_base\empty_test.py(9)call0()
-> v_on_frame0 = 10
(Pdb) n
> x:\scbr15\source\python\tests_base\empty_test.py(10)call0()
-> call1()
(Pdb) s
--Call--
> x:\scbr15\source\python\tests_base\empty_test.py(3)call1()
-> def call1():
(Pdb) n
> x:\scbr15\source\python\tests_base\empty_test.py(4)call1()
-> v_on_frame1 = 10
(Pdb) u
> x:\scbr15\source\python\tests_base\empty_test.py(10)call0()
-> call1()
(Pdb) v_on_frame0 = 40
(Pdb) d
> x:\scbr15\source\python\tests_base\empty_test.py(4)call1()
-> v_on_frame1 = 10
(Pdb) c
v_on_frame1 10
v_on_frame0 10
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070207/89e064ec/attachment.html 


More information about the Python-Dev mailing list