Catching console output to string?

Dag Sunde dag at orion.no
Tue Oct 31 13:36:38 EST 2000


Robert...
The following is taken from the manual... ;-)

"stderr
File objects corresponding to the interpreter's standard input, output and
error streams. stdin is used for all interpreter input except for scripts
but including calls to input() and raw_input(). stdout is used for the
output of print and expression statements and for the prompts of input() and
raw_input(). The interpreter's own prompts and (almost all of) its error
messages go to stderr. stdout and stderr needn't be built-in file objects:
any object is acceptable as long as it has a write() method that takes a
string argument. (Changing these objects doesn't affect the standard I/O
streams of processes executed by os.popen(), os.system() or the exec*()
family of functions in the os module.)"

This indicates that you can create your own ErrorText class, supply it with
a
write() method, and redirect sys.stderr to your own (instance) of the
ErrorText-class

...just an idea...

Dag.


"Robert Brotherus" <robert.brotherus at thermobio.com> wrote in message
news:HoAL5.179$RH2.10768 at read2.inet.fi...
> Hi,
>
> Many useful Python C-API function seem to print their output to STDOUT,
> which normally is the console screen. For example, the PyErr_Print()
prints
> most recent python error information. But how could I get the output of
such
> function to a string instead of STDOUT? I am writing a windows
COM-component
> that uses python as the underlying calculation engine. There is no console
> window - even screen - available to the component and it seems to me too
> clumsy to redirect STDOUT to a disk file and then read the file.
> Following code demonstrates what I would like to do:
>
> void MathServer :: CheckPyError()
> {
>     PyObject *error = PyErr_Occurred();
>     if (error != 0)
>     {
>        string ErrorText;
>        //  ErrorText = PyErr_Print();  // Get error text! But how??!!
>        throw MyPythonException(ErrorText);
>     }
> }
>
> Robert Brotherus
> robert.brotherus at thermobio.com
>
>





More information about the Python-list mailing list