@@ -119,22 +119,33 @@ cdef int typenum_from_format(str s) except *:
119
119
raise TypeError (" Format '" + s + " ' can only have native byteorder." )
120
120
return dt.num
121
121
122
+ cdef int descr_to_typenum(object dtype):
123
+ " Returns typenum for argumentd dtype that has attribute descr, assumed numpy.dtype"
124
+ obj = getattr (dtype, ' descr' )
125
+ if (not isinstance (obj, list ) or len (obj) != 1 ):
126
+ return - 1
127
+ obj = obj[0 ]
128
+ if (not isinstance (obj, tuple ) or len (obj) != 2 or obj[0 ]):
129
+ return - 1
130
+ obj = obj[1 ]
131
+ if not isinstance (obj, str ):
132
+ return - 1
133
+ return typenum_from_format(obj)
134
+
122
135
123
136
cdef int dtype_to_typenum(dtype) except * :
124
137
if isinstance (dtype, str ):
125
138
return typenum_from_format(dtype)
126
139
elif isinstance (dtype, bytes):
127
140
return typenum_from_format(dtype.decode(" UTF-8" ))
128
141
elif hasattr (dtype, ' descr' ):
129
- obj = getattr (dtype, ' descr' )
130
- if (not isinstance (obj, list ) or len (obj) != 1 ):
131
- return - 1
132
- obj = obj[0 ]
133
- if (not isinstance (obj, tuple ) or len (obj) != 2 or obj[0 ]):
134
- return - 1
135
- obj = obj[1 ]
136
- if not isinstance (obj, str ):
137
- return - 1
138
- return typenum_from_format(obj)
142
+ return descr_to_typenum(dtype)
139
143
else :
140
- return - 1
144
+ try :
145
+ dt = np.dtype(dtype)
146
+ if hasattr (dt, ' descr' ):
147
+ return descr_to_typenum(dt)
148
+ else :
149
+ return - 1
150
+ except Exception :
151
+ return - 1
0 commit comments