rich icon indicating copy to clipboard operation
rich copied to clipboard

Print cause of exceptions even when they have no traceback

Open nilsvu opened this issue 2 years ago • 5 comments

Exceptions from subprocesses raised by 'multiprocessing' don't include a traceback, so rich doesn't currently print them. They contain important information though, so this PR enables printing the exception without the traceback. This works because walk_tb doesn't do anything if the input is None.

Minimal example:

import rich.traceback
import multiprocessing


def fail():
    raise ValueError


if __name__ == "__main__":
    rich.traceback.install()
    with multiprocessing.Pool() as pool:
        # The traceback printed by rich doesn't point to the function `fail`,
        # but only to code in the `multiprocessing` module. This PR fixes that.
        pool.apply(fail)

Note that exceptions from multiprocessing include the traceback formatted as a string in the description of the exception (see this code in multiprocessing.pool: https://github.com/python/cpython/blob/main/Lib/multiprocessing/pool.py#L57). This will be printed by rich after this PR.

Type of changes

  • [x] Bug fix
  • [ ] New feature
  • [ ] Documentation / docstrings
  • [ ] Tests
  • [ ] Other

Checklist

  • [x] I've run the latest black with default args on new code.
  • [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • [x] I've added tests for new code.
  • [x] I accept that @willmcgugan may be pedantic in the code review.

nilsvu avatar Jul 16 '22 12:07 nilsvu

Can I see an example of how multiprocessing exceptions are printed?

willmcgugan avatar Jul 22 '22 15:07 willmcgugan

Sure, here's an example:

import rich.traceback
import multiprocessing


def fail():
    raise ValueError("Fail!")


if __name__ == "__main__":
    rich.traceback.install(suppress=[multiprocessing])
    with multiprocessing.Pool() as pool:
        pool.apply(fail)

Output with this PR:

Bildschirmfoto 2022-07-25 um 13 07 25

Without this PR only the bottom red box with the multiprocessing traceback is printed. Note that it doesn't point to where the exception is actually thrown in the code.

nilsvu avatar Jul 25 '22 11:07 nilsvu

Can you look at the CI fails please.

willmcgugan avatar Jul 25 '22 11:07 willmcgugan

Should be fixed, just needs your approval to run CI

nilsvu avatar Jul 25 '22 11:07 nilsvu

Codecov Report

Merging #2397 (0817587) into master (19e518f) will decrease coverage by 0.03%. The diff coverage is 98.34%.

@@            Coverage Diff             @@
##           master    #2397      +/-   ##
==========================================
- Coverage   98.71%   98.67%   -0.04%     
==========================================
  Files          73       72       -1     
  Lines        7771     7765       -6     
==========================================
- Hits         7671     7662       -9     
- Misses        100      103       +3     
Flag Coverage Δ
unittests 98.67% <98.34%> (-0.04%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/default_styles.py 100.00% <ø> (ø)
rich/progress.py 92.76% <ø> (ø)
rich/segment.py 98.72% <93.10%> (+<0.01%) :arrow_up:
rich/_inspect.py 100.00% <100.00%> (ø)
rich/box.py 100.00% <100.00%> (ø)
rich/cells.py 96.05% <100.00%> (-3.95%) :arrow_down:
rich/console.py 98.30% <100.00%> (+0.01%) :arrow_up:
rich/highlighter.py 100.00% <100.00%> (ø)
rich/pretty.py 99.22% <100.00%> (+<0.01%) :arrow_up:
rich/style.py 99.75% <100.00%> (-0.01%) :arrow_down:
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update aea574a...0817587. Read the comment docs.

codecov-commenter avatar Jul 25 '22 11:07 codecov-commenter

Thanks

willmcgugan avatar Sep 19 '22 10:09 willmcgugan