Commit 1d4e184
authored
Display exception notes in tracebacks (#14039)
[PEP 678](https://peps.python.org/pep-0678/) introduced the ability to
add notes to exception objects. This has been [released in Python
3.11](https://docs.python.org/3/library/exceptions.html#BaseException.add_note)
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):
```python
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](https://github.com/python/cpython/blob/main/Lib/traceback.py)
(search for `__notes__`) and improvements for dealing with edge cases
more nicely in
[cpython#103897](python/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](https://docs.python.org/3/tutorial/errors.html#enriching-exceptions-with-notes)
puts them in light gray, like the line numbers.
Here's how the test 2 looks like after these changes:

## 🐍 🤹♂️File tree
4 files changed
+93
-32
lines changed- IPython/core
- tests
- docs/source/whatsnew/pr
4 files changed
+93
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
| 95 | + | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
170 | | - | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
| 175 | + | |
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
| |||
363 | 363 | | |
364 | 364 | | |
365 | 365 | | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
366 | 389 | | |
367 | 390 | | |
368 | 391 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
186 | 195 | | |
187 | 196 | | |
188 | 197 | | |
| |||
582 | 591 | | |
583 | 592 | | |
584 | 593 | | |
585 | | - | |
| 594 | + | |
586 | 595 | | |
587 | 596 | | |
588 | 597 | | |
| |||
600 | 609 | | |
601 | 610 | | |
602 | 611 | | |
603 | | - | |
| 612 | + | |
604 | 613 | | |
605 | | - | |
| 614 | + | |
606 | 615 | | |
607 | 616 | | |
608 | 617 | | |
| |||
619 | 628 | | |
620 | 629 | | |
621 | 630 | | |
622 | | - | |
| 631 | + | |
623 | 632 | | |
624 | 633 | | |
625 | 634 | | |
626 | | - | |
| 635 | + | |
627 | 636 | | |
628 | 637 | | |
629 | 638 | | |
| |||
634 | 643 | | |
635 | 644 | | |
636 | 645 | | |
637 | | - | |
| 646 | + | |
638 | 647 | | |
639 | 648 | | |
640 | 649 | | |
| |||
654 | 663 | | |
655 | 664 | | |
656 | 665 | | |
657 | | - | |
658 | | - | |
659 | | - | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
660 | 669 | | |
661 | 670 | | |
662 | 671 | | |
663 | 672 | | |
664 | 673 | | |
665 | 674 | | |
666 | | - | |
667 | | - | |
668 | | - | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
669 | 679 | | |
670 | 680 | | |
671 | 681 | | |
672 | 682 | | |
673 | 683 | | |
674 | 684 | | |
675 | | - | |
676 | | - | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
677 | 688 | | |
678 | | - | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
679 | 693 | | |
680 | 694 | | |
681 | 695 | | |
682 | 696 | | |
683 | 697 | | |
684 | 698 | | |
685 | 699 | | |
686 | | - | |
| 700 | + | |
687 | 701 | | |
688 | 702 | | |
689 | 703 | | |
| |||
999 | 1013 | | |
1000 | 1014 | | |
1001 | 1015 | | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
1002 | 1022 | | |
1003 | | - | |
1004 | | - | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
1005 | 1037 | | |
1006 | 1038 | | |
1007 | 1039 | | |
| |||
1068 | 1100 | | |
1069 | 1101 | | |
1070 | 1102 | | |
1071 | | - | |
| 1103 | + | |
1072 | 1104 | | |
1073 | 1105 | | |
1074 | 1106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments