This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Chun-Yu Tseng
Recipients Antony.Lee, Chun-Yu Tseng, Jesús Gómez, georg.brandl, xdegaye
Date 2016-09-28.03:57:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1475035035.28.0.975423141287.issue26072@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-09-28 03:57:15Chun-Yu Tsengsetrecipients: + Chun-Yu Tseng, georg.brandl, xdegaye, Antony.Lee, Jesús Gómez
2016-09-28 03:57:15Chun-Yu Tsengsetmessageid: <1475035035.28.0.975423141287.issue26072@psf.upfronthosting.co.za>
2016-09-28 03:57:15Chun-Yu Tsenglinkissue26072 messages
2016-09-28 03:57:13Chun-Yu Tsengcreate