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.

classification
Title: eval != literal scope in nested function
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: carou, rhettinger
Priority: normal Keywords:

Created on 2005-02-02 20:59 by carou, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py carou, 2005-02-02 20:59 short demonstration
Messages (2)
msg24136 - (view) Author: Andrew Collier (carou) Date: 2005-02-02 20:59
python 2.3 as installed by default in MacOS X 10.3.7

This may be the same as item 991196, but that's my uneducated 
guess since I don't understand the cause of either one... (in fact it 
may not even really be a bug, but at the least this is behaviour I'm 
unable to explain).

It seems that fewer symbols are available to eval() than are 
available to literal code. The best way to describe the problem is 
with the attached short example file.

In the function evalfunction2(), a call via eval() is unable to 
resolve the symbol name evalfunction1 - even though it would be 
possible to call evalfunction1() directly.

But if the code *does* call evalfunction1() directly, then the eval() 
can see that symbol too(!).
msg24137 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-02-02 23:26
Logged In: YES 
user_id=80475

Sorry, this isn't a bug.  The eval() function is documented
to only search globals() and local().  The nested scope of
eval's caller is not included.

In your code, callfunction2 works because callfunction1 can
be found in the nested scope.   

In contrast, evalfunction2 fails because evalfunction1 is
not in the locals() or globals(); instead, is in a enclosing
nested scope which is not searched.

The reason that evalfunction2 works if you uncomment the
extra code is the subsequent reference to evalfunction1 gets
it brought into locals() at compilation time (when the def
is executed).

For real code, is you want a nested scope object to be found
by eval, then either reference it in the same function that
calls eval() or, more explicitly, just add it to a
dictionary of things that should be visible to eval().
History
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41518
2005-02-02 20:59:30caroucreate