@@ -292,37 +292,31 @@ append!(a::PyVector{T}, items) where {T} = PyVector{T}(append!(a.o, items))
292
292
# ########################################################################
293
293
# Lists and 1d arrays.
294
294
295
+ if VERSION < v " 1.1.0-DEV.392" # #29440
296
+ cirange (I,J) = CartesianIndices (map ((i,j) -> i: j, Tuple (I), Tuple (J)))
297
+ else
298
+ cirange (I,J) = I: J
299
+ end
300
+
295
301
# recursive conversion of A to a list of list of lists... starting
296
- # with dimension dim and index i in A.
297
- function array2py (A:: AbstractArray{T , N} , dim:: Integer , i:: Integer ) where {T, N}
298
- if dim > N
302
+ # with dimension dim and Cartesian index i in A.
303
+ function array2py (A:: AbstractArray{<:Any , N} , dim:: Integer , i:: CartesianIndex{N} ) where {N}
304
+ if dim > N # base case
299
305
return PyObject (A[i])
300
- elseif dim == N # special case last dim to coarsen recursion leaves
301
- len = size (A, dim)
302
- s = N == 1 ? 1 : stride (A, dim)
303
- o = PyObject (@pycheckn ccall ((@pysym :PyList_New ), PyPtr, (Int,), len))
304
- for j = 0 : len- 1
305
- oi = PyObject (A[i+ j* s])
306
- @pycheckz ccall ((@pysym :PyList_SetItem ), Cint, (PyPtr,Int,PyPtr),
307
- o, j, oi)
308
- pyincref (oi) # PyList_SetItem steals the reference
309
- end
310
- return o
311
- else # dim < N: store multidimensional array as list of lists
312
- len = size (A, dim)
313
- s = stride (A, dim)
314
- o = PyObject (@pycheckn ccall ((@pysym :PyList_New ), PyPtr, (Int,), len))
315
- for j = 0 : len- 1
316
- oi = array2py (A, dim+ 1 , i+ j* s)
306
+ else # recursively store multidimensional array as list of lists
307
+ ilast = CartesianIndex (ntuple (j -> j == dim ? lastindex (A, dim) : i[j], Val {N} ()))
308
+ o = PyObject (@pycheckn ccall ((@pysym :PyList_New ), PyPtr, (Int,), size (A, dim)))
309
+ for icur in cirange (i,ilast)
310
+ oi = array2py (A, dim+ 1 , icur)
317
311
@pycheckz ccall ((@pysym :PyList_SetItem ), Cint, (PyPtr,Int,PyPtr),
318
- o, j , oi)
312
+ o, icur[dim] - i[dim] , oi)
319
313
pyincref (oi) # PyList_SetItem steals the reference
320
314
end
321
315
return o
322
316
end
323
317
end
324
318
325
- array2py (A:: AbstractArray ) = array2py (A, 1 , 1 )
319
+ array2py (A:: AbstractArray ) = array2py (A, 1 , first ( CartesianIndices (A)) )
326
320
327
321
PyObject (A:: AbstractArray ) =
328
322
ndims (A) <= 1 || hasmethod (stride, Tuple{typeof (A),Int}) ? array2py (A) :
0 commit comments