Skip to content

Commit

Permalink
check for buffer endianness, throw more specific errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed May 27, 2015
1 parent 1149823 commit 845b947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/pyarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jltype_to_pyfmt{T}(::Type{T}) = jltype_to_pyfmt(IOBuffer(), T)

function jltype_to_pyfmt{T}(io::IO, ::Type{T})
if nfields(T) == 0
error("no fields for structure type $T")
throw(ArgumentError("no fields for structure type $T"))
end
write(io, "T{")
for n in fieldnames(T)
Expand All @@ -119,10 +119,10 @@ function jltype_to_pyfmt{T}(io::IO, ::Type{T})
elseif Base.isstructtype(T)
jltype_to_pyfmt(io, ty)
else
error("pyfmt unknown conversion for type $T")
throw(ArgumentError("unknown pyfmt conversion for type $T"))
end
else
error("pyfmt can only encode bits types")
throw(ArgumentError("$T is not a bits type"))
end
end
write(io, "}")
Expand Down Expand Up @@ -192,12 +192,12 @@ function PyArray(o::PyObject)
throw(ArgumentError("Python buffer has no format string"))
end
order, tys = parse_pyfmt(bytestring(view.buf.format))
if isempty(tys)
if order !== :native
throw(ArgumentError("PyArray cannot yet handle non-native endian buffers"))
elseif isempty(tys)
throw(ArgumentError("PyArray cannot yet handle structure types"))
end
ty = first(tys)
ndim = ndims(view)
return PyArray{ty, ndim}(o, view)
return PyArray{tys[1], ndims(view)}(o, view)
end

Base.size(a::PyArray) = a.dims
Expand Down
12 changes: 10 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ end

@pyimport array
@pyimport numpy as np
@pyimport numpy.random as random

@test roundtripeq(Any[1 2 3; 4 5 6])
@test roundtripeq(Any[])
Expand Down Expand Up @@ -264,6 +265,13 @@ view = PyCall.PyBuffer(a, PyCall.PyBUF_RECORDS)
@test PyCall.f_contiguous(view) == true
@test PyCall.pyfmt(view) == "B"

# check endianness
let a = random.randn(10,10),
bsa = a[:newbyteorder]()

@test_throws ArgumentError PyArray(bsa)
end

###################################################
# PyArray Tests

Expand Down Expand Up @@ -387,8 +395,8 @@ type Dummy end
type Test4
a::Dummy
end
@test_throws ErrorException PyCall.jltype_to_pyfmt(Test4)
@test_throws ArgumentError PyCall.jltype_to_pyfmt(Test4)

immutable Test5
end
@test_throws ErrorException PyCall.jltype_to_pyfmt(Test5)
@test_throws ArgumentError PyCall.jltype_to_pyfmt(Test5)

0 comments on commit 845b947

Please sign in to comment.