@@ -187,7 +187,7 @@ struct PyArraySource_ArrayInterface <: PyArraySource
187187 dict:: Py
188188 ptr:: Ptr{Cvoid}
189189 readonly:: Bool
190- handle:: Py
190+ handle:: Any
191191end
192192function PyArraySource_ArrayInterface (x:: Py , d:: Py = x. __array_interface__)
193193 # offset
@@ -202,14 +202,16 @@ function PyArraySource_ArrayInterface(x::Py, d::Py = x.__array_interface__)
202202 ptr = Ptr {Cvoid} (pyconvert (UInt, data[0 ]))
203203 readonly = pyconvert (Bool, data[1 ])
204204 pydel! (data)
205- handle = Py (( x, d) )
205+ handle = ( x, d)
206206 else
207- memview = @py memoryview (data === None ? x : data)
208- pydel! (data)
209- buf = UnsafePtr (C. PyMemoryView_GET_BUFFER (memview))
210- ptr = buf. buf[! ]
211- readonly = buf. readonly[] != 0
212- handle = Py ((x, memview))
207+ buf = Ref {C.Py_buffer} ()
208+ if C. PyObject_GetBuffer (data === None ? x : data, buf, C. PyBUF_RECORDS) < 0
209+ pythrow ()
210+ end
211+ finalizer (C. PyBuffer_Release, buf)
212+ ptr = buf[]. buf
213+ readonly = buf[]. readonly != 0
214+ handle = buf
213215 end
214216 PyArraySource_ArrayInterface (x, d, ptr, readonly, handle)
215217end
@@ -525,13 +527,15 @@ end
525527
526528struct PyArraySource_Buffer <: PyArraySource
527529 obj:: Py
528- memview:: Py
529- buf:: C.UnsafePtr{C.Py_buffer}
530+ buf:: Base.RefValue{C.Py_buffer}
530531end
531532function PyArraySource_Buffer (x:: Py )
532- memview = pybuiltins. memoryview (x)
533- buf = C. UnsafePtr (C. PyMemoryView_GET_BUFFER (memview))
534- PyArraySource_Buffer (x, memview, buf)
533+ buf = Ref {C.Py_buffer} ()
534+ if C. PyObject_GetBuffer (x, buf, C. PyBUF_RECORDS) < 0
535+ pythrow ()
536+ end
537+ finalizer (C. PyBuffer_Release, buf)
538+ PyArraySource_Buffer (x, buf)
535539end
536540
537541const PYARRAY_BUFFERFORMAT_TO_TYPE = let c = Utils. islittleendian () ? ' <' : ' >'
@@ -578,20 +582,20 @@ pyarray_bufferformat_to_type(fmt::String) = get(
578582)
579583
580584function pyarray_get_R (src:: PyArraySource_Buffer )
581- ptr = src. buf. format[]
582- return ptr == C_NULL ? UInt8 : pyarray_bufferformat_to_type (String (ptr))
585+ ptr = src. buf[] . format
586+ return ptr == C_NULL ? UInt8 : pyarray_bufferformat_to_type (unsafe_string (ptr))
583587end
584588
585- pyarray_get_ptr (src:: PyArraySource_Buffer , :: Type{R} ) where {R} = Ptr {R} (src. buf. buf[ ! ] )
589+ pyarray_get_ptr (src:: PyArraySource_Buffer , :: Type{R} ) where {R} = Ptr {R} (src. buf[] . buf )
586590
587- pyarray_get_N (src:: PyArraySource_Buffer ) = Int (src. buf. ndim[] )
591+ pyarray_get_N (src:: PyArraySource_Buffer ) = Int (src. buf[] . ndim )
588592
589593function pyarray_get_size (src:: PyArraySource_Buffer , :: Val{N} ) where {N}
590- size = src. buf. shape[]
594+ size = src. buf[] . shape
591595 if size == C_NULL
592- N == 0 ? () : N == 1 ? (Int (src. buf. len[] ),) : @assert false
596+ N == 0 ? () : N == 1 ? (Int (src. buf[] . len ),) : @assert false
593597 else
594- ntuple (i -> Int (size[i] ), N)
598+ ntuple (i -> Int (unsafe_load ( size, i) ), N)
595599 end
596600end
597601
@@ -601,18 +605,18 @@ function pyarray_get_strides(
601605 :: Type{R} ,
602606 size:: NTuple{N,Int} ,
603607) where {N,R}
604- strides = src. buf. strides[]
608+ strides = src. buf[] . strides
605609 if strides == C_NULL
606- itemsize = src. buf. shape[] == C_NULL ? 1 : src. buf. itemsize[]
610+ itemsize = src. buf[] . shape == C_NULL ? 1 : src. buf[] . itemsize
607611 Utils. size_to_cstrides (itemsize, size)
608612 else
609- ntuple (i -> Int (strides[i] ), N)
613+ ntuple (i -> Int (unsafe_load ( strides, i) ), N)
610614 end
611615end
612616
613- pyarray_get_M (src:: PyArraySource_Buffer ) = src. buf. readonly[] == 0
617+ pyarray_get_M (src:: PyArraySource_Buffer ) = src. buf[] . readonly == 0
614618
615- pyarray_get_handle (src:: PyArraySource_Buffer ) = src. memview
619+ pyarray_get_handle (src:: PyArraySource_Buffer ) = src. buf
616620
617621# AbstractArray methods
618622
0 commit comments