the example.cxx of pycxx crashes when run with python3.4 in debug mode.
this is due to following code:
Py::Long k( s.ptr() ); throw TestError( "convert a Py::String to an Py::Long must not succeed" ); } catch( Py::TypeError &e ) { std::cout << "PASSED: Correctly caught " << Py::type(e) << std::endl;
and it asserts here:
│508 #ifdef Py_DEBUG │ │509 /* PyObject_Str() must not be called with an exception set, │ │510 because it may clear it (directly or indirectly) and so the │ │511 caller looses its exception */ │ >│512 assert(!PyErr_Occurred()); │ │513 #endif
the reason is the example catches an exception but does not reset the exception within python. When it calls PyObject_Str on the exception it asserts.
Is the patch I need to call e.clear(); before calling
Py:type(e) etc?
Py::Long k( s.ptr() );
throw TestError( "convert a Py::String to an Py::Long must not succeed" );
}
catch( Py::TypeError &e )
{
e.clear(); // Fix crash in Py_DEBUG mode
std::cout << "PASSED: Correctly caught " << Py::type(e) << std::endl;
its required in any exception handler that calls a python method that can set an error.
Is there a better way to do this? within c++ one already has caught the exception and thus one hasn't lost it as python assumes.
E.g. clearing the error on construction of all exceptions.
It might also be worth asking python devs to revert this, I assume it could affect all high level wrappers of the python capi.
From the comment the assert is simply telling you that the code will not do what you think. It seems that the e.clear() will be required.
I don't have adebug build of 3.4 on hand. Could you try the patch and if it works I'll commit the change and release a updated pycxx kit. THere are number of other minor fixes that could do with a release.
it will certainly work, but I don't think its the right solution.
on the other hand it seems to be already documented:
http://cxx.sourceforge.net/PyCXX-Python3.html#h2_exceptions_catching
so its probably fine.
tested the patch works with py3.4beta3
Thanks I'll apply the patch.
Commited as r295