[despammed] Re: redirect sys.stdout to C++ ?

André Jonsson tatsujin at despammed.com
Sat Dec 28 13:43:21 EST 2002


Bengt Richter wrote:

>>I should probably make clear that I don't use an actual "interactive shell", I just 
>>feed an entered string into the PyRun_SimpleString() API call, thus the normal 
>         ^^^^^^^^^^^^^^ entered where & how? If it is in the same interpreter,
> perhaps a similar thing could happen.
> 
> Need more context to understand your problem. Can you reduce it to a minimal example
> showing how "entered strings" are entered and wind up being passed to PyRunSimpleString()?

Ok, I'm developing this C++ application that embeds Python. In this application there 
is a prompt, completely self-made, that is meant to accept one-liners of Python 
"code" as input. The strings input there are passed directly to PyRun_SimpleString().

To this embedded Python interpreter I've also added a module of my own that makes 
calls to some static functions in the application. One of these are a logging 
facility (or merely a 'print'-command inside my application).

Now, the thing is that I want all stdout/err output from any script that is run by 
the python interpreter, including -but not limited to- the prompt, to be output 
through the logging-function in the imported module (python code can also be executed 
from anywhere inside the application code (using PyRun_SimpleString()). To accomplish 
this I've made a redirection of sys.stdout and sys.stderr to call my own callback on 
any output to make the output visible inside my application.

Without this all output from the code the interpreter runs will be output to the 
application's stdout, which is of course not what I want.

Maybe that clears it up... Thanks for trying to help!

/André






More information about the Python-list mailing list