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

@ -8,10 +8,11 @@ Changelog
* Redesigned virtual call mechanism and user-facing syntax (breaking change!)
* Prevent implicit conversion of floating point values to integral types in
function arguments
* Transparent conversion of sparse and dense Eigen data types
* Transparent conversion of sparse and dense Eigen matrices and vectors
* ``std::vector<>`` type bindings analogous to Boost.Python's ``indexing_suite``
* Fixed incorrect default return value policy for functions returning a shared
pointer
* Don't allow registering a type via ``class_`` twice
* Don't allow casting a ``None`` value into a C++ lvalue reference
* Fixed a crash in ``enum_::operator==`` that was triggered by the ``help()`` command
* Improved detection of whether or not custom C++ types can be copy/move-constructed

View File

@ -131,4 +131,11 @@ void init_issues(py::module &m) {
.def("f", &A::f);
m2.def("call_f", call_f);
try {
py::class_<Placeholder>(m2, "Placeholder");
throw std::logic_error("Expected an exception!");
} catch (std::runtime_error &e) {
/* All good */
}
}

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;