From 47a5a7a001c0642eb85d44726decafd66a46e7e4 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 29 Nov 2017 16:50:18 -0600 Subject: [PATCH] Cleanup Julia 0.7 deprecations (#419) --- src/Compat.jl | 66 ++++++++++++++++++++++++----------------------- test/runtests.jl | 67 ++++++++++++++++++++++++++---------------------- 2 files changed, 70 insertions(+), 63 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index 6bb4c0a03..477f635ac 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -157,19 +157,36 @@ end export @compat +# https://github.com/JuliaLang/julia/pull/22064 +@static if !isdefined(Base, Symbol("@__MODULE__")) + # 0.7 + export @__MODULE__ + macro __MODULE__() + return current_module() + end + Base.expand(mod::Module, x::ANY) = eval(mod, :(expand($(QuoteNode(x))))) + Base.macroexpand(mod::Module, x::ANY) = eval(mod, :(macroexpand($(QuoteNode(x))))) + Base.include_string(mod::Module, code::String, fname::String) = + eval(mod, :(include_string($code, $fname))) + Base.include_string(mod::Module, code::AbstractString, fname::AbstractString="string") = + eval(mod, :(include_string($code, $fname))) +end + if VERSION < v"0.6.0-dev.2042" - immutable ExponentialBackOff - n::Int - first_delay::Float64 - max_delay::Float64 - factor::Float64 - jitter::Float64 - - function ExponentialBackOff(n, first_delay, max_delay, factor, jitter) - all(x->x>=0, (n, first_delay, max_delay, factor, jitter)) || error("all inputs must be non-negative") - new(n, first_delay, max_delay, factor, jitter) + include_string(@__MODULE__, """ + immutable ExponentialBackOff + n::Int + first_delay::Float64 + max_delay::Float64 + factor::Float64 + jitter::Float64 + + function ExponentialBackOff(n, first_delay, max_delay, factor, jitter) + all(x->x>=0, (n, first_delay, max_delay, factor, jitter)) || error("all inputs must be non-negative") + new(n, first_delay, max_delay, factor, jitter) + end end - end + """) """ ExponentialBackOff(; n=1, first_delay=0.05, max_delay=10.0, factor=5.0, jitter=0.1) @@ -287,7 +304,7 @@ else end # broadcast over same length tuples, from julia#16986 -if VERSION < v"0.6.0-dev.693" +@static if VERSION < v"0.6.0-dev.693" Base.Broadcast.broadcast{N}(f, t::NTuple{N}, ts::Vararg{NTuple{N}}) = map(f, t, ts...) end @@ -440,7 +457,7 @@ end end # https://github.com/JuliaLang/julia/pull/18727 -if VERSION < v"0.6.0-dev.838" +@static if VERSION < v"0.6.0-dev.838" Base.convert{T}(::Type{Set{T}}, s::Set{T}) = s Base.convert{T}(::Type{Set{T}}, s::Set) = Set{T}(s) end @@ -450,7 +467,7 @@ if VERSION < v"0.6.0-dev.2347" Base.isassigned(x::Base.RefValue) = isdefined(x, :x) end -if VERSION < v"0.6.0-dev.735" +@static if VERSION < v"0.6.0-dev.735" Base.unsafe_trunc{T<:Integer}(::Type{T}, x::Integer) = rem(x, T) end @@ -466,21 +483,6 @@ else using Base: StringVector end -# https://github.com/JuliaLang/julia/pull/22064 -@static if !isdefined(Base, Symbol("@__MODULE__")) - # 0.7 - export @__MODULE__ - macro __MODULE__() - return current_module() - end - Base.expand(mod::Module, x::ANY) = eval(mod, :(expand($(QuoteNode(x))))) - Base.macroexpand(mod::Module, x::ANY) = eval(mod, :(macroexpand($(QuoteNode(x))))) - Base.include_string(mod::Module, code::String, fname::String) = - eval(mod, :(include_string($code, $fname))) - Base.include_string(mod::Module, code::AbstractString, fname::AbstractString="string") = - eval(mod, :(include_string($code, $fname))) -end - # https://github.com/JuliaLang/julia/pull/19784 @static if isdefined(Base, :invokelatest) # 0.6 @@ -529,7 +531,7 @@ if VERSION < v"0.6.0-pre.beta.455" end # https://github.com/JuliaLang/julia/pull/22475 -if VERSION < v"0.7.0-DEV.843" +@static if VERSION < v"0.7.0-DEV.843" import Base: Val (::Type{Val})(x) = (Base.@_pure_meta; Val{x}()) # Also add methods for Val(x) that were previously Val{x} @@ -769,7 +771,7 @@ end end end -if VERSION < v"0.7.0-DEV.2377" +@static if VERSION < v"0.7.0-DEV.2377" (::Type{Matrix{T}}){T}(s::UniformScaling, dims::Dims{2}) = setindex!(zeros(T, dims), T(s.λ), diagind(dims...)) (::Type{Matrix{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, Dims((m, n))) @@ -788,7 +790,7 @@ if VERSION < v"0.7.0-DEV.2377" SparseMatrixCSC{Tv,Ti}(dims..., colptr, rowval, nzval) end end -if VERSION < v"0.7.0-DEV.2543" +@static if VERSION < v"0.7.0-DEV.2543" (::Type{Array{T}}){T}(s::UniformScaling, dims::Dims{2}) = Matrix{T}(s, dims) (::Type{Array{T}}){T}(s::UniformScaling, m::Integer, n::Integer) = Matrix{T}(s, m, n) end diff --git a/test/runtests.jl b/test/runtests.jl index 978bc1a88..ff2b667a0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,8 @@ using Compat using Compat.Test +const struct_sym = VERSION < v"0.7.0-DEV.1263" ? :type : :struct + # Issue #291 # 0.6 @test (1, 2) == @compat abs.((1, -2)) @@ -338,13 +340,15 @@ end @test !iszero([0, 1, 2, 3]) @test iszero([0, 0, 0, 0]) -x = view(1:10, 2:4) -D = Diagonal(x) -@test D[1,1] == 2 -@test D[3,3] == 4 -A = view(rand(5,5), 1:3, 1:3) -@test D*A == Diagonal(copy(x)) * copy(A) -@test A*D == copy(A) * Diagonal(copy(x)) +let + x = view(1:10, 2:4) + D = Diagonal(x) + @test D[1,1] == 2 + @test D[3,3] == 4 + A = view(rand(5,5), 1:3, 1:3) + @test D*A == Diagonal(copy(x)) * copy(A) + @test A*D == copy(A) * Diagonal(copy(x)) +end # julia#17623 # 0.6 @@ -365,11 +369,11 @@ end # julia#20006 @compat abstract type AbstractFoo20006 end eval(Expr( - :type, false, + struct_sym, false, Expr(:(<:), :(ConcreteFoo20006{T<:Int}), :AbstractFoo20006), quote end)) eval(Expr( - :type, false, + struct_sym, false, Expr(:(<:), :(ConcreteFoo20006N{T<:Int,N}), :AbstractFoo20006), quote end)) @compat ConcreteFoo200061{T<:Int} = ConcreteFoo20006N{T,1} @@ -541,20 +545,21 @@ f20500_2() = A20500_2 @inferred f20500_2() module CompatArray -using Compat -eval(Expr( - :type, false, - Expr(:(<:), :(CartesianArray{T,N}), :(AbstractArray{T,N})), - quote - parent::Array{T,N} - end)) -eval(Expr( - :type, false, - Expr(:(<:), :(LinearArray{T,N}), :(AbstractArray{T,N})), - quote - parent::Array{T,N} - end)) -@compat Base.IndexStyle(::Type{<:LinearArray}) = IndexLinear() + using Compat + const struct_sym = VERSION < v"0.7.0-DEV.1263" ? :type : :struct + eval(Expr( + struct_sym, false, + Expr(:(<:), :(CartesianArray{T,N}), :(AbstractArray{T,N})), + quote + parent::Array{T,N} + end)) + eval(Expr( + struct_sym, false, + Expr(:(<:), :(LinearArray{T,N}), :(AbstractArray{T,N})), + quote + parent::Array{T,N} + end)) + @compat Base.IndexStyle(::Type{<:LinearArray}) = IndexLinear() end @test IndexStyle(Array{Float32,2}) === IndexLinear() @test IndexStyle(CompatArray.CartesianArray{Float32,2}) === IndexCartesian() @@ -564,14 +569,14 @@ let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand( @test IndexStyle(b) === IndexLinear() end -for (A,val) in ((zeros(1:5, Float32, 3, 2), 0), - (ones(1:5, Float32, 3, 2), 1), - (zeros(1:5, Float32, (3, 2)), 0), - (ones(1:5, Float32, (3, 2)), 1)) +for (A,val) in ((fill!(Array{Float32}(3, 2), 0), 0), + (fill!(Array{Float32}(3, 2), 1), 1), + (fill!(Array{Float32}(3, 2), 0), 0), + (fill!(Array{Float32}(3, 2), 1), 1)) @test isa(A, Matrix{Float32}) && size(A) == (3,2) && all(x->x==val, A) end -for (A,val) in ((zeros(1:5, Float32), 0), - (ones(1:5, Float32), 1)) +for (A,val) in ((fill!(Array{Float32}(5), 0), 0), + (fill!(Array{Float32}(5), 1), 1)) @test isa(A, Vector{Float32}) && size(A) == (5,) && all(x->x==val, A) end @@ -649,7 +654,7 @@ let # https://en.wikipedia.org/wiki/Swatch_Internet_Time eval(Expr( - :type, false, + struct_sym, false, Expr(:(<:), :Beat, :(Dates.Period)), quote value::Int64 @@ -705,7 +710,7 @@ if VERSION < v"0.7.0-DEV.880" end # PR 22350 -eval(Expr(:type, false, :TestType, Expr(:block, :(a::Int), :b))) +eval(Expr(struct_sym, false, :TestType, Expr(:block, :(a::Int), :b))) @test fieldcount(TestType) == 2 @test fieldcount(Int) == 0