[IPython-dev] Chasing my tail with the "with" statement

Brian Granger ellisonbg.net at gmail.com
Fri Sep 11 19:36:07 EDT 2009


Hi,

I am converting some of the traps in IPython to use the with statement.  The
idea is that
things like sys.excepthook, sys.displayhook, etc. are not always set by
IPython,
but only when user code is actually being run.  But, I am running into a
problem
with sys.displayhook with the results of a magic function.

The prefilter machinery converts:

%alias -> get_ipython().magic("alias")

The end of the get_ipython().magic function looks like this:

            with nested(self.builtin_trap, self.display_trap):
                result = fn(magic_args)
            return result

This idea is that we use "with" to enable the display_trap, call the
magic and then return the result.  The builtin_trap works fine, but
the display_trap doesn't work.  The problem is that sys.displaytrap
is only called when something is returned.  But by then ("return result")
the "with" block has ended and the display_trap is deactivated.

There are other places that I can enable the display_trap, but we
loose the nice feature of only having the trap set when it is needed.

Any ideas of how we can get around having to leave display_trap set
all the time?

Cheers,

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20090911/e50b6e85/attachment.html>


More information about the IPython-dev mailing list