Skip to content

exec fails to take locals into account when running list comprehensions or functions #86084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
impact27 mannequin opened this issue Oct 3, 2020 · 7 comments
Open
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@impact27
Copy link
Mannequin

impact27 mannequin commented Oct 3, 2020

BPO 41918
Nosy @impact27, @havrikov

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-10-03.09:06:43.988>
labels = ['type-feature', '3.7']
title = 'exec fails to take locals into account when running list comprehensions or functions'
updated_at = <Date 2020-11-29.17:03:44.495>
user = 'https://github.com/impact27'

bugs.python.org fields:

activity = <Date 2020-11-29.17:03:44.495>
actor = 'patterns'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2020-10-03.09:06:43.988>
creator = 'qpeter'
dependencies = []
files = []
hgrepos = []
issue_num = 41918
keywords = []
message_count = 5.0
messages = ['377862', '378614', '380876', '380877', '382077']
nosy_count = 4.0
nosy_names = ['qpeter', 'alex0kamp', 'havrikov', 'patterns']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue41918'
versions = ['Python 3.7']

@impact27
Copy link
Mannequin Author

impact27 mannequin commented Oct 3, 2020

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

@impact27 impact27 mannequin added 3.7 (EOL) end of life type-feature A feature request or enhancement labels Oct 3, 2020
@impact27
Copy link
Mannequin Author

impact27 mannequin commented Oct 14, 2020

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

@impact27 impact27 mannequin changed the 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 Oct 14, 2020
@impact27 impact27 mannequin changed the 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 Oct 14, 2020
@alex0kamp
Copy link
Mannequin

alex0kamp mannequin commented Nov 13, 2020

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

@havrikov
Copy link
Mannequin

havrikov mannequin commented Nov 13, 2020

This issue also occurs in Python 3.8.6

@patterns
Copy link
Mannequin

patterns mannequin commented Nov 29, 2020

Also occurs in Python 3.6.9

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@oliverYoung2001
Copy link

This also occurs in Python 3.10.4 !!!

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
@rukayaj
Copy link

rukayaj commented Aug 8, 2024

And 3.11.9

Example:

example = """
fields_a = ['testa0', 'test1']
fields_b = ['testb0', 'test1']
print(fields_a)
print(fields_b)
print([field for field in fields_a if field in fields_b])
"""
locals = {}
globals = {}
exec(example, globals, locals)

Gives error:

['testa0', 'test1']
['testb0', 'test1']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<string>", line 6, in <module>
  File "<string>", line 6, in <listcomp>
NameError: name 'fields_b' is not defined

I used this as a workaround:

locals = {}
globals = {}
combined_context = globals.copy()
combined_context.update(locals)
exec(example, combined_context, combined_context)

See also this more recent issue: #121306

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants