Commit 7edd72db by Ivan Smirnov

Disallow registering dtypes multiple times

parent ccc69f91
...@@ -668,6 +668,9 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> { ...@@ -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) { static void register_dtype(std::initializer_list<field_descriptor> fields) {
if (dtype_ptr)
pybind11_fail("NumPy: dtype is already registered");
list names, formats, offsets; list names, formats, offsets;
for (auto field : fields) { for (auto field : fields) {
if (!field.descr) if (!field.descr)
......
...@@ -335,6 +335,7 @@ test_initializer numpy_dtypes([](py::module &m) { ...@@ -335,6 +335,7 @@ test_initializer numpy_dtypes([](py::module &m) {
m.def("f_simple", [](SimpleStruct s) { return s.y * 10; }); m.def("f_simple", [](SimpleStruct s) { return s.y * 10; });
m.def("f_packed", [](PackedStruct 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("f_nested", [](NestedStruct s) { return s.a.y * 10; });
m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z); });
}); });
#undef PYBIND11_PACKED #undef PYBIND11_PACKED
...@@ -196,3 +196,12 @@ def test_scalar_conversion(): ...@@ -196,3 +196,12 @@ def test_scalar_conversion():
with pytest.raises(TypeError) as excinfo: with pytest.raises(TypeError) as excinfo:
func(arr[0]) func(arr[0])
assert 'incompatible function arguments' in str(excinfo.value) 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment