Ticket #156: patch-napi.py_4rc2.diff

File patch-napi.py_4rc2.diff, 3.0 KB (added by Cpascual, 3 years ago)

proposed patch to support arrays of strings in the python bindings

  • .py

    old new  
    11681168            datafn = lambda: pdata.value 
    11691169        else: 
    11701170            # numeric - use numpy array 
    1171             if dtype=='char': dtype = '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) 
    11731173            if len(shape) == 1 and shape[0] == 1: 
    11741174                datafn = lambda: data[0] 
    11751175            else: 
     
    11851185        with its pointer. 
    11861186        """ 
    11871187        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,) 
    11931190        else: 
    11941191            # Convert scalars to vectors of length one 
    11951192            if numpy.prod(shape) == 1 and not hasattr(data,'shape'): 
    11961193                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 
    12051196            if str(data.dtype) != dtype: 
    12061197                raise ValueError,\ 
    12071198                    "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) 
    12081208 
    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             
    12181212        return data,pdata 
    12191213 
    12201214    def show(self, path=None, indent=0):