Skip to content

Commit

Permalink
Fix most of 0.7 depwarns
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Sep 13, 2017
1 parent 0550f89 commit 75e93fc
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.5
Compat 0.27.0
Compat 0.30.0
Conda 0.2
MacroTools 0.3
6 changes: 3 additions & 3 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/numpy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/pybuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
22 changes: 13 additions & 9 deletions src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/pyiterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
36 changes: 20 additions & 16 deletions src/pytype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 75e93fc

Please sign in to comment.