Skip to content

Commit 1d49006

Browse files
committed
Use Py_XINCREF and Py_XDECREF
1 parent 6d8fa8a commit 1d49006

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

include/pybind11/detail/class.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,12 @@ extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
251251
}
252252
});
253253

254+
PyType_Type.tp_dealloc(obj);
255+
254256
// Release the references to the internals capsules that were acquired in make_new_python_type.
255257
// See the comment there for details on preventing use-after-free during interpreter shutdown.
256-
if (PyObject *capsule = get_internals_capsule()) {
257-
Py_DECREF(capsule);
258-
}
259-
if (PyObject *capsule = get_local_internals_capsule()) {
260-
Py_DECREF(capsule);
261-
}
262-
263-
PyType_Type.tp_dealloc(obj);
258+
Py_XDECREF(get_internals_capsule());
259+
Py_XDECREF(get_local_internals_capsule());
264260
}
265261

266262
/** This metaclass is assigned by default to all pybind11 types and is required in order
@@ -844,12 +840,8 @@ inline PyObject *make_new_python_type(const type_record &rec) {
844840
// would recreate an empty internals and fail because the type registry is gone. By holding
845841
// references to the capsules, we ensure they outlive all pybind11 types. The decref happens
846842
// in pybind11_meta_dealloc.
847-
if (PyObject *capsule = get_internals_capsule()) {
848-
Py_INCREF(capsule);
849-
}
850-
if (PyObject *capsule = get_local_internals_capsule()) {
851-
Py_INCREF(capsule);
852-
}
843+
Py_XINCREF(get_internals_capsule());
844+
Py_XINCREF(get_local_internals_capsule());
853845

854846
return reinterpret_cast<PyObject *>(type);
855847
}

0 commit comments

Comments
 (0)