From 75e93fc6b43291e37cbaf301d9c626c9937695e5 Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Fri, 11 Aug 2017 22:07:55 -0400 Subject: [PATCH] Fix most of 0.7 depwarns --- REQUIRE | 2 +- deps/build.jl | 6 +++--- src/PyCall.jl | 9 ++++++++- src/io.jl | 2 +- src/numpy.jl | 2 +- src/pybuffer.jl | 9 ++++++--- src/pyinit.jl | 22 +++++++++++++--------- src/pyiterator.jl | 4 ++-- src/pytype.jl | 36 ++++++++++++++++++++---------------- test/runtests.jl | 10 +++++----- 10 files changed, 60 insertions(+), 42 deletions(-) diff --git a/REQUIRE b/REQUIRE index a08ef4f1..8aa1c2c4 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,4 +1,4 @@ julia 0.5 -Compat 0.27.0 +Compat 0.30.0 Conda 0.2 MacroTools 0.3 diff --git a/deps/build.jl b/deps/build.jl index ea57d865..c46b12eb 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -36,7 +36,7 @@ function pythonenv(cmd::Cmd) setenv(cmd, env) end -pyvar(python::AbstractString, mod::AbstractString, var::AbstractString) = chomp(readstring(pythonenv(`$python -c "import $mod; print($mod.$var)"`))) +pyvar(python::AbstractString, mod::AbstractString, var::AbstractString) = chomp(read(pythonenv(`$python -c "import $mod; print($mod.$var)"`), String)) pyconfigvar(python::AbstractString, var::AbstractString) = pyvar(python, "distutils.sysconfig", "get_config_var('$var')") pyconfigvar(python, var, default) = let v = pyconfigvar(python, var) @@ -113,7 +113,7 @@ function find_libpython(python::AbstractString) print(s, "\n\n") end println(STDERR, "---------------------------------- get_config_vars ---------------------------------------") - print(STDERR, readstring(`python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"`)) + print(STDERR, read(`python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"`, String)) println(STDERR, "--------------------------------- directory contents -------------------------------------") for libpath in libpaths if isdir(libpath) @@ -221,7 +221,7 @@ wstringconst(s) = string("Base.cconvert(Cwstring, \"", escape_string(s), "\")") # to prevent unnecessary recompilation and to minimize # problems in the unlikely event of read-only directories. function writeifchanged(filename, str) - if !isfile(filename) || readstring(filename) != str + if !isfile(filename) || read(filename, String) != str info(abspath(filename), " has been updated") write(filename, str) else diff --git a/src/PyCall.jl b/src/PyCall.jl index bab1c1b4..b173f8d5 100644 --- a/src/PyCall.jl +++ b/src/PyCall.jl @@ -462,8 +462,15 @@ end macro pyimport(name, optional_varname...) mname = modulename(name) Name = pyimport_name(name, optional_varname) + quoteName = Expr(:quote, Name) + # VERSION 0.7 + @static if isdefined(Base, Symbol("@isdefined")) + isdef_check = :(isdefined($__module__, $quoteName)) + else + isdef_check = :(isdefined($quoteName)) + end quote - if !isdefined($(Expr(:quote, Name))) + if !$isdef_check const $(esc(Name)) = pywrap(pyimport($mname)) elseif !isa($(esc(Name)), Module) error("@pyimport: ", $(Expr(:quote, Name)), " already defined") diff --git a/src/io.jl b/src/io.jl index 8dd84f86..a36383bc 100644 --- a/src/io.jl +++ b/src/io.jl @@ -108,7 +108,7 @@ function pyio_initialize() String(read(pyio_jl(self), nb < 0 ? typemax(Int) : nb)) : pybytes(read(pyio_jl(self), nb < 0 ? typemax(Int) : nb))) readall(self) = - @with_ioraise(self[:istextio] ? readstring(pyio_jl(self)) : + @with_ioraise(self[:istextio] ? read(pyio_jl(self), String) : pybytes(read(pyio_jl(self)))) readinto(self, b) = @with_ioraise(pybytes(readbytes!(pyio_jl(self), b))) write(self, b) = @with_ioraise(write(pyio_jl(self), b)) diff --git a/src/numpy.jl b/src/numpy.jl index 2b5f55eb..281438d4 100644 --- a/src/numpy.jl +++ b/src/numpy.jl @@ -74,7 +74,7 @@ function npyinitialize() # Parse __multiarray_api.h to obtain length and meaning of PyArray_API try hdrfile = open(joinpath(inc, "numpy", "__multiarray_api.h")) - hdr = readstring(hdrfile); + hdr = read(hdrfile, String) close(hdrfile) catch e error("could not read __multiarray_api.h to parse PyArray_API ", e) diff --git a/src/pybuffer.jl b/src/pybuffer.jl index 4055296e..430a55d7 100644 --- a/src/pybuffer.jl +++ b/src/pybuffer.jl @@ -40,7 +40,8 @@ end function pydecref(o::PyBuffer) # note that PyBuffer_Release sets o.obj to NULL, and # is a no-op if o.obj is already NULL - ccall(@pysym(:PyBuffer_Release), Void, (Ptr{PyBuffer},), &o) + # TODO change to `Ref{PyBuffer}` when 0.6 is dropped. + ccall(@pysym(:PyBuffer_Release), Void, (Any,), o) o end @@ -85,9 +86,10 @@ function Base.stride(b::PyBuffer, d::Integer) return Int(unsafe_load(b.buf.strides, d)) end +# TODO change to `Ref{PyBuffer}` when 0.6 is dropped. iscontiguous(b::PyBuffer) = 1 == ccall((@pysym :PyBuffer_IsContiguous), Cint, - (Ptr{PyBuffer}, Cchar), &b, 'A') + (Any, Cchar), b, 'A') ############################################################################# # pybuffer constant values from Include/object.h @@ -105,8 +107,9 @@ const PyBUF_INDIRECT = convert(Cint, 0x0100) | PyBUF_STRIDES # construct a PyBuffer from a PyObject, if possible function PyBuffer(o::Union{PyObject,PyPtr}, flags=PyBUF_SIMPLE) b = PyBuffer() + # TODO change to `Ref{PyBuffer}` when 0.6 is dropped. @pycheckz ccall((@pysym :PyObject_GetBuffer), Cint, - (PyPtr, Ptr{PyBuffer}, Cint), o, &b, flags) + (PyPtr, Any, Cint), o, b, flags) return b end diff --git a/src/pyinit.jl b/src/pyinit.jl index 622e1913..f07b6126 100644 --- a/src/pyinit.jl +++ b/src/pyinit.jl @@ -23,6 +23,18 @@ const pyxrange = Ref{PyPtr}(0) ######################################################################### +type EmptyStringList + x::Int32 + ptr::Ptr{Int32} + function EmptyStringList() + obj = new(0) + obj.ptr = pointer_from_objref(obj) + return obj + end +end +Base.unsafe_convert(::Type{Ptr{UInt32}}, x::EmptyStringList) = + Ptr{UInt32}(pointer_from_objref(x) + fieldoffset(EmptyStringList, 2)) + function __init__() # issue #189 libpy_handle = libpython === nothing ? C_NULL : @@ -87,15 +99,7 @@ function __init__() if !already_inited # some modules (e.g. IPython) expect sys.argv to be set - if pyversion.major < 3 - argv_s = "" - argv = unsafe_convert(Ptr{UInt8}, argv_s) - ccall(@pysym(:PySys_SetArgvEx), Void, (Cint,Ptr{Ptr{UInt8}},Cint), 1, &argv, 0) - else - argv_s = Cwchar_t[0] - argv = unsafe_convert(Ptr{Cwchar_t}, argv_s) - ccall(@pysym(:PySys_SetArgvEx), Void, (Cint, Ptr{Ptr{Cwchar_t}}, Cint), 1, &argv, 0) - end + ccall(@pysym(:PySys_SetArgvEx), Void, (Cint, Ptr{UInt32}, Cint), 1, EmptyStringList(), 0) # Some Python code checks sys.ps1 to see if it is running # interactively, and refuses to be interactive otherwise. diff --git a/src/pyiterator.jl b/src/pyiterator.jl index e3880d33..1366c105 100644 --- a/src/pyiterator.jl +++ b/src/pyiterator.jl @@ -49,8 +49,8 @@ const jlWrapIteratorType = PyTypeObject() function jlwrap_iterator(o::Any) if jlWrapIteratorType.tp_name == C_NULL # lazily initialize pyjlwrap_type!(jlWrapIteratorType, "PyCall.jlwrap_iterator") do t - t.tp_iter = cfunction(pyincref_, PyPtr, (PyPtr,)) # new reference to same object - t.tp_iternext = cfunction(pyjlwrap_iternext, PyPtr, (PyPtr,)) + t.tp_iter = cfunction(pyincref_, PyPtr, Tuple{PyPtr}) # new reference to same object + t.tp_iternext = cfunction(pyjlwrap_iternext, PyPtr, Tuple{PyPtr}) end end return pyjlwrap_new(jlWrapIteratorType, (o, Ref(start(o)))) diff --git a/src/pytype.jl b/src/pytype.jl index 6ea5a5c5..d827f073 100644 --- a/src/pytype.jl +++ b/src/pytype.jl @@ -68,15 +68,15 @@ end function PyGetSetDef(name::AbstractString, get::Function,set::Function, doc::AbstractString="") PyGetSetDef(gstring_ptr(name, name), - cfunction(get, PyPtr, (PyPtr,Ptr{Void})), - cfunction(set, Int, (PyPtr,PyPtr,Ptr{Void})), + cfunction(get, PyPtr, Tuple{PyPtr,Ptr{Void}}), + cfunction(set, Int, Tuple{PyPtr,PyPtr,Ptr{Void}}), isempty(doc) ? NULL_UInt8_Ptr : gstring_ptr(name, doc), C_NULL) end function PyGetSetDef(name::AbstractString, get::Function, doc::AbstractString="") PyGetSetDef(gstring_ptr(name, name), - cfunction(get, PyPtr, (PyPtr,Ptr{Void})), + cfunction(get, PyPtr, Tuple{PyPtr,Ptr{Void}}), C_NULL, isempty(doc) ? NULL_UInt8_Ptr : gstring_ptr(name, doc), C_NULL) @@ -319,8 +319,9 @@ function PyTypeObject!(init::Function, t::PyTypeObject, name::AbstractString, ba if t.tp_new == C_NULL t.tp_new = @pyglobal :PyType_GenericNew end - @pycheckz ccall((@pysym :PyType_Ready), Cint, (Ptr{PyTypeObject},), &t) - ccall((@pysym :Py_IncRef), Void, (Ptr{PyTypeObject},), &t) + # TODO change to `Ref{PyTypeObject}` when 0.6 is dropped. + @pycheckz ccall((@pysym :PyType_Ready), Cint, (Any,), t) + ccall((@pysym :Py_IncRef), Void, (Any,), t) return t end @@ -422,13 +423,13 @@ function pyjlwrap_init() PyMemberDef(C_NULL,0,0,0,C_NULL)) # all cfunctions must be compiled at runtime - pyjlwrap_dealloc_ptr = cfunction(pyjlwrap_dealloc, Void, (PyPtr,)) - pyjlwrap_repr_ptr = cfunction(pyjlwrap_repr, PyPtr, (PyPtr,)) - pyjlwrap_hash_ptr = cfunction(pyjlwrap_hash, UInt, (PyPtr,)) - pyjlwrap_hash32_ptr = cfunction(pyjlwrap_hash32, UInt32, (PyPtr,)) - pyjlwrap_call_ptr = cfunction(pyjlwrap_call, PyPtr, (PyPtr,PyPtr,PyPtr)) - pyjlwrap_getattr_ptr = cfunction(pyjlwrap_getattr, PyPtr, (PyPtr,PyPtr)) - pyjlwrap_getiter_ptr = cfunction(pyjlwrap_getiter, PyPtr, (PyPtr,)) + pyjlwrap_dealloc_ptr = cfunction(pyjlwrap_dealloc, Void, Tuple{PyPtr}) + pyjlwrap_repr_ptr = cfunction(pyjlwrap_repr, PyPtr, Tuple{PyPtr}) + pyjlwrap_hash_ptr = cfunction(pyjlwrap_hash, UInt, Tuple{PyPtr}) + pyjlwrap_hash32_ptr = cfunction(pyjlwrap_hash32, UInt32, Tuple{PyPtr}) + pyjlwrap_call_ptr = cfunction(pyjlwrap_call, PyPtr, Tuple{PyPtr,PyPtr,PyPtr}) + pyjlwrap_getattr_ptr = cfunction(pyjlwrap_getattr, PyPtr, Tuple{PyPtr,PyPtr}) + pyjlwrap_getiter_ptr = cfunction(pyjlwrap_getiter, PyPtr, Tuple{PyPtr}) # detect at runtime whether we are using Stackless Python try @@ -453,8 +454,9 @@ end function pyjlwrap_type!(init::Function, to::PyTypeObject, name::AbstractString) sz = sizeof(Py_jlWrap) + sizeof(PyPtr) # must be > base type PyTypeObject!(to, name, sz) do t::PyTypeObject - t.tp_base = ccall(:jl_value_ptr, Ptr{Void}, (Ptr{PyTypeObject},), &jlWrapType) - ccall((@pysym :Py_IncRef), Void, (Ptr{PyTypeObject},), &jlWrapType) + # TODO change to `Ref{PyTypeObject}` when 0.6 is dropped. + t.tp_base = ccall(:jl_value_ptr, Ptr{Void}, (Any,), jlWrapType) + ccall((@pysym :Py_IncRef), Void, (Any,), jlWrapType) init(t) end end @@ -467,8 +469,9 @@ pyjlwrap_type(init::Function, name::AbstractString) = # since, the jl_value_t* may be to a temporary copy. But don't need # to wrap isbits types in Python objects anyway.) function pyjlwrap_new(pyT::PyTypeObject, value::Any) + # TODO change to `Ref{PyTypeObject}` when 0.6 is dropped. o = PyObject(@pycheckn ccall((@pysym :_PyObject_New), - PyPtr, (Ptr{PyTypeObject},), &pyT)) + PyPtr, (Any,), pyT)) pycall_gc[o.o] = value p = convert(Ptr{Ptr{Void}}, o.o) unsafe_store!(p, ccall(:jl_value_ptr, Ptr{Void}, (Any,), value), 3) @@ -479,7 +482,8 @@ function pyjlwrap_new(x::Any) pyjlwrap_new(jlWrapType, x) end -is_pyjlwrap(o::PyObject) = jlWrapType.tp_new != C_NULL && ccall((@pysym :PyObject_IsInstance), Cint, (PyPtr,Ptr{PyTypeObject}), o, &jlWrapType) == 1 +# TODO change to `Ref{PyTypeObject}` when 0.6 is dropped. +is_pyjlwrap(o::PyObject) = jlWrapType.tp_new != C_NULL && ccall((@pysym :PyObject_IsInstance), Cint, (PyPtr, Any), o, jlWrapType) == 1 ################################################################ # Fallback conversion: if we don't have a better conversion function, diff --git a/test/runtests.jl b/test/runtests.jl index 6015a30e..a79d7656 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -196,7 +196,7 @@ let nm = tempname() pf[:write](pyutf8(nm)) pf[:flush]() end - @test readstring(nm) == nm + @test read(nm, String) == nm end # issue #112 @@ -420,9 +420,9 @@ end # @pycall macro expands correctly _pycall = GlobalRef(PyCall,:pycall) -@test macroexpand(:(@pycall foo(bar)::T)) == :($(_pycall)(foo, T, bar)) -@test macroexpand(:(@pycall foo(bar, args...)::T)) == :($(_pycall)(foo, T, bar, args...)) -@test macroexpand(:(@pycall foo(bar; kwargs...)::T)) == :($(_pycall)(foo, T, bar; kwargs...)) +@test macroexpand(@__MODULE__, :(@pycall foo(bar)::T)) == :($(_pycall)(foo, T, bar)) +@test macroexpand(@__MODULE__, :(@pycall foo(bar, args...)::T)) == :($(_pycall)(foo, T, bar, args...)) +@test macroexpand(@__MODULE__, :(@pycall foo(bar; kwargs...)::T)) == :($(_pycall)(foo, T, bar; kwargs...)) # basic @pywith functionality @@ -432,7 +432,7 @@ try @pywith pybuiltin("open")(fname,"w") as f begin f[:write]("test") end - open(readstring,fname)=="test" + open(io->read(io, String), fname)=="test" end finally rm(fname,force=true)