diff --git a/src/concrete/ctypes.jl b/src/concrete/ctypes.jl index b9b47c93..8b2e4f6f 100644 --- a/src/concrete/ctypes.jl +++ b/src/concrete/ctypes.jl @@ -45,16 +45,16 @@ function init_ctypes() rule = pyconvert_rule_ctypessimplevalue{T, false}() saferule = pyconvert_rule_ctypessimplevalue{T, true}() - t == "char_p" && pyconvert_add_rule(name, Cstring, saferule) - t == "wchar_p" && pyconvert_add_rule(name, Cwstring, saferule) - pyconvert_add_rule(name, T, saferule) - isuint && pyconvert_add_rule(name, UInt, sizeof(T) ≤ sizeof(UInt) ? saferule : rule) - isuint && pyconvert_add_rule(name, Int, sizeof(T) < sizeof(Int) ? saferule : rule) - isint && !isuint && pyconvert_add_rule(name, Int, sizeof(T) ≤ sizeof(Int) ? saferule : rule) - isint && pyconvert_add_rule(name, Integer, rule) - isfloat && pyconvert_add_rule(name, Float64, saferule) - isreal && pyconvert_add_rule(name, Real, rule) - isnumber && pyconvert_add_rule(name, Number, rule) - isptr && pyconvert_add_rule(name, Ptr, saferule) + t == "char_p" && pyconvert_add_rule(saferule, name, Cstring) + t == "wchar_p" && pyconvert_add_rule(saferule, name, Cwstring) + pyconvert_add_rule(saferule, name, T) + isuint && pyconvert_add_rule(sizeof(T) ≤ sizeof(UInt) ? saferule : rule, name, UInt) + isuint && pyconvert_add_rule(sizeof(T) < sizeof(Int) ? saferule : rule, name, Int) + isint && !isuint && pyconvert_add_rule(sizeof(T) ≤ sizeof(Int) ? saferule : rule, name, Int) + isint && pyconvert_add_rule(rule, name, Integer) + isfloat && pyconvert_add_rule(saferule, name, Float64) + isreal && pyconvert_add_rule(rule, name, Real) + isnumber && pyconvert_add_rule(rule, name, Number) + isptr && pyconvert_add_rule(saferule, name, Ptr) end end diff --git a/src/concrete/numpy.jl b/src/concrete/numpy.jl index 30eed08a..8b999d12 100644 --- a/src/concrete/numpy.jl +++ b/src/concrete/numpy.jl @@ -39,15 +39,15 @@ function init_numpy() rule = pyconvert_rule_numpysimplevalue{T, false}() saferule = pyconvert_rule_numpysimplevalue{T, true}() - pyconvert_add_rule(name, T, saferule, PYCONVERT_PRIORITY_ARRAY) - isuint && pyconvert_add_rule(name, UInt, sizeof(T) ≤ sizeof(UInt) ? saferule : rule) - isuint && pyconvert_add_rule(name, Int, sizeof(T) < sizeof(Int) ? saferule : rule) - isint && !isuint && pyconvert_add_rule(name, Int, sizeof(T) ≤ sizeof(Int) ? saferule : rule) - isint && pyconvert_add_rule(name, Integer, rule) - isfloat && pyconvert_add_rule(name, Float64, saferule) - isreal && pyconvert_add_rule(name, Real, rule) - iscomplex && pyconvert_add_rule(name, ComplexF64, saferule) - iscomplex && pyconvert_add_rule(name, Complex, rule) - isnumber && pyconvert_add_rule(name, Number, rule) + pyconvert_add_rule(saferule, name, T, PYCONVERT_PRIORITY_ARRAY) + isuint && pyconvert_add_rule(sizeof(T) ≤ sizeof(UInt) ? saferule : rule, name, UInt) + isuint && pyconvert_add_rule(sizeof(T) < sizeof(Int) ? saferule : rule, name, Int) + isint && !isuint && pyconvert_add_rule(sizeof(T) ≤ sizeof(Int) ? saferule : rule, name, Int) + isint && pyconvert_add_rule(rule, name, Integer) + isfloat && pyconvert_add_rule(saferule, name, Float64) + isreal && pyconvert_add_rule(rule, name, Real) + iscomplex && pyconvert_add_rule(saferule, name, ComplexF64) + iscomplex && pyconvert_add_rule(rule, name, Complex) + isnumber && pyconvert_add_rule(rule, name, Number) end end diff --git a/src/concrete/pandas.jl b/src/concrete/pandas.jl index b936c57c..ed91db16 100644 --- a/src/concrete/pandas.jl +++ b/src/concrete/pandas.jl @@ -2,6 +2,6 @@ pyconvert_rule_pandas_na(::Type{Nothing}, x::Py) = pyconvert_return(nothing) pyconvert_rule_pandas_na(::Type{Missing}, x::Py) = pyconvert_return(missing) function init_pandas() - pyconvert_add_rule("pandas._libs.missing:NAType", Missing, pyconvert_rule_pandas_na, PYCONVERT_PRIORITY_CANONICAL) - pyconvert_add_rule("pandas._libs.missing:NAType", Nothing, pyconvert_rule_pandas_na) + pyconvert_add_rule(pyconvert_rule_pandas_na, "pandas._libs.missing:NAType", Missing, PYCONVERT_PRIORITY_CANONICAL) + pyconvert_add_rule(pyconvert_rule_pandas_na, "pandas._libs.missing:NAType", Nothing) end diff --git a/src/convert.jl b/src/convert.jl index 498293a5..4689e525 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -16,17 +16,17 @@ const PYCONVERT_RULES = Dict{String, Vector{PyConvertRule}}() const PYCONVERT_EXTRATYPES = Py[] """ - pyconvert_add_rule(tname::String, T::Type, func::Function, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL) + pyconvert_add_rule(func::Function, tname::String, T::Type, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL) Add a new conversion rule for `pyconvert`. ### Arguments +- `func` is the function implementing the rule. - `tname` is a string of the form `"__module__:__qualname__"` identifying a Python type `t`, such as `"builtins:dict"`. This rule only applies to Python objects of this type. - `T` is a Julia type, such that this rule only applies when the target type intersects with `T`. -- `func` is the function implementing the rule. - `priority` determines whether to prioritise this rule above others. When `pyconvert(R, x)` is called, all rules such that `typeintersect(T, R) != Union{}` @@ -61,12 +61,15 @@ given Python type. Other priorities are reserved for internal use. """ -function pyconvert_add_rule(pytypename::String, type::Type, func::Function, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL) +function pyconvert_add_rule(func::Function, pytypename::String, type::Type, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL) @nospecialize type func push!(get!(Vector{PyConvertRule}, PYCONVERT_RULES, pytypename), PyConvertRule(type, func, priority)) return end +@deprecate(pyconvert_add_rule(pytypename::String, type::Type, func::Function, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL), + pyconvert_add_rule(func, pytypename, type, priority), false) + # Alternative ways to represent the result of conversion. if true # Returns either the result or Unconverted(). @@ -389,69 +392,69 @@ function init_pyconvert() push!(PYCONVERT_EXTRATYPES, pyimport("collections.abc" => ("Iterable", "Sequence", "Set", "Mapping"))...) priority = PYCONVERT_PRIORITY_WRAP - pyconvert_add_rule("juliacall:ValueBase", Any, pyconvert_rule_jlvalue, priority) + pyconvert_add_rule(pyconvert_rule_jlvalue, "juliacall:ValueBase", Any, priority) priority = PYCONVERT_PRIORITY_ARRAY - pyconvert_add_rule("", PyArray, pyconvert_rule_array_nocopy, priority) - pyconvert_add_rule("", PyArray, pyconvert_rule_array_nocopy, priority) - pyconvert_add_rule("", PyArray, pyconvert_rule_array_nocopy, priority) - pyconvert_add_rule("", PyArray, pyconvert_rule_array_nocopy, priority) + pyconvert_add_rule(pyconvert_rule_array_nocopy, "", PyArray, priority) + pyconvert_add_rule(pyconvert_rule_array_nocopy, "", PyArray, priority) + pyconvert_add_rule(pyconvert_rule_array_nocopy, "", PyArray, priority) + pyconvert_add_rule(pyconvert_rule_array_nocopy, "", PyArray, priority) priority = PYCONVERT_PRIORITY_CANONICAL - pyconvert_add_rule("builtins:NoneType", Nothing, pyconvert_rule_none, priority) - pyconvert_add_rule("builtins:bool", Bool, pyconvert_rule_bool, priority) - pyconvert_add_rule("builtins:float", Float64, pyconvert_rule_float, priority) - pyconvert_add_rule("builtins:complex", Complex{Float64}, pyconvert_rule_complex, priority) - pyconvert_add_rule("numbers:Integral", Integer, pyconvert_rule_int, priority) - pyconvert_add_rule("builtins:str", String, pyconvert_rule_str, priority) - pyconvert_add_rule("builtins:bytes", Base.CodeUnits{UInt8,String}, pyconvert_rule_bytes, priority) - pyconvert_add_rule("builtins:range", StepRange{<:Integer,<:Integer}, pyconvert_rule_range, priority) - pyconvert_add_rule("numbers:Rational", Rational{<:Integer}, pyconvert_rule_fraction, priority) - pyconvert_add_rule("collections.abc:Iterable", PyIterable, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Sequence", PyList, pyconvert_rule_sequence, priority) - pyconvert_add_rule("collections.abc:Set", PySet, pyconvert_rule_set, priority) - pyconvert_add_rule("collections.abc:Mapping", PyDict, pyconvert_rule_mapping, priority) - pyconvert_add_rule("io:IOBase", PyIO, pyconvert_rule_io, priority) - pyconvert_add_rule("_io:_IOBase", PyIO, pyconvert_rule_io, priority) - pyconvert_add_rule("pandas.core.frame:DataFrame", PyPandasDataFrame, pyconvert_rule_pandasdataframe, priority) - pyconvert_add_rule("pandas.core.arrays.base:ExtensionArray", PyList, pyconvert_rule_sequence, priority) - pyconvert_add_rule("builtins:tuple", NamedTuple, pyconvert_rule_iterable, priority) - pyconvert_add_rule("builtins:tuple", Tuple, pyconvert_rule_iterable, priority) - pyconvert_add_rule("datetime:datetime", DateTime, pyconvert_rule_datetime, priority) - pyconvert_add_rule("datetime:date", Date, pyconvert_rule_date, priority) - pyconvert_add_rule("datetime:time", Time, pyconvert_rule_time, priority) - pyconvert_add_rule("builtins:BaseException", PyException, pyconvert_rule_exception, priority) + pyconvert_add_rule(pyconvert_rule_none, "builtins:NoneType", Nothing, priority) + pyconvert_add_rule(pyconvert_rule_bool, "builtins:bool", Bool, priority) + pyconvert_add_rule(pyconvert_rule_float, "builtins:float", Float64, priority) + pyconvert_add_rule(pyconvert_rule_complex, "builtins:complex", Complex{Float64}, priority) + pyconvert_add_rule(pyconvert_rule_int, "numbers:Integral", Integer, priority) + pyconvert_add_rule(pyconvert_rule_str, "builtins:str", String, priority) + pyconvert_add_rule(pyconvert_rule_bytes, "builtins:bytes", Base.CodeUnits{UInt8,String}, priority) + pyconvert_add_rule(pyconvert_rule_range, "builtins:range", StepRange{<:Integer,<:Integer}, priority) + pyconvert_add_rule(pyconvert_rule_fraction, "numbers:Rational", Rational{<:Integer}, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Iterable", PyIterable, priority) + pyconvert_add_rule(pyconvert_rule_sequence, "collections.abc:Sequence", PyList, priority) + pyconvert_add_rule(pyconvert_rule_set, "collections.abc:Set", PySet, priority) + pyconvert_add_rule(pyconvert_rule_mapping, "collections.abc:Mapping", PyDict, priority) + pyconvert_add_rule(pyconvert_rule_io, "io:IOBase", PyIO, priority) + pyconvert_add_rule(pyconvert_rule_io, "_io:_IOBase", PyIO, priority) + pyconvert_add_rule(pyconvert_rule_pandasdataframe, "pandas.core.frame:DataFrame", PyPandasDataFrame, priority) + pyconvert_add_rule(pyconvert_rule_sequence, "pandas.core.arrays.base:ExtensionArray", PyList, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "builtins:tuple", NamedTuple, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "builtins:tuple", Tuple, priority) + pyconvert_add_rule(pyconvert_rule_datetime, "datetime:datetime", DateTime, priority) + pyconvert_add_rule(pyconvert_rule_date, "datetime:date", Date, priority) + pyconvert_add_rule(pyconvert_rule_time, "datetime:time", Time, priority) + pyconvert_add_rule(pyconvert_rule_exception, "builtins:BaseException", PyException, priority) priority = PYCONVERT_PRIORITY_NORMAL - pyconvert_add_rule("builtins:NoneType", Missing, pyconvert_rule_none, priority) - pyconvert_add_rule("builtins:bool", Number, pyconvert_rule_bool, priority) - pyconvert_add_rule("numbers:Real", Number, pyconvert_rule_float, priority) - pyconvert_add_rule("builtins:float", Nothing, pyconvert_rule_float, priority) - pyconvert_add_rule("builtins:float", Missing, pyconvert_rule_float, priority) - pyconvert_add_rule("numbers:Complex", Number, pyconvert_rule_complex, priority) - pyconvert_add_rule("numbers:Integral", Number, pyconvert_rule_int, priority) - pyconvert_add_rule("builtins:str", Symbol, pyconvert_rule_str, priority) - pyconvert_add_rule("builtins:str", Char, pyconvert_rule_str, priority) - pyconvert_add_rule("builtins:bytes", Vector{UInt8}, pyconvert_rule_bytes, priority) - pyconvert_add_rule("builtins:range", UnitRange{<:Integer}, pyconvert_rule_range, priority) - pyconvert_add_rule("numbers:Rational", Number, pyconvert_rule_fraction, priority) - pyconvert_add_rule("collections.abc:Iterable", Vector, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Iterable", Tuple, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Iterable", Pair, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Iterable", Set, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Sequence", Vector, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Sequence", Tuple, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Set", Set, pyconvert_rule_iterable, priority) - pyconvert_add_rule("collections.abc:Mapping", Dict, pyconvert_rule_mapping, priority) - pyconvert_add_rule("", Array, pyconvert_rule_array, priority) - pyconvert_add_rule("", Array, pyconvert_rule_array, priority) - pyconvert_add_rule("", Array, pyconvert_rule_array, priority) - pyconvert_add_rule("", Array, pyconvert_rule_array, priority) - pyconvert_add_rule("", AbstractArray, pyconvert_rule_array, priority) - pyconvert_add_rule("", AbstractArray, pyconvert_rule_array, priority) - pyconvert_add_rule("", AbstractArray, pyconvert_rule_array, priority) - pyconvert_add_rule("", AbstractArray, pyconvert_rule_array, priority) + pyconvert_add_rule(pyconvert_rule_none, "builtins:NoneType", Missing, priority) + pyconvert_add_rule(pyconvert_rule_bool, "builtins:bool", Number, priority) + pyconvert_add_rule(pyconvert_rule_float, "numbers:Real", Number, priority) + pyconvert_add_rule(pyconvert_rule_float, "builtins:float", Nothing, priority) + pyconvert_add_rule(pyconvert_rule_float, "builtins:float", Missing, priority) + pyconvert_add_rule(pyconvert_rule_complex, "numbers:Complex", Number, priority) + pyconvert_add_rule(pyconvert_rule_int, "numbers:Integral", Number, priority) + pyconvert_add_rule(pyconvert_rule_str, "builtins:str", Symbol, priority) + pyconvert_add_rule(pyconvert_rule_str, "builtins:str", Char, priority) + pyconvert_add_rule(pyconvert_rule_bytes, "builtins:bytes", Vector{UInt8}, priority) + pyconvert_add_rule(pyconvert_rule_range, "builtins:range", UnitRange{<:Integer}, priority) + pyconvert_add_rule(pyconvert_rule_fraction, "numbers:Rational", Number, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Iterable", Vector, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Iterable", Tuple, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Iterable", Pair, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Iterable", Set, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Sequence", Vector, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Sequence", Tuple, priority) + pyconvert_add_rule(pyconvert_rule_iterable, "collections.abc:Set", Set, priority) + pyconvert_add_rule(pyconvert_rule_mapping, "collections.abc:Mapping", Dict, priority) + pyconvert_add_rule(pyconvert_rule_array, "", Array, priority) + pyconvert_add_rule(pyconvert_rule_array, "", Array, priority) + pyconvert_add_rule(pyconvert_rule_array, "", Array, priority) + pyconvert_add_rule(pyconvert_rule_array, "", Array, priority) + pyconvert_add_rule(pyconvert_rule_array, "", AbstractArray, priority) + pyconvert_add_rule(pyconvert_rule_array, "", AbstractArray, priority) + pyconvert_add_rule(pyconvert_rule_array, "", AbstractArray, priority) + pyconvert_add_rule(pyconvert_rule_array, "", AbstractArray, priority) priority = PYCONVERT_PRIORITY_FALLBACK - pyconvert_add_rule("builtins:object", Py, pyconvert_rule_object, priority) + pyconvert_add_rule(pyconvert_rule_object, "builtins:object", Py, priority) end