don't allow registering a class twice (fixes #218)

This commit is contained in:
Wenzel Jakob
2016-05-31 09:53:28 +02:00
parent 5eda97d7e4
commit 38d8b8cfe2
3 changed files with 18 additions and 3 deletions

View File

@@ -520,6 +520,14 @@ protected:
}
}
auto &internals = get_internals();
auto tindex = std::type_index(*(rec->type));
if (internals.registered_types_cpp.find(tindex) !=
internals.registered_types_cpp.end())
pybind11_fail("generic_type: type \"" + std::string(rec->name) +
"\" is already registered!");
object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false);
object name(PYBIND11_FROM_STRING(rec->name), false);
auto type = (PyHeapTypeObject*) type_holder.ptr();
@@ -528,12 +536,11 @@ protected:
pybind11_fail("generic_type: unable to create type object!");
/* Register supplemental type information in C++ dict */
auto &internals = get_internals();
detail::type_info *tinfo = new detail::type_info();
tinfo->type = (PyTypeObject *) type;
tinfo->type_size = rec->type_size;
tinfo->init_holder = rec->init_holder;
internals.registered_types_cpp[std::type_index(*(rec->type))] = tinfo;
internals.registered_types_cpp[tindex] = tinfo;
internals.registered_types_py[type] = tinfo;
object scope_module;