Skip to content

Commit abe0d62

Browse files
authored
fix: custom type check for unsupported type (#2407)
1 parent beaa723 commit abe0d62

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/ansys/dpf/core/custom_type_field.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class CustomTypeField(_FieldBase):
6464
The ``CustomTypeField`` class gives you the ability to choose the most optimized unitary
6565
data type for a given usage, and hence, allows you to optimize memory usage.
6666
67+
The unitary data type must have an equivalent ctype (check this with ``np.ctypeslib.as_ctypes_type``).
68+
6769
This can be evaluated data from the :class:`Operator <ansys.dpf.core.Operator>` class
6870
or created directly by an instance of this class.
6971
@@ -75,6 +77,7 @@ class CustomTypeField(_FieldBase):
7577
----------
7678
unitary_type : numpy.dtype
7779
The data vector of the Field will be a vector of this custom unitary type.
80+
This dtype must have an equivalent ctype (check this with ``np.ctypeslib.as_ctypes_type``).
7881
nentities : int, optional
7982
Number of entities reserved. The default is ``0``.
8083
field : CustomTypeField, ansys.grpc.dpf.field_pb2.Field, ctypes.c_void_p, optional
@@ -118,6 +121,13 @@ def __init__(
118121
raise errors.DpfVersionNotSupported("5.0")
119122
if unitary_type is not None:
120123
self._type = np.dtype(unitary_type)
124+
try:
125+
# Check the type can be converted to a ctype
126+
np.ctypeslib.as_ctypes_type(self._type)
127+
except NotImplementedError as e:
128+
raise ValueError(
129+
f"CustomTypeField: invalid unitary_type {self._type} (numpy: NotImplementedError: {e})."
130+
)
121131
else:
122132
self._type = unitary_type
123133
super().__init__(nentities=nentities, field=field, server=server)

src/ansys/dpf/gate/dpf_vector.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def __del__(self):
164164
class DPFVectorCustomType(DPFVectorBase):
165165
def __init__(self, unitary_type, owner=None, api=dpf_vector_capi.DpfVectorCAPI):
166166
self.type = unitary_type
167+
try:
168+
# Check the type can be converted to a ctype
169+
np.ctypeslib.as_ctypes_type(self.type)
170+
except NotImplementedError as e:
171+
raise ValueError(
172+
f"DPFVectorCustomType: invalid unitary_type {self.type} (numpy: NotImplementedError: {e})."
173+
)
167174
super().__init__(owner, api)
168175
self._array = MutableListChar()
169176

tests/test_custom_type_field.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
import numpy as np
24+
import pytest
2425

2526
from ansys import dpf
2627
from ansys.dpf import core
@@ -282,6 +283,9 @@ def test_check_types_custom_type_field(server_type):
282283
pfield4 = core.CustomTypeField(np.float64, server=server_type)
283284
pfield5 = core.CustomTypeField(np.int8, server=server_type)
284285

286+
with pytest.raises(ValueError, match="CustomTypeField: invalid unitary_type"):
287+
_ = core.CustomTypeField(np.complex128, server=server_type)
288+
285289
forward = dpf.core.operators.utility.forward(server=server_type)
286290
forward.connect(0, pfield)
287291
forward.connect(1, pfield2)

tests/test_dpf_vector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from ansys.dpf import core as dpf
2626
from ansys.dpf.core import fields_factory
27+
from ansys.dpf.gate.dpf_vector import DPFVectorCustomType
2728
import conftest
2829

2930

@@ -104,3 +105,8 @@ def test_update_empty_dpf_vector_custom_type_field(server_type):
104105
dp = field._data_pointer
105106
dp = None
106107
assert np.allclose(field.get_entity_data(1), [0])
108+
109+
110+
def test_invalid_unitary_type_dpf_vector_custom_type(server_type):
111+
with pytest.raises(ValueError, match="DPFVectorCustomType: invalid unitary_type"):
112+
DPFVectorCustomType(unitary_type=np.complex128)

0 commit comments

Comments
 (0)