File tree 2 files changed +15
-0
lines changed
2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change 259
259
# endif
260
260
#endif
261
261
262
+ #if !defined(NDEBUG) && !defined(PY_ASSERT_GIL_HELD_INCREF_DECREF) \
263
+ && !defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
264
+ # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF
265
+ #endif
266
+
262
267
// #define PYBIND11_STR_LEGACY_PERMISSIVE
263
268
// If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject
264
269
// (probably surprising and never documented, but this was the
Original file line number Diff line number Diff line change @@ -246,6 +246,11 @@ class handle : public detail::object_api<handle> {
246
246
const handle &inc_ref () const & {
247
247
#ifdef PYBIND11_HANDLE_REF_DEBUG
248
248
inc_ref_counter (1 );
249
+ #endif
250
+ #if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
251
+ if (m_ptr != nullptr && !PyGILState_Check ()) {
252
+ throw std::runtime_error (" pybind11::handle::inc_ref() PyGILState_Check() failure." );
253
+ }
249
254
#endif
250
255
Py_XINCREF (m_ptr);
251
256
return *this ;
@@ -257,6 +262,11 @@ class handle : public detail::object_api<handle> {
257
262
this function automatically. Returns a reference to itself.
258
263
\endrst */
259
264
const handle &dec_ref () const & {
265
+ #if defined(PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF)
266
+ if (m_ptr != nullptr && !PyGILState_Check ()) {
267
+ throw std::runtime_error (" pybind11::handle::dec_ref() PyGILState_Check() failure." );
268
+ }
269
+ #endif
260
270
Py_XDECREF (m_ptr);
261
271
return *this ;
262
272
}
You can’t perform that action at this time.
0 commit comments