[issue41918] exec fails to take locals into account when running list comprehensions or functions

undefined blinded report at bugs.python.org
Fri Nov 13 06:43:19 EST 2020


undefined blinded <alex0kamp at googlemail.com> added the comment:

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
```

----------
nosy: +alex0kamp

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


More information about the Python-bugs-list mailing list