Skip to content

Commit 314ac27

Browse files
authored
Merge pull request #425 from yuyichao/0.7
Fix most of 0.7 depwarns
2 parents 0550f89 + c6c19c1 commit 314ac27

File tree

13 files changed

+166
-132
lines changed

13 files changed

+166
-132
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
julia 0.5
2-
Compat 0.27.0
2+
Compat 0.32.0
33
Conda 0.2
44
MacroTools 0.3

deps/build.jl

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import Conda
1010

1111
immutable UseCondaPython <: Exception end
1212

13-
try # make sure deps.jl file is removed on error
14-
1513
#########################################################################
1614

1715
# Fix the environment for running `python`, and setts IO encoding to UTF-8.
@@ -36,7 +34,7 @@ function pythonenv(cmd::Cmd)
3634
setenv(cmd, env)
3735
end
3836

39-
pyvar(python::AbstractString, mod::AbstractString, var::AbstractString) = chomp(readstring(pythonenv(`$python -c "import $mod; print($mod.$var)"`)))
37+
pyvar(python::AbstractString, mod::AbstractString, var::AbstractString) = chomp(read(pythonenv(`$python -c "import $mod; print($mod.$var)"`), String))
4038

4139
pyconfigvar(python::AbstractString, var::AbstractString) = pyvar(python, "distutils.sysconfig", "get_config_var('$var')")
4240
pyconfigvar(python, var, default) = let v = pyconfigvar(python, var)
@@ -113,7 +111,7 @@ function find_libpython(python::AbstractString)
113111
print(s, "\n\n")
114112
end
115113
println(STDERR, "---------------------------------- get_config_vars ---------------------------------------")
116-
print(STDERR, readstring(`python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"`))
114+
print(STDERR, read(`python -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_vars())"`, String))
117115
println(STDERR, "--------------------------------- directory contents -------------------------------------")
118116
for libpath in libpaths
119117
if isdir(libpath)
@@ -142,76 +140,6 @@ include("depsutils.jl")
142140

143141
#########################################################################
144142

145-
const python = try
146-
let py = get(ENV, "PYTHON", isfile("PYTHON") ? readchomp("PYTHON") :
147-
Compat.Sys.islinux() || Sys.ARCH (:i686, :x86_64) ? "python" : ""),
148-
vers = isempty(py) ? v"0.0" : convert(VersionNumber, pyconfigvar(py,"VERSION","0.0"))
149-
if vers < v"2.7"
150-
if isempty(py)
151-
throw(UseCondaPython())
152-
else
153-
error("Python version $vers < 2.7 is not supported")
154-
end
155-
end
156-
157-
# check word size of Python via sys.maxsize, since a common error
158-
# on Windows is to link a 64-bit Julia to a 32-bit Python.
159-
pywordsize = parse(UInt64, pysys(py, "maxsize")) > (UInt64(1)<<32) ? 64 : 32
160-
if pywordsize != Sys.WORD_SIZE
161-
error("$py is $(pywordsize)-bit, but Julia is $(Sys.WORD_SIZE)-bit")
162-
end
163-
164-
py
165-
end
166-
catch e1
167-
if Sys.ARCH in (:i686, :x86_64)
168-
if isa(e1, UseCondaPython)
169-
info("Using the Python distribution in the Conda package by default.\n",
170-
"To use a different Python version, set ENV[\"PYTHON\"]=\"pythoncommand\" and re-run Pkg.build(\"PyCall\").")
171-
else
172-
info( "No system-wide Python was found; got the following error:\n",
173-
"$e1\nusing the Python distribution in the Conda package")
174-
end
175-
abspath(Conda.PYTHONDIR, "python" * (Compat.Sys.iswindows() ? ".exe" : ""))
176-
else
177-
error("No system-wide Python was found; got the following error:\n",
178-
"$e1")
179-
end
180-
end
181-
182-
use_conda = dirname(python) == abspath(Conda.PYTHONDIR)
183-
if use_conda
184-
Conda.add("numpy")
185-
end
186-
187-
const (libpython, libpy_name) = find_libpython(python)
188-
const programname = pysys(python, "executable")
189-
190-
# Get PYTHONHOME, either from the environment or from Python
191-
# itself (if it is not in the environment or if we are using Conda)
192-
PYTHONHOME = if !haskey(ENV, "PYTHONHOME") || use_conda
193-
# PYTHONHOME tells python where to look for both pure python
194-
# and binary modules. When it is set, it replaces both
195-
# `prefix` and `exec_prefix` and we thus need to set it to
196-
# both in case they differ. This is also what the
197-
# documentation recommends. However, they are documented
198-
# to always be the same on Windows, where it causes
199-
# problems if we try to include both.
200-
exec_prefix = pysys(python, "exec_prefix")
201-
Compat.Sys.iswindows() ? exec_prefix : pysys(python, "prefix") * ":" * exec_prefix
202-
else
203-
ENV["PYTHONHOME"]
204-
end
205-
206-
# cache the Python version as a Julia VersionNumber
207-
const pyversion = VersionNumber(pyvar(python, "platform", "python_version()"))
208-
209-
info("PyCall is using $python (Python $pyversion) at $programname, libpython = $libpy_name")
210-
211-
if pyversion < v"2.7"
212-
error("Python 2.7 or later is required for PyCall")
213-
end
214-
215143
# A couple of key strings need to be stored as constants so that
216144
# they persist throughout the life of the program. In Python 3,
217145
# they need to be wchar_t* data.
@@ -221,15 +149,86 @@ wstringconst(s) = string("Base.cconvert(Cwstring, \"", escape_string(s), "\")")
221149
# to prevent unnecessary recompilation and to minimize
222150
# problems in the unlikely event of read-only directories.
223151
function writeifchanged(filename, str)
224-
if !isfile(filename) || readstring(filename) != str
152+
if !isfile(filename) || read(filename, String) != str
225153
info(abspath(filename), " has been updated")
226154
write(filename, str)
227155
else
228156
info(abspath(filename), " has not changed")
229157
end
230158
end
231159

232-
writeifchanged("deps.jl", """
160+
try # make sure deps.jl file is removed on error
161+
python = try
162+
let py = get(ENV, "PYTHON", isfile("PYTHON") ? readchomp("PYTHON") :
163+
Compat.Sys.islinux() || Sys.ARCH (:i686, :x86_64) ? "python" : ""),
164+
vers = isempty(py) ? v"0.0" : convert(VersionNumber, pyconfigvar(py,"VERSION","0.0"))
165+
if vers < v"2.7"
166+
if isempty(py)
167+
throw(UseCondaPython())
168+
else
169+
error("Python version $vers < 2.7 is not supported")
170+
end
171+
end
172+
173+
# check word size of Python via sys.maxsize, since a common error
174+
# on Windows is to link a 64-bit Julia to a 32-bit Python.
175+
pywordsize = parse(UInt64, pysys(py, "maxsize")) > (UInt64(1)<<32) ? 64 : 32
176+
if pywordsize != Sys.WORD_SIZE
177+
error("$py is $(pywordsize)-bit, but Julia is $(Sys.WORD_SIZE)-bit")
178+
end
179+
180+
py
181+
end
182+
catch e1
183+
if Sys.ARCH in (:i686, :x86_64)
184+
if isa(e1, UseCondaPython)
185+
info("Using the Python distribution in the Conda package by default.\n",
186+
"To use a different Python version, set ENV[\"PYTHON\"]=\"pythoncommand\" and re-run Pkg.build(\"PyCall\").")
187+
else
188+
info( "No system-wide Python was found; got the following error:\n",
189+
"$e1\nusing the Python distribution in the Conda package")
190+
end
191+
abspath(Conda.PYTHONDIR, "python" * (Compat.Sys.iswindows() ? ".exe" : ""))
192+
else
193+
error("No system-wide Python was found; got the following error:\n",
194+
"$e1")
195+
end
196+
end
197+
198+
use_conda = dirname(python) == abspath(Conda.PYTHONDIR)
199+
if use_conda
200+
Conda.add("numpy")
201+
end
202+
203+
(libpython, libpy_name) = find_libpython(python)
204+
programname = pysys(python, "executable")
205+
206+
# Get PYTHONHOME, either from the environment or from Python
207+
# itself (if it is not in the environment or if we are using Conda)
208+
PYTHONHOME = if !haskey(ENV, "PYTHONHOME") || use_conda
209+
# PYTHONHOME tells python where to look for both pure python
210+
# and binary modules. When it is set, it replaces both
211+
# `prefix` and `exec_prefix` and we thus need to set it to
212+
# both in case they differ. This is also what the
213+
# documentation recommends. However, they are documented
214+
# to always be the same on Windows, where it causes
215+
# problems if we try to include both.
216+
exec_prefix = pysys(python, "exec_prefix")
217+
Compat.Sys.iswindows() ? exec_prefix : pysys(python, "prefix") * ":" * exec_prefix
218+
else
219+
ENV["PYTHONHOME"]
220+
end
221+
222+
# cache the Python version as a Julia VersionNumber
223+
pyversion = VersionNumber(pyvar(python, "platform", "python_version()"))
224+
225+
info("PyCall is using $python (Python $pyversion) at $programname, libpython = $libpy_name")
226+
227+
if pyversion < v"2.7"
228+
error("Python 2.7 or later is required for PyCall")
229+
end
230+
231+
writeifchanged("deps.jl", """
233232
const python = "$(escape_string(python))"
234233
const libpython = "$(escape_string(libpy_name))"
235234
const pyprogramname = "$(escape_string(programname))"
@@ -242,16 +241,16 @@ writeifchanged("deps.jl", """
242241
const conda = $use_conda
243242
""")
244243

245-
# Make subsequent builds (e.g. Pkg.update) use the same Python by default:
246-
writeifchanged("PYTHON", isfile(programname) ? programname : python)
244+
# Make subsequent builds (e.g. Pkg.update) use the same Python by default:
245+
writeifchanged("PYTHON", isfile(programname) ? programname : python)
247246

248-
#########################################################################
247+
#########################################################################
249248

250249
catch
251250

252-
# remove deps.jl (if it exists) on an error, so that PyCall will
253-
# not load until it is properly configured.
254-
isfile("deps.jl") && rm("deps.jl")
255-
rethrow()
251+
# remove deps.jl (if it exists) on an error, so that PyCall will
252+
# not load until it is properly configured.
253+
isfile("deps.jl") && rm("deps.jl")
254+
rethrow()
256255

257256
end

src/PyCall.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ end
432432
pyimport(name::Symbol) = pyimport(string(name))
433433

434434
# convert expressions like :math or :(scipy.special) into module name strings
435+
modulename(s::QuoteNode) = modulename(s.value)
435436
modulename(s::Symbol) = string(s)
436437
function modulename(e::Expr)
437438
if e.head == :.
@@ -462,8 +463,15 @@ end
462463
macro pyimport(name, optional_varname...)
463464
mname = modulename(name)
464465
Name = pyimport_name(name, optional_varname)
466+
quoteName = Expr(:quote, Name)
467+
# VERSION 0.7
468+
@static if isdefined(Base, Symbol("@isdefined"))
469+
isdef_check = :(isdefined($__module__, $quoteName))
470+
else
471+
isdef_check = :(isdefined($quoteName))
472+
end
465473
quote
466-
if !isdefined($(Expr(:quote, Name)))
474+
if !$isdef_check
467475
const $(esc(Name)) = pywrap(pyimport($mname))
468476
elseif !isa($(esc(Name)), Module)
469477
error("@pyimport: ", $(Expr(:quote, Name)), " already defined")

src/conversions.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,13 @@ function convert{K,V}(::Type{Dict{K,V}}, o::PyObject)
565565
end
566566

567567
#########################################################################
568-
# Range: integer ranges are converted to xrange,
569-
# while other ranges (<: AbstractVector) are converted to lists
568+
# AbstractRange: integer ranges are converted to xrange,
569+
# while other ranges (<: AbstractVector) are converted to lists
570570

571571
xrange(start, stop, step) = pycall(pyxrange[], PyObject,
572572
start, stop, step)
573573

574-
function PyObject{T<:Integer}(r::Range{T})
574+
function PyObject{T<:Integer}(r::AbstractRange{T})
575575
s = step(r)
576576
f = first(r)
577577
l = last(r) + s
@@ -583,7 +583,7 @@ function PyObject{T<:Integer}(r::Range{T})
583583
end
584584
end
585585

586-
function convert{T<:Range}(::Type{T}, o::PyObject)
586+
function convert{T<:AbstractRange}(::Type{T}, o::PyObject)
587587
v = PyVector(o)
588588
len = length(v)
589589
if len == 0
@@ -722,7 +722,7 @@ function pysequence_query(o::PyObject)
722722
len = @pycheckz ccall((@pysym :PySequence_Size), Int, (PyPtr,), o)
723723
return typetuple([pytype_query(PyObject(ccall((@pysym :PySequence_GetItem), PyPtr, (PyPtr,Int), o,i-1)), PyAny) for i = 1:len])
724724
elseif pyisinstance(o, pyxrange[])
725-
return Range
725+
return AbstractRange
726726
elseif ispybytearray(o)
727727
return Vector{UInt8}
728728
else

src/gc.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ end
2828

2929
# "embed" a reference to jo in po, using the weak-reference mechanism
3030
function pyembed(po::PyObject, jo::Any)
31+
# If there's a need to support immutable embedding,
32+
# the API needs to be changed to return the pointer.
33+
isimmutable(jo) && ArgumentError("pyembed: immutable argument now allowed")
3134
if weakref_callback_obj.o == C_NULL
3235
weakref_callback_obj.o = pyincref(pymethod(weakref_callback,
3336
"weakref_callback",

src/io.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function pyio_initialize()
108108
String(read(pyio_jl(self), nb < 0 ? typemax(Int) : nb)) :
109109
pybytes(read(pyio_jl(self), nb < 0 ? typemax(Int) : nb)))
110110
readall(self) =
111-
@with_ioraise(self[:istextio] ? readstring(pyio_jl(self)) :
111+
@with_ioraise(self[:istextio] ? read(pyio_jl(self), String) :
112112
pybytes(read(pyio_jl(self))))
113113
readinto(self, b) = @with_ioraise(pybytes(readbytes!(pyio_jl(self), b)))
114114
write(self, b) = @with_ioraise(write(pyio_jl(self), b))

src/numpy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function npyinitialize()
7474
# Parse __multiarray_api.h to obtain length and meaning of PyArray_API
7575
try
7676
hdrfile = open(joinpath(inc, "numpy", "__multiarray_api.h"))
77-
hdr = readstring(hdrfile);
77+
hdr = read(hdrfile, String)
7878
close(hdrfile)
7979
catch e
8080
error("could not read __multiarray_api.h to parse PyArray_API ", e)

src/pybuffer.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ end
4040
function pydecref(o::PyBuffer)
4141
# note that PyBuffer_Release sets o.obj to NULL, and
4242
# is a no-op if o.obj is already NULL
43-
ccall(@pysym(:PyBuffer_Release), Void, (Ptr{PyBuffer},), &o)
43+
# TODO change to `Ref{PyBuffer}` when 0.6 is dropped.
44+
ccall(@pysym(:PyBuffer_Release), Void, (Any,), o)
4445
o
4546
end
4647

@@ -85,9 +86,10 @@ function Base.stride(b::PyBuffer, d::Integer)
8586
return Int(unsafe_load(b.buf.strides, d))
8687
end
8788

89+
# TODO change to `Ref{PyBuffer}` when 0.6 is dropped.
8890
iscontiguous(b::PyBuffer) =
8991
1 == ccall((@pysym :PyBuffer_IsContiguous), Cint,
90-
(Ptr{PyBuffer}, Cchar), &b, 'A')
92+
(Any, Cchar), b, 'A')
9193

9294
#############################################################################
9395
# pybuffer constant values from Include/object.h
@@ -105,8 +107,9 @@ const PyBUF_INDIRECT = convert(Cint, 0x0100) | PyBUF_STRIDES
105107
# construct a PyBuffer from a PyObject, if possible
106108
function PyBuffer(o::Union{PyObject,PyPtr}, flags=PyBUF_SIMPLE)
107109
b = PyBuffer()
110+
# TODO change to `Ref{PyBuffer}` when 0.6 is dropped.
108111
@pycheckz ccall((@pysym :PyObject_GetBuffer), Cint,
109-
(PyPtr, Ptr{PyBuffer}, Cint), o, &b, flags)
112+
(PyPtr, Any, Cint), o, b, flags)
110113
return b
111114
end
112115

src/pyclass.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Multiple dispatch works, too:
156156
"""
157157
macro pydef(class_expr)
158158
class_name, _, _ = parse_pydef_toplevel(class_expr)
159-
:(const $(esc(class_name)) = @pydef_object($(esc(class_expr))))
159+
esc(:(const $class_name = $PyCall.@pydef_object($class_expr)))
160160
end
161161

162162
"""

src/pyinit.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ function __init__()
8787

8888
if !already_inited
8989
# some modules (e.g. IPython) expect sys.argv to be set
90-
if pyversion.major < 3
91-
argv_s = ""
92-
argv = unsafe_convert(Ptr{UInt8}, argv_s)
93-
ccall(@pysym(:PySys_SetArgvEx), Void, (Cint,Ptr{Ptr{UInt8}},Cint), 1, &argv, 0)
90+
@static if VERSION >= v"0.7.0-DEV.1963"
91+
ref0 = Ref{UInt32}(0)
92+
Base.@gc_preserve ref0 ccall(@pysym(:PySys_SetArgvEx), Void,
93+
(Cint, Ref{Ptr{Void}}, Cint),
94+
1, pointer_from_objref(ref0), 0)
9495
else
95-
argv_s = Cwchar_t[0]
96-
argv = unsafe_convert(Ptr{Cwchar_t}, argv_s)
97-
ccall(@pysym(:PySys_SetArgvEx), Void, (Cint, Ptr{Ptr{Cwchar_t}}, Cint), 1, &argv, 0)
96+
ccall(@pysym(:PySys_SetArgvEx), Void, (Cint, Ptr{Cwstring}, Cint),
97+
1, [""], 0)
9898
end
9999

100100
# Some Python code checks sys.ps1 to see if it is running

0 commit comments

Comments
 (0)