Skip to content

Commit d499544

Browse files
authored
Merge pull request #2180 from fofoni/fix-ipython-scope
Fix `NameError` when a pasted function attempts to access a variable in its outer scope
2 parents d4080b8 + 2dd0256 commit d499544

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

manimlib/scene/scene.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ def embed(
220220
self.save_state()
221221
self.show_animation_progress = show_animation_progress
222222

223-
# Create embedded IPython terminal to be configured
224-
shell = InteractiveShellEmbed.instance()
225-
226-
# Use the locals namespace of the caller
223+
# Create embedded IPython terminal configured to have access to
224+
# the local namespace of the caller
227225
caller_frame = inspect.currentframe().f_back
228-
local_ns = dict(caller_frame.f_locals)
226+
module = get_module(caller_frame.f_globals["__file__"])
227+
shell = InteractiveShellEmbed(user_module=module)
229228

230-
# Add a few custom shortcuts
229+
# Add a few custom shortcuts to that local namespace
230+
local_ns = dict(caller_frame.f_locals)
231231
local_ns.update(
232232
play=self.play,
233233
wait=self.wait,
@@ -244,6 +244,9 @@ def embed(
244244
notouch=lambda: shell.enable_gui(None),
245245
)
246246

247+
# Update the shell module with the caller's locals + shortcuts
248+
module.__dict__.update(local_ns)
249+
247250
# Enables gui interactions during the embed
248251
def inputhook(context):
249252
while not context.input_is_ready():
@@ -278,13 +281,7 @@ def custom_exc(shell, etype, evalue, tb, tb_offset=None):
278281
shell.magic(f"xmode {self.embed_exception_mode}")
279282

280283
# Launch shell
281-
shell(
282-
local_ns=local_ns,
283-
# Pretend like we're embeding in the caller function, not here
284-
stack_depth=2,
285-
# Specify that the present module is the caller's, not here
286-
module=get_module(caller_frame.f_globals["__file__"])
287-
)
284+
shell()
288285

289286
# End scene when exiting an embed
290287
if close_scene_on_exit:

0 commit comments

Comments
 (0)