[issue24800] Incorrect handling of local variables in comprehensions with exec()

2015-08-05 Thread Peter Eastman
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

[issue24800] Incorrect handling of local variables in comprehensions with exec()

2015-08-05 Thread Peter Eastman
Peter Eastman added the comment: I don't believe that explanation is correct. You can just as easily get the same problem without explicitly passing a map to exec(). For example: def f(): script = print(a) print([a for i in range(5)]) a = 5 exec(script) f

[issue24800] Incorrect handling of local variables in comprehensions with exec()

2015-08-05 Thread Peter Eastman
Peter Eastman added the comment: Then fix the documentation. This behavior directly contradicts the documentation of the exec() function. The question is not what scope the comprehension runs in, it's what scope the script runs in. See my third example. A comprehension in the f() function