Skip to content

gh-74185: repr() of ImportError now contains attributes name and path #136770

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

Merged
merged 31 commits into from
Aug 14, 2025

Conversation

Yoav11
Copy link
Contributor

@Yoav11 Yoav11 commented Jul 19, 2025

This PR was created during Europython 2025's sprint weekend.

This is a patch on #1011. authored by serhiy-storchaka and arhadthedev

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Yoav11 Yoav11 changed the title gh-74185: (patch) repr() of ImportError now contains attributes name and path. gh-74185: repr() of ImportError now contains attributes name and path. (patch) Jul 19, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland
Copy link
Member

You need to sign the CLA.

Requesting @encukou who wanted to finish this per #1011 (comment)

@picnixz picnixz changed the title gh-74185: repr() of ImportError now contains attributes name and path. (patch) gh-74185: repr() of ImportError now contains attributes name and path Jul 19, 2025
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

This looks good now. I'll let @serhiy-storchaka have a look as well.

@Yoav11
Copy link
Contributor Author

Yoav11 commented Jul 20, 2025

This looks good now. I'll let @serhiy-storchaka have a look as well.

thanks for the review @picnixz ! 🙏

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

LGTM!

I plan to merge this around Wednesday if there are no objections.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Sorry for ignoring this PR earlier. It did not looked important to me, and I was no longer sure about its usefulness, because path is usually so long.

Why use PyUnicodeWriter at first place? Does it make the code smaller/clearer? Is the performance difference too large?

@Yoav11
Copy link
Contributor Author

Yoav11 commented Aug 13, 2025

Sorry for ignoring this PR earlier. It did not looked important to me, and I was no longer sure about its usefulness, because path is usually so long.

Why use PyUnicodeWriter at first place? Does it make the code smaller/clearer? Is the performance difference too large?

@picnixz recommended PyUnicodeWriter in #136770 (comment)

@serhiy-storchaka
Copy link
Member

What were the problems with recreating the strings? Do you have any benchmark data?

@encukou
Copy link
Member

encukou commented Aug 13, 2025

@serhiy-storchaka, what would be your preferred alternative to PyUnicodeWriter? As far as I know, @vstinner added it exactly for use cases like this.

@serhiy-storchaka
Copy link
Member

The original PR used PyUnicode_Substring() and PyUnicode_FromFormat() (similar code is also used in other places). It was pretty compact, half as short as the current code in this PR. It had also fast path for the case without keyword argument. I ask what are the benefits of the larger code. Is it performance? I would like to see the data.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

I'm fine with PyUnicodeWriter usage in this PR. It makes the code more verbose (than the first PR version), but the code is easy to read.

@Yoav11
Copy link
Contributor Author

Yoav11 commented Aug 13, 2025

The original PR used PyUnicode_Substring() and PyUnicode_FromFormat() (similar code is also used in other places). It was pretty compact, half as short as the current code in this PR. It had also fast path for the case without keyword argument. I ask what are the benefits of the larger code. Is it performance? I would like to see the data.

@serhiy-storchaka Please see a small benchmark test of the two alternatives here Yoav11#1
at least from this naive test it would appear to me that the PyUnicdeWriter implementation is ~25% faster. Lmk if you would like me to change the benchmark / try a different test case

@encukou
Copy link
Member

encukou commented Aug 14, 2025

The code is bigger because it uses an extra function call rather than %s with hasargs ? ", " : "". It would be possible to do that with PyUnicodeWriter_Format too, and get back to roughly the same size of code.
I personally find the if (hasargs) {PyUnicodeWriter_WriteASCII(...)} more readable.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your benchmark, @Yoav11. I checked myself, indeed, there is a measurable difference.

LGTM. 👍

@vstinner vstinner merged commit c87b66b into python:main Aug 14, 2025
44 checks passed
@vstinner
Copy link
Member

Merged, thanks.

@Yoav11
Copy link
Contributor Author

Yoav11 commented Aug 14, 2025

thank you @vstinner @serhiy-storchaka @encukou @StanFromIreland @picnixz ! 🚀 🐍

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

Successfully merging this pull request may close these issues.