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

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

Yoav11
Copy link

@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
@Yoav11 Yoav11 requested a review from picnixz July 20, 2025 08:48
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
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.

Comment on lines 203 to 207
* The :meth:`~object.__repr__` of :class:`ImportError` and :class:`ModuleNotFoundError`
now shows "name" and "path" as ``name=<name>`` and ``path=<path>`` if they were given
as keyword arguments at construction time.
(Contributed by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir in :gh:`74185`.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @Yoav11, there's a conflict with main in this place. It needs to be resolved.

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?

if (PyUnicodeWriter_WriteSubstring(
writer, r, 0, PyUnicode_GET_LENGTH(r) - 1) < 0)
{
Py_XDECREF(r);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Py_XDECREF(r);
Py_DECREF(r);

Py_XDECREF(r);
goto error;
}
Py_XDECREF(r);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Py_XDECREF(r);
Py_DECREF(r);

{
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
PyImportErrorObject *exc = PyImportErrorObject_CAST(self);
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
Copy link
Member

Choose a reason for hiding this comment

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

If there are no name and path, it can just return the result of BaseException_repr(). There is no need to create a PyUnicodeWriter.

@Yoav11
Copy link
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
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

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.