From 99131a85b613aa95a7aa33d0fb1b131f0d1622e8 Mon Sep 17 00:00:00 2001 From: Ethan Steinberg Date: Mon, 17 Jul 2023 00:05:52 -0700 Subject: [PATCH] Provide `PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF` as an option (#4753) * Remove GIL checks * Update common.h * Add flag * style: pre-commit fixes * Update pytypes.h * style: pre-commit fixes * Update common.h * style: pre-commit fixes * Update pytypes.h * style: pre-commit fixes * Update common.h * style: pre-commit fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/detail/common.h | 4 ++-- include/pybind11/pytypes.h | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index ad11b9607..7ebc01c5d 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -324,8 +324,8 @@ PYBIND11_WARNING_POP #endif // See description of PR #4246: -#if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) && !defined(PYPY_VERSION) \ - && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) +#if !defined(PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF) && !defined(NDEBUG) \ + && !defined(PYPY_VERSION) && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF) # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF #endif diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index c93e3d3b9..64aad6347 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -303,7 +303,12 @@ private: stderr, "%s is being called while the GIL is either not held or invalid. Please see " "https://pybind11.readthedocs.io/en/stable/advanced/" - "misc.html#common-sources-of-global-interpreter-lock-errors for debugging advice.\n", + "misc.html#common-sources-of-global-interpreter-lock-errors for debugging advice.\n" + "If you are convinced there is no bug in your code, you can #define " + "PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF" + "to disable this check. In that case you have to ensure this #define is consistently " + "used for all translation units linked into a given pybind11 extension, otherwise " + "there will be ODR violations.", function_name.c_str()); fflush(stderr); if (Py_TYPE(m_ptr)->tp_name != nullptr) {