Chun-Yu Tseng added the comment:
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:
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.
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.
Added file: http://bugs.python.org/file45115/default.patch
Python tracker <rep...@bugs.python.org>
Python-bugs-list mailing list