Skip to content

Commit e02e167

Browse files
Added documentation
1 parent e260032 commit e02e167

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/scyjava/_script.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,18 @@ def apply(self, arg):
9090
):
9191
# Last statement looks like an expression. Evaluate!
9292
last = ast.Expression(block.body.pop().value)
93-
94-
# _globals = {name: module for name, module in sys.modules.items() if name != '__main__'}
95-
# _globals = {__builtins__: builtins, '__name__': '__main__','__file__': '<string>', '__package__': None,}
96-
# _globals.update(globals())
97-
# _globals = None
98-
# _globals = locals()
99-
script_globals = script_locals
93+
# See here for why this implementation: https://docs.python.org/3/library/functions.html#exec
94+
# When `exec` gets two separate objects as *globals* and *locals*, the code will be executed as if it were embedded in a class definition.
95+
# This means functions and classes defined in the executed code will not be able to access variables assigned at the top level
96+
# (as the “top level” variables are treated as class variables in a class definition).
97+
_globals = script_locals
10098
exec(
101-
compile(block, "<string>", mode="exec"), script_globals, script_locals
99+
compile(block, "<string>", mode="exec"), _globals, script_locals
102100
)
103101
if last is not None:
104102
return_value = eval(
105103
compile(last, "<string>", mode="eval"),
106-
script_globals,
104+
_globals,
107105
script_locals,
108106
)
109107
except Exception:

0 commit comments

Comments
 (0)