Disallow registering dtypes multiple times

This commit is contained in:
Ivan Smirnov 2016-10-20 16:57:12 +01:00
parent ccc69f91f4
commit 7edd72db24
3 changed files with 13 additions and 0 deletions

View File

@ -668,6 +668,9 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> {
}
static void register_dtype(std::initializer_list<field_descriptor> fields) {
if (dtype_ptr)
pybind11_fail("NumPy: dtype is already registered");
list names, formats, offsets;
for (auto field : fields) {
if (!field.descr)

View File

@ -335,6 +335,7 @@ test_initializer numpy_dtypes([](py::module &m) {
m.def("f_simple", [](SimpleStruct s) { return s.y * 10; });
m.def("f_packed", [](PackedStruct s) { return s.y * 10; });
m.def("f_nested", [](NestedStruct s) { return s.a.y * 10; });
m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z); });
});
#undef PYBIND11_PACKED

View File

@ -196,3 +196,12 @@ def test_scalar_conversion():
with pytest.raises(TypeError) as excinfo:
func(arr[0])
assert 'incompatible function arguments' in str(excinfo.value)
@pytest.requires_numpy
def test_register_dtype():
from pybind11_tests import register_dtype
with pytest.raises(RuntimeError) as excinfo:
register_dtype()
assert 'dtype is already registered' in str(excinfo.value)