Skip to content

Commit

Permalink
Move cleaning up of type info in internals to tp_dealloc on pybind11_…
Browse files Browse the repository at this point in the history
…metaclass
  • Loading branch information
YannickJadoul committed Oct 15, 2020
1 parent 51978a8 commit ae96b37
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
40 changes: 40 additions & 0 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,44 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
return self;
}

/// Cleanup the type-info for a pybind11-registered type.
extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
auto *type = (PyTypeObject *) obj;
auto &internals = get_internals();

// A pybind11-registered type will:
// 1) be found in internals.registered_types_py
// 2) have exactly one associated `detail::type_info`
auto found_type = internals.registered_types_py.find(type);
if (found_type != internals.registered_types_py.end() &&
found_type->second.size() == 1 &&
found_type->second[0]->type == type) {

auto *tinfo = found_type->second[0];
auto tindex = std::type_index(*tinfo->cpptype);
internals.direct_conversions.erase(tindex);

if (tinfo->module_local)
registered_local_types_cpp().erase(tindex);
else
internals.registered_types_cpp.erase(tindex);
internals.registered_types_py.erase(tinfo->type);

// Actually just `std::erase_if`, but that's only available in C++20
auto &cache = internals.inactive_override_cache;
for (auto it = cache.begin(), last = cache.end(); it != last; ) {
if (it->first == (PyObject *) tinfo->type)
it = cache.erase(it);
else
++it;
}

delete tinfo;
}

PyType_Type.tp_dealloc(obj);
}

/** This metaclass is assigned by default to all pybind11 types and is required in order
for static properties to function correctly. Users may override this using `py::metaclass`.
Return value: New reference. */
Expand Down Expand Up @@ -225,6 +263,8 @@ inline PyTypeObject* make_default_metaclass() {
type->tp_getattro = pybind11_meta_getattro;
#endif

type->tp_dealloc = pybind11_meta_dealloc;

if (PyType_Ready(type) < 0)
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");

Expand Down
30 changes: 0 additions & 30 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,30 +1021,6 @@ inline dict globals() {
}

PYBIND11_NAMESPACE_BEGIN(detail)
/// Cleanup the type-info for a pybind11-registered type.
PYBIND11_NOINLINE inline void cleanup_type_info(detail::type_info *tinfo) {
auto &internals = get_internals();
auto tindex = std::type_index(*tinfo->cpptype);
internals.direct_conversions.erase(tindex);

if (tinfo->module_local)
registered_local_types_cpp().erase(tindex);
else
internals.registered_types_cpp.erase(tindex);
internals.registered_types_py.erase(tinfo->type);

// Actually just `std::erase_if`, but that's only available in C++20
auto &cache = internals.inactive_override_cache;
for (auto it = cache.begin(), last = cache.end(); it != last; ) {
if (it->first == (PyObject *) tinfo->type)
it = cache.erase(it);
else
++it;
}

delete tinfo;
}

/// Generic support for creating new Python heap types
class generic_type : public object {
template <typename...> friend class class_;
Expand Down Expand Up @@ -1086,12 +1062,6 @@ class generic_type : public object {
internals.registered_types_cpp[tindex] = tinfo;
internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo };

// Clean up our internals after the Python type object gets garbage collected
weakref(m_ptr, cpp_function([tinfo](handle wr) {
cleanup_type_info(tinfo);
wr.dec_ref();
})).release();

if (rec.bases.size() > 1 || rec.multiple_inheritance) {
mark_parents_nonsimple(tinfo->type);
tinfo->simple_ancestors = false;
Expand Down

0 comments on commit ae96b37

Please sign in to comment.