pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.18k stars 2.06k forks source link

[BUG]: Coverity warning: Uncaught exceptions #4835

Open jengelh opened 10 months ago

jengelh commented 10 months ago

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.11.1

Problem description

There is a report made for pytypes.h:

CID 323117 (#1 of 1): Uncaught exception (UNCAUGHT_EXCEPT)
exn_spec_violation: An exception of type std::runtime_error is thrown but the
exception specification noexcept doesn't allow it to be thrown. This will result in a call to terminate().
387    object &operator=(object &&other) noexcept {
388        if (this != &other) {
389            handle temp(m_ptr);
390            m_ptr = other.m_ptr;
391            other.m_ptr = nullptr;
   fun_call_w_exception: Called function dec_ref throws an exception of type std::runtime_error. [show details]
392            temp.dec_ref();
393        }
394        return *this;
395    }
267 const handle &dec_ref() const & {
268#ifdef PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF
269        if (m_ptr != nullptr && !PyGILState_Check()) {
   1. fun_call_w_exception: Called function throw_gilstate_error throws an exception of type std::runtime_error.
270            throw_gilstate_error("pybind11::handle::dec_ref()");
271        }
272#endif
273        Py_XDECREF(m_ptr);
274        return *this;
275    }
301    void throw_gilstate_error(const std::string &function_name) const {
302        fprintf(
303            stderr,
304            "%s is being called while the GIL is either not held or invalid. Please see "
305            "https://pybind11.readthedocs.io/en/stable/advanced/"
306            "misc.html#common-sources-of-global-interpreter-lock-errors for debugging advice.\n"
307            "If you are convinced there is no bug in your code, you can #define "
308            "PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF"
309            "to disable this check. In that case you have to ensure this #define is consistently "
310            "used for all translation units linked into a given pybind11 extension, otherwise "
311            "there will be ODR violations.",
312            function_name.c_str());
313        fflush(stderr);
314        if (Py_TYPE(m_ptr)->tp_name != nullptr) {
315            fprintf(stderr,
316                    "The failing %s call was triggered on a %s object.\n",
317                    function_name.c_str(),
318                    Py_TYPE(m_ptr)->tp_name);
319            fflush(stderr);
320        }
   1. exception_thrown: An exception of type std::runtime_error is thrown.
321        throw std::runtime_error(function_name + " PyGILState_Check() failure.");
322    }

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

jengelh commented 10 months ago

a second instance of noexcept-is-too-much:

CID 322135 (#1 of 1): Uncaught exception (UNCAUGHT_EXCEPT)
exn_spec_violation: An exception of type pybind11::error_already_set is thrown
but the exception specification noexcept doesn't allow it to be thrown. This will result in a call to terminate().
2665 inline const char *error_already_set::what() const noexcept {
    fun_call_w_exception: Called function [gil_scoped_acquire] throws an exception of type pybind11::error_already_set.
2666    gil_scoped_acquire gil;
2667    error_scope scope;
2668    return m_fetched_error->error_string().c_str();
2669}