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

71 views Asked by At

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()?

1

There are 1 answers

0
user2357112 On BEST ANSWER

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.