Skip to content

Commit 5d10a34

Browse files
author
Christopher Doris
committed
dev
1 parent f64e0ff commit 5d10a34

28 files changed

+238
-162
lines changed

src/PyIO.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export PyIO
3737
ispyreftype(::Type{PyIO}) = true
3838
pyptr(io::PyIO) = pyptr(io.ref)
3939
Base.unsafe_convert(::Type{CPyPtr}, io::PyIO) = checknull(pyptr(io))
40-
C.PyObject_TryConvert__initial(o, ::Type{PyIO}) = C.putresult(PyIO, PyIO(pyborrowedref(o)))
40+
C.PyObject_TryConvert__initial(o, ::Type{PyIO}) = C.putresult(PyIO(pyborrowedref(o)))
4141

4242
"""
4343
PyIO(f, o; ...)

src/PyIterable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export PyIterable
1313
ispyreftype(::Type{<:PyIterable}) = true
1414
pyptr(x::PyIterable) = pyptr(x.ref)
1515
Base.unsafe_convert(::Type{CPyPtr}, x::PyIterable) = checknull(pyptr(x))
16-
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyIterable} = C.putresult(T, T(pyborrowedref(o)))
16+
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyIterable} = C.putresult(T(pyborrowedref(o)))
1717

1818
Base.length(x::PyIterable) = Int(pylen(x))
1919

src/PyList.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pyptr(x::PyList) = begin
2323
ptr
2424
end
2525
Base.unsafe_convert(::Type{CPyPtr}, x::PyList) = checknull(pyptr(x))
26-
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyList} = C.putresult(T, T(pyborrowedref(o)))
26+
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyList} = C.putresult(T(pyborrowedref(o)))
2727

2828
# Base.length(x::PyList) = @pyv `len($x)`::Int
2929
Base.length(x::PyList) = Int(pylen(x))

src/PyObject.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ pynewobject(p::Ptr, check::Bool=false) = (check && isnull(p)) ? pythrow() : PyOb
2727
pyborrowedobject(p::Ptr, check::Bool=false) = (check && isnull(p)) ? pythrow() : PyObject(Val(:nocopy), pyborrowedref(p))
2828
pylazyobject(mk) = PyObject(Val(:lazy), mk)
2929

30-
C.PyObject_TryConvert__initial(o, ::Type{PyObject}) = C.putresult(PyObject, pyborrowedobject(o))
30+
C.PyObject_TryConvert__initial(o, ::Type{PyObject}) = C.putresult(pyborrowedobject(o))
3131

32+
Base.convert(::Type{Any}, x::PyObject) = x
3233
Base.convert(::Type{PyObject}, x::PyObject) = x
3334
Base.convert(::Type{PyObject}, x) = PyObject(x)
3435

36+
Base.convert(::Type{T}, x::PyObject) where {T} = pyconvert(T, x)
37+
3538
### Cache some common values
3639

3740
const _pynone = pylazyobject(() -> pynone(PyRef))

src/PyPandasDataFrame.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export PyPandasDataFrame
7474
ispyreftype(::Type{PyPandasDataFrame}) = true
7575
pyptr(df::PyPandasDataFrame) = df.ref
7676
Base.unsafe_convert(::Type{CPyPtr}, df::PyPandasDataFrame) = checknull(pyptr(df))
77-
C.PyObject_TryConvert__initial(o, ::Type{PyPandasDataFrame}) = C.putresult(PyPandasDataFrame, PyPandasDataFrame(pyborrowedref(o)))
77+
C.PyObject_TryConvert__initial(o, ::Type{PyPandasDataFrame}) = C.putresult(PyPandasDataFrame(pyborrowedref(o)))
7878

7979
Base.show(io::IO, x::PyPandasDataFrame) = print(io, pystr(String, x))
8080
Base.show(io::IO, mime::MIME, o::PyPandasDataFrame) = _py_mime_show(io, mime, o)

src/PyRef.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ispyreftype(::Type{PyRef}) = true
3636
pyptr(x::PyRef) = x.ptr
3737
isnull(x::PyRef) = isnull(pyptr(x))
3838
Base.unsafe_convert(::Type{CPyPtr}, x::PyRef) = pyptr(x)
39-
C.PyObject_TryConvert__initial(o, ::Type{PyRef}) = C.putresult(PyRef, pyborrowedref(o))
39+
C.PyObject_TryConvert__initial(o, ::Type{PyRef}) = C.putresult(pyborrowedref(o))
4040

4141
PyRef(x) = begin
4242
ptr = C.PyObject_From(x)

src/PySet.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pyptr(x::PySet) = begin
2323
ptr
2424
end
2525
Base.unsafe_convert(::Type{CPyPtr}, x::PySet) = checknull(pyptr(x))
26-
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PySet} = C.putresult(T, T(pyborrowedref(o)))
26+
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PySet} = C.putresult(T(pyborrowedref(o)))
2727

2828
Base.iterate(x::PySet{T}, it::PyRef) where {T} = begin
2929
ptr = C.PyIter_Next(it)

src/cpython/CPython.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module CPython
22

33
using Libdl
4-
import ..Python: CONFIG, isnull, ism1, PYERR, NOTIMPLEMENTED, _typeintersect, tryconvert, ispyreftype, pyptr, putresult, takeresult, moveresult, CACHE, Python
4+
import ..Python: CONFIG, isnull, ism1, PYERR, NOTIMPLEMENTED, _typeintersect, tryconvert, ispyreftype, pyptr, putresult, takeresult, CACHE, Python
55
using Base: @kwdef
66
using UnsafePointers: UnsafePtr
77

@@ -56,6 +56,12 @@ include("newtype.jl")
5656
include("juliaerror.jl")
5757
include("juliabase.jl")
5858
include("juliaraw.jl")
59+
include("juliaany.jl")
60+
include("juliaiterator.jl")
61+
include("juliatype.jl")
62+
include("juliadict.jl")
63+
include("juliaarray.jl")
64+
include("juliavector.jl")
5965
include("arg.jl")
6066

6167
__init__() = begin
@@ -132,7 +138,7 @@ __init__() = begin
132138
(Dict, PyMapping_ConvertRule_dict),
133139
])
134140
PyObject_TryConvert_AddRules("julia.ValueBase", [
135-
(Any, PyJuliaValue_TryConvert_any),
141+
(Any, PyJuliaValue_TryConvert_any, 200),
136142
])
137143
PyObject_TryConvert_AddExtraTypes([
138144
PyIterableABC_Type,

src/cpython/arg.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ PyArg_Find(args::PyPtr, kwargs::PyPtr, i::Union{Int,Nothing}, k::Union{String,No
7676
return PyPtr()
7777
end
7878

79-
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, kwargs::PyPtr=PyPtr(), i::Union{Int,Nothing}=nothing, k::Union{String,Nothing}=nothing, d::Union{PyPtr,NODEFAULT}=NODEFAULT()) where {T} = begin
79+
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, kwargs::PyPtr=PyPtr(), i::Union{Int,Nothing}=nothing, k::Union{String,Nothing}=nothing, d::Union{T,NODEFAULT}=NODEFAULT()) where {T} = begin
8080
ro = PyArg_Find(args, kwargs, i, k)
8181
if isnull(ro)
8282
if k !== nothing
@@ -96,14 +96,14 @@ PyArg_GetArg(::Type{T}, name::String, args::PyPtr, kwargs::PyPtr=PyPtr(), i::Uni
9696
PyErr_SetString(PyExc_TypeError(), "Argument $(k !== nothing ? "'$k'" : i !== nothing ? "$i" : error("impossible")) to $name() must be convertible to a Julia '$T'")
9797
return -1
9898
else
99-
putresult(T, d)
99+
putresult(d)
100100
return 0
101101
end
102102
else
103103
return 0
104104
end
105105
end
106-
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, i::Union{Int,Nothing}, k::Union{String,Nothing}=nothing, d::Union{PyPtr,NODEFAULT}=NODEFAULT()) where {T} =
106+
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, i::Union{Int,Nothing}, k::Union{String,Nothing}=nothing, d::Union{T,NODEFAULT}=NODEFAULT()) where {T} =
107107
PyArg_GetArg(T, name, args, PyPtr(), i, k, d)
108-
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, kwargs::PyPtr, k::Union{String,Nothing}, d::Union{PyPtr, NODEFAULT}=NODEFAULT()) where {T} =
108+
PyArg_GetArg(::Type{T}, name::String, args::PyPtr, kwargs::PyPtr, k::Union{String,Nothing}, d::Union{T, NODEFAULT}=NODEFAULT()) where {T} =
109109
PyArg_GetArg(T, name, args, kwargs, nothing, k, d)

src/cpython/bool.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ PyBool_From(x::Bool) = (o = x ? Py_True() : Py_False(); Py_IncRef(o); o)
1111

1212
PyBool_Check(o) = Py_TypeCheckExact(o, PyBool_Type())
1313

14-
PyBool_TryConvertRule_bool(o, ::Type{T}, ::Type{Bool}) where {T} =
14+
PyBool_TryConvertRule_bool(o, ::Type{Bool}) =
1515
if Py_Is(o, Py_True())
16-
putresult(T, true)
16+
putresult(true)
1717
elseif Py_Is(o, Py_False())
18-
putresult(T, false)
18+
putresult(false)
1919
else
2020
PyErr_SetString(PyExc_TypeError(), "Expecting a 'bool' but got a '$(PyType_Name(Py_Type(o)))'")
2121
-1

src/cpython/bytes.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ PyBytes_AsVector(o, ::Type{T}=UInt8) where {T} = begin
2727
copy(Base.unsafe_wrap(Vector{T}, Ptr{T}(ptr[]), len[]))
2828
end
2929

30-
PyBytes_TryConvertRule_vector(o, ::Type{T}, ::Type{Vector{X}}) where {T,X} = begin
30+
PyBytes_TryConvertRule_vector(o, ::Type{Vector{X}}) where {X} = begin
3131
v = PyBytes_AsVector(o, X)
3232
isempty(v) && PyErr_IsSet() && return -1
33-
return putresult(T, v)
33+
return putresult(v)
3434
end
3535

36-
PyBytes_TryConvertRule_string(o, ::Type{T}, ::Type{String}) where {T} = begin
36+
PyBytes_TryConvertRule_string(o, ::Type{String}) = begin
3737
v = PyBytes_AsString(o)
3838
isempty(v) && PyErr_IsSet() && return -1
39-
return putresult(T, v)
39+
return putresult(v)
4040
end

src/cpython/collections.jl

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
PyABC_Register(s, t) = begin
2+
r = PyObject_GetAttrString(t, "register")
3+
isnull(r) && return Cint(-1)
4+
u = PyObject_CallNice(r, PyObjectRef(s))
5+
Py_DecRef(r)
6+
isnull(u) && return Cint(-1)
7+
Py_DecRef(u)
8+
Cint(0)
9+
end
10+
111
for n in [:Container, :Hashable, :Iterable, :Iterator, :Reversible, :Generator, :Sized,
212
:Callable, :Collection, :Sequence, :MutableSequence, :ByteString, :Set, :MutableSet,
313
:Mapping, :MutableMapping, :MappingView, :ItemsView, :KeysView, :ValuesView,
@@ -24,7 +34,7 @@ for n in [:Container, :Hashable, :Iterable, :Iterator, :Reversible, :Generator,
2434
end
2535
end
2636

27-
PyIterable_ConvertRule_vector(o, ::Type{T}, ::Type{S}) where {T, S<:Vector} = begin
37+
PyIterable_ConvertRule_vector(o, ::Type{S}) where {S<:Vector} = begin
2838
it = PyObject_GetIter(o)
2939
isnull(it) && return -1
3040
xs = S()
@@ -41,14 +51,14 @@ PyIterable_ConvertRule_vector(o, ::Type{T}, ::Type{S}) where {T, S<:Vector} = be
4151
return -1
4252
else
4353
Py_DecRef(it)
44-
return putresult(T, xs)
54+
return putresult(xs)
4555
end
4656
end
4757
end
48-
PyIterable_ConvertRule_vector(o, ::Type{T}, ::Type{Vector}) where {T} =
49-
PyIterable_ConvertRule_vector(o, T, Vector{Python.PyObject})
58+
PyIterable_ConvertRule_vector(o, ::Type{Vector}) =
59+
PyIterable_ConvertRule_vector(o, Vector{Python.PyObject})
5060

51-
PyIterable_ConvertRule_set(o, ::Type{T}, ::Type{S}) where {T, S<:Set} = begin
61+
PyIterable_ConvertRule_set(o, ::Type{S}) where {S<:Set} = begin
5262
it = PyObject_GetIter(o)
5363
isnull(it) && return -1
5464
xs = S()
@@ -65,14 +75,14 @@ PyIterable_ConvertRule_set(o, ::Type{T}, ::Type{S}) where {T, S<:Set} = begin
6575
return -1
6676
else
6777
Py_DecRef(it)
68-
return putresult(T, xs)
78+
return putresult(xs)
6979
end
7080
end
7181
end
72-
PyIterable_ConvertRule_set(o, ::Type{T}, ::Type{Set}) where {T} =
82+
PyIterable_ConvertRule_set(o, ::Type{Set}) =
7383
PyIterable_ConvertRule_set(o, T, Set{Python.PyObject})
7484

75-
PyIterable_ConvertRule_tuple(o, ::Type{T}, ::Type{S}) where {T,S<:Tuple} = begin
85+
PyIterable_ConvertRule_tuple(o, ::Type{S}) where {S<:Tuple} = begin
7686
if !(Tuple isa DataType)
7787
PyErr_SetString(PyExc_Exception(), "When converting Python 'tuple' to Julia 'Tuple', the destination type must be a 'DataType', i.e. not parametric and not a union. Got '$S'.")
7888
return -1
@@ -112,13 +122,13 @@ PyIterable_ConvertRule_tuple(o, ::Type{T}, ::Type{S}) where {T,S<:Tuple} = begin
112122
return -1
113123
else
114124
Py_DecRef(it)
115-
return putresult(T, S(xs))
125+
return putresult(S(xs))
116126
end
117127
end
118128
end
119-
PyIterable_ConvertRule_tuple(o, ::Type{T}, ::Type{Tuple{}}) where {T} = putresult(T, ())
129+
PyIterable_ConvertRule_tuple(o, ::Type{Tuple{}}) = putresult(())
120130

121-
PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{Pair{K,V}}) where {T,K,V} = begin
131+
PyIterable_ConvertRule_pair(o, ::Type{Pair{K,V}}) where {K,V} = begin
122132
it = PyObject_GetIter(o)
123133
isnull(it) && return -1
124134
# get the first item
@@ -158,20 +168,20 @@ PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{Pair{K,V}}) where {T,K,V} = beg
158168
end
159169
# done
160170
Py_DecRef(it)
161-
putresult(T, Pair{K,V}(k, v))
171+
putresult(Pair{K,V}(k, v))
162172
end
163-
PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{Pair{K}}) where {T,K} =
164-
PyIterable_ConvertRule_pair(o, T, Pair{K,Python.PyObject})
165-
PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{Pair{K,V} where K}) where {T,V} =
166-
PyIterable_ConvertRule_pair(o, T, Pair{Python.PyObject,V})
167-
PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{Pair}) where {T} =
168-
PyIterable_ConvertRule_pair(o, T, Pair{Python.PyObject,Python.PyObject})
169-
PyIterable_ConvertRule_pair(o, ::Type{T}, ::Type{S}) where {T,S<:Pair} = begin
173+
PyIterable_ConvertRule_pair(o, ::Type{Pair{K}}) where {K} =
174+
PyIterable_ConvertRule_pair(o, Pair{K,Python.PyObject})
175+
PyIterable_ConvertRule_pair(o, ::Type{Pair{K,V} where K}) where {V} =
176+
PyIterable_ConvertRule_pair(o, Pair{Python.PyObject,V})
177+
PyIterable_ConvertRule_pair(o, ::Type{Pair}) =
178+
PyIterable_ConvertRule_pair(o, Pair{Python.PyObject,Python.PyObject})
179+
PyIterable_ConvertRule_pair(o, ::Type{S}) where {S<:Pair} = begin
170180
PyErr_SetString(PyExc_Exception(), "When converting Python iterable to Julia 'Pair', the destination type cannot be too complicated: the two types must either be fully specified or left unspecified. Got '$S'.")
171181
return -1
172182
end
173183

174-
PyMapping_ConvertRule_dict(o, ::Type{T}, ::Type{S}) where {T, S<:Dict} = begin
184+
PyMapping_ConvertRule_dict(o, ::Type{S}) where {S<:Dict} = begin
175185
it = PyObject_GetIter(o)
176186
isnull(it) && return -1
177187
xs = S()
@@ -196,13 +206,13 @@ PyMapping_ConvertRule_dict(o, ::Type{T}, ::Type{S}) where {T, S<:Dict} = begin
196206
return -1
197207
else
198208
Py_DecRef(it)
199-
return putresult(T, xs)
209+
return putresult(xs)
200210
end
201211
end
202212
end
203-
PyMapping_ConvertRule_dict(o, ::Type{T}, ::Type{Dict{K}}) where {T,K} =
204-
PyMapping_ConvertRule_dict(o, T, Dict{K,Python.PyObject})
205-
PyMapping_ConvertRule_dict(o, ::Type{T}, ::Type{Dict{K,V} where K}) where {T,V} =
206-
PyMapping_ConvertRule_dict(o, T, Dict{Python.PyObject,V})
207-
PyMapping_ConvertRule_dict(o, ::Type{T}, ::Type{Dict}) where {T} =
208-
PyMapping_ConvertRule_dict(o, T, Dict{Python.PyObject,Python.PyObject})
213+
PyMapping_ConvertRule_dict(o, ::Type{Dict{K}}) where {K} =
214+
PyMapping_ConvertRule_dict(o, Dict{K,Python.PyObject})
215+
PyMapping_ConvertRule_dict(o, ::Type{Dict{K,V} where K}) where {V} =
216+
PyMapping_ConvertRule_dict(o, Dict{Python.PyObject,V})
217+
PyMapping_ConvertRule_dict(o, ::Type{Dict}) =
218+
PyMapping_ConvertRule_dict(o, Dict{Python.PyObject,Python.PyObject})

src/cpython/complex.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ PyComplex_From(x::Union{Float16,Float32,Float64}) = PyComplex_FromDoubles(x, 0)
1919
PyComplex_From(x::Complex{<:Union{Float16,Float32,Float64}}) = PyComplex_FromDoubles(real(x), imag(x))
2020

2121
# "Complexable" means a 'complex' or anything with a '__complex__' method
22-
PyComplexable_TryConvertRule_convert(o, ::Type{T}, ::Type{S}) where {T,S} = begin
22+
PyComplexable_TryConvertRule_convert(o, ::Type{S}) where {S} = begin
2323
x = PyComplex_AsComplex(o)
2424
ism1(x) && PyErr_IsSet() && return -1
25-
putresult(T, convert(S, x))
25+
putresult(convert(S, x))
2626
end
2727

28-
PyComplexable_TryConvertRule_tryconvert(o, ::Type{T}, ::Type{S}) where {T,S} = begin
28+
PyComplexable_TryConvertRule_tryconvert(o, ::Type{S}) where {S} = begin
2929
x = PyComplexable_AsComplex(o)
3030
ism1(x) && PyErr_IsSet() && return -1
31-
putresult(T, tryconvert(S, x))
31+
putresult(tryconvert(S, x))
3232
end

src/cpython/ctypes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
struct PySimpleCData_TryConvert_value{R,tr} end
22

3-
(::PySimpleCData_TryConvert_value{R,tr})(o, ::Type{T}, ::Type{S}) where {T,S,R,tr} = begin
3+
(::PySimpleCData_TryConvert_value{R,tr})(o, ::Type{S}) where {S,R,tr} = begin
44
ptr = PySimpleObject_GetValue(o, Ptr{R})
55
val = unsafe_load(ptr)
6-
putresult(T, tr ? tryconvert(S, val) : convert(S, val))
6+
putresult(tr ? tryconvert(S, val) : convert(S, val))
77
end

src/cpython/float.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ PyFloat_CheckExact(o) = Py_TypeCheckExact(o, PyFloat_Type())
1111
PyFloat_From(o::Union{Float16,Float32,Float64}) = PyFloat_FromDouble(o)
1212

1313
# "Floatable" means a 'float' or anything with a '__float__' method
14-
PyFloatable_TryConvertRule_convert(o, ::Type{T}, ::Type{S}) where {T,S} = begin
14+
PyFloatable_TryConvertRule_convert(o, ::Type{S}) where {S} = begin
1515
x = PyFloat_AsDouble(o)
1616
ism1(x) && PyErr_IsSet() && return -1
17-
putresult(T, convert(S, x))
17+
putresult(convert(S, x))
1818
end
1919

20-
PyFloatable_TryConvertRule_tryconvert(o, ::Type{T}, ::Type{S}) where {T,S} = begin
20+
PyFloatable_TryConvertRule_tryconvert(o, ::Type{S}) where {S} = begin
2121
x = PyFloat_AsDouble(o)
2222
ism1(x) && PyErr_IsSet() && return -1
23-
putresult(T, tryconvert(S, x))
23+
putresult(tryconvert(S, x))
2424
end

src/cpython/int.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ PyLong_From(x::Integer) = begin
2828
end
2929

3030
# "Longable" means an 'int' or anything with an '__int__' method.
31-
PyLongable_TryConvertRule_integer(o, ::Type{T}, ::Type{S}) where {T, S<:Integer} = begin
31+
PyLongable_TryConvertRule_integer(o, ::Type{S}) where {S<:Integer} = begin
3232
# first try to convert to Clonglong (or Culonglong if unsigned)
3333
x = S <: Unsigned ? PyLong_AsUnsignedLongLong(o) : PyLong_AsLongLong(o)
3434
if !ism1(x) || !PyErr_IsSet()
3535
# success
36-
return putresult(T, tryconvert(S, x))
36+
return putresult(tryconvert(S, x))
3737
elseif PyErr_IsSet(PyExc_OverflowError())
3838
# overflows Clonglong or Culonglong
3939
PyErr_Clear()
@@ -44,20 +44,20 @@ PyLongable_TryConvertRule_integer(o, ::Type{T}, ::Type{S}) where {T, S<:Integer}
4444
# try converting to String then BigInt then S
4545
so = PyObject_Str(o)
4646
isnull(so) && return -1
47-
r = PyUnicode_TryConvertRule_string(so, String, String)
47+
s = PyUnicode_AsString(so)
4848
Py_DecRef(so)
49-
r == 1 || return r
50-
y = tryparse(BigInt, takeresult(String))
49+
isempty(s) && PyErr_IsSet() && return -1
50+
y = tryparse(BigInt, s)
5151
y === nothing && (PyErr_SetString(PyExc_ValueError(), "Cannot convert this '$(PyType_Name(Py_Type(o)))' to a Julia 'BigInt' because its string representation cannot be parsed as an integer"); return -1)
52-
return putresult(T, tryconvert(S, y))
52+
return putresult(tryconvert(S, y))
5353
end
5454
else
5555
# other error
5656
return -1
5757
end
5858
end
5959

60-
PyLongable_TryConvertRule_tryconvert(o, ::Type{T}, ::Type{S}) where {T,S} = begin
61-
r = PyLong_TryConvertRule_integer(o, Integer, Integer)
62-
r == 1 ? putresult(T, tryconvert(S, takeresult(Integer))) : r
60+
PyLongable_TryConvertRule_tryconvert(o, ::Type{S}) where {S} = begin
61+
r = PyLongable_TryConvertRule_integer(o, Integer)
62+
r == 1 ? putresult(tryconvert(S, takeresult(Integer))) : r
6363
end

0 commit comments

Comments
 (0)