This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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: alex0kamp, havrikov, patterns, qpeter
Priority: normal Keywords:

Created on 2020-10-03 09:06 by qpeter, last changed 2022-04-11 14:59 by admin.

Messages (5)
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
```
msg380876 - (view) Author: undefined blinded (alex0kamp) Date: 2020-11-13 11:43
This seems to happen only when both arguments to exec are used:

```
Python 3.7.7 (default, Mar 10 2020, 15:43:27)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exec('text = ["hallo"]\ntext.append("Welt")\nprint(" ".join([text[i] for i in range(0, 2)]))')
hallo Welt
>>> exec('text = ["hallo"]\ntext.append("Welt")\nprint(" ".join([text[i] for i in range(0, 2)]))', {})
hallo Welt
>>> exec('text = ["hallo"]\ntext.append("Welt")\nprint(" ".join(text))', {}, {})
hallo Welt
>>> exec('text = ["hallo"]\ntext.append("Welt")\nprint(" ".join([text[i] for i in range(0, 2)]))', {}, {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3, in <module>
  File "<string>", line 3, in <listcomp>
NameError: name 'text' is not defined
```
msg380877 - (view) Author: Nikolas Havrikov (havrikov) Date: 2020-11-13 11:50
This issue also occurs in Python 3.8.6
msg382077 - (view) Author: patterns (patterns) Date: 2020-11-29 17:03
Also occurs in Python 3.6.9
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86084
2020-11-29 17:03:44patternssetnosy: + patterns
messages: + msg382077
2020-11-13 11:50:17havrikovsetnosy: + havrikov
messages: + msg380877
2020-11-13 11:43:19alex0kampsetnosy: + alex0kamp
messages: + msg380876
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