@@ -1021,6 +1021,30 @@ inline dict globals() {
1021
1021
}
1022
1022
1023
1023
PYBIND11_NAMESPACE_BEGIN (detail)
1024
+ // / Cleanup the type-info for a pybind11-registered type.
1025
+ PYBIND11_NOINLINE inline void cleanup_type_info (detail::type_info *tinfo) {
1026
+ auto &internals = get_internals ();
1027
+ auto tindex = std::type_index (*tinfo->cpptype );
1028
+ internals.direct_conversions .erase (tindex);
1029
+
1030
+ if (tinfo->module_local )
1031
+ registered_local_types_cpp ().erase (tindex);
1032
+ else
1033
+ internals.registered_types_cpp .erase (tindex);
1034
+ internals.registered_types_py .erase (tinfo->type );
1035
+
1036
+ // Actually just `std::erase_if`, but that's only available in C++20
1037
+ auto &cache = internals.inactive_override_cache ;
1038
+ for (auto it = cache.begin (), last = cache.end (); it != last; ) {
1039
+ if (it->first == (PyObject *) tinfo->type )
1040
+ it = cache.erase (it);
1041
+ else
1042
+ ++it;
1043
+ }
1044
+
1045
+ delete tinfo;
1046
+ }
1047
+
1024
1048
// / Generic support for creating new Python heap types
1025
1049
class generic_type : public object {
1026
1050
template <typename ...> friend class class_ ;
@@ -1037,11 +1061,10 @@ class generic_type : public object {
1037
1061
" \" is already registered!" );
1038
1062
1039
1063
m_ptr = make_new_python_type (rec);
1040
- auto type = (PyTypeObject *) m_ptr;
1041
1064
1042
1065
/* Register supplemental type information in C++ dict */
1043
1066
auto *tinfo = new detail::type_info ();
1044
- tinfo->type = type ;
1067
+ tinfo->type = (PyTypeObject *) m_ptr ;
1045
1068
tinfo->cpptype = rec.type ;
1046
1069
tinfo->type_size = rec.type_size ;
1047
1070
tinfo->type_align = rec.type_align ;
@@ -1056,31 +1079,16 @@ class generic_type : public object {
1056
1079
1057
1080
auto &internals = get_internals ();
1058
1081
auto tindex = std::type_index (*rec.type );
1059
- auto module_local = rec.module_local ;
1060
1082
tinfo->direct_conversions = &internals.direct_conversions [tindex];
1061
- if (module_local)
1083
+ if (rec. module_local )
1062
1084
registered_local_types_cpp ()[tindex] = tinfo;
1063
1085
else
1064
1086
internals.registered_types_cpp [tindex] = tinfo;
1065
- internals.registered_types_py [type ] = { tinfo };
1087
+ internals.registered_types_py [(PyTypeObject *) m_ptr ] = { tinfo };
1066
1088
1067
1089
// Clean up our internals after the Python type object gets garbage collected
1068
- weakref (m_ptr, cpp_function ([type, tindex, module_local](handle wr) {
1069
- auto &internals = get_internals ();
1070
- internals.direct_conversions .erase (tindex);
1071
- if (module_local)
1072
- registered_local_types_cpp ().erase (tindex);
1073
- else
1074
- internals.registered_types_cpp .erase (tindex);
1075
- internals.registered_types_py .erase (type);
1076
- // Actually just `std::erase_if`, but that's only available in C++20
1077
- auto &cache = internals.inactive_override_cache ;
1078
- for (auto it = cache.begin (), last = cache.end (); it != last; ) {
1079
- if (it->first == (PyObject *) type)
1080
- it = cache.erase (it);
1081
- else
1082
- ++it;
1083
- }
1090
+ weakref (m_ptr, cpp_function ([tinfo](handle wr) {
1091
+ cleanup_type_info (tinfo);
1084
1092
wr.dec_ref ();
1085
1093
})).release ();
1086
1094
0 commit comments