RE: Python Embedding: redirecting output

Hammond, Mark ((@)
Thu, 09 Feb 95 09:06:00 PST

>I'm busy embedding Python into a distributed application environment
>and I need to gather up Python's normal and exceptional text output
>and return it to the application's front end as part of a command's
>return value. While driving from the top is easy with run_command(),
>gathering the output seems non-trivial. So far, my strongest candidate
>is to reimplement output on the standard sys.stderr and sys.stdout
>objects and to diddle run_command() to initiate and terminate
>result gathering. Have others done this and found better ways or
>have suggestions to give?

The simplest way of grabbing output is to override sys.stdout and
sys.stderr. All that is required is that the object you redirect to has a
"write" method.

An apporach you could take would be to run_command() a command for
re-assigning sys.stdout/err to one of your objects. Then run_command() the
real command, and conther run_command() to restore it back. However, in
some imbedded applications, it is often OK to redirect stdout once at the
start for the life of the application.

Mark.