You can clone with
HTTPS or Subversion.
I want to run a script in an interactive ipython session with %run, which sets up a Tee object for both sys.stdout and sys.stderr s.t. all output is written to 2 StringIO objects in addition to the 'normal' sys.stdout, sys.stderr:
import IPython.utils.io as ipio
savedstdout = cStringIO.StringIO()
savedstderr = cStringIO.StringIO()
_streamteeout = ipio.Tee(savedstdout, channel='stdout')
_streamteeerr = ipio.Tee(savedstderr, channel='stderr')
The problem is that after running this script in the interactive ipython terminal command history and readline functionality is broken, e.g. pressing the up arrow results in
In : ^[[A
Is it possible to fix this behavior ?
The reason for setting up the 2 Tee objects is that at the end of the script (via an exit hook) I want to write all captured content to a server.
Just make sure to close the tees at the end of your script, and you should be fine:
Is that satisfactory?
Thx Min for your reply!
I actually do this in the exit handler of my script but this handler is run not till i quit the ipython terminal. Therefore, I either have to close the tees manually at the end of my script or close them directly after starting the script with %run which is what I would like to avoid.
By exit handler, do you mean something called by atexit.register()? Is there any reason you can't just close them at the end of your script?
Yes, I use atexit.register() in a module which is used/imported by all my scripts. Well, I could just close the streams at the end of my scripts but I would like to avoid modifying them.
Ah, I see. As a workaround, you can probably make your own magic command that will run a script and then run a cleanup function. (Don't try to run everything registered with atexit, because that could break IPython). But this does sound like a bug, so I'd like to fix it if possible.
I'm not entirely clear why it happens at all, though - shouldn't readline be acting at a level that's not affected by redirecting stdout?
Ah, good point. That is weird.
I have just verified that I do not have the described problem in a normal python shell - so its unlikely that the problem is caused by readline..
Well I can't for the life of me figure out what we do differently from regular Python that would matter here,
but you are right - code.interact does not bork readline if you redirect sys.stdout, so it's definitely fixable.