No tab completion if sys.stdout is redirected

Dirk Loss lists at dirk-loss.de
Tue Dec 18 04:06:09 EST 2007


Hi,

I want to have tab completion in my program 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 Linux (standard readline
module), both using Python 2.5.1.

Things I have tried without success:
- Simulate all other methods of sys.stdout with __getattr__
   def __getattr__(self, name): return getattr(self.orig_stdout, name)
- Use other forms of interactive input
   * import code; code.interact()
   * input()

Regards
Dirk



More information about the Python-list mailing list