Skip to content

Commit 6edb493

Browse files
author
Christopher Doris
committed
Allow any string or integer to be converted to python
Specifically this adds methods `Py(::AbstractString)`, `Py(::AbstractChar)`, `Py(::Integer)`, `Py(::Rational{<:Integer})`, `Py(::AbstractRange{<:Integer})` whereas previously we only allowed the builtin Julia string, char and integer types.
1 parent a9b77e4 commit 6edb493

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/Core/Py.jl

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,16 @@ end
122122
Py(x::Py) = x
123123
Py(x::Nothing) = pybuiltins.None
124124
Py(x::Bool) = x ? pybuiltins.True : pybuiltins.False
125-
Py(x::Union{String,SubString{String},Char}) = pystr(x)
125+
Py(x::Union{AbstractString,AbstractChar}) = pystr(x)
126126
Py(x::Base.CodeUnits{UInt8,String}) = pybytes(x)
127127
Py(x::Base.CodeUnits{UInt8,SubString{String}}) = pybytes(x)
128128
Py(x::Tuple) = pytuple_fromiter(x)
129129
Py(x::Pair) = pytuple_fromiter(x)
130-
Py(x::Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt}) =
131-
pyint(x)
132-
Py(
133-
x::Rational{
134-
<:Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt},
135-
},
136-
) = pyfraction(x)
130+
Py(x::Integer) = pyint(x)
131+
Py(x::Rational{<:Integer}) = pyfraction(x)
137132
Py(x::Union{Float16,Float32,Float64}) = pyfloat(x)
138-
Py(x::Complex{<:Union{Float16,Float32,Float64}}) = pycomplex(x)
139-
Py(
140-
x::AbstractRange{
141-
<:Union{Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128,BigInt},
142-
},
143-
) = pyrange_fromrange(x)
133+
Py(x::Union{Complex{Float16},Complex{Float32},Complex{Float64}}) = pycomplex(x)
134+
Py(x::AbstractRange{<:Integer}) = pyrange_fromrange(x)
144135
Py(x::Date) = pydate(x)
145136
Py(x::Time) = pytime(x)
146137
Py(x::DateTime) = pydatetime(x)

src/Core/builtins.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ pystr(x) = pynew(errcheck(@autopy x C.PyObject_Str(x_)))
592592
pystr(x::String) = pystr_fromUTF8(x)
593593
pystr(x::SubString{String}) = pystr_fromUTF8(x)
594594
pystr(x::Char) = pystr(string(x))
595+
pystr(x::AbstractString) = pystr(convert(String, x)::String)
596+
pystr(x::AbstractChar) = pystr(convert(Char, x)::Char)
595597
pystr(::Type{String}, x) = (s = pystr(x); ans = pystr_asstring(s); pydel!(s); ans)
596598

597599
pystr_asUTF8bytes(x::Py) = pynew(errcheck(C.PyUnicode_AsUTF8String(x)))

0 commit comments

Comments
 (0)