Ticket #156: patch-napi.py_4rc2.diff
| File patch-napi.py_4rc2.diff, 3.0 KB (added by Cpascual, 3 years ago) |
|---|
-
.py
old new 1168 1168 datafn = lambda: pdata.value 1169 1169 else: 1170 1170 # numeric - use numpy array 1171 if dtype=='char': d type = 'uint8'1172 data = numpy.zeros(shape,dtype)1171 if dtype=='char': data = numpy.zeros(shape[:-1],dtype = 'S%i'%shape[-1]) 1172 else: data = numpy.zeros(shape,dtype) 1173 1173 if len(shape) == 1 and shape[0] == 1: 1174 1174 datafn = lambda: data[0] 1175 1175 else: … … 1185 1185 with its pointer. 1186 1186 """ 1187 1187 if dtype == "char": 1188 # Character data - pad with zeros to the right length 1189 if not _is_string_like(data): 1190 raise ValueError,"Expected character data: %s"%(self._loc()) 1191 if len(data) < shape[0]: 1192 data += '\000'*(shape[0]-len(data)) 1188 data=numpy.array(data).astype('S%i'%shape[-1]) 1189 data_shape=data.shape+(data.itemsize,) 1193 1190 else: 1194 1191 # Convert scalars to vectors of length one 1195 1192 if numpy.prod(shape) == 1 and not hasattr(data,'shape'): 1196 1193 data = numpy.array([data],dtype=dtype) 1197 # Check that dimensions match 1198 # Ick! need to exclude dimensions of length 1 in order to catch 1199 # array slices such as a[:,1], which only report one dimension 1200 input_shape = numpy.array([i for i in data.shape if i != 1]) 1201 target_shape = numpy.array([i for i in shape if i != 1]) 1202 if len(input_shape) != len(target_shape) or (input_shape != target_shape).any(): 1203 raise ValueError,\ 1204 "Shape mismatch %s!=%s: %s"%(data.shape,shape,self.filename) 1194 data_shape=data.shape 1195 #Check data type 1205 1196 if str(data.dtype) != dtype: 1206 1197 raise ValueError,\ 1207 1198 "Type mismatch %s!=%s: %s"%(dtype,data.dtype,self._loc()) 1199 1200 # Check that dimensions match 1201 # Ick! need to exclude dimensions of length 1 in order to catch 1202 # array slices such as a[:,1], which only report one dimension 1203 input_shape = numpy.array([i for i in data_shape if i != 1]) 1204 target_shape = numpy.array([i for i in shape if i != 1]) 1205 if len(input_shape) != len(target_shape) or (input_shape != target_shape).any(): 1206 raise ValueError,\ 1207 "Shape mismatch %s!=%s: %s"%(data_shape,shape,self.filename) 1208 1208 1209 if dtype == 'char': 1210 # String: hand it over as usual for strings. Assumes the string 1211 # is the correct length for the storage area. 1212 pdata = data 1213 else: 1214 # Vector: assume it is of the correct storage class and size 1215 data = numpy.ascontiguousarray(data) 1216 pdata = data.ctypes.data 1217 1209 data = numpy.ascontiguousarray(data) 1210 pdata = data.ctypes.data 1211 1218 1212 return data,pdata 1219 1213 1220 1214 def show(self, path=None, indent=0):
