Skip to content

Commit

Permalink
Misc fixes (#485)
Browse files Browse the repository at this point in the history
* Remove try catch in pysequence_query

* Avoid double check for isdict in PyDict constructor

Also - split long line is_mapping_object

The `pyisinstance(o, :PyDict_Type)` is fast, so not a major speedup, but seems fair.
  • Loading branch information
JobJob authored and stevengj committed Apr 4, 2018
1 parent 74a2223 commit 0f3ad3c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ include("numpy.jl")
# scipy scalar array members, grrr.
function is_mapping_object(o::PyObject)
pyisinstance(o, @pyglobalobj :PyDict_Type) ||
(pyquery((@pyglobal :PyMapping_Check), o) && ccall((@pysym :PyObject_HasAttrString), Cint, (PyPtr,Ptr{UInt8}), o, "items") == 1)
(pyquery((@pyglobal :PyMapping_Check), o) &&
ccall((@pysym :PyObject_HasAttrString), Cint, (PyPtr,Ptr{UInt8}), o, "items") == 1)
end

"""
Expand All @@ -420,7 +421,7 @@ mutable struct PyDict{K,V,isdict} <: AbstractDict{K,V}
# isdict = true for python dict, otherwise is a generic Mapping object

function PyDict{K,V,isdict}(o::PyObject) where {K,V,isdict}
if o.o != C_NULL && !is_mapping_object(o)
if !isdict && o.o != C_NULL && !is_mapping_object(o)
throw(ArgumentError("only Dict and Mapping objects can be converted to PyDict"))
end
return new{K,V,isdict}(o)
Expand Down Expand Up @@ -711,24 +712,22 @@ function pysequence_query(o::PyObject)
# problems
if pyisinstance(o, @pyglobalobj :PyTuple_Type)
len = @pycheckz ccall((@pysym :PySequence_Size), Int, (PyPtr,), o)
return typetuple([pytype_query(PyObject(ccall((@pysym :PySequence_GetItem), PyPtr, (PyPtr,Int), o,i-1)), PyAny) for i = 1:len])
return typetuple(pytype_query(PyObject(ccall((@pysym :PySequence_GetItem), PyPtr, (PyPtr,Int), o,i-1)), PyAny) for i = 1:len)
elseif pyisinstance(o, pyxrange[])
return AbstractRange
elseif ispybytearray(o)
return Vector{UInt8}
elseif !haskey(o, "__array_interface__")
# only handle PyList for now
return pyisinstance(o, @pyglobalobj :PyList_Type) ? Array : Union{}
else
try
otypestr = get(o["__array_interface__"], PyObject, "typestr")
typestr = convert(AbstractString, otypestr)
T = npy_typestrs[typestr[2:end]]
if T == PyPtr
T = PyObject
end
return Array{T}
catch
# only handle PyList for now
return pyisinstance(o, @pyglobalobj :PyList_Type) ? Array : Union{}
otypestr = get(o["__array_interface__"], PyObject, "typestr")
typestr = convert(AbstractString, otypestr) # Could this just be String now?
T = npy_typestrs[typestr[2:end]]
if T == PyPtr
T = PyObject
end
return Array{T}
end
end

Expand Down

0 comments on commit 0f3ad3c

Please sign in to comment.