Chun-Yu Tseng added the comment:

What Antony Lee mentioned are two different cases. 

The former is what PDB should behave because it is not reasonable to inspect a 
variable does not exist in the current frame. If you want to do so, you have to 
reference the variable `x` as a closure inside inner function `g` in your 
source code before running PDB.

The latter is same as what Jesús Gómez confirmed. It's a problem about creating 
correct closures in an interaction prompt of PDB. It can be found in almost 
every versions of Python. The following are several forms of the problem:


# (1) raise exception 

  1      def f():
  2          x = 2
  3  ->        import pdb; pdb.set_trace();
(Pdb) (lambda: x)()
*** NameError: name 'x' is not defined


# (2) no exception, but get the wrong value

  1      x = 100
  2      def f():
  3          x = 2
  4  ->        import pdb; pdb.set_trace();
  5      f()
(Pdb) x
2
(Pdb) (lambda: x)()
100


# (3) similar to (1), but this one usually makes me upset if I forget the cause 
of the problem

(Pdb) ll
  1      def f():
  2  ->        import pdb; pdb.set_trace();
(Pdb) x = 5
(Pdb) [i for i in range(10) if (i % x) == 0]
*** NameError: name 'x' is not defined
(Pdb) x
5


It's easy to alleviate the problem by using `interact` command to start an 
interactive interpreter.

  1      def f():
  2  ->        import pdb; pdb.set_trace();
(Pdb) x = 5
(Pdb) interact
*interactive*
>>> [i for i in range(10) if (i % x) == 0]
[0, 5]

Now the behavior looks right.

However, the problem still exists for those PDB users don't know how to pass 
it, and it's quite inconvenient to start an interactive interpreter every time. 
Maybe it's worth to keep the behavior of PDB consistent and expectable just 
like the behavior inside interactive interpreter.

I will try to send a patch to solve the problem in these days. Please stop me 
if it's inappropriate.

----------
nosy: +Chun-Yu Tseng
versions: +Python 2.7, Python 3.3, Python 3.4, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26072>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to