classification
Title: Module-level stack scopes have incorrect bindings.
Type: behavior Stage: test needed
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, georg.brandl, nejucomo
Priority: normal Keywords:

Created on 2007-07-04 19:53 by nejucomo, last changed 2010-08-22 00:31 by georg.brandl. This issue is now closed.

Files
File name Uploaded Description Edit
python-module-binding-bug.tar.bz2 nejucomo, 2007-07-04 19:53 A tarball with a README and four simple modules illustrating this problem.
buggy.py ajaksu2, 2009-03-31 01:29
triggerPdb.py ajaksu2, 2009-03-31 01:29
triggerBug.py ajaksu2, 2009-03-31 01:29
inspectStack.py ajaksu2, 2009-03-31 01:30
Messages (3)
msg32450 - (view) Author: Nefarious CodeMonkey, Jr. (nejucomo) Date: 2007-07-04 19:53
For a variable defined in a module scope, and stack frame object's locals and globals scope dictionaries each contain that variables name mapped to None.

This confuses python code which inspects the bindings, such as pdb.  Such code incorrectly reports the value as None.

It may be that the binding values are correct at the time of exception generation, but altered by the time the exception traceback is caught.  This problem may also be related to module imports.

Here's are two simple examples, the first does not trigger this problem, the second does.

Both use this module called "buggy.py":
# Begin buggy.py
whatKnightsSay = 'Ni!'
print 'We are the Knights who say: %r' % (whatKnightsSay,)
42 + whatKnightsSay
# End buggy.py

# Begin a non-failing example:
$ pdb buggy.py
> /home/n/sandbox/python-module-binding-bug-src/buggy.py(1)<module>()
-> whatKnightsSay = 'Ni!'
(Pdb) print whatKnightsSay
*** NameError: name 'whatKnightsSay' is not defined
(Pdb) next
> /home/n/sandbox/python-module-binding-bug-src/buggy.py(2)<module>()
-> print 'We are the Knights who say: %r' % (whatKnightsSay,)
(Pdb) print whatKnightsSay
Ni!
(Pdb) next
We are the Knights who say: 'Ni!'
> /home/n/sandbox/python-module-binding-bug-src/buggy.py(3)<module>()
-> 42 + whatKnightsSay
(Pdb) print whatKnightsSay
Ni!
(Pdb) next
TypeError: "unsupported operand type(s) for +: 'int' and 'str'"
> /home/n/sandbox/python-module-binding-bug-src/buggy.py(3)<module>()
-> 42 + whatKnightsSay
(Pdb) print whatKnightsSay
Ni!
(Pdb) next
--Return--
> /home/n/sandbox/python-module-binding-bug-src/buggy.py(3)<module>()->None
-> 42 + whatKnightsSay
(Pdb) print whatKnightsSay
Ni!
(Pdb) next
TypeError: "unsupported operand type(s) for +: 'int' and 'str'"
> <string>(1)<module>()->None
(Pdb) print whatKnightsSay
Ni!
# End succeeding example.


And here's a simple failing example, which relies on a second module:
# Begin triggerPdb.py
import pdb
pdb.set_trace()
import buggy
# End triggerPdb.py

Now pdb get's confused about the module level binding:
# Begin failure example:
$ python triggerPdb.py
> /home/n/sandbox/python-module-binding-bug-src/triggerPdb.py(3)<module>()
-> import buggy
(Pdb) next
We are the Knights who say: 'Ni!'
TypeError: "unsupported operand type(s) for +: 'int' and 'str'"
> /home/n/sandbox/python-module-binding-bug-src/triggerPdb.py(3)<module>()
-> import buggy
(Pdb) down
> /home/n/sandbox/python-module-binding-bug/buggy.py(3)<module>()
-> 42 + whatKnightsSay
(Pdb) list
  1     whatKnightsSay = 'Ni!'
  2     print 'We are the Knights who say: %r' % (whatKnightsSay,)
  3  -> 42 + whatKnightsSay
[EOF]
(Pdb) p whatKnightsSay
None
 
# End failing example.


I've included these two simple sources, as well as another that does traceback inspection in an except clause (which fails like the second example here).




msg84739 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-03-31 01:29
Confirmed in trunk, adding the bare .py files.
msg114625 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-08-22 00:31
Cannot reproduce in trunk.
History
Date User Action Args
2010-08-22 00:31:21georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg114625

resolution: fixed
2009-03-31 01:30:05ajaksu2setfiles: + inspectStack.py
2009-03-31 01:29:55ajaksu2setfiles: + triggerBug.py
2009-03-31 01:29:41ajaksu2setfiles: + triggerPdb.py
2009-03-31 01:29:27ajaksu2setfiles: + buggy.py

type: behavior
versions: + Python 2.6, - Python 2.5
nosy: + ajaksu2

messages: + msg84739
stage: test needed
2007-07-04 19:53:11nejucomocreate