No tab completion if sys.stdout is redirected

Discussion in 'Python' started by Dirk Loss, Dec 18, 2007.

  1. Dirk Loss

    Dirk Loss Guest

    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
     
    Dirk Loss, Dec 18, 2007
    #1
    1. Advertising

  2. 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
     
    Bjoern Schliessmann, Dec 18, 2007
    #2
    1. Advertising

  3. Dirk Loss

    Dirk Loss Guest

    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
     
    Dirk Loss, Dec 19, 2007
    #3
  4. 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.
     
    Bjoern Schliessmann, Dec 19, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    670
    Roman Neuhauser
    Apr 4, 2005
  2. News Sympatico
    Replies:
    0
    Views:
    317
    News Sympatico
    May 21, 2005
  3. Replies:
    2
    Views:
    666
    velle
    Jan 5, 2006
  4. Replies:
    0
    Views:
    490
  5. Replies:
    2
    Views:
    353
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page