Skip to content

Commit 55e11de

Browse files
author
Christopher Doris
committed
move wrapper types into separate modules
1 parent a086fdf commit 55e11de

File tree

12 files changed

+187
-94
lines changed

12 files changed

+187
-94
lines changed

src/Convert/Convert.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ using Dates: Date, Time, DateTime, Second, Millisecond, Microsecond, Nanosecond
5050
import PythonCall:
5151
pyconvert, @pyconvert, pyconvert_add_rule, pyconvert_return, pyconvert_unconverted
5252

53+
# internal API
54+
export pyconvert_add_rule,
55+
pyconvert_return,
56+
pyconvert_isunconverted,
57+
pyconvert_result,
58+
pyconvert_tryconvert,
59+
PYCONVERT_PRIORITY_ARRAY,
60+
PYCONVERT_PRIORITY_CANONICAL,
61+
PYCONVERT_PRIORITY_NORMAL
62+
5363
include("pyconvert.jl")
5464
include("rules.jl")
5565
include("ctypes.jl")

src/Core/Core.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ import PythonCall:
128128
pydel!,
129129
unsafe_pynext
130130

131+
# internal API
132+
export pynew,
133+
pyisnull,
134+
pycopy!,
135+
getptr,
136+
pydel!,
137+
unsafe_pynext,
138+
PyNULL,
139+
@autopy,
140+
pystr_fromUTF8,
141+
pystr_asUTF8vector,
142+
pybytes_asvector
143+
131144
const ROOT_DIR = dirname(dirname(@__DIR__))
132145

133146
include("Py.jl")

src/Wrap/PyArray.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
module PyArrays
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...C
6+
using ...Core
7+
using ...Convert
8+
9+
using Base: @propagate_inbounds
10+
11+
import ...PythonCall: PyArray, ispy, Py
12+
13+
114
struct UnsafePyObject
215
ptr::C.PyPtr
316
end
@@ -670,3 +683,23 @@ function pyarray_check_T(::Type{T}, ::Type{R}) where {T,R}
670683
error("invalid eltype T=$T for raw eltype R=$R")
671684
end
672685
end
686+
687+
function __init__()
688+
priority = PYCONVERT_PRIORITY_ARRAY
689+
pyconvert_add_rule("<arraystruct>", PyArray, pyconvert_rule_array_nocopy, priority)
690+
pyconvert_add_rule("<arrayinterface>", PyArray, pyconvert_rule_array_nocopy, priority)
691+
pyconvert_add_rule("<array>", PyArray, pyconvert_rule_array_nocopy, priority)
692+
pyconvert_add_rule("<buffer>", PyArray, pyconvert_rule_array_nocopy, priority)
693+
694+
priority = PYCONVERT_PRIORITY_NORMAL
695+
pyconvert_add_rule("<arraystruct>", Array, pyconvert_rule_array, priority)
696+
pyconvert_add_rule("<arrayinterface>", Array, pyconvert_rule_array, priority)
697+
pyconvert_add_rule("<array>", Array, pyconvert_rule_array, priority)
698+
pyconvert_add_rule("<buffer>", Array, pyconvert_rule_array, priority)
699+
pyconvert_add_rule("<arraystruct>", AbstractArray, pyconvert_rule_array, priority)
700+
pyconvert_add_rule("<arrayinterface>", AbstractArray, pyconvert_rule_array, priority)
701+
pyconvert_add_rule("<array>", AbstractArray, pyconvert_rule_array, priority)
702+
pyconvert_add_rule("<buffer>", AbstractArray, pyconvert_rule_array, priority)
703+
end
704+
705+
end

src/Wrap/PyDict.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
module PyDicts
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Core
6+
using ...Convert
7+
8+
import ...PythonCall: PyDict, ispy, Py
9+
110
PyDict{K}(x = pydict()) where {K} = PyDict{K,Py}(x)
211
PyDict(x = pydict()) = PyDict{Py,Py}(x)
312

@@ -99,3 +108,14 @@ function Base.get!(f::Union{Function,Type}, x::PyDict{K,V}, k) where {K,V}
99108
return x[k] = convert(V, f())
100109
end
101110
end
111+
112+
function __init__()
113+
pyconvert_add_rule(
114+
"collections.abc:Mapping",
115+
PyDict,
116+
pyconvert_rule_mapping,
117+
PYCONVERT_PRIORITY_CANONICAL,
118+
)
119+
end
120+
121+
end

src/Wrap/PyIO.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
module PyIOs
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Core
6+
using ...Convert
7+
8+
import ...PythonCall: PyIO, ispy, Py
9+
110
pyio_finalize!(io::PyIO) = begin
211
C.CTX.is_initialized || return
312
io.own ? close(io) : flush(io)
@@ -213,3 +222,10 @@ function Base.position(io::PyIO)
213222
return pyconvert(Int, @py io.tell()) - length(io.ibuf)
214223
end
215224
end
225+
226+
function __init__()
227+
pyconvert_add_rule("io:IOBase", PyIO, pyconvert_rule_io, PYCONVERT_PRIORITY_CANONICAL)
228+
pyconvert_add_rule("_io:_IOBase", PyIO, pyconvert_rule_io, PYCONVERT_PRIORITY_CANONICAL)
229+
end
230+
231+
end

src/Wrap/PyIterable.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
module PyIterables
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Core
6+
using ...Convert
7+
8+
import ...PythonCall: PyIterable, ispy, Py
9+
110
PyIterable(x) = PyIterable{Py}(x)
211

312
ispy(x::PyIterable) = true
@@ -23,3 +32,14 @@ function pyconvert_rule_iterable(
2332
) where {T<:PyIterable,T1}
2433
pyconvert_return(T1(x))
2534
end
35+
36+
function __init__()
37+
pyconvert_add_rule(
38+
"collections.abc:Iterable",
39+
PyIterable,
40+
pyconvert_rule_iterable,
41+
PYCONVERT_PRIORITY_CANONICAL,
42+
)
43+
end
44+
45+
end

src/Wrap/PyList.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
module PyLists
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Core
6+
using ...Convert
7+
8+
import ...PythonCall: PyList, ispy, Py
9+
110
PyList(x = pylist()) = PyList{Py}(x)
211

312
ispy(::PyList) = true
@@ -88,3 +97,20 @@ end
8897
function Base.copy(x::PyList{T}) where {T}
8998
return PyList{T}(@py x.copy())
9099
end
100+
101+
function __init__()
102+
pyconvert_add_rule(
103+
"collections.abc:Sequence",
104+
PyList,
105+
pyconvert_rule_sequence,
106+
PYCONVERT_PRIORITY_CANONICAL,
107+
)
108+
pyconvert_add_rule(
109+
"pandas.core.arrays.base:ExtensionArray",
110+
PyList,
111+
pyconvert_rule_sequence,
112+
PYCONVERT_PRIORITY_CANONICAL,
113+
)
114+
end
115+
116+
end

src/Wrap/PyPandasDataFrame.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
module PyPandasDataFrames
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Convert
6+
7+
using Tables: Tables
8+
9+
import ...PythonCall: PyPandasDataFrame, ispy, Py
10+
111
ispy(x::PyPandasDataFrame) = true
212
Py(x::PyPandasDataFrame) = x.py
313

@@ -66,3 +76,14 @@ function _columns(df, columnnames, columntypes)
6676
table = Tables.DictColumnTable(schema, coldict)
6777
Tables.columns(table)
6878
end
79+
80+
function __init__()
81+
pyconvert_add_rule(
82+
"pandas.core.frame:DataFrame",
83+
PyPandasDataFrame,
84+
pyconvert_rule_pandasdataframe,
85+
PYCONVERT_PRIORITY_CANONICAL,
86+
)
87+
end
88+
89+
end

src/Wrap/PySet.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
module PySets
2+
3+
using ...PythonCall
4+
using ...Utils
5+
using ...Core
6+
using ...Convert
7+
8+
import ...PythonCall: PySet, ispy, Py
9+
110
PySet(x = pyset()) = PySet{Py}(x)
211

312
ispy(::PySet) = true
@@ -86,3 +95,14 @@ end
8695
function Base.copy(x::PySet{T}) where {T}
8796
return PySet{T}(@py x.copy())
8897
end
98+
99+
function __init__()
100+
pyconvert_add_rule(
101+
"collections.abc:Set",
102+
PySet,
103+
pyconvert_rule_set,
104+
PYCONVERT_PRIORITY_CANONICAL,
105+
)
106+
end
107+
108+
end

src/Wrap/PyTable.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
module PyTables
2+
3+
using ...PythonCall
4+
5+
import ...PythonCall: PyTable
6+
17
PyTable(x) = pyconvert(PyTable, x)
28

39
# Tables.istable(x::Py) = Tables.istable(@pyconvert(PyTable, x, return false))
@@ -6,3 +12,5 @@ PyTable(x) = pyconvert(PyTable, x)
612
# Tables.columnaccess(x::Py) = Tables.columnaccess(@pyconvert(PyTable, x, return false))
713
# Tables.columns(x::Py) = Tables.columns(pyconvert(PyTable, x))
814
# Tables.materializer(x::Py) = Tables.materializer(pyconvert(PyTable, x))
15+
16+
end

0 commit comments

Comments
 (0)