Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:36:14+00:00 2026-05-28T01:36:14+00:00

Currently, I have an application which uses the cmd.Cmd module for the command line

  • 0

Currently, I have an application which uses the cmd.Cmd module for the command line interface, tab-completion works perfectly.

Now, I’d like to replace sys.stdout, with another object (For example, in order to capture what’s being written.)

Following snippet should be completely transparent in theory, as every get/set operation to the Std object is redirected to the actial sys.__stdout__.

class Std(object):
    def __getattribute__(self, name):
        if name in ('__getattribute__', '__setattr__'):
            return object.__getattribute__(self, name)
        else:
            return getattr(sys.__stdout__, name)

    def __setattr__(self, name, value):
        setattr(sys.__stdout__, name, value)

sys.stdout = Std()

For example, sys.stdout.fileno() will still print 1.
However, the readline tab-completion of Cmd.cmd does no longer work now…

Okay, let’s inherit from file. (stdout is a file object.)

class Std(file):
    def __init__(self):
        pass
    def __getattribute__(self, name):
        if name in ('__getattribute__', '__setattr__'):
            return object.__getattribute__(self, name)
        else:
            return getattr(sys.__stdout__, name)

    def __setattr__(self, name, value):
        setattr(sys.__stdout__, name, value)

sys.stdout = Std()

And now I get:

Traceback (most recent call last):
  File "./shell.py", line 61, in <module>
    print '#1'
ValueError: I/O operation on closed file

However, the following assertion does not fail:

assert not sys.stdout.closed

Somehow, I guess, Python is overoptimizing something, and bypassing Std.write.

What should I do to replace stdout, without loosing readline support…?

Jonathan

-edit-

I’m also trying to replace sys.stdin.
Passing it to cmd.Cmd does not work, because raw_input is used for readline support and Python’s raw_input does not accept a file descriptor. It falls back to whatever pty is assigned to sys.stdin.

When I create a new pty (through os.openpty()), and assign this pair to sys.stdin/out, readline autocompletion through that pty works perfectly, but again, when wrapped in a proxy object, it works, but without autocompletion.

Trying to understand the source of readline, but it isn’t easy:
http://svn.python.org/projects/python/branches/release25-maint/Modules/readline.c

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:36:15+00:00Added an answer on May 28, 2026 at 1:36 am

    I don’t know precisely why replacing sys.stdout does not work, but in any case you can fix your immediate problems by passing your own file object to the constructor for cmd.Cmd.

    Here is an example script (partly borrowed from PyMOTW), that demonstrates tab-completion:

    import sys, cmd
    
    class Std(object):
        def __getattribute__(self, name):
            if name in ('__getattribute__', '__setattr__'):
                return object.__getattribute__(self, name)
            else:
                return getattr(sys.__stdout__, name)
    
        def __setattr__(self, name, value):
            setattr(sys.__stdout__, name, value)
    
    class HelloWorld(cmd.Cmd):
        FRIENDS = [ 'Alice', 'Adam', 'Barbara', 'Bob' ]
    
        def do_greet(self, person):
            "Greet the person"
            if person and person in self.FRIENDS:
                greeting = 'hi, %s!' % person
            elif person:
                greeting = "hello, " + person
            else:
                greeting = 'hello'
            print greeting
    
        def complete_greet(self, text, line, begidx, endidx):
            if not text:
                completions = self.FRIENDS[:]
            else:
                completions = [f for f in self.FRIENDS
                               if f.startswith(text)]
            return completions
    
        def do_EOF(self, line):
            return True
    
    if __name__ == '__main__':
    
        HelloWorld(stdout=Std()).cmdloop()
    

    For your purposes, this is a much better way of doing things, because it ensures you will only be capturing the output that your Cmd instance produces.

    Note that the file object that you pass in is also available as a stdout attribute on the Cmd instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I currently have an application which uses a regular ListView with groups to show
I currently have an application which uses EF database first which was built against
For a system monitoring Java application which currently runs on the command line and
We have a web application which uses Struts 2, Spring and Hibernate. Currently a
Background Currently I have a C# Silverlight business application which uses RIA Services. The
Background Currently I have a C# Silverlight business application which uses RIA Services. The
I have an application which uses context sensitive datasources. Currently I keep the datasource
I have a WPF application which uses a (currently) local database to act as
I have a currency converter application for iphone which uses web service. The web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.