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
protect against broken repr in lib.pretty #4459
Conversation
try: | ||
return repr(obj) | ||
except Exception as e: | ||
return "<repr(obj) failed: %s>" % e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it useful to include the type of the exception as well.
return "<repr(obj) failed: {0}: {1}>".format(type(e), e)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree that reporting the exception message is useful, can we still at least include the usual <module.type at 0x.....>
information? That will help locate the source of the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type info / address added, along with some extra paranoia about bad objects
don't trust exception strings or class attributes. adds _safe_getattr, used in a few higher level places to protect against bad objects
I like it. The docstring on |
cleaned up the docstring a bit, thanks. |
For the record, there are a couple of pathological cases which could still fail. Exceptions inheriting from BaseException directly won't be caught - that's kind of the design of the system, to allow I don't think we need to worry about either of those cases, I just wanted to mention it in case anyone disagrees. |
Merging this. |
protect against broken repr in lib.pretty
protect against broken repr in lib.pretty
for example: