How to suppress exception printing to console?

Qi no at no.no
Thu May 31 23:06:31 EDT 2012


On 2012-5-31 23:01, Ulrich Eckhardt wrote:
> Am 31.05.2012 09:57, schrieb Qi:
> The first sentence is clear. The second sentence rather sounds as if you
> were implementing a Python module in C++. This is the opposite to
> embedding Python in C++, it's rather embedding C++ in Python. Or is it a
> C++ function called by Python which in turn was embedded by C++?

I think it's bidirectional.
In C++ I use PyRun_SimpleString to run some Python code, then
C++ code can get object from Python code, or call Python function.
Also Python code can call C++ code.

I think that's called "binding"?


> I can only guess what you are doing, maybe you should provide a simple
> piece of code (or, rather, one C++ piece and a Python piece) that
> demonstrates the issue. What I could imagine is that the Python
> interpreter shuts down with something it considers an unhandled
> exception, which it then prints to stdout before exiting. When
> embedding, that shouldn't happen from just calling a Python function in
> a loaded script, those should just make the error available to the C++
> side via PyErr functions etc.


PyRun_SimpleString("SomeCppFunc(1, 2)");

SomeCppFunc is C++ function bound to Python, and in SomeCppFunc
it detects the parameter mismatch (such as it expects the first
parameter to be a string), it throws an exception.
Then the C++ binding code catches the exception, and call
PyErr_SetString to propagate it to Python.

Then Python will print the error message to console.
What I want to do is to suppress the error message printing...


> What I found useful when embedding was that I could assign to sys.stdout
> in order to redirect the output. In my case, the target was a window and
> not a console. I'd consider that a workaround though. I really suspect
> that Python considers the error unhandled and therefore dumps the info
> to stdout.

Can I redirect sys.stdout in C++?


Thanks


-- 
WQ



More information about the Python-list mailing list