Skip to content

bpo-30639: Lazily compute repr for error#2132

Merged
1st1 merged 6 commits into
python:masterfrom
takluyver:bpo-30639
Oct 24, 2017
Merged

bpo-30639: Lazily compute repr for error#2132
1st1 merged 6 commits into
python:masterfrom
takluyver:bpo-30639

Conversation

@takluyver

@takluyver takluyver commented Jun 12, 2017

Copy link
Copy Markdown
Contributor

If the TypeError is caught and handled by calling code, there's no need to compute the object's repr for the error message - the repr may take a long time (real case encountered in IPython & Jedi), or it may throw another error.

If the error results in a traceback, it will still compute the repr and produce the message as before.

I think this defensiveness is worthwhile for inspect functions because, by design, we're calling them with arbitrary objects which might be badly behaved, and expecting them to be useful nonetheless.

An alternative fix would be to use object.__repr__() to always get the plain <foo object at 0x123abc...> representation.

https://bugs.python.org/issue30639

@mention-bot

Copy link
Copy Markdown

@takluyver, thanks for your PR! By analyzing the history of the files in this pull request, we identified @larryhastings, @1st1 and @zestyping to be potential reviewers.

@marco-buttu marco-buttu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A part of the newline that should be removed, the rest LGTM

Comment thread Lib/inspect.py Outdated
lines.pop(0)
return '\n'.join(lines)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new line should be removed.

Comment thread Lib/inspect.py Outdated
'function, traceback, frame, or code object'.format(object))
raise TypeError('object of type {} is not a module, class, method, '
'function, traceback, frame, or code object'.format(
type(object).__name__))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I did not tell you in the previous message, but I realized just now. You should align the type(object).__name__ to 'function'.

@takluyver

Copy link
Copy Markdown
Contributor Author

Can I prevail on anyone to merge this? @serhiy-storchaka , I implemented the change you suggested on the issue.

@serhiy-storchaka serhiy-storchaka requested a review from 1st1 June 20, 2017 15:22
@serhiy-storchaka

Copy link
Copy Markdown
Member

Could you please take a look on this change @1st1?

@1st1 1st1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a NEWS entry using blurb.

Comment thread Lib/inspect.py
'function, traceback, frame, or code object'.format(object))
raise TypeError('object of type {} is not a module, class, method, '
'function, traceback, frame, or code object'.format(
type(object).__name__))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change the error message to "module, class, method, function, traceback, frame, or code object was expected, got {}".

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@takluyver

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@serhiy-storchaka, @1st1: please review the changes made to this pull request.

@1st1

1st1 commented Oct 24, 2017

Copy link
Copy Markdown
Member

Merged. Thanks a lot!

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 this pull request may close these issues.

7 participants