-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py::print doesn't work while a PyErr is set #404
Comments
I think this is a very reasonable workaround. Something similar is already done here: https://github.com/pybind/pybind11/blob/master/include/pybind11/cast.h#L115. Probably the best thing would be to add Thoughts? |
Yes, I agree. Also, an RIAA-type struct might be nice for this and the one in class error_scope {
PyObject *type, *value, *trace;
error_scope() { PyErr_Fetch(&type, &value, &traceback); }
~error_scope() { PyErr_Restore(type, value, traceback); }
}; |
I think you meant RAII :) -- pushed a commit for both cases. |
There is actually a more serious underlying issue here: any code calling the Python C API from a C++ destructor is potentially unsafe because of the way |
Oh god yes, let's keep the RIAA out of pybind! |
@dean0x7d
py::print
fails when a python error is currently set; I eventually tracked it down to these lines insimple_collector
/unpacking_collector
:If PyErr is currently set, the call fails, and py::print throws an exception. This is particular a problem if py::print is used in a destructor because the destructor may well have been invoked as part of stack unwinding because of a previous throw error_already_set().
I ran into this just now while updating PR #264, where exactly this happened: one of the tests was to test that an exception happened, but during the exception,
ConstructorStats::print_destroyed
is invoked, which now usespy::print
.I put in this workaround to temporarily work around the issue, but I think something similar should be moved up into
py::print
.The text was updated successfully, but these errors were encountered: