Skip to content

Commit 5eca22e

Browse files
committed
Refine pyconvert registration and docs
1 parent f42e34a commit 5eca22e

File tree

7 files changed

+116
-386
lines changed

7 files changed

+116
-386
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Changes to core functionality:
66
* Comparisons like `==(::Py, ::Py)`, `<(::Py, ::Number)`, `isless(::Number, ::Py)` now return `Bool` instead of `Py`.
77
* `pyconvert` rules are now scoped by target type instead of prioritized; rules are ordered by Python type specificity and creation order.
8+
### Conversion
9+
* `pyconvert_add_rule` now has the signature `pyconvert_add_rule(func::Function, t::String, ::Type{T}, ::Type{S}=T)`; the optional scope `S` controls when a rule is considered during conversion.
810
* Changes to `PythonCall.GC` (now more like `Base.GC`):
911
* `enable(true)` replaces `enable()`.
1012
* `enable(false)` replaces `disable()`.

docs/src/conversion-to-julia.md

Lines changed: 64 additions & 52 deletions
Large diffs are not rendered by default.

src/Convert/ctypes.jl

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

37-
function ctypes_rule_specs()
38-
specs = PyConvertRuleSpec[]
39-
for (t, T) in CTYPES_SIMPLE_TYPES
40-
isptr = endswith(t, "_p")
41-
isreal = !isptr
42-
isnumber = isreal
43-
isfloat = t in ("float", "double")
44-
isint = isreal && !isfloat
45-
isuint = isint && (startswith(t, "u") || t == "size_t")
46-
47-
name = "ctypes:c_$t"
48-
rule = pyconvert_rule_ctypessimplevalue{T,false}()
49-
saferule = pyconvert_rule_ctypessimplevalue{T,true}()
50-
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))
71-
end
72-
return specs
73-
end
74-

src/Convert/numpy.jl

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -97,89 +97,3 @@ const NUMPY_SIMPLE_TYPES = [
9797
("complex128", ComplexF64),
9898
]
9999

100-
function numpy_rule_specs()
101-
specs = PyConvertRuleSpec[]
102-
# simple numeric scalar types
103-
for (t, T) in NUMPY_SIMPLE_TYPES
104-
isbool = occursin("bool", t)
105-
isint = occursin("int", t) || isbool
106-
isuint = occursin("uint", t) || isbool
107-
isfloat = occursin("float", t)
108-
iscomplex = occursin("complex", t)
109-
isreal = isint || isfloat
110-
isnumber = isreal || iscomplex
111-
112-
name = "numpy:$t"
113-
rule = pyconvert_rule_numpysimplevalue{T,false}()
114-
saferule = pyconvert_rule_numpysimplevalue{T,true}()
115-
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))
145-
end
146-
147-
# datetime64
148-
push!(
149-
specs,
150-
(func = pyconvert_rule_datetime64, tname = "numpy:datetime64", type = DateTime64, scope = Any),
151-
)
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-
),
161-
)
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))
164-
165-
# timedelta64
166-
push!(
167-
specs,
168-
(func = pyconvert_rule_timedelta64, tname = "numpy:timedelta64", type = TimeDelta64, scope = Any),
169-
)
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-
),
179-
)
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
184-
end
185-

src/Convert/pandas.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
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 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-
]
14-
end
15-

src/Convert/pyconvert.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ end
99
const PYCONVERT_RULES = Dict{String,Vector{PyConvertRule}}()
1010
const PYCONVERT_RULE_ORDER = Ref{Int}(0)
1111
const PYCONVERT_EXTRATYPES = Py[]
12-
const PyConvertRuleSpec = NamedTuple{(:func, :tname, :type, :scope),Tuple{Function,String,Type,Type}}
1312

1413
"""
1514
pyconvert_add_rule(func::Function, tname::String, ::Type{T}, ::Type{S}=T) where {T,S}

0 commit comments

Comments
 (0)