Search code examples
pythonpdb

pdb: "var = value" did not create the var in current function?


See following example:

$ cat -n foo.py
     1  def func():
     2      v = another_func()
     3      print v
     4
     5  func()
$ pdb foo.py
> /root/tmp/foo.py(1)<module>()
-> def func():
(Pdb) break 2
Breakpoint 1 at /root/tmp/foo.py:2
(Pdb) cont
> /root/tmp/foo.py(2)func()
-> v = another_func()
(Pdb) v = 123            <-- manually create the var
(Pdb) jump 3             <-- skip another_func()
> /root/tmp/foo.py(3)func()
-> print v
(Pdb) next
UnboundLocalError: "local variable 'v' referenced before assignment"
> /root/tmp/foo.py(3)func()
-> print v
(Pdb)

Where is the var v if it's not created in the func()?


Solution

  • This is a pdb bug, where changes to local variables are lost after a jump. It was reported years ago, but it never got fixed.