mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Split `format_traceback()
from
save_traceback()
`
This commit is contained in:
parent
e8f3c4902d
commit
2ae1d7f9c3
@ -51,6 +51,7 @@ def strip_escape_sequences(text: str, /) -> str:
|
||||
|
||||
|
||||
def error_info(messages: str, extensions: str, traceback: str) -> str:
|
||||
"""Format the traceback and extensions list with environment information."""
|
||||
import platform
|
||||
|
||||
import docutils
|
||||
@ -87,8 +88,8 @@ Traceback
|
||||
"""
|
||||
|
||||
|
||||
def save_traceback(app: Sphinx | None, exc: BaseException) -> str:
|
||||
"""Save the given exception's traceback in a temporary file."""
|
||||
def format_traceback(app: Sphinx | None, exc: BaseException) -> str:
|
||||
"""Format the given exception's traceback with environment information."""
|
||||
if isinstance(exc, SphinxParallelError):
|
||||
exc_format = '(Error in parallel process)\n' + exc.traceback
|
||||
else:
|
||||
@ -106,6 +107,12 @@ def save_traceback(app: Sphinx | None, exc: BaseException) -> str:
|
||||
if ext.version != 'builtin'
|
||||
)
|
||||
|
||||
return error_info(last_msgs, exts_list, exc_format)
|
||||
|
||||
|
||||
def save_traceback(app: Sphinx | None, exc: BaseException) -> str:
|
||||
"""Save the given exception's traceback in a temporary file."""
|
||||
output = format_traceback(app=app, exc=exc)
|
||||
with tempfile.NamedTemporaryFile(
|
||||
suffix='.log', prefix='sphinx-err-', delete=False
|
||||
) as f:
|
||||
|
@ -5,8 +5,7 @@ import traceback
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sphinx.errors import SphinxParallelError
|
||||
from sphinx.util.console import strip_escape_sequences
|
||||
from sphinx._cli.util.errors import format_traceback
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sphinx.application import Sphinx
|
||||
@ -14,52 +13,10 @@ if TYPE_CHECKING:
|
||||
|
||||
def save_traceback(app: Sphinx | None, exc: BaseException) -> str:
|
||||
"""Save the given exception's traceback in a temporary file."""
|
||||
import platform
|
||||
|
||||
import docutils
|
||||
import jinja2
|
||||
import pygments
|
||||
|
||||
import sphinx
|
||||
|
||||
if isinstance(exc, SphinxParallelError):
|
||||
exc_format = '(Error in parallel process)\n' + exc.traceback
|
||||
else:
|
||||
exc_format = traceback.format_exc()
|
||||
|
||||
if app is None:
|
||||
last_msgs = exts_list = ''
|
||||
else:
|
||||
extensions = app.extensions.values()
|
||||
last_msgs = '\n'.join(
|
||||
f'# {strip_escape_sequences(s).strip()}' for s in app.messagelog
|
||||
)
|
||||
exts_list = '\n'.join(
|
||||
f'# {ext.name} ({ext.version})'
|
||||
for ext in extensions
|
||||
if ext.version != 'builtin'
|
||||
)
|
||||
|
||||
with NamedTemporaryFile(
|
||||
'w', encoding='utf-8', suffix='.log', prefix='sphinx-err-', delete=False
|
||||
) as f:
|
||||
f.write(f"""\
|
||||
# Platform: {sys.platform}; ({platform.platform()})
|
||||
# Sphinx version: {sphinx.__display_version__}
|
||||
# Python version: {platform.python_version()} ({platform.python_implementation()})
|
||||
# Docutils version: {docutils.__version__}
|
||||
# Jinja2 version: {jinja2.__version__}
|
||||
# Pygments version: {pygments.__version__}
|
||||
|
||||
# Last messages:
|
||||
{last_msgs}
|
||||
|
||||
# Loaded extensions:
|
||||
{exts_list}
|
||||
|
||||
# Traceback:
|
||||
{exc_format}
|
||||
""")
|
||||
f.write(format_traceback(app, exc))
|
||||
return f.name
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user