Skip to content

Commit

Permalink
Improve "key not found" error messages for string/symbol keys
Browse files Browse the repository at this point in the history
We don't know how to represent arbitrary objects as strings at this
low level, but this takes care of two very common cases.
  • Loading branch information
d-torrance committed Aug 17, 2024
1 parent 7ad574a commit 125d3ed
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions M2/Macaulay2/d/hashtables.dd
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ export copy(o:HashTable,newClass:HashTable,newParent:HashTable,newMutable:bool):
export copy(o:HashTable,newClass:HashTable,newMutable:bool):HashTable := copy(o,newClass,o.parent,newMutable);
-----------------------------------------------------------------------------

KeyNotFound(object:string, key:Expr):Expr := (
msg := "key not found in " + object;
when key
is x:stringCell do msg = msg + ": \"" + present(x.v) + "\""
is x:SymbolClosure do msg = msg + ": " + x.symbol.word.name
else nothing;
buildErrorPacket(msg));

export lookup1(object:HashTable,key:Expr,keyhash:hash_t):Expr := (
-- warning: can return notfoundE, which should not be given to the user
res := notfoundE;
Expand Down Expand Up @@ -340,7 +348,7 @@ export lookup1force(object:HashTable,key:Expr,keyhash:hash_t):Expr := (
bucket = bucket.next;
);
if lockit then unlock(object.mutex);
buildErrorPacket("key not found in hash table"));
KeyNotFound("hash table", key));
export lookup1force(object:HashTable,key:Expr):Expr := lookup1force(object,key,hash(key));
export lookup1Q(object:HashTable,key:Expr,keyhash:hash_t):bool := (
lockit := object.Mutable && !object.beingInitialized;
Expand Down Expand Up @@ -813,7 +821,7 @@ export subvalue(left:Expr,right:Expr):Expr := (
if !f.isopen then return buildErrorPacket("database closed");
when dbmfetch(f.handle,key.v)
is a:string do Expr(stringCell(a))
else buildErrorPacket("encountered missing value"))
else KeyNotFound("database", right))
else buildErrorPacket("expected a string as key to database"))
-- # typical value: symbol #, List, ZZ, Thing
-- # typical value: symbol #, BasicList, ZZ, Thing
Expand All @@ -828,7 +836,7 @@ export subvalue(left:Expr,right:Expr):Expr := (
d := dc.dictionary;
when lookup(s.v,d.symboltable)
is x:Symbol do Expr(SymbolClosure(if x.thread then threadFrame else dc.frame,x))
else buildErrorPacket("key not found in dictionary")
else KeyNotFound("dictionary", right)
)
else buildErrorPacket("expected key for dictionary to be a string")
)
Expand Down

0 comments on commit 125d3ed

Please sign in to comment.