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() raises NameError: name '...' is not defined
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: alexb, eric.smith, steven.daprano
Priority: normal Keywords:

Created on 2018-08-23 17:49 by alexb, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg323965 - (view) Author: Alex (alexb) Date: 2018-08-23 17:49
Builtin eval() function raises NameError on a valid expression:


--- example of bug on Python 3.4
Python 3.4.5 (default, May 29 2017, 15:17:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1,2,3]
>>> [x for i in x]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> eval('[x for i in x]', {}, dict(x=x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined


--- example on Python 2.7 (no bug)
Python 2.7.5 (default, May  3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1,2,3]
>>> [x for i in x]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> eval('[x for i in x]', {}, dict(x=x))
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]


---

Bug reproduced on:
- Windows 7: Python 3.4, 3.7
- Red Hat Enterprise Linux Server: Python 3.4

Works without errors on:
- Windows 7: Python 2.7
- Red Hat Enterprise Linux Server: Python 2.7
msg323981 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-08-24 00:45
I believe you're seeing this: https://docs.python.org/3/reference/executionmodel.html#naming-and-binding

See the paragraph that starts with "Class definition blocks and arguments to exec() and eval() are special in the context of name resolution", and the example that follows it.

Basically, you're running eval() as if it were at class scope. I think (but am not positive) that this statement, from the exec() documentation, also applies to eval(): "Remember that at module level, globals and locals are the same dictionary. If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition."

If so, then eval()'s docs should reference this, too.
msg324104 - (view) Author: Alex (alexb) Date: 2018-08-25 22:19
Eric, thank you for the clarification. Do you want me to close the ticket?
msg324105 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-08-25 22:23
I'll close this. I'm still reviewing the docs on it, and I might open a documentation issue once I understand it better.
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78664
2018-08-25 22:23:13eric.smithsetstatus: open -> closed
type: behavior
messages: + msg324105

resolution: not a bug
stage: resolved
2018-08-25 22:19:06alexbsetmessages: + msg324104
2018-08-24 00:49:18steven.dapranosetnosy: + steven.daprano
2018-08-24 00:45:17eric.smithsetnosy: + eric.smith
messages: + msg323981
2018-08-23 17:49:17alexbcreate