Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > No tab completion if sys.stdout is redirected

Reply
Thread Tools

No tab completion if sys.stdout is redirected

 
 
Dirk Loss
Guest
Posts: n/a
 
      12-18-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
 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      12-18-2007
Dirk Loss wrote:
> 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

--
BOFH excuse #350:

paradigm shift...without a clutch

 
Reply With Quote
 
 
 
 
Dirk Loss
Guest
Posts: n/a
 
      12-19-2007
Bjoern Schliessmann wrote:
> 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
 
Reply With Quote
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      12-19-2007
Dirk Loss wrote:
> Bjoern Schliessmann wrote:


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


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

--
BOFH excuse #226:

A star wars satellite accidently blew up the WAN.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing tab completion using python Siddhant Python 1 03-22-2008 01:18 PM
tab completion? Siddhant Python 4 03-04-2008 05:26 PM
Inserting a tab with tab completion enabled on win32 News Sympatico Python 0 05-21-2005 03:36 AM
python-mode tab completion problem test1dellboy3@yahoo.co.uk Python 2 02-03-2005 11:10 PM
Spelling Completion or Code completion in ASP? ziggs ASP General 2 04-21-2004 02:02 PM



Advertisments