Magic local scope #295

Merged
merged 3 commits into from Mar 28, 2011

Projects

None yet

2 participants

@takluyver
Member

I'm not sure if this is how we want to do this, or if we want to do it at all, but here it is for consideration.

Sage bug 10933 (http://trac.sagemath.org/sage_trac/ticket/10933) highlighted that we couldn't use the %time magic command inside a function. This captures the stack frame which called magic_time, and uses its local namespace to evaluate or execute the code passed to %time. It could also be used by other magic functions, although so far I've only added it to magic_time.

@takluyver takluyver commented on an outdated diff Mar 24, 2011
IPython/core/interactiveshell.py
@@ -1734,6 +1735,10 @@ class InteractiveShell(Configurable, Magic):
valid Python code you can type at the interpreter, including loops and
compound statements.
"""
+ # Save the scope of the call so magic functions like %time can
+ # evaluate expressions in it.
+ self._magic_locals = inspect.stack()[1][0].f_locals
@takluyver
takluyver Mar 24, 2011 Member

Look at utils.frame, and add a decorator so we're only doing this if the magic command needs it.

@fperez
Member
fperez commented Mar 24, 2011

Reviewed during sage days 29 sprint, ready for merge after minor fixes discussed there. Thanks!

@takluyver
Member

OK, I've revised it as we agreed. I've also rebased on master, so that I could test it easily.

@fperez fperez merged commit 9a34540 into ipython:master Mar 28, 2011
@fperez
Member
fperez commented Mar 28, 2011

I've merged and pushed it with --no-ff to preserve the history together, since it was 3 commits. My rule of thumb is to force no-ff even if the merge could have been fast-forward if the branch has 3 or more commits, since at that point it becomes useful to see them together. For just one or two, I let the fast-forward go through.

Thanks for the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment