How to suppress exception printing to console?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Thu May 31 11:01:29 EDT 2012


Am 31.05.2012 09:57, schrieb Qi:
> I have an application that embedding Python into C++.
> When any exception occurred in C++ code, PyErr_SetString will
> be called to propagate the exception to Python.

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++?


> The problem is, some unit tests trigger exception on intention.
> So it's OK to have the exceptions. But Python will still print
> the exception to console, polluting the unit test output.

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.


> My question is, is there any way to disable exception reporting
> to console from either C++ or Python code?

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.

Good luck!

Uli



More information about the Python-list mailing list