[IPython-dev] Grab current namespace for magic commands

Thomas Kluyver takowl at gmail.com
Mon Mar 21 22:35:45 EDT 2011


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 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?

Thanks,
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20110322/40bf9492/attachment.html>


More information about the IPython-dev mailing list