[New-bugs-announce] [issue24800] Incorrect handling of local variables in comprehensions with exec()

Peter Eastman report at bugs.python.org
Wed Aug 5 21:44:41 CEST 2015


New submission from Peter Eastman:

The following script demonstrates a bug in the exec() function in Python 3.4.  (It works correctly in 2.7).

script = """
print(a)
print([a for i in range(5)])
"""
exec(script, globals(), {"a":5})

It produces the following output:

5
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    exec(script, globals(), {"a":5})
  File "<string>", line 3, in <module>
  File "<string>", line 3, in <listcomp>
NameError: name 'a' is not defined

The variable "a" is getting passed to the script, as expected: printing it out works correctly.  But if you use it in a comprehension, the interpreter claims it does not exist.

----------
messages: 248064
nosy: Peter Eastman
priority: normal
severity: normal
status: open
title: Incorrect handling of local variables in comprehensions with exec()
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24800>
_______________________________________


More information about the New-bugs-announce mailing list