No tab completion if sys.stdout is redirected

D

Dirk Loss

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
 
B

Bjoern Schliessmann

Dirk said:
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?

readline module applies its autocompletion functions to (and only
to) sys.stdout.

Regards,


Björn
 
D

Dirk Loss

Bjoern said:
readline module applies its autocompletion functions to (and only
to) sys.stdout.

I see. Then I guess I'll have to avoid redirecting sys.stdout and
come up with some kind of workaround instead.
Nevertheless, thanks for the info.

Regards
Dirk
 
B

Bjoern Schliessmann

Dirk said:
Bjoern Schliessmann wrote:

I see. Then I guess I'll have to avoid redirecting sys.stdout and
come up with some kind of workaround instead.

Just use a "central" function for printing output. Inside this
function, you can redirect the output wherever you wish without
touching sys.stdout.

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top