fix: SMPClient context logs formatted traceback, not repr#64
Merged
JPHutchins merged 1 commit intointercreate:mainfrom May 17, 2025
Merged
Conversation
Previously, exiting the SMPClient async context with an exception
results in logging f"{traceback=}" whose output looks like:
traceback=<traceback object at 0x7f4c3fea2980>
as part of a full log message like so:
Exception in SMPClient: exc_type=<class 'smpclient.transport.SMPTransportDisconnected'>, exc_value=SMPTransportDisconnected('SmpNativeTransport cancelled receive due to disconnection'), traceback=<traceback object at 0x7f4c3fea2980>
Instead, format the exception using traceback.format_exc() (which also
grabs the originating exception with sys.exception()). Now the output
looks like:
Exception in SMPClient:
Traceback (most recent call last):
File "<string>", line 11, in run_echo_test
File "/opt/axe/develop/latest/desktop/sysroots/x86-64-v3-poky-linux/usr/lib/python3.12/site-packages/smpclient/__init__.py", line 160, in request
frame = await self._transport.send_and_receive(request.BYTES)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 67, in send_and_receive
File "<string>", line 62, in receive
smpclient.transport.SMPTransportDisconnected: SmpNativeTransport cancelled receive due to disconnection
JPHutchins
approved these changes
May 2, 2025
Collaborator
JPHutchins
left a comment
There was a problem hiding this comment.
Thanks! This is a great improvement. Getting good exceptions from asyncio is always challenging, and I'm not certain of best practices. What do you think about raising the exception after the disconnect so that users can catch them?
I read through this conversation recently, but I'm not sure that I absorbed it!
https://discuss.python.org/t/asyncio-tasks-and-exception-handling-recommended-idioms/23806
JPHutchins
approved these changes
May 17, 2025
Collaborator
JPHutchins
left a comment
There was a problem hiding this comment.
I've looked into this and it seems like suppressing exceptions is the expected behavior for async context managers. The exception cannot be (easily) retrieved outside of the block because the context has ended.
So, logging will have to do for now!
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.
Previously, exiting the SMPClient async context with an exception results in logging f"{traceback=}" whose output looks like:
as part of a full log message like so:
Instead, format the exception using traceback.format_exc() (which also grabs the originating exception with sys.exception()). Now the output looks like: