Skip to content

Commit 3beea65

Browse files
committed
Adapt to python3.8 C API change
Do `Py_DECREF(type)` on all python objects on deallocation fix pybind#1946
1 parent 96be2c1 commit 3beea65

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

include/pybind11/detail/class.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,19 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
350350
auto type = Py_TYPE(self);
351351
type->tp_free(self);
352352

353+
#if PY_VERSION_HEX < 0x03080000
353354
// `type->tp_dealloc != pybind11_object_dealloc` means that we're being called
354355
// as part of a derived type's dealloc, in which case we're not allowed to decref
355356
// the type here. For cross-module compatibility, we shouldn't compare directly
356357
// with `pybind11_object_dealloc`, but with the common one stashed in internals.
357358
auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
358359
if (type->tp_dealloc == pybind11_object_type->tp_dealloc)
359360
Py_DECREF(type);
361+
#else
362+
// This was not needed before Python 3.8 (Python issue 35810)
363+
// https://github.com/pybind/pybind11/issues/1946
364+
Py_DECREF(type);
365+
#endif
360366
}
361367

362368
/** Create the type which can be used as a common base for all classes. This is

0 commit comments

Comments
 (0)