[IPython-user] PyReadline: No tab completion if sys.stdout is redirected

Jörgen Stenarson jorgen.stenarson@bostream...
Wed Dec 19 13:25:49 CST 2007


Hi Dirk,

in the future please send questions about pyreadline to 
ipython-user@scipy.net. Then more people see them and can respond.

I'm not sure why things work like this but I have attached a script with 
a possible workaround.

/Jörgen

Dirk Loss skrev:
> Hi Jörgen,
> 
> I want to have tab completion in my program [1] for interactive input.
> Using readline and rlcompleter this works nicely. But I also have to
> catch and modify all "print" output, so I redirect sys.stdout
> to a custom file-like object. The problem is: After the redirection,
> readline support seems suddenly disabled. Why?
> 
> Here's an example:
> 
> - cut ----
> import sys
> import rlcompleter
> import readline
> 
> class DottedWriter(object):
>     """Just a simple example for a sys.stdout wrapper"""
>     def __init__(self, orig_stdout):
>         self.orig_stdout = orig_stdout
> 
>     def write(self, text):
>         self.orig_stdout.write("." + text)
> 
> readline.parse_and_bind("tab: complete")
> mywriter = DottedWriter(sys.stdout)
> 
> raw_input("Press TAB to see that completion works. Then press ENTER:")
> print "Replacing sys.stdout with a custom file-like object..."
> sys.stdout = mywriter
> raw_input("Now tab completion doesn't work anymore. Please try:")
> - cut ----
> 
> In the first raw_input() call, tab completion works, in the second it
> doesn't. Am I doing something wrong here or is it a bug?
> 
> Reproduced on Windows (with pyreadline 1.5) and Python 2.5.1.
> 
> IPython has tab completion and ANSI colors for the standard console
> on Windows, and I found lots of "print" statements in the code -- don't
> they redirect sys.stdout as well? In fact, this nice combination of ANSI
> colors and tab completion is what I am really after. So if there's any
> workaround to achieve that without replacing every "print" statement
> with a custom function, that would solve my problem as well.
> 
> Any help or info would be greatly appreciated.
> 
> Regards
> Dirk
> 
> 
> [1] I am working on the Windows port of Scapy, a GPLed network sniffing
> and packet crafting tool <http://www.secdev.org/projects/scapy/
> 

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: stdout_problem_workaround.py
Url: http://lists.ipython.scipy.org/pipermail/ipython-user/attachments/20071219/bfb1d76a/attachment.pl 


More information about the IPython-user mailing list