Title: Issues when showing broken `Exception.__notes__` · Issue #103895 · python/cpython · GitHub
Open Graph Title: Issues when showing broken `Exception.__notes__` · Issue #103895 · python/cpython
X Title: Issues when showing broken `Exception.__notes__` · Issue #103895 · python/cpython
Description: Bug report When notes on an exception cannot be shown because the repr and str raise exceptions, a
Open Graph Description: Bug report When notes on an exception cannot be shown because the repr and str raise exceptions, a
X Description: Bug report When notes on an exception cannot be shown because the repr and str raise exceptions, a <note str() failed> message is shown instead. In the case where exception.__notes__ is not a...
Opengraph URL: https://github.com/python/cpython/issues/103895
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Issues when showing broken `Exception.__notes__`","articleBody":"# Bug report\r\n\r\nWhen [notes on an exception](https://docs.python.org/3/library/exceptions.html#BaseException.__notes__) cannot be shown because the `repr` and `str` raise exceptions, a `\u003cnote str() failed\u003e` message is shown instead. In the case where `exception.__notes__` is not a sequence and cannot be shown, a `\u003c__notes__ repr() failed\u003e` is shown. In the second case, the message does not include a trailing newline.\r\n\r\n`__notes__` is a list containing a broken \"note\" - output includes a newline:\r\n```python\r\nclass A:\r\n def __repr__(self):\r\n raise Exception()\r\n\r\ne = Exception()\r\ne.__notes__ = [A()] # !!!\r\nraise e\r\n```\r\n```\r\nuser@host $ ./repro.py\r\nTraceback (most recent call last):\r\n File \"./repro.py\", line 8, in \u003cmodule\u003e\r\n raise e\r\nException\r\n\u003cnote str() failed\u003e\r\nuser@host $ \r\n```\r\n\r\n`__notes__` is just a single broken \"note\" - output does not include a newline:\r\n```python\r\nclass A:\r\n def __repr__(self):\r\n raise Exception()\r\n\r\ne = Exception()\r\ne.__notes__ = A() # !!!\r\nraise e\r\n```\r\n```\r\nuser@host $ ./repro.py\r\nTraceback (most recent call last):\r\n File \"./repro.py\", line 8, in \u003cmodule\u003e\r\n raise e\r\nException\r\n\u003c__notes__ repr() failed\u003euser@host $ \r\n```\r\n\r\nAdditionally, when `__notes__` is a string/bytes, the contents are expoded over multiple lines because of an `isinstance(__notes__, Sequence)` check.\r\n\r\nString:\r\n```python\r\ne = Exception()\r\ne.__notes__ = \"a note\"\r\nraise e\r\n```\r\n```\r\nuser@host $ ./repro.py\r\nTraceback (most recent call last):\r\n File \"./repro.py\", line 8, in \u003cmodule\u003e\r\n raise e\r\nException\r\na\r\n\r\nn\r\no\r\nt\r\ne\r\nuser@host $ \r\n```\r\n\r\nBytes:\r\n```python\r\ne = Exception()\r\ne.__notes__ = b\"a note\"\r\nraise e\r\n```\r\n```\r\nuser@host $ ./repro.py\r\nTraceback (most recent call last):\r\n File \"./repro.py\", line 8, in \u003cmodule\u003e\r\n raise e\r\nException\r\n97\r\n32\r\n110\r\n111\r\n116\r\n101\r\nuser@host $ \r\n```\r\n\r\nEven though the above are all edge cases, since there are some checks that handle these cases already, it makes sense to handle them more gracefully.\r\n\r\n# Your environment\r\n\r\n- CPython versions tested on: 3.12.0a7+\r\n- Operating system and architecture: macOS Mojave (x86)\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-103897\r\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/pR0Ps","@type":"Person","name":"pR0Ps"},"datePublished":"2023-04-26T21:37:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/103895/cpython/issues/103895"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:33daed5a-05ee-7c08-91c0-96f835ff1164 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC04:1E165C:AC6D4:E9929:696962C4 |
| html-safe-nonce | f4b67595ff6c8f6b5ec36dd26dd3fd1c54a2a4e6e1bad997e9e5189539f03705 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzA0OjFFMTY1QzpBQzZENDpFOTkyOTo2OTY5NjJDNCIsInZpc2l0b3JfaWQiOiI0MDc3NDY1NzIxNDkyODI0NzcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 777cc15156cc262403b3012f3042473cc67e25a0fb9297eb0809b38588bc3839 |
| hovercard-subject-tag | issue:1685766667 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/103895/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f8cb4b7840553b6bea42e0ce1d92aca0f582f65e1fd76a01d228e071e610b231/python/cpython/issues/103895 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f8cb4b7840553b6bea42e0ce1d92aca0f582f65e1fd76a01d228e071e610b231/python/cpython/issues/103895 |
| og:image:alt | Bug report When notes on an exception cannot be shown because the repr and str raise exceptions, a |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pR0Ps |
| hostname | github.com |
| expected-hostname | github.com |
| None | 48487c1ad776a7975b7132d95f4240ff3ae37cd5b8e3cb597102a4edb76738f1 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 669463fcc54773a88c1f5a44eef6b99a5504b9c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width