[issue26072] pdb fails to access variables closed over

Chun-Yu Tseng report at bugs.python.org
Sun Oct 16 05:55:22 EDT 2016


Chun-Yu Tseng added the comment:

Surrender.

After digging into the implementation of how CPython handles closures, I am sure that it is impossible to solve this problem now correctly. What I can do is create a patch to let Pdb print enough information when the problem occurs. It would look like:

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

                Pdb can not use the local variable 'x' to create a closure in
                your evaluation. Instead, it tries to use 'x' in globals().
                This behavior is due to the limitation of dynamic
                interpretation within a debugger session.

                Hint: type 'help interact' to check 'interact' command.

*** NameError: name 'x' is not defined

I believe it would be helpful and less annoyed for those who encounters this problem. Call for maintainers review, please.


PS.
1. Ruby does not have this problem. Whether it exists depends on how a programming language to implement closures. However, I think that what CPython do (by `PyCellObject`) is smart and efficient.

2. I tried to solve the problem by copying local variables to globals() (just like what `interact` command do), but it results in **more** problems. The most typical one is that if I eval `globals()` in such an affected environment, I will always get the wrong result.

3. I also tried to patch and evaluate again what user inputs when catching a closure-related `NameError`. Obviously, it is not a good idea due to side effects of evaluation.

4. The last possible solution I think of is import `ast` and do some hacking ... it's overkill, and it also brings up other side effects.

----------
keywords: +patch
Added file: http://bugs.python.org/file45115/default.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26072>
_______________________________________


More information about the Python-bugs-list mailing list