[issue34483] eval() raises NameError: name '...' is not defined

Alex report at bugs.python.org
Thu Aug 23 13:49:17 EDT 2018


New submission from Alex <bojenko.alex at gmail.com>:

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

----------
components: Interpreter Core
messages: 323965
nosy: alexb
priority: normal
severity: normal
status: open
title: eval() raises NameError: name '...' is not defined
versions: Python 3.4, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34483>
_______________________________________


More information about the Python-bugs-list mailing list