diff --git a/docs/changelog.rst b/docs/changelog.rst index 7cf26e107..c9e988b1d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 diff --git a/example/issues.cpp b/example/issues.cpp index e1647081f..dfe20fff8 100644 --- a/example/issues.cpp +++ b/example/issues.cpp @@ -131,4 +131,11 @@ void init_issues(py::module &m) { .def("f", &A::f); m2.def("call_f", call_f); + + try { + py::class_(m2, "Placeholder"); + throw std::logic_error("Expected an exception!"); + } catch (std::runtime_error &e) { + /* All good */ + } } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 964aea90e..4ffe1108f 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -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;