55
66# conversions from Julia types to PyObject:
77
8- @static if pyversion < v " 3"
9- PyObject (i:: Unsigned ) = PyObject (@pycheckn ccall (@pysym (:PyInt_FromSize_t ),
10- PyPtr, (UInt,), i))
11- PyObject (i:: Integer ) = PyObject (@pycheckn ccall (@pysym (:PyInt_FromSsize_t ),
12- PyPtr, (Int,), i))
13- else
14- PyObject (i:: Unsigned ) = PyObject (@pycheckn ccall (@pysym (:PyLong_FromUnsignedLongLong ),
15- PyPtr, (Culonglong,), i))
16- PyObject (i:: Integer ) = PyObject (@pycheckn ccall (@pysym (:PyLong_FromLongLong ),
17- PyPtr, (Clonglong,), i))
18- end
8+ PyObject (i:: Unsigned ) = PyObject (@pycheckn ccall (@pysym (:PyLong_FromUnsignedLongLong ),
9+ PyPtr, (Culonglong,), i))
10+ PyObject (i:: Integer ) = PyObject (@pycheckn ccall (@pysym (:PyLong_FromLongLong ),
11+ PyPtr, (Clonglong,), i))
1912
2013PyObject (b:: Bool ) = PyObject (@pycheckn ccall ((@pysym :PyBool_FromLong ),
2114 PyPtr, (Clong,), b))
@@ -31,25 +24,17 @@ PyObject(n::Nothing) = pyerr_check("PyObject(nothing)", pyincref(pynothing[]))
3124
3225# conversions to Julia types from PyObject
3326
34- @static if pyversion < v " 3"
35- convert (:: Type{T} , po:: PyObject ) where {T<: Integer } =
36- T (@pycheck ccall (@pysym (:PyInt_AsSsize_t ), Int, (PyPtr,), po))
37- elseif pyversion < v " 3.2"
38- convert (:: Type{T} , po:: PyObject ) where {T<: Integer } =
39- T (@pycheck ccall (@pysym (:PyLong_AsLongLong ), Clonglong, (PyPtr,), po))
40- else
41- function convert (:: Type{T} , po:: PyObject ) where {T<: Integer }
42- overflow = Ref {Cint} ()
43- val = T (@pycheck ccall (@pysym (:PyLong_AsLongLongAndOverflow ), Clonglong, (PyPtr, Ref{Cint}), po, overflow))
44- iszero (overflow[]) || throw (InexactError (:convert , T, po))
45- return val
46- end
47- function convert (:: Type{Integer} , po:: PyObject )
48- overflow = Ref {Cint} ()
49- val = @pycheck ccall (@pysym (:PyLong_AsLongLongAndOverflow ), Clonglong, (PyPtr, Ref{Cint}), po, overflow)
50- iszero (overflow[]) || return convert (BigInt, po)
51- return val
52- end
27+ function convert (:: Type{T} , po:: PyObject ) where {T<: Integer }
28+ overflow = Ref {Cint} ()
29+ val = T (@pycheck ccall (@pysym (:PyLong_AsLongLongAndOverflow ), Clonglong, (PyPtr, Ref{Cint}), po, overflow))
30+ iszero (overflow[]) || throw (InexactError (:convert , T, po))
31+ return val
32+ end
33+ function convert (:: Type{Integer} , po:: PyObject )
34+ overflow = Ref {Cint} ()
35+ val = @pycheck ccall (@pysym (:PyLong_AsLongLongAndOverflow ), Clonglong, (PyPtr, Ref{Cint}), po, overflow)
36+ iszero (overflow[]) || return convert (BigInt, po)
37+ return val
5338end
5439
5540convert (:: Type{Bool} , po:: PyObject ) =
7863
7964function PyObject (s:: AbstractString )
8065 sb = String (s)
81- if pyunicode_literals || ! isascii (sb)
82- PyObject (@pycheckn ccall (@pysym (PyUnicode_DecodeUTF8),
83- PyPtr, (Ptr{UInt8}, Int, Ptr{UInt8}),
84- sb, sizeof (sb), C_NULL ))
85- else
86- pybytes (sb)
87- end
66+ PyObject (@pycheckn ccall (@pysym (PyUnicode_DecodeUTF8),
67+ PyPtr, (Ptr{UInt8}, Int, Ptr{UInt8}),
68+ sb, sizeof (sb), C_NULL ))
8869end
8970
9071const _ps_ptr= Ptr{UInt8}[C_NULL ]
@@ -713,16 +694,9 @@ include("pydates.jl")
713694# A type-query function f(o::PyObject) returns the Julia type
714695# for use with the convert function, or Union{} if there isn't one.
715696
716- @static if pyversion < v " 3"
717- pyint_query (o:: PyObject ) = pyisinstance (o, @pyglobalobj :PyInt_Type ) ?
718- (pyisinstance (o, @pyglobalobj :PyBool_Type ) ? Bool : Int) :
719- pyisinstance (o, @pyglobalobj :PyLong_Type ) ? BigInt :
720- pyisinstance (o, npy_integer) ? Int : Union{}
721- else
722- pyint_query (o:: PyObject ) = pyisinstance (o, @pyglobalobj :PyLong_Type ) ?
723- (pyisinstance (o, @pyglobalobj :PyBool_Type ) ? Bool : Integer) :
724- pyisinstance (o, npy_integer) ? Integer : Union{}
725- end
697+ pyint_query (o:: PyObject ) = pyisinstance (o, @pyglobalobj :PyLong_Type ) ?
698+ (pyisinstance (o, @pyglobalobj :PyBool_Type ) ? Bool : Integer) :
699+ pyisinstance (o, npy_integer) ? Integer : Union{}
726700
727701pyfloat_query (o:: PyObject ) = pyisinstance (o, @pyglobalobj :PyFloat_Type ) || pyisinstance (o, npy_floating) ? Float64 : Union{}
728702
0 commit comments