Skip to content

Commit

Permalink
Drop 0.4 and 0.7 fixes
Browse files Browse the repository at this point in the history
* Drop remaining 0.4 support code
* Deprecate remaining 0.4 Compat bindings
* Remove tests for 0.4 Compat
* Do not run deprecated tests on 0.6
* Recover one test that's incorrectly moved to deprecated (StringVector)
* Add compat for at-nospecialize with tests
* Add compat for `read(..., String)` with tests
  • Loading branch information
yuyichao committed Jul 23, 2017
1 parent 3f2391c commit 165600e
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1,634 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ Currently, the `@compat` macro supports the following syntaxes:
`Sys.islinux`, `Sys.isunix`, and `Sys.iswindows`, respectively. These are available in the `Compat.Sys`
submodule. ([#22182])

* `readstring` is replaced by methods of `read`. ([#22864])

`read(::IO, ::Type{String})`, `read(::AbstractString, ::Type{String})`,
and `read(::Cmd, ::Type{String})` are defined for 0.6 and below.


## New macros

* `@__DIR__` has been added ([#18380])
Expand All @@ -183,6 +189,8 @@ Currently, the `@compat` macro supports the following syntaxes:
vectorized function have migrated. These macros will be dropped when the
support for `0.6` is dropped from `Compat`.

* `@nospecialize` has been added ([#22666]).

## Other changes

* On versions of Julia that do not contain a Base.Threads module, Compat defines a Threads module containing a no-op `@threads` macro.
Expand Down Expand Up @@ -305,3 +313,5 @@ includes this fix. Find the minimum version from there.
[#22475]: https://github.com/JuliaLang/julia/issues/22475
[#22633]: https://github.com/JuliaLang/julia/issues/22633
[#22629]: https://github.com/JuliaLang/julia/issues/22629
[#22666]: https://github.com/JuliaLang/julia/pull/22666
[#22864]: https://github.com/JuliaLang/julia/pull/22864
231 changes: 26 additions & 205 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,126 +4,31 @@ module Compat

using Base.Meta

if !isdefined(Base, Symbol("@nospecialize"))
# 0.7
macro nospecialize(arg)
earg = esc(arg)
if isa(arg, Symbol)
return :($earg::ANY)
end
return earg
end
export @nospecialize
end

"""Get just the function part of a function declaration."""
withincurly(ex) = isexpr(ex, :curly) ? ex.args[1] : ex

if VERSION < v"0.6.0-dev.2043"
Base.take!(t::Task) = consume(t)
end

function rewrite_show(ex)
if isexpr(ex, :call)
Expr(:call, rewrite_show(ex.args[1]), ex.args[2:end]...)
elseif isexpr(ex, :curly)
Expr(:curly, rewrite_show(ex.args[1]), ex.args[2:end]...)
else
:(Base.writemime)
end
end

function rewrite_dict(ex)
length(ex.args) == 1 && return ex

f = ex.args[1]
if isexpr(f, :curly)
newex = Expr(:typed_dict, :($(f.args[2])=>$(f.args[3])))
else
newex = Expr(:dict)
end

for i = 2:length(ex.args)
pair = ex.args[i]
!isexpr(pair, :(=>)) && return ex
push!(newex.args, pair)
end
newex
end

function rewrite_ordereddict(ex)
length(ex.args) == 1 && return ex

f = ex.args[1]
newex = Expr(:call, f, :[])

for i = 2:length(ex.args)
pair = ex.args[i]
!isexpr(pair, :(=>)) && return ex
push!(newex.args[2].args, Expr(:tuple, pair.args...))
end

newex
end

# rewrites all subexpressions of the form `a => b` to `(a, b)`
function rewrite_pairs_to_tuples!(expr::Expr)
if expr.head == :(=>)
expr.head = :tuple
end
for subexpr in expr.args
isa(subexpr, Expr) && rewrite_pairs_to_tuples!(subexpr)
end
return expr
end

function is_quote_symbol(ex::ANY, val::Symbol)
if isa(ex, QuoteNode)
return (ex::QuoteNode).value === val
elseif isa(ex, Expr)
ex = ex::Expr
return ex.head === :quote && length(ex.args) == 1 && ex.args[1] === val
end
return false
end

is_index_style(ex::Expr) = ex == :(Compat.IndexStyle) || ex == :(Base.IndexStyle) ||
(ex.head == :(.) && (ex.args[1] == :Compat || ex.args[1] == :Base) &&
ex.args[2] == Expr(:quote, :IndexStyle))

is_index_style(arg) = false

# rewrites accesses to IOContext dicts
function rewrite_iocontext!(expr::Expr)
args = expr.args
nargs = length(args)
if nargs == 4 && expr.head === :call && args[1] === :get && args[4] === false
key = args[3]
if is_quote_symbol(key, :limit) || is_quote_symbol(key, :compact)
if VERSION >= v"0.5.0-dev+1936" && VERSION < v"0.5.0-dev+4305"
args[1] = :(Base.limit_output)
deleteat!(args, 3:4)
elseif VERSION < v"0.5.0-dev+1936"
expr.head = :quote
args[1] = false
deleteat!(args, 3:4)
end
elseif is_quote_symbol(key, :multiline)
if VERSION < v"0.5.0-dev+4305"
expr.head = :quote
args[1] = false
deleteat!(args, 3:4)
end
end
end
end

# JuliaLang/julia#10543
if !isdefined(Base, :tryparse)
function tryparse{T}(::Type{T}, args...)
try
Nullable(Base.parse(T, args...))
catch
Nullable{T}()
end
end
end

import Base.unsafe_convert

function new_style_call_overload(ex::Expr)
Base.depwarn("new_style_call_overload is deprecated.", :new_style_call_overload)
false
end

istopsymbol(ex, mod, sym) = ex in (sym, Expr(:(.), mod, Expr(:quote, sym)))

if VERSION < v"0.6.0-dev.2782"
Expand All @@ -145,18 +50,13 @@ function _compat(ex::Expr)
end
elseif ex.head === :curly
f = ex.args[1]
if ex == :(Ptr{Void})
# Do not change Ptr{Void} to Ptr{Nothing}: 0.4.0-dev+768
return ex
elseif VERSION < v"0.6.0-dev.2575" #20414
if VERSION < v"0.6.0-dev.2575" #20414
ex = Expr(:curly, map(a -> isexpr(a, :call, 2) && a.args[1] == :(<:) ?
:($TypeVar($(QuoteNode(gensym(:T))), $(a.args[2]), false)) :
isexpr(a, :call, 2) && a.args[1] == :(>:) ?
:($TypeVar($(QuoteNode(gensym(:T))), $(a.args[2]), $Any, false)) : a,
ex.args)...)
end
elseif ex.head === :macrocall
f = ex.args[1]
elseif ex.head === :quote && isa(ex.args[1], Symbol)
# Passthrough
return ex
Expand Down Expand Up @@ -240,98 +140,7 @@ macro compat(ex...)
esc(_compat(ex[1]))
end

export @compat, @inline, @noinline

import Base.@irrational

import Base: remotecall, remotecall_fetch, remotecall_wait, remote_do

import Base.Filesystem

if !isdefined(Base, :istextmime)
export istextmime
istextmime(m::@compat(Union{MIME,AbstractString})) = istext(m)
end

function primarytype(t::ANY)
tn = t.name
if isdefined(tn, :primary)
return tn.primary
else
return tn.wrapper
end
end

if !isdefined(Base, :Threads)
@eval module Threads
macro threads(expr)
return esc(expr)
end
threadid() = 1
nthreads() = 1
export @threads, threadid, nthreads
end
export Threads
end

if !isdefined(Base, :normalize)
function normalize!(v::AbstractVector, p::Real=2)
nrm = norm(v, p)
__normalize!(v, nrm)
end

@inline function __normalize!(v::AbstractVector, nrm::AbstractFloat)
#The largest positive floating point number whose inverse is less than
#infinity
δ = inv(prevfloat(typemax(nrm)))
if nrm δ #Safe to multiply with inverse
invnrm = inv(nrm)
scale!(v, invnrm)
else # scale elements to avoid overflow
εδ = eps(one(nrm))/δ
scale!(v, εδ)
scale!(v, inv(nrm*εδ))
end
v
end

copy_oftype{T,N}(A::AbstractArray{T,N}, ::Type{T}) = copy(A)
copy_oftype{T,N,S}(A::AbstractArray{T,N}, ::Type{S}) = convert(AbstractArray{S,N}, A)

function normalize(v::AbstractVector, p::Real = 2)
nrm = norm(v, p)
if !isempty(v)
vv = copy_oftype(v, typeof(v[1]/nrm))
return __normalize!(vv, nrm)
else
T = typeof(zero(eltype(v))/nrm)
return T[]
end
end

export normalize, normalize!
end

import Base.AsyncCondition
import Base: srand, rand, rand!


if !isdefined(Base, :pointer_to_string)

function pointer_to_string(p::Ptr{UInt8}, len::Integer, own::Bool=false)
a = ccall(:jl_ptr_to_array_1d, Vector{UInt8},
(Any, Ptr{UInt8}, Csize_t, Cint), Vector{UInt8}, p, len, own)
ccall(:jl_array_to_string, Ref{String}, (Any,), a)
end

pointer_to_string(p::Ptr{UInt8}, own::Bool=false) =
pointer_to_string(p, ccall(:strlen, Csize_t, (Cstring,), p), own)

end

import Base.promote_eltype_op

import Base.LinAlg.BLAS.@blasfunc
export @compat

import Base: redirect_stdin, redirect_stdout, redirect_stderr
if VERSION < v"0.6.0-dev.374"
Expand Down Expand Up @@ -412,20 +221,23 @@ end

# julia#18977
if !isdefined(Base, :xor)
# 0.6
const xor = $
const = xor
export xor,
end

# julia#19246
if !isdefined(Base, :numerator)
# 0.6
const numerator = num
const denominator = den
export numerator, denominator
end

# julia #19950
if !isdefined(Base, :iszero)
# 0.6
iszero(x) = x == zero(x)
iszero(x::Number) = x == 0
iszero(x::AbstractArray) = all(iszero, x)
Expand All @@ -434,6 +246,7 @@ end

# julia #20407
if !isdefined(Base, :(>:))
# 0.6
const >: = let
_issupertype(a::ANY, b::ANY) = issubtype(b, a)
end
Expand Down Expand Up @@ -568,6 +381,7 @@ end

# https://github.com/JuliaLang/julia/pull/22064
if !isdefined(Base, Symbol("@__MODULE__"))
# 0.7
export @__MODULE__
macro __MODULE__()
return current_module()
Expand All @@ -582,6 +396,7 @@ end

# https://github.com/JuliaLang/julia/pull/19784
if isdefined(Base, :invokelatest)
# 0.6
import Base.invokelatest
else
invokelatest(f, args...) = eval(current_module(), Expr(:call, f, map(QuoteNode, args)...))
Expand Down Expand Up @@ -662,6 +477,12 @@ module Sys
end
end

if VERSION < v"0.7.0-DEV.1053"
Base.read(obj::IO, ::Type{String}) = readstring(obj)
Base.read(obj::AbstractString, ::Type{String}) = readstring(obj)
Base.read(obj::Cmd, ::Type{String}) = readstring(obj)
end

include("deprecated.jl")

end # module Compat
28 changes: 23 additions & 5 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ end
using .CompatCartesian
export @ngenerate, @nsplat

function primarytype(@nospecialize(t))
tn = t.name
if isdefined(tn, :primary)
return tn.primary
else
return tn.wrapper
end
end

export @functorize
macro functorize(f)
code = f === :scalarmax ? :(Base.scalarmax) :
Expand All @@ -160,13 +169,22 @@ if VERSION >= v"0.6.0"
Base.@deprecate_binding KERNEL Sys.KERNEL
Base.@deprecate_binding UTF8String Core.String
Base.@deprecate_binding ASCIIString Core.String
Base.@deprecate_binding unsafe_convert Base.unsafe_convert
Base.@deprecate_binding remote_do Base.remote_do
Base.@deprecate_binding Filesystem Base.Filesystem
Base.@deprecate_binding AsyncCondition Base.AsyncCondition
Base.@deprecate_binding promote_eltype_op Base.promote_eltype_op
@eval Base.@deprecate_binding $(Symbol("@irrational")) Base.$(Symbol("@irrational"))
@eval Base.@deprecate_binding $(Symbol("@blasfunc")) Base.LinAlg.BLAS.$(Symbol("@blasfunc"))
else
const KERNEL = Sys.KERNEL
const UTF8String = Core.String
const ASCIIString = Core.String
import Base.unsafe_convert
import Base.remote_do
import Base.Filesystem
import Base.AsyncCondition
import Base.promote_eltype_op
import Base.@irrational
import Base.LinAlg.BLAS.@blasfunc
end

# More things that could be removed in Compat.jl
# - new_style_call_overload
# - import Base.Filesystem
# - import Base.LinAlg.BLAS.@blasfunc
Loading

0 comments on commit 165600e

Please sign in to comment.