classification
Title: exec fails to take locals into account when running list comprehensions or functions
Type: enhancement Stage:
Components: Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: qpeter
Priority: normal Keywords:

Created on 2020-10-03 09:06 by qpeter, last changed 2020-10-14 13:33 by qpeter.

Messages (2)
msg377862 - (view) Author: Quentin Peter (qpeter) * Date: 2020-10-03 09:06
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
msg378614 - (view) Author: Quentin Peter (qpeter) * Date: 2020-10-14 13:33
Fails for functions as well:
```
In [4]: exec(compile('print(my_var)\ndef a():\n print(my_var)\na()', '<stdin>', 'exec'), globals(), {"my_var": 0})
0
Traceback (most recent call last):

File "<ipython-input-4-f091e49f2781>", line 1, in <module>
exec(compile('print(my_var)\ndef a():\n print(my_var)\na()', '<stdin>', 'exec'), globals(), {"my_var": 0})

File "<stdin>", line 4, in <module>

 File "<stdin>", line 3, in a

NameError: name 'my_var' is not defined
```
History
Date User Action Args
2020-10-14 13:33:59qpetersetmessages: + msg378614
title: exec fails to take locals into account when running list comprehensions -> exec fails to take locals into account when running list comprehensions or functions
2020-10-03 09:06:43qpetercreate