Skip to content

Commit

Permalink
Print full traceback on internal fatal error
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Jan 14, 2025
1 parent e7dd42e commit df29cf9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Features added
Patch by Chris Barrick.
* #13227: Implement the :rst:role:`kbd` role as a ``SphinxRole``.
Patch by Adam Turner.
* #13163: Always print tracebacks in full when Sphinx encounters an internal
error.
Patch by Kevin Deldycke.

Bugs fixed
----------
Expand Down
6 changes: 5 additions & 1 deletion sphinx/_cli/util/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def handle_exception(
print_traceback: bool = False,
message_log: Collection[str] = (),
extensions: Collection[Extension] = (),
print_full_traceback: bool = False,
) -> None:
from bdb import BdbQuit
from traceback import TracebackException
Expand Down Expand Up @@ -225,9 +226,12 @@ def print_red(*values: str) -> None:

print_red(__('Exception occurred:'))
print_err(formatted_tb)
traceback_info_path = save_traceback(
traceback_output = full_exception_context(
exception, message_log=message_log, extensions=extensions
)
traceback_info_path = write_temporary_file(traceback_output)
if print_full_traceback:
print_err(traceback_output)
print_err(__('The full traceback has been saved in:'))
print_err(traceback_info_path)
print_err()
Expand Down
9 changes: 7 additions & 2 deletions sphinx/cmd/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import multiprocessing
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, TextIO

import sphinx._cli.util.errors
import sphinx.locale
Expand All @@ -23,7 +23,6 @@

if TYPE_CHECKING:
from collections.abc import Collection, Sequence
from typing import Any, TextIO

from sphinx.extension import Extension

Expand All @@ -37,15 +36,18 @@ def handle_exception(
if app is not None:
message_log: Sequence[str] = app.messagelog
extensions: Collection[Extension] = app.extensions.values()
print_full_traceback: bool = app.config.print_traceback
else:
message_log = extensions = ()
print_full_traceback = True
return sphinx._cli.util.errors.handle_exception(
exception,
stderr=stderr,
use_pdb=args.pdb,
print_traceback=args.verbosity or args.traceback,
message_log=message_log,
extensions=extensions,
print_full_traceback=print_full_traceback,
)


Expand Down Expand Up @@ -435,15 +437,18 @@ def build_main(argv: Sequence[str]) -> int:
if app is not None:
message_log: Sequence[str] = app.messagelog
extensions: Collection[Extension] = app.extensions.values()
print_full_traceback: bool = app.config.print_traceback
else:
message_log = extensions = ()
print_full_traceback = True
sphinx._cli.util.errors.handle_exception(
exc,
stderr=args.error,
use_pdb=args.pdb,
print_traceback=args.verbosity or args.traceback,
message_log=message_log,
extensions=extensions,
print_full_traceback=print_full_traceback,
)
return 2
finally:
Expand Down

0 comments on commit df29cf9

Please sign in to comment.