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

Improve feedback/information with "broken repr" #1118

Closed
blueyed opened this issue May 19, 2021 · 3 comments
Closed

Improve feedback/information with "broken repr" #1118

blueyed opened this issue May 19, 2021 · 3 comments

Comments

@blueyed
Copy link
Contributor

blueyed commented May 19, 2021

If the __repr__ of a local object fails, it gets reported as "<broken repr>":

if PY2:
def safe_repr(value):
# type: (Any) -> str
try:
rv = repr(value).decode("utf-8", "replace")
# At this point `rv` contains a bunch of literal escape codes, like
# this (exaggerated example):
#
# u"\\x2f"
#
# But we want to show this string as:
#
# u"/"
try:
# unicode-escape does this job, but can only decode latin1. So we
# attempt to encode in latin1.
return rv.encode("latin1").decode("unicode-escape")
except Exception:
# Since usually strings aren't latin1 this can break. In those
# cases we just give up.
return rv
except Exception:
# If e.g. the call to `repr` already fails
return u"<broken repr>"
else:
def safe_repr(value):
# type: (Any) -> str
try:
return repr(value)
except Exception:
return "<broken repr>"

It would be more helpful if it would include more information there, e.g. at least the class name of the exception.
It could also be something more sophisticated like:

def _try_repr_or_str(obj):
    try:
        return repr(obj)
    except (KeyboardInterrupt, SystemExit):
        raise
    except BaseException:
        return '{}("{}")'.format(type(obj).__name__, obj)


def _format_repr_exception(exc: BaseException, obj: Any) -> str:
    try:
        exc_info = _try_repr_or_str(exc)
    except (KeyboardInterrupt, SystemExit):
        raise
    except BaseException as exc:
        exc_info = "unpresentable exception ({})".format(_try_repr_or_str(exc))
    return "<[{} raised in repr()] {} object at 0x{:x}>".format(
        exc_info, type(obj).__name__, id(obj)
    )

(via https://github.com/blueyed/pytest/blob/8eb380425395332ffdf2cd42b8d7cf8ffab2ea2e/src/_pytest/_io/saferepr.py#L6-L24)

This would help with investigating why a repr is broken actually, e.g. with celery/py-amqp#361.

(slightly related: there was some related (still open) PR for raven-python about this already: getsentry/raven-python#1294)

@github-actions
Copy link

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@sl0thentr0py
Copy link
Member

hi @blueyed, we released an undocumented feature def sentry_repr in #1322.
Could you try it out and see if it fixes your problem?

@github-actions
Copy link

github-actions bot commented Mar 8, 2022

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

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

No branches or pull requests

2 participants