Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 0.6 depwarns #356

Merged
merged 1 commit into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import Compat.String
import Base.unsafe_convert
import MacroTools # because of issue #270

if isdefined(Base, :Iterators)
import Base.Iterators: filter
end

#########################################################################

include(joinpath(dirname(@__FILE__), "..", "deps","depsutils.jl"))
Expand Down
9 changes: 5 additions & 4 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ convert{T<:Integer}(::Type{T}, po::PyObject) =

if Sys.WORD_SIZE == 32
convert{T<:Union{Int64,UInt64}}(::Type{T}, po::PyObject) =
@pycheck ccall((@pysym :PyLong_AsLongLong), T, (PyPtr,), asscalar(po))
@pycheck(ccall((@pysym :PyLong_AsLongLong), UInt64, (PyPtr,), asscalar(po))) % T
end

convert(::Type{Bool}, po::PyObject) =
Expand Down Expand Up @@ -140,9 +140,10 @@ pyptr_query(po::PyObject) = pyisinstance(po, c_void_p_Type) || pyisinstance(po,
# typealias PyAny Union{PyObject, Int, Bool, Float64, Complex128, AbstractString, Function, Dict, Tuple, Array}
abstract PyAny

pyany_toany(T::Type) = T
pyany_toany(T::Type{PyAny}) = Any
pyany_toany(T::Type{Vararg{PyAny}}) = Vararg{Any}
function pyany_toany(T::Type)
T === Vararg{PyAny} ? Vararg{Any} : T
end
pyany_toany(::Type{PyAny}) = Any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type{Vararg{PyAny}} isn't a valid type anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the full story but it was never an actual type (you can't find a value that matches this type)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, so why does T === Vararg{PyAny} still work if that is not a type?

Copy link
Collaborator Author

@yuyichao yuyichao Jan 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of it has always been strange. It is essentially only meaningful in a tuple but it's hard to only allow it there without breaking many other things. It is used to be allowed to use it just like a type though almost all types constructed this way are invalid. This is now disallowed.

It is still allowed to be used as a value and is still treated as a type for dispatch purpose even if it never actually was one. I believe the long term goal is to get rid of it or somehow being able to pass it around without dealing with this special case or generating invalid types.

pyany_toany{T<:Tuple}(t::Type{T}) = Tuple{map(pyany_toany, t.types)...}

# PyAny acts like Any for conversions, except for converting PyObject (below)
Expand Down
6 changes: 3 additions & 3 deletions src/pydates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ PyDelta_FromDSU(days, seconds, useconds) =
days, seconds, useconds,
1, DeltaType[]))

PyObject(p::Dates.Day) = PyDelta_FromDSU(Int(p), 0, 0)
PyObject(p::Dates.Day) = PyDelta_FromDSU(Dates.value(p), 0, 0)

function PyObject(p::Dates.Second)
# normalize to make Cint overflow less likely
s = Int(p)
s = Dates.value(p)
d = div(s, 86400)
s -= d * 86400
PyDelta_FromDSU(d, s, 0)
end

function PyObject(p::Dates.Millisecond)
# normalize to make Cint overflow less likely
ms = Int(p)
ms = Dates.value(p)
s = div(ms, 1000)
ms -= s * 1000
d = div(s, 86400)
Expand Down
4 changes: 3 additions & 1 deletion src/pyeval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ macro py_str(code, options...)
code, locals = interpolate_pycode(code)
input_type = '\n' in code ? Py_file_input : Py_eval_input
fname = make_fname(@__FILE__)
assignlocals = Expr(:block, [:(m[$v] = PyObject($(esc(ex)))) for (v,ex) in locals if isa(v,String)]...)
assignlocals = Expr(:block, [(isa(v,String) ?
:(m[$v] = PyObject($(esc(ex)))) :
nothing) for (v,ex) in locals]...)
code_expr = Expr(:call, esc(:(Base.string)))
i0 = start(code)
for i in sort!(collect(filter(k -> isa(k,Integer), keys(locals))))
Expand Down
13 changes: 10 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Base.Test, PyCall, Compat
import Compat.String

if isdefined(Base, :Iterators)
filter(f, itr) = collect(Iterators.filter(f, itr))
filter(f, d::Associative) = Base.filter(f, d)
end

PYTHONPATH=get(ENV,"PYTHONPATH","")
PYTHONHOME=get(ENV,"PYTHONHOME","")
PYTHONEXECUTABLE=get(ENV,"PYTHONEXECUTABLE","")
Expand Down Expand Up @@ -96,11 +101,13 @@ array2py2arrayeq(x) = PyCall.py2array(Float64,PyCall.array2py(x)) == x
@test roundtrip(2:2.0:10) == convert(Vector{Float64}, 2:2.0:10)

@pyimport math
@test_approx_eq math.sin(3) sin(3)
@test math.sin(3) sin(3)

@test collect(PyObject([1,"hello",5])) == [1,"hello",5]

@test try @eval (@pyimport os.path) catch ex isa(ex, ArgumentError) end
@test try @eval (@pyimport os.path) catch ex
isa(ex, ArgumentError)
end

@test PyObject("hello") == PyObject("hello")
@test PyObject("hello") != PyObject("hellö")
Expand Down Expand Up @@ -166,7 +173,7 @@ end
let buf = IOBuffer("hello\nagain"), obuf = PyObject(buf)
@test !obuf[:writable]()
@test obuf[:readable]()
@test obuf[:readlines]() == ["hello\n","again"]
@test obuf[:readlines]() == readlines(IOBuffer("hello\nagain"))
end
let buf = IOBuffer("hello\nagain"), obuf = PyObject(buf)
@test obuf[:read](5) == convert(Vector{UInt8}, "hello")
Expand Down