Skip to content

Commit 4b2e73b

Browse files
author
Dorian Birraux
committed
Adding error message when PA type is invalid.
1 parent 0ef46f7 commit 4b2e73b

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

wolframclient/language/array.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ def __init__(self, array, type, shape=None):
1616
self.array = array
1717
self.shape = shape or (len(array),)
1818
self.type = type
19-
try:
20-
self.struct = constants.STRUCT_MAPPING[type]
21-
except KeyError:
19+
self._valid_type_or_fail(type)
20+
self.struct = constants.STRUCT_MAPPING[type]
21+
22+
def _valid_type_or_fail(self, type):
23+
if type not in constants.STRUCT_MAPPING:
2224
raise WolframLanguageException(
2325
"Type %s is not one of the supported array types: %s."
2426
% (type, ", ".join(constants.STRUCT_MAPPING.keys()))
@@ -35,4 +37,10 @@ def __len__(self):
3537

3638

3739
class PackedArray(NumericArray):
38-
pass
40+
41+
def _valid_type_or_fail(self, type):
42+
if type not in constants.VALID_PACKED_ARRAY_LABEL_TYPES:
43+
raise WolframLanguageException(
44+
"Type %s is not one of the supported packed array types: %s."
45+
% (type, ', '.join(constants.VALID_PACKED_ARRAY_LABEL_TYPES_TUPLE))
46+
)

wolframclient/serializers/wxfencoder/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def _bytes(value):
9191
)
9292
)
9393

94+
VALID_PACKED_ARRAY_LABEL_TYPES_TUPLE = ('Integer8', 'Integer16', 'Integer32', 'Integer64', 'Real32', 'Real64', 'ComplexReal32', 'ComplexReal64')
95+
VALID_PACKED_ARRAY_LABEL_TYPES = frozenset(VALID_PACKED_ARRAY_LABEL_TYPES_TUPLE)
96+
9497
STRUCT_MAPPING = Settings(
9598
Integer8=struct.Struct(b"<b"),
9699
UnsignedInteger8=struct.Struct(b"<B"),
@@ -104,4 +107,4 @@ def _bytes(value):
104107
Real64=struct.Struct(b"<d"),
105108
ComplexReal32=struct.Struct(b"<f"),
106109
ComplexReal64=struct.Struct(b"<d"),
107-
)
110+
)

0 commit comments

Comments
 (0)