diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 0b9ea42d..73f30477 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -388,7 +388,11 @@ inline void clear_patients(PyObject *self) { auto *instance = reinterpret_cast(self); auto &internals = get_internals(); auto pos = internals.patients.find(self); - assert(pos != internals.patients.end()); + + if (pos == internals.patients.end()) { + pybind11_fail("FATAL: Internal consistency check failed: Invalid clear_patients() call."); + } + // Clearing the patients can cause more Python code to run, which // can invalidate the iterator. Extract the vector of patients // from the unordered_map first.