summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/dataclasses.py4
1 files changed, 4 insertions, 0 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 05d62b6..efd8346 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -429,6 +429,10 @@ def _create_fn(name, args, body, *, globals=None, locals=None,
# Compute the text of the entire function.
txt = f' def {name}({args}){return_annotation}:\n{body}'
+ # Free variables in exec are resolved in the global namespace.
+ # The global namespace we have is user-provided, so we can't modify it for
+ # our purposes. So we put the things we need into locals and introduce a
+ # scope to allow the function we're creating to close over them.
local_vars = ', '.join(locals.keys())
txt = f"def __create_fn__({local_vars}):\n{txt}\n return {name}"
ns = {}