Skip to content

Commit

Permalink
Merge pull request #20376 from JuliaLang/sk/eval
Browse files Browse the repository at this point in the history
Optional module argument to `@eval` passed to `eval`
  • Loading branch information
StefanKarpinski authored Feb 4, 2017
2 parents 509c775 + e4f4e62 commit e480969
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 70 deletions.
2 changes: 1 addition & 1 deletion base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ end
"""
`Base.LineEdit.tabwidth` controls the presumed tab width of code pasted into the REPL.
You can modify it by doing `eval(Base.LineEdit, :(tabwidth = 4))`, for example.
You can modify it by doing `@eval Base.LineEdit tabwidth = 4`, for example.
Must satisfy `0 < tabwidth <= 16`.
"""
Expand Down
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function __atreplinit(repl)
end
end
end
_atreplinit(repl) = eval(Main, :($__atreplinit($repl)))
_atreplinit(repl) = @eval Main $__atreplinit($repl)

function _start()
empty!(ARGS)
Expand Down
4 changes: 2 additions & 2 deletions base/clusterserialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ function deserialize_global_from_main(s::ClusterSerializer, sym)
sym_isconst = deserialize(s)
v = deserialize(s)
if sym_isconst
eval(Main, :(const $sym = $v))
@eval Main const $sym = $v
else
eval(Main, :($sym = $v))
@eval Main $sym = $v
end
end

Expand Down
70 changes: 35 additions & 35 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ for f in ( :acos_fast, :acosh_fast, :angle_fast, :asin_fast, :asinh_fast,
:cosh_fast, :exp10_fast, :exp2_fast, :exp_fast, :expm1_fast,
:lgamma_fast, :log10_fast, :log1p_fast, :log2_fast, :log_fast,
:sin_fast, :sinh_fast, :sqrt_fast, :tan_fast, :tanh_fast )
eval(FastMath, :(Base.@dep_vectorize_1arg Number $f))
@eval FastMath Base.@dep_vectorize_1arg Number $f
end
for f in (
:invdigamma, # base/special/gamma.jl
Expand All @@ -265,7 +265,7 @@ end
@dep_vectorize_1arg Complex float
# base/dates/*.jl
for f in (:unix2datetime, :rata2datetime, :julian2datetime) # base/dates/conversions.jl
eval(Dates, :(Base.@dep_vectorize_1arg Real $f))
@eval Dates Base.@dep_vectorize_1arg Real $f
end
for f in (
# base/dates/accessors.jl
Expand All @@ -279,15 +279,15 @@ for f in (
:daysofweekinmonth, :monthname, :monthabbr, :daysinmonth,
:isleapyear, :dayofyear, :daysinyear, :quarterofyear, :dayofquarter,
)
eval(Dates, :(Base.@dep_vectorize_1arg Dates.TimeType $f))
@eval Dates Base.@dep_vectorize_1arg Dates.TimeType $f
end
for f in (
:hour, :minute, :second, :millisecond, # base/dates/accessors.jl
:Date, :datetime2unix, :datetime2rata, :datetime2julian, # base/dates/conversions.jl
)
eval(Dates, :(Base.@dep_vectorize_1arg Dates.DateTime $f))
@eval Dates Base.@dep_vectorize_1arg Dates.DateTime $f
end
eval(Dates, :(Base.@dep_vectorize_1arg Dates.Date Datetime)) # base/dates/conversions.jl
@eval Dates Base.@dep_vectorize_1arg Dates.Date Datetime # base/dates/conversions.jl

# Deprecate @vectorize_2arg-vectorized functions from...
for f in (
Expand All @@ -304,7 +304,7 @@ for f in (
end
# base/fastmath.jl
for f in (:pow_fast, :atan2_fast, :hypot_fast, :max_fast, :min_fast, :minmax_fast)
eval(FastMath, :(Base.@dep_vectorize_2arg Number $f))
@eval FastMath Base.@dep_vectorize_2arg Number $f
end
for f in (
:max, :min, # base/math.jl
Expand Down Expand Up @@ -356,14 +356,14 @@ end
@deprecate abs(x::AbstractSparseVector) abs.(x)

# Deprecate @textmime into the Multimedia module, #18441
eval(Multimedia, :(macro textmime(mime)
@eval Multimedia macro textmime(mime)
Base.depwarn(string("`@textmime \"mime\"` is deprecated; use ",
"`Base.Multimedia.istextmime(::MIME\"mime\") = true` instead"
), :textmime)
quote
Base.Multimedia.istextmime(::MIME{$(Meta.quot(Symbol(mime)))}) = true
end
end))
end

@deprecate ipermutedims(A::AbstractArray,p) permutedims(A, invperm(p))

Expand Down Expand Up @@ -440,7 +440,7 @@ function reduced_dims0(a::AbstractArray, region)
end

# #18218
eval(Base.LinAlg, quote
@eval Base.LinAlg begin
function arithtype(T)
Base.depwarn(string("arithtype is now deprecated. If you were using it inside a ",
"promote_op call, use promote_op(LinAlg.matprod, Ts...) instead. Otherwise, ",
Expand All @@ -455,7 +455,7 @@ eval(Base.LinAlg, quote
:arithtype)
Int
end
end)
end

# #19246
@deprecate den denominator
Expand All @@ -468,7 +468,7 @@ Filesystem.stop_watching(stream::Filesystem._FDWatcher) = depwarn("stop_watching
@deprecate takebuf_string(b) String(take!(b))

# #19288
eval(Base.Dates, quote
@eval Base.Dates begin
function recur{T<:TimeType}(fun::Function, dr::StepRange{T}; negate::Bool=false, limit::Int=10000)
Base.depwarn("Dates.recur is deprecated, use filter instead.",:recur)
if negate
Expand All @@ -478,7 +478,7 @@ eval(Base.Dates, quote
end
end
recur{T<:TimeType}(fun::Function, start::T, stop::T; step::Period=Day(1), negate::Bool=false, limit::Int=10000) = recur(fun, start:step:stop; negate=negate)
end)
end

# Index conversions revamp; #19730
function getindex(A::LogicalIndex, i::Int)
Expand Down Expand Up @@ -655,13 +655,15 @@ function _depwarn_bczpres!(f, args...)
depwarn(_depstring_bczpres(), :broadcast_zpreserving!)
return _broadcast_zpreserving!(f, args...)
end
eval(SparseArrays, :(broadcast_zpreserving(f, args...) = Base._depwarn_bczpres(f, args...)))
eval(SparseArrays, :(broadcast_zpreserving(f, A::SparseMatrixCSC, B::SparseMatrixCSC) = Base._depwarn_bczpres(f, A, B)))
eval(SparseArrays, :(broadcast_zpreserving(f, A::SparseMatrixCSC, B::Union{Array,BitArray,Number}) = Base._depwarn_bczpres(f, A, B)))
eval(SparseArrays, :(broadcast_zpreserving(f, A::Union{Array,BitArray,Number}, B::SparseMatrixCSC) = Base._depwarn_bczpres(f, A, B)))
eval(SparseArrays, :(broadcast_zpreserving!(f, args...) = Base._depwarn_bczpres!(f, args...)))
eval(SparseArrays, :(broadcast_zpreserving!(f, C::SparseMatrixCSC, A::SparseMatrixCSC, B::Union{Array,BitArray,Number}) = Base._depwarn_bczpres!(f, C, A, B)))
eval(SparseArrays, :(broadcast_zpreserving!(f, C::SparseMatrixCSC, A::Union{Array,BitArray,Number}, B::SparseMatrixCSC) = Base._depwarn_bczpres!(f, C, A, B)))
@eval SparseArrays begin
broadcast_zpreserving(f, args...) = Base._depwarn_bczpres(f, args...)
broadcast_zpreserving(f, A::SparseMatrixCSC, B::SparseMatrixCSC) = Base._depwarn_bczpres(f, A, B)
broadcast_zpreserving(f, A::SparseMatrixCSC, B::Union{Array,BitArray,Number}) = Base._depwarn_bczpres(f, A, B)
broadcast_zpreserving(f, A::Union{Array,BitArray,Number}, B::SparseMatrixCSC) = Base._depwarn_bczpres(f, A, B)
broadcast_zpreserving!(f, args...) = Base._depwarn_bczpres!(f, args...)
broadcast_zpreserving!(f, C::SparseMatrixCSC, A::SparseMatrixCSC, B::Union{Array,BitArray,Number}) = Base._depwarn_bczpres!(f, C, A, B)
broadcast_zpreserving!(f, C::SparseMatrixCSC, A::Union{Array,BitArray,Number}, B::SparseMatrixCSC) = Base._depwarn_bczpres!(f, C, A, B)
end

# #19719
@deprecate getindex(t::Tuple, r::AbstractArray) getindex(t, vec(r))
Expand Down Expand Up @@ -1030,7 +1032,7 @@ iteratoreltype(::Type{Task}) = EltypeUnknown()

isempty(::Task) = error("isempty not defined for Tasks")

eval(Base.Test, quote
@eval Base.Test begin
approx_full(x::AbstractArray) = x
approx_full(x::Number) = x
approx_full(x) = full(x)
Expand Down Expand Up @@ -1095,7 +1097,7 @@ eval(Base.Test, quote
:(test_approx_eq($(esc(a)), $(esc(b)), $(string(a)), $(string(b))))
end
export @test_approx_eq
end)
end

# Deprecate partial linear indexing
function partial_linear_indexing_warning_lookup(nidxs_remaining)
Expand Down Expand Up @@ -1177,7 +1179,7 @@ function colon{T<:Dates.Period}(start::T, stop::T)
end

# LibGit2 refactor (#19839)
eval(Base.LibGit2, quote
@eval Base.LibGit2 begin
Base.@deprecate_binding Oid GitHash
Base.@deprecate_binding GitAnyObject GitUnknownObject

Expand All @@ -1187,23 +1189,21 @@ eval(Base.LibGit2, quote
@deprecate revparse(repo::GitRepo, objname::AbstractString) GitObject(repo, objname) false
@deprecate object(repo::GitRepo, te::GitTreeEntry) GitObject(repo, te) false
@deprecate commit(ann::GitAnnotated) GitHash(ann) false
end)
end

# when this deprecation is deleted, remove all calls to it, and all
# negate=nothing keyword arguments, from base/dates/adjusters.jl
eval(Dates, quote
function deprecate_negate(f, func, sig, negate)
if negate === nothing
return func
else
msg = "$f($sig; negate=$negate) is deprecated, use $f("
negate && (msg *= "!")
msg *= "$sig) instead."
Base.depwarn(msg, f)
return negate ? !func : func
end
@eval Dates function deprecate_negate(f, func, sig, negate)
if negate === nothing
return func
else
msg = "$f($sig; negate=$negate) is deprecated, use $f("
negate && (msg *= "!")
msg *= "$sig) instead."
Base.depwarn(msg, f)
return negate ? !func : func
end
end)
end

# FloatRange replaced by StepRangeLen

Expand Down
12 changes: 8 additions & 4 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ evaluates expressions in that module.
Core.eval

"""
@eval
@eval [mod,] ex

This comment has been minimized.

Copy link
@yuyichao

yuyichao Feb 27, 2017

Contributor

Just found this a little bit misleading. Should this be @eval [mod] ex or @eval([mod,] ex) ? The use of [] as optional seems pretty standard at this point though @eval mod, ex won't do what the doc suggests it is....

Evaluate an expression and return the value.
Evaluate an expression with values interpolated into it using `eval`.
If two arguments are provided, the first is the module to evaluate in.
"""
macro eval(x)
:($(esc(:eval))($(Expr(:quote,x))))
macro eval(ex)
:(eval($(current_module()), $(Expr(:quote,ex))))
end
macro eval(mod, ex)
:(eval($(esc(mod)), $(Expr(:quote,ex))))
end

"""
Expand Down
2 changes: 1 addition & 1 deletion base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2371,4 +2371,4 @@ clear!(sym::Symbol, pids=workers(); mod=Main) = clear!([sym], pids; mod=mod)
clear!(syms, pid::Int; mod=Main) = clear!(syms, [pid]; mod=mod)

clear_impl!(syms, mod::Module) = foreach(x->clear_impl!(x,mod), syms)
clear_impl!(sym::Symbol, mod::Module) = isdefined(mod, sym) && eval(mod, :(global $sym = nothing))
clear_impl!(sym::Symbol, mod::Module) = isdefined(mod, sym) && @eval(mod, global $sym = nothing)
2 changes: 1 addition & 1 deletion base/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ macro test_warn(msg, expr)
quote
let fname = tempname(), have_color = Base.have_color
try
eval(Base, :(have_color = false))
@eval Base have_color = false
ret = open(fname, "w") do f
redirect_stderr(f) do
$(esc(expr))
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ A = [[i i; i i] for i=1:2]
@test cumsum(A) == Any[[1 1; 1 1], [3 3; 3 3]]
@test cumprod(A) == Any[[1 1; 1 1], [4 4; 4 4]]

isdefined(Main, :TestHelpers) || eval(Main, :(include("TestHelpers.jl")))
isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl")
using TestHelpers.OAs

@testset "prepend/append" begin
Expand Down
14 changes: 7 additions & 7 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type Circ_{T} x::Circ_{T} end

abstract Sup2a_
abstract Sup2b_{A <: Sup2a_, B} <: Sup2a_
@test_throws ErrorException eval(:(abstract Qux2_{T} <: Sup2b_{Qux2_{Int}, T})) # wrapped in eval to avoid #16793
@test_throws ErrorException @eval abstract Qux2_{T} <: Sup2b_{Qux2_{Int}, T} # wrapped in eval to avoid #16793

# issue #3890
type A3890{T1}
Expand Down Expand Up @@ -3351,7 +3351,7 @@ macro m8846(a, b=0)
end
@test @m8846(a) === (:a, 0)
@test @m8846(a,1) === (:a, 1)
@test_throws MethodError eval(:(@m8846(a,b,c)))
@test_throws MethodError @eval @m8846(a,b,c)

# a simple case of parametric dispatch with unions
let foo{T}(x::Union{T,Void},y::Union{T,Void}) = 1
Expand Down Expand Up @@ -3420,7 +3420,7 @@ end
@test_throws UndefVarError @M6846.f()

# issue #14758
@test isa(eval(:(f14758(; $([]...)) = ())), Function)
@test isa(@eval(f14758(; $([]...)) = ()), Function)

# issue #14767
@inline f14767(x) = x ? A14767 : ()
Expand Down Expand Up @@ -4575,15 +4575,15 @@ end
@test M14893.f14893() == 14893

# issue #18725
@test_nowarn eval(Main, :(begin
@test_nowarn @eval Main begin
f18725(x) = 1
f18725(x) = 2
end))
end
@test Main.f18725(0) == 2
@test_warn "WARNING: Method definition f18725(Any) in module Module18725" eval(Main, :(module Module18725
@test_warn "WARNING: Method definition f18725(Any) in module Module18725" @eval Main module Module18725
f18725(x) = 1
f18725(x) = 2
end))
end

# issue #19599
f19599{T}(x::((S)->Vector{S})(T)...) = 1
Expand Down
4 changes: 2 additions & 2 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,11 @@ end

let save_color = Base.have_color
try
eval(Base, :(have_color = false))
@eval Base have_color = false
@test sprint(Base.Docs.repl_latex, "") == "\"\" can be typed by \\sqrt<tab>\n\n"
@test sprint(Base.Docs.repl_latex, "x̂₂") == "\"x̂₂\" can be typed by x\\hat<tab>\\_2<tab>\n\n"
finally
eval(Base, :(have_color = $save_color))
@eval Base have_color = $save_color
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/keywordargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ let opts = (:a=>3, :b=>4)
end

# pr #18396, kwargs before Base is defined
eval(Core.Inference, quote
@eval Core.Inference begin
f18396(;kwargs...) = g18396(;kwargs...)
g18396(;x=1,y=2) = x+y
end)
end
@test Core.Inference.f18396() == 3

# issue #7045, `invoke` with keyword args
Expand Down
2 changes: 1 addition & 1 deletion test/libgit2.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

isdefined(Main, :TestHelpers) || eval(Main, :(include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))))
isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
using TestHelpers

const LIBGIT2_MIN_VER = v"0.23.0"
Expand Down
2 changes: 1 addition & 1 deletion test/lineedit.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

using Base.LineEdit
isdefined(Main, :TestHelpers) || eval(Main, :(include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))))
isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl"))
using TestHelpers

function run_test(d,buf)
Expand Down
12 changes: 12 additions & 0 deletions test/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ show_sexpr(ioB,:(1+1))
show_sexpr(ioB,QuoteNode(1),1)

end

# test base/expr.jl
baremodule B
eval = 0
x = 1
module M; x = 2; end
import Base
@Base.eval x = 3
@Base.eval M x = 4
end
@test B.x == 3
@test B.M.x == 4
8 changes: 4 additions & 4 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ end
let
old_have_color = Base.have_color
try
eval(Base, :(have_color = true))
@eval Base have_color = true
buf = IOBuffer()
print_with_color(:red, buf, "foo")
@test startswith(String(take!(buf)), Base.text_colors[:red])
finally
eval(Base, :(have_color = $(old_have_color)))
@eval Base have_color = $(old_have_color)
end
end

Expand All @@ -585,7 +585,7 @@ end
let
old_have_color = Base.have_color
try
eval(Base, :(have_color = true))
@eval Base have_color = true
buf = IOBuffer()
print_with_color(:red, buf, "foo")
# Check that we get back to normal text color in the end
Expand All @@ -595,7 +595,7 @@ let
print_with_color(:red, buf, "foo"; bold = true)
@test String(take!(buf)) == "\e[1m\e[31mfoo\e[39m\e[22m"
finally
eval(Base, :(have_color = $(old_have_color)))
@eval Base have_color = $(old_have_color)
end
end

Expand Down
Loading

0 comments on commit e480969

Please sign in to comment.