Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

IPython.utils.Tee in ipython terminal -> breaks readline/history #2676

Open
thisch opened this Issue · 10 comments

3 participants

@thisch

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 sys
import cStringIO
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 [2]: ^[[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.

@minrk
Owner

Just make sure to close the tees at the end of your script, and you should be fine:

_streamteeout.close()
_streamteeerr.close()

Is that satisfactory?

@thisch

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.

@takluyver
Owner

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?

@thisch

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.

@takluyver
Owner

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.

@minrk
Owner
@takluyver
Owner

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?

@minrk
Owner

Ah, good point. That is weird.

@thisch

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

@minrk
Owner

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.

@minrk minrk removed the prio-low label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.