changing local variable values in python debugger

Jeremy Jones zanesdad at bellsouth.net
Fri Sep 17 09:03:25 EDT 2004


I've been googling for a couple of days regarding the Python debugger 
and haven't found an answer.  Is there any way to modify a local 
variable in the Python debugger (pdb)?  Given the following (totally 
contrived, for example only) Python file:

#!/usr/bin/env python

import pdb
f = 'aaa'

def b(f):
    print f


def main():
    global f
    for f in ['a', 'b', 'c']:
        if f == 'd':
            b(f)

if __name__ == "__main__":
    pdb.run('main()')

I can modify global f like this (so that method b() will be called):

[jmjones at qatestrunner debugger]$ python dbg_test.py
 > <string>(1)?()
(Pdb) s
--Call--
 > /home/jmjones/src/py_test/debugger/dbg_test.py(10)main()
-> def main():
(Pdb) l 1, 20
  1     #!/usr/bin/env python
  2
  3     import pdb
  4     f = 'aaa'
  5
  6     def b(f):
  7         print f
  8
  9
 10  -> def main():
 11         global f
 12         for f in ['a', 'b', 'c']:
 13             if f == 'd':
 14                 b(f)
 15
 16     if __name__ == "__main__":
 17         pdb.run('main()')
[EOF]
(Pdb) b 12
Breakpoint 1 at /home/jmjones/src/py_test/debugger/dbg_test.py:12
(Pdb) c
 > /home/jmjones/src/py_test/debugger/dbg_test.py(12)main()
-> for f in ['a', 'b', 'c']:
(Pdb) f
'aaa'
(Pdb) s
 > /home/jmjones/src/py_test/debugger/dbg_test.py(13)main()
-> if f == 'd':
(Pdb) f
'a'
(Pdb) global f; f = 'd'
(Pdb) f
'd'
(Pdb) s
 > /home/jmjones/src/py_test/debugger/dbg_test.py(14)main()
-> b(f)
(Pdb) s
--Call--
 > /home/jmjones/src/py_test/debugger/dbg_test.py(6)b()
-> def b(f):
(Pdb)


However, if I modify the source file so f is not a global, like this:

#!/usr/bin/env python

import pdb

def b(f):
    print f


def main():
    for f in ['a', 'b', 'c']:
        if f == 'd':
            b(f)

if __name__ == "__main__":
    pdb.run('main()')


How do I modify f?  Here's my attempt (and global doesn't work here and 
shouldn't since it's not global in this example):

[jmjones at qatestrunner debugger]$ python dbg_test.py
 > <string>(1)?()
(Pdb) s
--Call--
 > /home/jmjones/src/py_test/debugger/dbg_test.py(9)main()
-> def main():
(Pdb) l 1, 20
  1     #!/usr/bin/env python
  2
  3     import pdb
  4
  5     def b(f):
  6         print f
  7
  8
  9  -> def main():
 10         for f in ['a', 'b', 'c']:
 11             if f == 'd':
 12                 b(f)
 13
 14     if __name__ == "__main__":
 15         pdb.run('main()')
[EOF]
(Pdb) b 11
Breakpoint 1 at /home/jmjones/src/py_test/debugger/dbg_test.py:11
(Pdb) c
 > /home/jmjones/src/py_test/debugger/dbg_test.py(11)main()
-> if f == 'd':
(Pdb) f
'a'
(Pdb) f = 'd'
(Pdb) f
'a'
(Pdb) global f; f = 'd'
(Pdb) f
'a'
(Pdb)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040917/c45717b8/attachment.html>


More information about the Python-list mailing list