##// END OF EJS Templates
Display exception notes in tracebacks (#14039)...
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](https://github.com/python/cpython/pull/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: ![image](https://user-images.githubusercontent.com/16963011/234723689-6bbfe0ff-94d4-4a90-9da6-acfe1c8e5edf.png) ## :snake: :man_juggling:

File last commit:

r22590:c64b5204
r28313:1d4e1847 merge
Show More
make.cmd
82 lines | 2.6 KiB | application/x-dos-batch | BatchLexer
@ECHO OFF
REM ~ Windows command line make file for Sphinx documentation
SETLOCAL
SET SPHINXOPTS=
SET SPHINXBUILD=sphinx-build
SET PAPER=
SET SRCDIR=source
SET PYTHON=python
IF "%PAPER%" == "" SET PAPER=a4
SET ALLSPHINXOPTS=-d build\doctrees -D latex_paper_size=%PAPER% %SPHINXOPTS% %SRCDIR%
FOR %%X IN (%SPHINXBUILD%.exe) DO SET P=%%~$PATH:X
FOR %%L IN (html html_noapi pickle htmlhelp latex changes linkcheck) DO (
IF "%1" == "%%L" (
IF "%P%" == "" (
ECHO.
ECHO Error: Sphinx is not available. Please make sure it is correctly installed.
GOTO END
)
MD build\doctrees 2>NUL
MD build\%1 || GOTO DIR_EXIST
%PYTHON% autogen_config.py && ECHO Created docs for config options
%PYTHON% autogen_magics.py && ECHO Created docs for line ^& cell magics
%PYTHON% autogen_shortcuts.py && ECHO Created docs for shortcuts
IF NOT "%1" == "html_noapi" (
%PYTHON% autogen_api.py && ECHO Build API docs finished
%SPHINXBUILD% -b %1 %ALLSPHINXOPTS% build\%1
) ELSE (
%SPHINXBUILD% -b html %ALLSPHINXOPTS% build\%1
)
IF NOT ERRORLEVEL 0 GOTO ERROR
ECHO.
ECHO Build finished. Results are in build\%1.
IF "%1" == "pickle" (
ECHO Now you can process the pickle files or run
ECHO sphinx-web build\pickle to start the sphinx-web server.
)
IF "%1" == "htmlhelp" (
ECHO Now you can run HTML Help Workshop with the
ECHO .hhp project file in build/htmlhelp.
)
IF "%1" == "linkcheck" (
ECHO Look for any errors in the above output
ECHO or in build\linkcheck\output.rst.
)
GOTO END
)
)
IF "%1" == "clean" (
RD /s /q build dist %SRCDIR%\api\generated 2>NUL
IF ERRORLEVEL 0 ECHO Build environment cleaned!
GOTO END
)
ECHO.
ECHO Please use "make [target]" where [target] is one of:
ECHO.
ECHO html to make standalone HTML files
ECHO html_noapi same as above, without the time consuming API docs
ECHO jsapi to make standalone HTML files for the Javascript API
ECHO pickle to make pickle files (usable by e.g. sphinx-web)
ECHO htmlhelp to make HTML files and a HTML help project
ECHO latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
ECHO changes to make an overview over all changed/added/deprecated items
ECHO linkcheck to check all external links for integrity
GOTO END
:DIR_EXIST
ECHO.
ECHO Info: Run "make clean" to clean build environment
:ERROR
ECHO.
ECHO Error: Build process failed!
:END
ENDLOCAL