File tree 2 files changed +19
-0
lines changed
2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -323,6 +323,15 @@ PYBIND11_WARNING_POP
323
323
# define PYBIND11_HAS_U8STRING
324
324
#endif
325
325
326
+ // See description of PR #4246:
327
+ #if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) \
328
+ && !(defined(PYPY_VERSION) \
329
+ && defined(_MSC_VER)) /* PyPy Windows: pytest hangs indefinitely at the end of the \
330
+ process (see PR #4268) */ \
331
+ && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
332
+ # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF
333
+ #endif
334
+
326
335
// #define PYBIND11_STR_LEGACY_PERMISSIVE
327
336
// If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject
328
337
// (probably surprising and never documented, but this was the
Original file line number Diff line number Diff line change @@ -249,6 +249,11 @@ class handle : public detail::object_api<handle> {
249
249
const handle &inc_ref () const & {
250
250
#ifdef PYBIND11_HANDLE_REF_DEBUG
251
251
inc_ref_counter (1 );
252
+ #endif
253
+ #if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
254
+ if (m_ptr != nullptr && !PyGILState_Check ()) {
255
+ throw std::runtime_error (" pybind11::handle::inc_ref() PyGILState_Check() failure." );
256
+ }
252
257
#endif
253
258
Py_XINCREF (m_ptr);
254
259
return *this ;
@@ -260,6 +265,11 @@ class handle : public detail::object_api<handle> {
260
265
this function automatically. Returns a reference to itself.
261
266
\endrst */
262
267
const handle &dec_ref () const & {
268
+ #if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
269
+ if (m_ptr != nullptr && !PyGILState_Check ()) {
270
+ throw std::runtime_error (" pybind11::handle::dec_ref() PyGILState_Check() failure." );
271
+ }
272
+ #endif
263
273
Py_XDECREF (m_ptr);
264
274
return *this ;
265
275
}
You can’t perform that action at this time.
0 commit comments