How to suppress exception printing to console?

Q

Qi

Hi guys,

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 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.

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


Thanks
 
S

Steven D'Aprano

Hi guys,

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 problem is, some unit tests trigger exception on intention.

I'm sorry, I don't understand what you mean by "on intention".

So it's
OK to have the exceptions. But Python will still print the exception to
console, polluting the unit test output.

Are you using the Python unittest module, or C++ unit tests?

Python's unittest catches and suppresses exceptions, so it should not be
printing exceptions except as part of the normal unittest output.

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

If your unit tests are raising exceptions, your tests are broken. They
should either raise expected exceptions, in which case the exception is a
passing test, or they are failing tests, or they are bugs in your test
code. Fix the failing tests and the bugs in the test code, don't hide
them.
 
Q

Qi

I'm sorry, I don't understand what you mean by "on intention".

I mean the exception is expected.
The test will only pass if the embedded Python code raise
certain exception.
Are you using the Python unittest module, or C++ unit tests?

Python's unittest catches and suppresses exceptions, so it should not be
printing exceptions except as part of the normal unittest output.

I use a C++ unit test framework.

If your unit tests are raising exceptions, your tests are broken. They
should either raise expected exceptions, in which case the exception is a
passing test, or they are failing tests, or they are bugs in your test
code. Fix the failing tests and the bugs in the test code, don't hide
them.

No, I'm not hiding the exception.
I'm trying to hide the exception report text because it's not
necessary since the exception is expected.
 
U

Ulrich Eckhardt

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
 
Q

Qi

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
 
U

Ulrich Eckhardt

Am 01.06.2012 05:06, schrieb Qi:
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.

I think this has nothing to do with the called C++ code, I guess the
same happens if you call PyRun_SimpleString("raise Exception()");.

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

Don't use PyRun_SimpleString() or catch the exception there. The point
is that it runs the whole string as a module, like running a script from
the commandline, and a pending exception on exit is then reported to stdout.

What I do here is that I create a module using the C API that I register
with Py_InitModule(). It contains the C++ functions exported to Python.
I then load a script using PyImport_ImportModule() and use
PyObject_GetAttrString(), PyCallable_Check() and PyObject_CallObject()
to run the main function of that script.

Can I redirect sys.stdout in C++?

Maybe, I haven't tried. Since I require a proper main function in the
Python code anyway, I added a few more requirements, i.e. that it uses
one of the provided C++ functions for output. I think you can simply
assign "sys.stdout.write = log_string", where log_string is the provided
C++ function.

Uli
 
Q

Qi

Don't use PyRun_SimpleString() or catch the exception there. The point
is that it runs the whole string as a module, like running a script from
the commandline, and a pending exception on exit is then reported to stdout.

Good hint, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top