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

Patch traceback.TracebackException.format_exception_only() #7

Closed
Zac-HD opened this issue Apr 28, 2022 · 0 comments · Fixed by #8
Closed

Patch traceback.TracebackException.format_exception_only() #7

Zac-HD opened this issue Apr 28, 2022 · 0 comments · Fixed by #8

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Apr 28, 2022

It turns out that this is used by pytest, and so we'll need to add something along the lines of

import collections, traceback

def _safe_string(value, what, func=str):
    try:
        return func(value)
    except:
        return f"<{what} {func.__name__}() failed>"

def format_exception_only_with_note(self):
    yield from format_exception_only_base(self)
    if isinstance(self.__notes__, collections.abc.Sequence):
        for note in self.__notes__:
            note = _safe_string(note, "note")
            yield from [l + "\n" for l in note.split("\n")]
    elif self.__notes__ is not None:
        yield _safe_string(self.__notes__, "__notes__", func=repr)

format_exception_only_base = traceback.TracebackException.format_exception_only
traceback.TracebackException.format_exception_only = format_exception_only_with_note

(quick dump from local debugging; I'll arrange a better PR in the next few days)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant