Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object field to KeyError #55477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ An indexing operation into an `AbstractDict` (`Dict`) or `Set` like object tried
delete a non-existent element.
"""
struct KeyError <: Exception
object
key
end

KeyError(key) = KeyError(nothing, key)

KeyTypeError(K, key) = TypeError(:var"dict key", K, key)

const secret_table_token = :__c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
Expand Down Expand Up @@ -545,7 +548,7 @@ end
function getindex(t::AbstractDict{<:Any,V}, key) where V
v = get(t, key, secret_table_token)
if v === secret_table_token
throw(KeyError(key))
throw(KeyError(t, key))
end
return v::V
end
Expand Down
2 changes: 1 addition & 1 deletion base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ push!(s::BitSet, ns::Integer...) = (for n in ns; push!(s, n); end; s)
delete!(s, n)
n
else
throw(KeyError(n))
throw(KeyError(s, n))
end
end

Expand Down
2 changes: 1 addition & 1 deletion base/compiler/cicache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ end

function getindex(wvc::WorldView{InternalCodeCache}, mi::MethodInstance)
r = get(wvc, mi, nothing)
r === nothing && throw(KeyError(mi))
r === nothing && throw(KeyError(wvc, mi))
return r::CodeInstance
end

Expand Down
8 changes: 4 additions & 4 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ end

function getindex(h::Dict{K,V}, key) where V where K
index = ht_keyindex(h, key)
return index < 0 ? throw(KeyError(key)) : @assume_effects :noub @inbounds h.vals[index]::V
return index < 0 ? throw(KeyError(h, key)) : @assume_effects :noub @inbounds h.vals[index]::V
end

"""
Expand Down Expand Up @@ -580,7 +580,7 @@ end

function pop!(h::Dict, key)
index = ht_keyindex(h, key)
return index > 0 ? _pop!(h, index) : throw(KeyError(key))
return index > 0 ? _pop!(h, index) : throw(KeyError(h, key))
end

"""
Expand Down Expand Up @@ -822,7 +822,7 @@ function getindex(dict::ImmutableDict, key)
isequal(dict.key, key) && return dict.value
dict = dict.parent
end
throw(KeyError(key))
throw(KeyError(dict, key))
end
function get(dict::ImmutableDict, key, default)
while isdefined(dict, :parent)
Expand Down Expand Up @@ -982,7 +982,7 @@ end

function getindex(dict::PersistentDict{K,V}, key::K) where {K,V}
found = KeyValue.get(dict, key)
found === nothing && throw(KeyError(key))
found === nothing && throw(KeyError(dict, key))
return only(found)
end

Expand Down
2 changes: 1 addition & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function parse_bool_env(name::String, val::String = ENV[name]; throw::Bool=false
end
end

getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(k)), k)
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k)
getindex(ENV::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(ENV, k)), k)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption of ::EnvDict referring to the global ENV is also present in other functions (not written by you). I wonder if it deserve a separate issue? I guess, EnvDict structure is a private API, still, if someone creates its own instance it basically won't really work properly with other functions anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnvDict is a singleton type, so it is fine to refer to "the global ENV" as it is the only instance that can exist anyway.

get(::EnvDict, k::AbstractString, def) = access_env(Returns(def), k)
get(f::Callable, ::EnvDict, k::AbstractString) = access_env(k->f(), k)
function get!(default::Callable, ::EnvDict, k::AbstractString)
Expand Down
4 changes: 2 additions & 2 deletions base/iddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end

function getindex(d::IdDict{K,V}, @nospecialize(key)) where {K, V}
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
val === secret_table_token && throw(KeyError(key))
val === secret_table_token && throw(KeyError(d, key))
return val::V
end

Expand All @@ -113,7 +113,7 @@ end

function pop!(d::IdDict{K,V}, @nospecialize(key)) where {K, V}
val = pop!(d, key, secret_table_token)
val === secret_table_token && throw(KeyError(key))
val === secret_table_token && throw(KeyError(d, key))
return val::V
end

Expand Down
2 changes: 1 addition & 1 deletion base/idset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function _pop!(s::IdSet, @nospecialize(x))
end
removed
end
pop!(s::IdSet, @nospecialize(x)) = _pop!(s, x) == -1 ? throw(KeyError(x)) : x
pop!(s::IdSet, @nospecialize(x)) = _pop!(s, x) == -1 ? throw(KeyError(s, x)) : x
pop!(s::IdSet, @nospecialize(x), @nospecialize(default)) = _pop!(s, x) == -1 ? default : x
delete!(s::IdSet, @nospecialize(x)) = (_pop!(s, x); s)

Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2547,7 +2547,7 @@ end
root_module(key::PkgId) = @lock require_lock loaded_modules[key]
function root_module(where::Module, name::Symbol)
key = identify_package(where, String(name))
key isa PkgId || throw(KeyError(name))
key isa PkgId || throw(KeyError(where, name))
return root_module(key)
end
maybe_root_module(key::PkgId) = @lock require_lock get(loaded_modules, key, nothing)
Expand Down
2 changes: 1 addition & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ end

function pop!(s::Set, x)
index = ht_keyindex(s.dict, x)
index < 1 && throw(KeyError(x))
index < 1 && throw(KeyError(s, x))
result = @inbounds s.dict.keys[index]
_delete!(s.dict, index)
result
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ in(key_value::Pair, io::IO) = false
haskey(io::IOContext, key) = haskey(io.dict, key)
haskey(io::IO, key) = false
getindex(io::IOContext, key) = getindex(io.dict, key)
getindex(io::IO, key) = throw(KeyError(key))
getindex(io::IO, key) = throw(KeyError(io, key))
get(io::IOContext, key, default) = get(io.dict, key, default)
get(io::IO, key, default) = default
keys(io::IOContext) = keys(io.dict)
Expand Down
2 changes: 1 addition & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ end
# Deprecate these in v2 (RedirectStdStream support)
iterate(p::Pipe) = (p.out, 1)
iterate(p::Pipe, i::Int) = i == 1 ? (p.in, 2) : nothing
getindex(p::Pipe, key::Int) = key == 1 ? p.out : key == 2 ? p.in : throw(KeyError(key))
getindex(p::Pipe, key::Int) = key == 1 ? p.out : key == 2 ? p.in : throw(KeyError(p, key))

"""
redirect_stdout([stream]) -> stream
Expand Down
2 changes: 1 addition & 1 deletion base/terminfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function getindex(ti::TermInfo, key::Symbol)
haskey(ti.numbers, key) && return ti.numbers[key]
haskey(ti.strings, key) && return ti.strings[key]
haskey(ti.aliases, key) && return getindex(ti, ti.aliases[key])
throw(KeyError(key))
throw(KeyError(ti, key))
end

keys(ti::TermInfo) = keys(ti.flags) ∪ keys(ti.numbers) ∪ keys(ti.strings) ∪ keys(ti.aliases)
Expand Down
8 changes: 4 additions & 4 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ end
map!(f, iter::ValueIterator{<:WeakKeyDict})= map!(f, values(iter.dict.ht))

function get(wkh::WeakKeyDict{K}, key, default) where {K}
key === nothing && throw(KeyError(nothing))
key === nothing && throw(KeyError(wkh, nothing))
lock(wkh) do
return get(wkh.ht, key, default)
end
end
function get(default::Callable, wkh::WeakKeyDict{K}, key) where {K}
key === nothing && throw(KeyError(nothing))
key === nothing && throw(KeyError(wkh, nothing))
lock(wkh) do
return get(default, wkh.ht, key)
end
end
function pop!(wkh::WeakKeyDict{K}, key) where {K}
key === nothing && throw(KeyError(nothing))
key === nothing && throw(KeyError(wkh, nothing))
lock(wkh) do
return pop!(wkh.ht, key)
end
Expand Down Expand Up @@ -175,7 +175,7 @@ function haskey(wkh::WeakKeyDict{K}, key) where {K}
end
end
function getindex(wkh::WeakKeyDict{K}, key) where {K}
key === nothing && throw(KeyError(nothing))
key === nothing && throw(KeyError(wkh, nothing))
lock(wkh) do
return getindex(wkh.ht, key)
end
Expand Down