5

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 Answers1

1

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you, but this does not really help. In `cmd.Cmd.__init__` is checked whether stdout is None, if that's the case, it has a fallback to sys.stdout. So, it's exactly the same, as asigning to sys.stdin/out. – Jonathan Slenders Jan 07 '12 at 21:14
  • @JonathanSlenders. I wouldn't have offered the solution without testing it first. For me, it fixes the problem with tab-completion. I didn't do any testing for `stdin`, because it was not part of your original question - but I did read the documentation for [cmd.Cmd](http://docs.python.org/library/cmd.html#cmd.Cmd) and [use_rawinput](http://docs.python.org/library/cmd.html#cmd.Cmd.use_rawinput), which seem to have relevant information regarding that issue. – ekhumoro Jan 07 '12 at 23:29
  • @JonathanSlenders. I have updated my answer with the script I used for testing. – ekhumoro Jan 07 '12 at 23:41
  • Thanks, it works, but only if you don't assign the same Std() instance to sys.stdout. That means, the command line itself is captured, but any print statement inside complete_greet for example will not be captured. To me, it looks like a limitation of the readline implementation of CPython 2.6 and 2.7. The readline library seems to work only when sys.stdout is a file object, and sys.stdout.isatty() is True, however, you cannot inherit custom stdout objects from file, because Python will bypass write calls in that case. The great thing however is that it seems to work in Python 3.0! – Jonathan Slenders Jan 08 '12 at 09:22