yglukhov / nimpy

Nim - Python bridge
MIT License
1.48k stars 60 forks source link

passing pointer? #256

Closed JorySchossau closed 2 years ago

JorySchossau commented 2 years ago

I'm trying to avoid a data copy, and use numpy.as_array to create a numpy array from data I already have in the right format, but I don't know how to package a pointer for nimpy to pass along. Is there some fancy way to use PyCapsules for this? I see a previous user has asked, but closed their own issue without any information.

JorySchossau commented 2 years ago

To deter more #denvercoder9 situations, this is for posterity:

   let np = pyImport("numpy")                                                                         
   let ctypes = pyImport("ctypes")                                                                    
   var mem = cast[ptr UncheckedArray[cfloat]](alloc0 4*sizeof(cfloat))                                    
   var thepointer = ctypes.cast(cast[uint](mem),ctypes.POINTER(nimpy.getAttr(ctypes, "c_float")))     
   var numpyarray = np.ctypeslib.as_array(thepointer, (4,))