Repository navigation
feat(socketcan): name the channel when binding a socket fails - #2184
Open
ChuangMuLinze wants to merge 1 commit into
Open
ChuangMuLinze wants to merge 1 commit into
ChuangMuLinze wants to merge 1 commit into
Conversation
bind_socket() re-raised the OSError from socket.bind() unchanged, so client code that catches it had no way to tell which bus failed when several were in use. Add the channel as a note on the exception, following the pattern used by candump for the same failure.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2104
Description of the Change
bind_socket()re-raised theOSErrorfromsocket.bind()unchanged. Client code that catches it therefore had no way to tell which bus failed when several were in use, unless debug logging was on.The channel is now attached as a note on the original exception, and the failure is logged:
A traceback now ends with:
I used
add_note()rather than re-raising a new exception so thatexcept OSErrorkeeps working anderrno/strerrorare preserved — only the context is added.add_note()is available on every version python-can supports (requires-python = ">=3.11").How I Tested It
Two tests in
test/test_socketcan_helpers.py, both driven by a mocked socket so they need no CAN hardware:test_bind_socket_adds_context_to_oserror— the raisedOSErrorcarries a note naming the channel. Fails onmain(expected the channel in []), passes with the change.test_bind_socket_keeps_original_error— the exception stays anOSErrorwitherrno == 19and the originalstrerror.black --checkandruffreport nothing new on the touched files.Scope
I kept this to the one call site the issue points at. The issue also suggests auditing other places that raise naked
OSErrors without context — I'd rather do that separately, so this stays reviewable in one pass.