[IPython-dev] Grab current namespace for magic commands

Brian Granger ellisonbg at gmail.com
Mon Mar 21 23:03:42 EDT 2011


Thomas,

I think there is another issue, and that is the status of magic
command in multiline input blocks.  Fernando, what is the status of
that type of thing?  I think it works fine IIRC.

> At sagedays29, I've been pointed at an issue
> (http://trac.sagemath.org/sage_trac/ticket/10933) where people want to use
> the %time magic command inside a function. However, magic_time evaluates its
> argument in the main user_ns namespace, not the namespace of the function.

I think this does make sense if we are going to allow magics to work
inside functions.  Probably have locals be the function's scope and
globals be user_ns?

> I believe it's possible to get the relevant namespace by using
> inspect.stack(). If we do this in interactiveshell.magic(), we can get a
> reference to the frame from which magic() was called, and then evaluate the
> code with the locals and globals of that frame. Does this sound like
> something worth doing, or an awkward kludge? Is there a nicer way to do this
> that I've overlooked?

Sounds good to me as long as multiline magics are working as expected.

Cheers,

Brian

> Thanks,
> Thomas
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>



-- 
Brian E. Granger, Ph.D.
Assistant Professor of Physics
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu
ellisonbg at gmail.com



More information about the IPython-dev mailing list