Skip to content

Fix NameError when a pasted function attempts to access a variable in its outer scope #2180

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

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions manimlib/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ def embed(
self.save_state()
self.show_animation_progress = show_animation_progress

# Create embedded IPython terminal to be configured
shell = InteractiveShellEmbed.instance()

# Use the locals namespace of the caller
# Create embedded IPython terminal configured to have access to
# the local namespace of the caller
caller_frame = inspect.currentframe().f_back
local_ns = dict(caller_frame.f_locals)
module = get_module(caller_frame.f_globals["__file__"])
shell = InteractiveShellEmbed(user_module=module)

# Add a few custom shortcuts
# Add a few custom shortcuts to that local namespace
local_ns = dict(caller_frame.f_locals)
local_ns.update(
play=self.play,
wait=self.wait,
Expand All @@ -244,6 +244,9 @@ def embed(
notouch=lambda: shell.enable_gui(None),
)

# Update the shell module with the caller's locals + shortcuts
module.__dict__.update(local_ns)

# Enables gui interactions during the embed
def inputhook(context):
while not context.input_is_ready():
Expand Down Expand Up @@ -278,13 +281,7 @@ def custom_exc(shell, etype, evalue, tb, tb_offset=None):
shell.magic(f"xmode {self.embed_exception_mode}")

# Launch shell
shell(
local_ns=local_ns,
# Pretend like we're embeding in the caller function, not here
stack_depth=2,
# Specify that the present module is the caller's, not here
module=get_module(caller_frame.f_globals["__file__"])
)
shell()

# End scene when exiting an embed
if close_scene_on_exit:
Expand Down