mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-30 00:47:12 +00:00
Disallow registering dtypes multiple times
This commit is contained in:
parent
ccc69f91f4
commit
7edd72db24
@ -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) {
|
|||||||
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():
|
|||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user