Skip to content

Commit 219f9a2

Browse files
author
Christopher Doris
committed
move PyObjectArray into API
1 parent c2033bd commit 219f9a2

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

src/API/exports.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export pyjlvalue
133133
export pyjlraw
134134
export pybinaryio
135135
export pytextio
136+
export PyObjectVector
137+
export PyObjectMatrix
138+
export PyObjectArray
136139

137140
# Compat
138141
export pytable

src/API/types.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,21 @@ struct PySet{T} <: AbstractSet{T}
229229
py::Py
230230
PySet{T}(x = pyset()) where {T} = new{T}(ispy(x) ? Py(x) : pyset(x))
231231
end
232+
233+
"""
234+
PyObjectArray(undef, dims...)
235+
PyObjectArray(array)
236+
237+
An array of `Py`s which supports the Python buffer protocol.
238+
239+
Internally, the objects are stored as an array of pointers.
240+
"""
241+
mutable struct PyObjectArray{N} <: AbstractArray{Py,N}
242+
ptrs::Array{Ptr{Cvoid},N}
243+
function PyObjectArray{N}(::UndefInitializer, dims::NTuple{N,Integer}) where {N}
244+
x = new{N}(fill(C_NULL, dims))
245+
finalizer(JlWrap.pyobjectarray_finalizer, x)
246+
end
247+
end
248+
const PyObjectVector = PyObjectArray{1}
249+
const PyObjectMatrix = PyObjectArray{2}

src/JlWrap/JlWrap.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import ..PythonCall:
2424
pyjlvalue,
2525
pyjlraw,
2626
pybinaryio,
27-
pytextio
27+
pytextio,
28+
PyObjectVector,
29+
PyObjectMatrix,
30+
PyObjectArray
2831

2932
using Pkg: Pkg
3033
using Base: @propagate_inbounds, allocatedinline

src/JlWrap/objectarray.jl

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
"""
2-
PyObjectArray(undef, dims...)
3-
PyObjectArray(array)
4-
5-
An array of `Py`s which supports the Python buffer protocol.
6-
7-
Internally, the objects are stored as an array of pointers.
8-
"""
9-
mutable struct PyObjectArray{N} <: AbstractArray{Py,N}
10-
ptrs::Array{C.PyPtr,N}
11-
function PyObjectArray{N}(::UndefInitializer, dims::NTuple{N,Integer}) where {N}
12-
x = new{N}(fill(C.PyNULL, dims))
13-
finalizer(pyobjectarray_finalizer, x)
14-
end
15-
end
16-
const PyObjectVector = PyObjectArray{1}
17-
const PyObjectMatrix = PyObjectArray{2}
18-
export PyObjectVector, PyObjectMatrix, PyObjectArray
19-
201
PyObjectArray{N}(undef::UndefInitializer, dims::Vararg{Integer,N}) where {N} =
212
PyObjectArray(undef, dims)
223
PyObjectArray(undef::UndefInitializer, dims::NTuple{N,Integer}) where {N} =
@@ -44,20 +25,20 @@ end
4425
@boundscheck checkbounds(x, i...)
4526
@inbounds ptr = x.ptrs[i...]
4627
ptr == C_NULL && throw(UndefRefError())
47-
return pynew(incref(ptr))
28+
return pynew(incref(Ptr{C.PyPtr}(ptr)))
4829
end
4930

5031
@propagate_inbounds function Base.setindex!(x::PyObjectArray, v, i::Integer...)
5132
@boundscheck checkbounds(x, i...)
5233
v_ = Py(v)
53-
@inbounds decref(x.ptrs[i...])
54-
@inbounds x.ptrs[i...] = incref(getptr(v_))
34+
@inbounds decref(C.PyPtr(x.ptrs[i...]))
35+
@inbounds x.ptrs[i...] = getptr(incref(v_))
5536
return x
5637
end
5738

5839
@propagate_inbounds function Base.deleteat!(x::PyObjectVector, i::Integer)
5940
@boundscheck checkbounds(x, i)
60-
@inbounds decref(x.ptrs[i])
41+
@inbounds decref(C.PyPtr(x.ptrs[i]))
6142
deleteat!(x.ptrs, i)
6243
return x
6344
end

0 commit comments

Comments
 (0)