From 6ff74153c417e4bacbccbf3dcc10f18c2e6396bc Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Sat, 7 May 2016 18:42:54 -0400 Subject: [PATCH] Fix depwarn on 0.5 String, Symbol, precision(BigFloat) Also fix an incorrect use of unsafe_convert --- REQUIRE | 2 +- src/PyCall.jl | 7 ++++--- src/conversions.jl | 15 +++++---------- src/numpy.jl | 2 +- src/pyclass.jl | 2 +- src/pydates.jl | 4 ++-- src/pytype.jl | 5 +++-- test/runtests.jl | 3 ++- 8 files changed, 19 insertions(+), 21 deletions(-) diff --git a/REQUIRE b/REQUIRE index 32510197..89c733a1 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,4 +1,4 @@ julia 0.4 -Compat 0.7.14 +Compat 0.7.15 Conda 0.1.6 MacroTools 0.2 diff --git a/src/PyCall.jl b/src/PyCall.jl index aa919bcb..ac491b10 100644 --- a/src/PyCall.jl +++ b/src/PyCall.jl @@ -24,6 +24,7 @@ import Base: sigatomic_begin, sigatomic_end ## Compatibility import for v0.4, v0.5 using Compat +import Compat.String import Base.unsafe_convert ######################################################################### @@ -286,7 +287,7 @@ keys(o::PyObject) = Symbol[m[1] for m in pycall(inspect["getmembers"], # and providing access to o's members (converted to PyAny) as w.member. # we skip wrapping Julia reserved words (which cannot be type members) -const reserved = Set{ASCIIString}(["while", "if", "for", "try", "return", "break", "continue", "function", "macro", "quote", "let", "local", "global", "const", "abstract", "typealias", "type", "bitstype", "immutable", "ccall", "do", "module", "baremodule", "using", "import", "export", "importall", "pymember", "false", "true", "Tuple"]) +const reserved = Set{String}(["while", "if", "for", "try", "return", "break", "continue", "function", "macro", "quote", "let", "local", "global", "const", "abstract", "typealias", "type", "bitstype", "immutable", "ccall", "do", "module", "baremodule", "using", "import", "export", "importall", "pymember", "false", "true", "Tuple"]) """ pywrap(o::PyObject) @@ -302,11 +303,11 @@ function pywrap(o::PyObject, mname::Symbol=:__anon__) pycall(inspect["getmembers"], PyObject, o)) filter!(m -> !(m[1] in reserved), members) m = Module(mname, false) - consts = [Expr(:const, Expr(:(=), symbol(x[1]), convert(PyAny, x[2]))) for x in members] + consts = [Expr(:const, Expr(:(=), Symbol(x[1]), convert(PyAny, x[2]))) for x in members] exports = try convert(Vector{Symbol}, o["__all__"]) catch - [symbol(x[1]) for x in filter(x -> x[1][1] != '_', members)] + [Symbol(x[1]) for x in filter(x -> x[1][1] != '_', members)] end eval(m, Expr(:toplevel, consts..., :(pymember(s) = $(getindex)($(o), s)), Expr(:export, exports...))) diff --git a/src/conversions.jl b/src/conversions.jl index 2b4e0687..4cb3e0ce 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -58,14 +58,9 @@ convert(::Type{Void}, po::PyObject) = nothing ######################################################################### # String conversions (both bytes arrays and unicode strings) -PyObject(s::UTF8String) = - PyObject(@pycheckn ccall(@pysym(PyUnicode_DecodeUTF8), - PyPtr, (Ptr{UInt8}, Int, Ptr{UInt8}), - s, sizeof(s), C_NULL)) - function PyObject(s::AbstractString) - sb = bytestring(s) - if pyunicode_literals + sb = String(s) + if pyunicode_literals || !isascii(sb) PyObject(@pycheckn ccall(@pysym(PyUnicode_DecodeUTF8), PyPtr, (Ptr{UInt8}, Int, Ptr{UInt8}), sb, sizeof(sb), C_NULL)) @@ -92,7 +87,7 @@ end # TODO: should symbols be converted to a subclass of Python strings/bytes, # so that PyAny conversion can convert it back to a Julia symbol? PyObject(s::Symbol) = PyObject(string(s)) -convert(::Type{Symbol}, po::PyObject) = symbol(convert(AbstractString, po)) +convert(::Type{Symbol}, po::PyObject) = Symbol(convert(AbstractString, po)) ######################################################################### # ByteArray conversions @@ -621,7 +616,7 @@ function mpmath_init() copy!(mpf, mpmath["mpf"]) copy!(mpc, mpmath["mpc"]) end - curprec = get_bigfloat_precision() + curprec = precision(BigFloat) if mpprec[1] != curprec mpprec[1] = curprec mpmath["mp"]["prec"] = mpprec[1] @@ -702,7 +697,7 @@ pyfloat_query(o::PyObject) = pyisinstance(o, @pyglobalobj :PyFloat_Type) || pyi pycomplex_query(o::PyObject) = pyisinstance(o, @pyglobalobj :PyComplex_Type) || pyisinstance(o, npy_complexfloating) ? Complex128 : Union{} -pystring_query(o::PyObject) = pyisinstance(o, @pyglobalobj PyString_Type) ? AbstractString : pyisinstance(o, @pyglobalobj :PyUnicode_Type) ? UTF8String : Union{} +pystring_query(o::PyObject) = pyisinstance(o, @pyglobalobj PyString_Type) ? AbstractString : pyisinstance(o, @pyglobalobj :PyUnicode_Type) ? String : Union{} # Given call overloading, all PyObjects are callable already, so # we never automatically convert to Function. diff --git a/src/numpy.jl b/src/numpy.jl index f340ffc1..540a4c89 100644 --- a/src/numpy.jl +++ b/src/numpy.jl @@ -85,7 +85,7 @@ function npyinitialize() end API = pointer_to_array(PyArray_API, (PyArray_API_length,)) for m in eachmatch(r, hdr) # build npy_api table - npy_api[symbol(m.captures[1])] = API[parse(Int, m.captures[2])+1] + npy_api[Symbol(m.captures[1])] = API[parse(Int, m.captures[2])+1] end if !haskey(npy_api, :PyArray_New) error("failure parsing NumPy PyArray_API symbol table") diff --git a/src/pyclass.jl b/src/pyclass.jl index 494b9563..1d0c691d 100644 --- a/src/pyclass.jl +++ b/src/pyclass.jl @@ -105,7 +105,7 @@ function parse_pydef(expr) else error("$access is not a valid accessor; must be either get or set!") end - jl_fun_name = gensym(symbol(attribute,:_,access)) + jl_fun_name = gensym(Symbol(attribute,:_,access)) push!(function_defs, :(function $jl_fun_name($(args...)) $rhs end)) diff --git a/src/pydates.jl b/src/pydates.jl index d9cbc412..4a535414 100644 --- a/src/pydates.jl +++ b/src/pydates.jl @@ -98,8 +98,8 @@ function PyObject(p::Dates.Millisecond) end for T in (:Date, :DateTime, :Delta) - f = symbol(string("Py", T, "_Check")) - t = Expr(:., :PyDateTimeAPI, QuoteNode(symbol(string(T, "Type")))) + f = Symbol(string("Py", T, "_Check")) + t = Expr(:., :PyDateTimeAPI, QuoteNode(Symbol(string(T, "Type")))) @eval $f(o::PyObject) = pyisinstance(o, $t) end diff --git a/src/pytype.jl b/src/pytype.jl index a761e0c5..ed6f7034 100644 --- a/src/pytype.jl +++ b/src/pytype.jl @@ -261,7 +261,7 @@ type PyTypeObject # Julia-specific fields, after the end of the Python structure: # save the tp_name Julia string so that it is not garbage-collected - tp_name_save::ASCIIString + tp_name_save # This is a gc slot that is never read from function PyTypeObject(name::AbstractString, basicsize::Integer, init::Function) # figure out Py_TPFLAGS_DEFAULT, depending on Python version @@ -280,7 +280,8 @@ type PyTypeObject Py_TPFLAGS_HAVE_CLASS | Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | Py_TPFLAGS_HAVE_INDEX) - name_save = bytestring(name) + # This and the `unsafe_convert` below emulate a `ccall` + name_save = Base.cconvert(Ptr{UInt8}, name) t = new(0,C_NULL,0, unsafe_convert(Ptr{UInt8}, name_save), convert(Int, basicsize), 0, diff --git a/test/runtests.jl b/test/runtests.jl index bdecfc53..36093987 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,5 @@ using Base.Test, PyCall, Compat +import Compat.String PYTHONPATH=get(ENV,"PYTHONPATH","") PYTHONHOME=get(ENV,"PYTHONHOME","") @@ -132,7 +133,7 @@ end # in Python 3, we need a specific encoding to write strings or bufferize them # (http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface) pyutf8(s::PyObject) = pycall(s["encode"], PyObject, "utf-8") -pyutf8(s::ByteString) = pyutf8(PyObject(s)) +pyutf8(s::String) = pyutf8(PyObject(s)) # IO (issue #107) #@test roundtripeq(STDOUT) # No longer true since #250