Skip to content

Commit e7378b7

Browse files
committed
Fortran order is not supported, raise an error when trying to read an
array saved using it
1 parent 7c445e3 commit e7378b7

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

NumPyArray.wl

+38-33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ numeric data types are supported"
1313
ReadNumPyArray::invalidFile = "`1` is not a valid NPY file"
1414
ReadNumPyArray::invalidVersion = "NPY version `1`.`2` not supported"
1515
ReadNumPyArray::invalidDType = "Invalid dtype `1`"
16+
ReadNumPyArray::fortranOrder = "Fortran order is not supported"
1617

1718
Begin["Private`"]
1819

@@ -38,41 +39,45 @@ ReadNumPyArray[file_] := Module[{fs, formatVersion, headerLength, retVal},
3839
fortranOrder = headerDic[["fortran_order"]];
3940
shape = headerDic[["shape"]];
4041

41-
(* Endianness *)
42-
byteOrder =
43-
Switch[StringTake[dtype, 1], "<", -1, ">", 1, _, $ByteOrdering];
42+
(* Fortran order is not supported *)
43+
If[! fortranOrder,
4444

45-
(* Data type *)
46-
pyType = StringTake[dtype, {2, -1}];
47-
mathematicaDataType = Switch[pyType,
48-
"b1", "Integer8",
49-
"B1", "UnsignedInteger8",
50-
"i1", "Integer8",
51-
"u1", "UnsignedInteger8",
52-
"i2", "Integer16",
53-
"u2", "UnsignedInteger16",
54-
"i4", "Integer32",
55-
"u4", "UnsignedInteger32",
56-
"i8", "Integer64",
57-
"u8", "UnsignedInteger64",
58-
"i16", "Integer128",
59-
"u16", "UnsignedInteger128",
60-
"f4", "Real32",
61-
"f8", "Real64",
62-
(* "f16", "Real128", *)
63-
"c8", "Complex64",
64-
"c16", "Complex128",
65-
(* "c32", "Complex256", *)
66-
_, Message[ReadNumPyArray::invalidDType, pyType]; "Unknown"
67-
];
45+
(* Endianness *)
46+
byteOrder =
47+
Switch[StringTake[dtype, 1], "<", -1, ">", 1, _, $ByteOrdering];
6848

69-
(* Read data *)
70-
If[mathematicaDataType == "Unknown", $Failed,
71-
data =
72-
BinaryReadList[fs, mathematicaDataType,
73-
ByteOrdering -> byteOrder];
74-
ArrayReshape[data, shape]
75-
]
49+
(* Data type *)
50+
pyType = StringTake[dtype, {2, -1}];
51+
mathematicaDataType = Switch[pyType,
52+
"b1", "Integer8",
53+
"B1", "UnsignedInteger8",
54+
"i1", "Integer8",
55+
"u1", "UnsignedInteger8",
56+
"i2", "Integer16",
57+
"u2", "UnsignedInteger16",
58+
"i4", "Integer32",
59+
"u4", "UnsignedInteger32",
60+
"i8", "Integer64",
61+
"u8", "UnsignedInteger64",
62+
"i16", "Integer128",
63+
"u16", "UnsignedInteger128",
64+
"f4", "Real32",
65+
"f8", "Real64",
66+
(* "f16", "Real128", *)
67+
"c8", "Complex64",
68+
"c16", "Complex128",
69+
(* "c32", "Complex256", *)
70+
_, Message[ReadNumPyArray::invalidDType, pyType]; "Unknown"
71+
];
72+
73+
(* Read data *)
74+
If[mathematicaDataType == "Unknown", $Failed,
75+
data =
76+
BinaryReadList[fs, mathematicaDataType,
77+
ByteOrdering -> byteOrder];
78+
ArrayReshape[data, shape]
79+
]
80+
, Message[ReadNumPyArray::fortranOrder]; $Failed]
7681
, Message[ReadNumPyArray::invalidVersion, formatVersion[[1]],
7782
formatVersion[[2]]]; $Failed]
7883
, Message[ReadNumPyArray::invalidFile, file]; $Failed];

0 commit comments

Comments
 (0)