[New-bugs-announce] [issue41918] exec fails to take locals into account when running list comprehensions

Quentin Peter report at bugs.python.org
Sat Oct 3 05:06:44 EDT 2020


New submission from Quentin Peter <qpeter at bluewin.ch>:

The exec function fails to take locals into account when executing a list comprehension:
```
Python 3.7.7 (default, Mar 10 2020, 15:43:33) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(compile('[my_var for i in range(1)]\n', '<stdin>', 'single'), {**globals(), "my_var": 0}, None)
[0]
>>> exec(compile('[my_var for i in range(1)]\n', '<stdin>', 'single'), globals(), {"my_var": 0})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
NameError: name 'my_var' is not defined
>>> 
```
This is the cause of https://bugs.python.org/issue21161

----------
messages: 377862
nosy: qpeter
priority: normal
severity: normal
status: open
title: exec fails to take locals into account when running list comprehensions
type: enhancement
versions: Python 3.7

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


More information about the New-bugs-announce mailing list