GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Display exception notes in tracebacks by EtiennePelletier · Pull Request #14039 · ipython/ipython

Conversation

@EtiennePelletier

@EtiennePelletier EtiennePelletier commented Apr 26, 2023

Copy link
Copy Markdown
Contributor

PEP 678 introduced the ability to add notes to exception objects. This has been released in Python 3.11 and is currently not implemented in IPython. These changes are fully compatible with older Python versions that don't include PEP 678.

Here's a sample test that shows the consistency in Python's stdlib traceback module (test 1) and the difference between Python and IPython's runtimes (test 2):

import traceback

print('--- test 1 ---')
try:
    raise Exception('Testing notes')
except Exception as e:
    e.add_note('Does this work?')
    e.add_note('Yes!')
    traceback.print_exc()

print('\n--- test 2 ---')
try:
    raise Exception('Testing notes')
except Exception as e:
    e.add_note('Does this work?')
    e.add_note('No!')
    raise

When executed with Python 3.11, both notes are displayed in both tracebacks:

$ python test.py 
--- test 1 ---
Traceback (most recent call last):
  File "/app/test.py", line 5, in <module>
    raise Exception('Testing notes')
Exception: Testing notes
Does this work?
Yes!

--- test 2 ---
Traceback (most recent call last):
  File "/app/test.py", line 13, in <module>
    raise Exception('Testing notes')
Exception: Testing notes
Does this work?
No!

In IPython's VerboseTB does not yet handle exception notes:

$ ipython test.py 
--- test 1 ---
Traceback (most recent call last):
  File "/app/test.py", line 5, in <module>
    raise Exception('Testing notes')
Exception: Testing notes
Does this work?
Yes!

--- test 2 ---
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
File /app/test.py:13
     11 print('\n--- test 2 ---')
     12 try:
---> 13     raise Exception('Testing notes')
     14 except Exception as e:
     15     e.add_note('Does this work?')

Exception: Testing notes

The changes I am suggesting are inspired from implementation of Lib/traceback.py (search for __notes__) and improvements for dealing with edge cases more nicely in cpython#103897.

Although notes are meant to be strings only, I kept some inspiration from the existing exception handling to ensure that the notes are uncolored and bytes decoded, if there are any. I am definitely open to using a different color if deemed better. For context, bpython keeps the notes uncolored, and Python's tutorial puts them in light gray, like the line numbers.

Here's how the test 2 looks like after these changes:
image

🐍 🤹‍♂️

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@EtiennePelletier
Display exception notes in tracebacks
074040e
@Zac-HD

Zac-HD commented Apr 28, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Oops, I checked for existing issues a few hours before this was opened and then created #14040 - it has tests and a changelog, but is a bit less careful about edge cases. Cherry-pick whatever you want and then I'll close my PR?

EtiennePelletier reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Carreau

Carreau commented Apr 28, 2023

Copy link
Copy Markdown
Member

I'm guessing you both are at PyCon sprints !? Enjoy ! I'll likely get that in during May :-).

EtiennePelletier and Zac-HD reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Carreau Carreau added this to the 8.14 milestone Apr 28, 2023
Zac-HD and others added 2 commits April 30, 2023 22:06
@Zac-HD @EtiennePelletier
Show PEP-678 notes in ultratb (Plain), add tests
ef994b4
@EtiennePelletier
Apply some small formatting fixes in ultratb
f7014a8
@EtiennePelletier
EtiennePelletier force-pushed the main branch from 95e4f74 to f7014a8 Compare May 1, 2023 04:30
@EtiennePelletier

EtiennePelletier commented May 1, 2023

Copy link
Copy Markdown
Contributor Author

Changes cherry-picked, good catch Zac on the Plain traceback format!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Carreau
Carreau reviewed Jun 2, 2023
View reviewed changes
Comment thread .gitignore
*.swp
.pytest_cache
.python-version
.venv*/

@Carreau Carreau Jun 2, 2023

Copy link
Copy Markdown
Member

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.

You likely want that in your global gitignore to not have to do it for all your repos.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EtiennePelletier reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@Carreau

Carreau commented Jun 2, 2023

Copy link
Copy Markdown
Member

Let's try this. Sorry I was mostly travelling last month.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Carreau
Carreau merged commit 1d4e184 into ipython:main Jun 2, 2023
@Carreau Carreau mentioned this pull request Jun 2, 2023
Show PEP-678 notes in ultratb #14040
Closed
This was referenced Jun 21, 2023
Use __notes__ for exceptions when reporting keys during IO scverse/anndata#868
Closed
IPython REPL doesn't render exception notes #13849
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Carreau Carreau Carreau left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

8.14

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@EtiennePelletier @Zac-HD @Carreau
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.