Skip to content

Commit 7d98a71

Browse files
committed
Centralize pyconvert rule registration
1 parent 56c3a12 commit 7d98a71

File tree

10 files changed

+285
-186
lines changed

10 files changed

+285
-186
lines changed

src/Convert/Convert.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ include("numpy.jl")
3535
include("pandas.jl")
3636

3737
function __init__()
38-
init_pyconvert()
39-
init_ctypes()
40-
init_numpy()
41-
init_pandas()
38+
init_pyconvert_extratypes()
4239
end
4340

4441
end

src/Convert/ctypes.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const CTYPES_SIMPLE_TYPES = [
3434
("void_p", Ptr{Cvoid}),
3535
]
3636

37-
function init_ctypes()
37+
function ctypes_rule_specs()
38+
specs = PyConvertRuleSpec[]
3839
for (t, T) in CTYPES_SIMPLE_TYPES
3940
isptr = endswith(t, "_p")
4041
isreal = !isptr
@@ -47,18 +48,26 @@ function init_ctypes()
4748
rule = pyconvert_rule_ctypessimplevalue{T,false}()
4849
saferule = pyconvert_rule_ctypessimplevalue{T,true}()
4950

50-
t == "char_p" && pyconvert_add_rule(saferule, name, Cstring)
51-
t == "wchar_p" && pyconvert_add_rule(saferule, name, Cwstring)
52-
pyconvert_add_rule(saferule, name, T)
53-
isuint && pyconvert_add_rule(sizeof(T) sizeof(UInt) ? saferule : rule, name, UInt)
54-
isuint && pyconvert_add_rule(sizeof(T) < sizeof(Int) ? saferule : rule, name, Int)
55-
isint &&
56-
!isuint &&
57-
pyconvert_add_rule(sizeof(T) sizeof(Int) ? saferule : rule, name, Int)
58-
isint && pyconvert_add_rule(rule, name, Integer)
59-
isfloat && pyconvert_add_rule(saferule, name, Float64)
60-
isreal && pyconvert_add_rule(rule, name, Real)
61-
isnumber && pyconvert_add_rule(rule, name, Number)
62-
isptr && pyconvert_add_rule(saferule, name, Ptr)
51+
t == "char_p" && push!(specs, (func = saferule, tname = name, type = Cstring, scope = Cstring))
52+
t == "wchar_p" && push!(specs, (func = saferule, tname = name, type = Cwstring, scope = Cwstring))
53+
push!(specs, (func = saferule, tname = name, type = T, scope = T))
54+
isuint && push!(
55+
specs,
56+
(func = sizeof(T) sizeof(UInt) ? saferule : rule, tname = name, type = UInt, scope = UInt),
57+
)
58+
isuint && push!(
59+
specs,
60+
(func = sizeof(T) < sizeof(Int) ? saferule : rule, tname = name, type = Int, scope = Int),
61+
)
62+
isint && !isuint && push!(
63+
specs,
64+
(func = sizeof(T) sizeof(Int) ? saferule : rule, tname = name, type = Int, scope = Int),
65+
)
66+
isint && push!(specs, (func = rule, tname = name, type = Integer, scope = Integer))
67+
isfloat && push!(specs, (func = saferule, tname = name, type = Float64, scope = Float64))
68+
isreal && push!(specs, (func = rule, tname = name, type = Real, scope = Real))
69+
isnumber && push!(specs, (func = rule, tname = name, type = Number, scope = Number))
70+
isptr && push!(specs, (func = saferule, tname = name, type = Ptr, scope = Ptr))
6371
end
72+
return specs
6473
end

src/Convert/numpy.jl

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ const NUMPY_SIMPLE_TYPES = [
9797
("complex128", ComplexF64),
9898
]
9999

100-
function init_numpy()
100+
function numpy_rule_specs()
101+
specs = PyConvertRuleSpec[]
101102
# simple numeric scalar types
102103
for (t, T) in NUMPY_SIMPLE_TYPES
103104
isbool = occursin("bool", t)
@@ -112,49 +113,72 @@ function init_numpy()
112113
rule = pyconvert_rule_numpysimplevalue{T,false}()
113114
saferule = pyconvert_rule_numpysimplevalue{T,true}()
114115

115-
pyconvert_add_rule(saferule, name, T, Any)
116-
isuint && pyconvert_add_rule(sizeof(T) sizeof(UInt) ? saferule : rule, name, UInt)
117-
isuint && pyconvert_add_rule(sizeof(T) < sizeof(Int) ? saferule : rule, name, Int)
118-
isint &&
119-
!isuint &&
120-
pyconvert_add_rule(sizeof(T) sizeof(Int) ? saferule : rule, name, Int)
121-
isint && pyconvert_add_rule(rule, name, Integer)
122-
isfloat && pyconvert_add_rule(saferule, name, Float64)
123-
isreal && pyconvert_add_rule(rule, name, Real)
124-
iscomplex && pyconvert_add_rule(saferule, name, ComplexF64)
125-
iscomplex && pyconvert_add_rule(rule, name, Complex)
126-
isnumber && pyconvert_add_rule(rule, name, Number)
116+
push!(specs, (func = saferule, tname = name, type = T, scope = Any))
117+
isuint && push!(
118+
specs,
119+
(
120+
func = sizeof(T) sizeof(UInt) ? saferule : rule,
121+
tname = name,
122+
type = UInt,
123+
scope = UInt,
124+
),
125+
)
126+
isuint && push!(
127+
specs,
128+
(
129+
func = sizeof(T) < sizeof(Int) ? saferule : rule,
130+
tname = name,
131+
type = Int,
132+
scope = Int,
133+
),
134+
)
135+
isint && !isuint && push!(
136+
specs,
137+
(func = sizeof(T) sizeof(Int) ? saferule : rule, tname = name, type = Int, scope = Int),
138+
)
139+
isint && push!(specs, (func = rule, tname = name, type = Integer, scope = Integer))
140+
isfloat && push!(specs, (func = saferule, tname = name, type = Float64, scope = Float64))
141+
isreal && push!(specs, (func = rule, tname = name, type = Real, scope = Real))
142+
iscomplex && push!(specs, (func = saferule, tname = name, type = ComplexF64, scope = ComplexF64))
143+
iscomplex && push!(specs, (func = rule, tname = name, type = Complex, scope = Complex))
144+
isnumber && push!(specs, (func = rule, tname = name, type = Number, scope = Number))
127145
end
128146

129147
# datetime64
130-
pyconvert_add_rule(
131-
pyconvert_rule_datetime64,
132-
"numpy:datetime64",
133-
DateTime64,
134-
Any,
148+
push!(
149+
specs,
150+
(func = pyconvert_rule_datetime64, tname = "numpy:datetime64", type = DateTime64, scope = Any),
135151
)
136-
pyconvert_add_rule(pyconvert_rule_datetime64, "numpy:datetime64", InlineDateTime64)
137-
pyconvert_add_rule(
138-
pyconvert_rule_datetime64,
139-
"numpy:datetime64",
140-
NumpyDates.DatesInstant,
152+
push!(specs, (func = pyconvert_rule_datetime64, tname = "numpy:datetime64", type = InlineDateTime64, scope = InlineDateTime64))
153+
push!(
154+
specs,
155+
(
156+
func = pyconvert_rule_datetime64,
157+
tname = "numpy:datetime64",
158+
type = NumpyDates.DatesInstant,
159+
scope = NumpyDates.DatesInstant,
160+
),
141161
)
142-
pyconvert_add_rule(pyconvert_rule_datetime64, "numpy:datetime64", Missing, Missing)
143-
pyconvert_add_rule(pyconvert_rule_datetime64, "numpy:datetime64", Nothing, Nothing)
162+
push!(specs, (func = pyconvert_rule_datetime64, tname = "numpy:datetime64", type = Missing, scope = Missing))
163+
push!(specs, (func = pyconvert_rule_datetime64, tname = "numpy:datetime64", type = Nothing, scope = Nothing))
144164

145165
# timedelta64
146-
pyconvert_add_rule(
147-
pyconvert_rule_timedelta64,
148-
"numpy:timedelta64",
149-
TimeDelta64,
150-
Any,
166+
push!(
167+
specs,
168+
(func = pyconvert_rule_timedelta64, tname = "numpy:timedelta64", type = TimeDelta64, scope = Any),
151169
)
152-
pyconvert_add_rule(pyconvert_rule_timedelta64, "numpy:timedelta64", InlineTimeDelta64)
153-
pyconvert_add_rule(
154-
pyconvert_rule_timedelta64,
155-
"numpy:timedelta64",
156-
NumpyDates.DatesPeriod,
170+
push!(specs, (func = pyconvert_rule_timedelta64, tname = "numpy:timedelta64", type = InlineTimeDelta64, scope = InlineTimeDelta64))
171+
push!(
172+
specs,
173+
(
174+
func = pyconvert_rule_timedelta64,
175+
tname = "numpy:timedelta64",
176+
type = NumpyDates.DatesPeriod,
177+
scope = NumpyDates.DatesPeriod,
178+
),
157179
)
158-
pyconvert_add_rule(pyconvert_rule_timedelta64, "numpy:timedelta64", Missing, Missing)
159-
pyconvert_add_rule(pyconvert_rule_timedelta64, "numpy:timedelta64", Nothing, Nothing)
180+
push!(specs, (func = pyconvert_rule_timedelta64, tname = "numpy:timedelta64", type = Missing, scope = Missing))
181+
push!(specs, (func = pyconvert_rule_timedelta64, tname = "numpy:timedelta64", type = Nothing, scope = Nothing))
182+
183+
return specs
160184
end

src/Convert/pandas.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
pyconvert_rule_pandas_na(::Type{Nothing}, x::Py) = pyconvert_return(nothing)
22
pyconvert_rule_pandas_na(::Type{Missing}, x::Py) = pyconvert_return(missing)
33

4-
function init_pandas()
5-
pyconvert_add_rule(
6-
pyconvert_rule_pandas_na,
7-
"pandas._libs.missing:NAType",
8-
Missing,
9-
Any,
10-
)
11-
pyconvert_add_rule(pyconvert_rule_pandas_na, "pandas._libs.missing:NAType", Nothing, Nothing)
4+
function pandas_rule_specs()
5+
return PyConvertRuleSpec[
6+
(func = pyconvert_rule_pandas_na, tname = "pandas._libs.missing:NAType", type = Missing, scope = Any),
7+
(
8+
func = pyconvert_rule_pandas_na,
9+
tname = "pandas._libs.missing:NAType",
10+
type = Nothing,
11+
scope = Nothing,
12+
),
13+
]
1214
end

0 commit comments

Comments
 (0)