Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display locals in traceback when requested #2900

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ TMT_SHOW_TRACEBACK
By default, when tmt reports an error, the corresponding
traceback is not printed out. When ``TMT_SHOW_TRACEBACK`` is
set to any string except ``0``, traceback would be printed out.
When set to ``full``, traceback would list also local variables
in each stack frame.

TMT_OUTPUT_WIDTH
By default, the output width of commands like ``tmt * show`` is constrained
Expand Down
39 changes: 32 additions & 7 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,37 @@ def render_run_exception(exception: RunError) -> Iterator[str]:
yield from render_run_exception_streams(exception.stdout, exception.stderr, verbose=verbose)


def render_exception_stack(exception: BaseException) -> Iterator[str]:
""" Render traceback of the given exception """

exception_traceback = traceback.TracebackException(
type(exception),
exception,
exception.__traceback__,
capture_locals=True)

# N806: allow upper-case names to make them look like formatting
# tags in strings below.
R = functools.partial(click.style, fg='red') # noqa: N806
Y = functools.partial(click.style, fg='yellow') # noqa: N806
B = functools.partial(click.style, fg='blue') # noqa: N806

yield R('Traceback (most recent call last):')
yield ''

for frame in exception_traceback.stack:
yield f'File {Y(frame.filename)}, line {Y(str(frame.lineno))}, in {Y(frame.name)}'
yield f' {B(frame.line)}'

if os.getenv('TMT_SHOW_TRACEBACK', '0').lower() == 'full' and frame.locals:
yield ''

for k, v in frame.locals.items():
yield f' {B(k)} = {Y(v)}'

yield ''


def render_exception(exception: BaseException) -> Iterator[str]:
""" Render the exception and its causes for printing """

Expand All @@ -2486,14 +2517,8 @@ def _indent(iterable: Iterable[str]) -> Iterator[str]:
yield from render_run_exception(exception)

if os.getenv('TMT_SHOW_TRACEBACK', '0') != '0':
formatted_exc = traceback.format_exception(
type(exception),
exception,
exception.__traceback__,
chain=False)

yield ''
yield from _indent(formatted_exc)
yield from _indent(render_exception_stack(exception))

# Follow the chain and render all causes
def _render_cause(number: int, cause: BaseException) -> Iterator[str]:
Expand Down
Loading