From 8699639b8df63ce153fe65da0c8426a9dfdffcc2 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 17 Oct 2022 10:18:22 -0700 Subject: [PATCH 1/3] pybind11/pytypes.h `inc_ref()`, `dec_ref()` `PyGILState_Check()` **excluding** `nullptr` Guarded by `PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF` --- include/pybind11/detail/common.h | 5 +++++ include/pybind11/pytypes.h | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 9b74323f67..ffa1feaf3e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -262,6 +262,11 @@ # define PYBIND11_HAS_U8STRING #endif +#if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) \ + && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) +# define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF +#endif + // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // (probably surprising and never documented, but this was the diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 80b49ec397..99ae05d99e 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -246,6 +246,11 @@ class handle : public detail::object_api { const handle &inc_ref() const & { #ifdef PYBIND11_HANDLE_REF_DEBUG inc_ref_counter(1); +#endif +#if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) + if (m_ptr != nullptr && !PyGILState_Check()) { + throw std::runtime_error("pybind11::handle::inc_ref() PyGILState_Check() failure."); + } #endif Py_XINCREF(m_ptr); return *this; @@ -257,6 +262,11 @@ class handle : public detail::object_api { this function automatically. Returns a reference to itself. \endrst */ const handle &dec_ref() const & { +#if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) + if (m_ptr != nullptr && !PyGILState_Check()) { + throw std::runtime_error("pybind11::handle::dec_ref() PyGILState_Check() failure."); + } +#endif Py_XDECREF(m_ptr); return *this; } From 6d202aab3fbd0817852c33b5aa19a0812b2ee0b7 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 17 Oct 2022 12:07:10 -0700 Subject: [PATCH 2/3] Disable `PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF` for PyPy under Windows. --- include/pybind11/detail/common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index ffa1feaf3e..71bcb6072a 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -262,7 +262,9 @@ # define PYBIND11_HAS_U8STRING #endif +// See description of PR #4246: #if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) \ + && !(defined(PYPY_VERSION) && defined(_MSC_VER)) /* Tests hang indefinitely at startup. */ \ && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF #endif From 311544001903ce1ddfe3c8c83304cbc5c5f6aab4 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 21 Oct 2022 12:32:08 -0700 Subject: [PATCH 3/3] Add reference to PR #4268 (PyPy Windows) --- include/pybind11/detail/common.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 71bcb6072a..36432a300f 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -264,7 +264,9 @@ // See description of PR #4246: #if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) \ - && !(defined(PYPY_VERSION) && defined(_MSC_VER)) /* Tests hang indefinitely at startup. */ \ + && !(defined(PYPY_VERSION) \ + && defined(_MSC_VER)) /* PyPy Windows: pytest hangs indefinitely at the end of the \ + process (see PR #4268) */ \ && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF #endif