Skip to content

Commit

Permalink
fix #30633, use repr to show arbitrary objects in error messages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jan 14, 2019
1 parent b5a5946 commit 2502790
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ function sizehint!(d::IdDict, newsz)
end

function setindex!(d::IdDict{K,V}, @nospecialize(val), @nospecialize(key)) where {K, V}
!isa(key, K) && throw(ArgumentError("$key is not a valid key for type $K"))
!isa(key, K) && throw(ArgumentError("$(limitrepr(key)) is not a valid key for type $K"))
val = convert(V, val)
if d.ndel >= ((3*length(d.ht))>>2)
rehash!(d, max(length(d.ht)>>1, 32))
Expand Down
4 changes: 2 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ end
function setindex!(h::Dict{K,V}, v0, key0) where V where K
key = convert(K, key0)
if !isequal(key, key0)
throw(ArgumentError("$key0 is not a valid key for type $K"))
throw(ArgumentError("$(limitrepr(key0)) is not a valid key for type $K"))
end
setindex!(h, v0, key)
end
Expand Down Expand Up @@ -439,7 +439,7 @@ get!(f::Function, collection, key)
function get!(default::Callable, h::Dict{K,V}, key0) where V where K
key = convert(K, key0)
if !isequal(key, key0)
throw(ArgumentError("$key0 is not a valid key for type $K"))
throw(ArgumentError("$(limitrepr(key0)) is not a valid key for type $K"))
end
return get!(default, h, key)
end
Expand Down
10 changes: 5 additions & 5 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ function showerror(io::IO, ex::SystemError)
extrainfo = ex.extrainfo
end
if extrainfo === nothing
print(io, "SystemError: $(ex.prefix): $errstring")
print(io, "SystemError: $(ex.prefix): ", errstring)
else
print(io, "SystemError (with $extrainfo): $(ex.prefix): $errstring")
print(io, "SystemError (with $extrainfo): $(ex.prefix): ", errstring)
end
end

Expand All @@ -144,9 +144,9 @@ showerror(io::IO, ex::KeyError) = (print(io, "KeyError: key ");
show(io, ex.key);
print(io, " not found"))
showerror(io::IO, ex::InterruptException) = print(io, "InterruptException:")
showerror(io::IO, ex::ArgumentError) = print(io, "ArgumentError: $(ex.msg)")
showerror(io::IO, ex::AssertionError) = print(io, "AssertionError: $(ex.msg)")
showerror(io::IO, ex::OverflowError) = print(io, "OverflowError: $(ex.msg)")
showerror(io::IO, ex::ArgumentError) = print(io, "ArgumentError: ", ex.msg)
showerror(io::IO, ex::AssertionError) = print(io, "AssertionError: ", ex.msg)
showerror(io::IO, ex::OverflowError) = print(io, "OverflowError: ", ex.msg)

showerror(io::IO, ex::UndefKeywordError) =
print(io, "UndefKeywordError: keyword argument $(ex.var) not assigned")
Expand Down
6 changes: 3 additions & 3 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ indexing behaviors. This must return either an `Int` or an `AbstractArray` of
`Int`s.
"""
to_index(i::Integer) = convert(Int,i)::Int
to_index(i::Bool) = throw(ArgumentError("invalid index: $i of type $(typeof(i))"))
to_index(i::Bool) = throw(ArgumentError("invalid index: $i of type Bool"))
to_index(I::AbstractArray{Bool}) = LogicalIndex(I)
to_index(I::AbstractArray) = I
to_index(I::AbstractArray{Union{}}) = I
to_index(I::AbstractArray{<:Union{AbstractArray, Colon}}) =
throw(ArgumentError("invalid index: $I of type $(typeof(I))"))
throw(ArgumentError("invalid index: $(limitrepr(I)) of type $(typeof(I))"))
to_index(::Colon) = throw(ArgumentError("colons must be converted by to_indices(...)"))
to_index(i) = throw(ArgumentError("invalid index: $i of type $(typeof(i))"))
to_index(i) = throw(ArgumentError("invalid index: $(limitrepr(i)) of type $(typeof(i))"))

# The general to_indices is mostly defined in multidimensional.jl, but this
# definition is required for bootstrap:
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ previously marked position. Throw an error if the stream is not marked.
See also [`mark`](@ref), [`unmark`](@ref), [`ismarked`](@ref).
"""
function reset(io::T) where T<:IO
ismarked(io) || throw(ArgumentError("$(T) not marked"))
ismarked(io) || throw(ArgumentError("$T not marked"))
m = io.mark
seek(io, m)
io.mark = -1 # must be after seek, or seek may fail
Expand Down
2 changes: 2 additions & 0 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ julia> repr(big(1/3), context=:compact => true)
"""
repr(x; context=nothing) = sprint(show, x; context=context)

limitrepr(x) = repr(x, context = :limit=>true)

# IOBuffer views of a (byte)string:

"""
Expand Down
6 changes: 3 additions & 3 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ lock(f, wkh::WeakKeyDict) = lock(f, wkh.lock)
trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock)

function setindex!(wkh::WeakKeyDict{K}, v, key) where K
!isa(key, K) && throw(ArgumentError("$key is not a valid key for type $K"))
!isa(key, K) && throw(ArgumentError("$(limitrepr(key)) is not a valid key for type $K"))
finalizer(wkh.finalizer, key)
lock(wkh) do
wkh.ht[WeakRef(key)] = v
Expand All @@ -95,11 +95,11 @@ end
get(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get(wkh.ht, key, default), wkh)
get(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get(default, wkh.ht, key), wkh)
function get!(wkh::WeakKeyDict{K}, key, default) where {K}
!isa(key, K) && throw(ArgumentError("$key is not a valid key for type $K"))
!isa(key, K) && throw(ArgumentError("$(limitrepr(key)) is not a valid key for type $K"))
lock(() -> get!(wkh.ht, WeakRef(key), default), wkh)
end
function get!(default::Callable, wkh::WeakKeyDict{K}, key) where {K}
!isa(key, K) && throw(ArgumentError("$key is not a valid key for type $K"))
!isa(key, K) && throw(ArgumentError("$(limitrepr(key)) is not a valid key for type $K"))
lock(() -> get!(default, wkh.ht, WeakRef(key)), wkh)
end
pop!(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> pop!(wkh.ht, key), wkh)
Expand Down
4 changes: 4 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,7 @@ end
@test output[6][1:8] == " [10003]"
end
end

# issue #30633
@test_throws ArgumentError("invalid index: \"foo\" of type String") [1]["foo"]
@test_throws ArgumentError("invalid index: nothing of type Nothing") [1][nothing]

0 comments on commit 2502790

Please sign in to comment.