-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Print key in KeyError in map.__getitem__/__delitem__ #5397
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
71e8a37
Print key in map.getitem/delitem KeyError
sarlinpe f2d1951
Add tests
sarlinpe 0a07d1e
Fix tests
sarlinpe ce7a05d
Make robust
sarlinpe ce0b107
Make clang-tidy happy
sarlinpe fcc6bde
Return a Python str
sarlinpe c3c9e2b
Show beginning and end of the message
sarlinpe 917226f
Avoid implicit conversion
sarlinpe bd60a6a
Merge branch 'master' into sarlinpe/map-key-error-info
sarlinpe ef17743
Split out `format_message_key_error_key_object()` to reduce amount of…
rwgk f80d132
Use `"✄✄✄"` instead of `"..."`
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this more closely, there are a bunch of things that can go wrong:
cast(k)
may fail,str(...)
may fail,.cast<std::string>()
may fail.Any of those failures will be a secondary error, masking the desired
KeyError
. This can be extremely confusing.Making this robust is not easy.
What's needed is something like (untested; only to give the idea):
This would be implemented in a helper function.
Another thought: if str() fails, try repr().
Oh, there should also be a size limit. I.e. you don't want to produce a 1M numpy array as a str.
Sorry to make this difficult, but these things sure will bite, someone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwgk PTAL. I added a test for long keys. I don't know what kind of object would fail in str of cast - happy to add a test if you have a suggestion. What is the benefit of
set_error + throw error_already_set()
overthrow key_error()
?