Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Yoav11 wants to merge20 commits intopython:main
base:main
Choose a base branch
Loading
fromYoav11:importerror-repr

Conversation

Yoav11
Copy link

@Yoav11Yoav11 commentedJul 19, 2025
edited
Loading

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

This is a patch on#1011. authored byserhiy-storchaka andarhadthedev

@python-cla-bot
Copy link

python-cla-botbot commentedJul 19, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

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

@Yoav11Yoav11 changed the titlegh-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

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

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

@StanFromIreland
Copy link
Member

You need to sign the CLA.

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


if (r&& (exc->name||exc->path)) {
/* remove ')' */
Py_SETREF(r,PyUnicode_Substring(r,0,PyUnicode_GET_LENGTH(r)-1));
Copy link
Member

@picnixzpicnixzJul 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

Let's usePyUnicodeWriter instead to avoids re-creating strings:

PyUnicodeWriter*writer=PyUnicodeWriter_Create(0);if (writer==NULL) gotoerror;if (PyUnicodeWriter_WriteSubstring(writer,r,0,l-1)<0) gotoerror;if (exc->name) {if (hasargs) {if (PyUnicodeWriter_WriteASCII(writer,", ",2)<0) gotoerror;  }  ...}...returnPyUnicodeWriter_Finish(writer);error:/* cleanup */

@picnixzpicnixz changed the titlegh-74185: repr() of ImportError now contains attributes name and path. (patch)gh-74185: repr() of ImportError now contains attributes name and pathJul 19, 2025
PyObject*r=BaseException_repr(self);
PyImportErrorObject*exc=PyImportErrorObject_CAST(self);
PyUnicodeWriter*writer=PyUnicodeWriter_Create(0);
if (writer==NULL) gotoerror;
Copy link
Member

Choose a reason for hiding this comment

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

Sorry but my suggestion wasn'tPEP-7 compliant. So you should rewrite this function in aPEP-7 compliant way.

Copy link

@ynir3ynir3Jul 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

@picnixz are you referring tobraces are required everywhere, even where C permits them to be omitted ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

ynir3 reacted with thumbs up emoji
Comment on lines 1880 to 1882
if (PyUnicodeWriter_WriteASCII(writer,"name='",6)<0) gotoerror;
if (PyUnicodeWriter_WriteSubstring(writer,exc->name,0,PyUnicode_GET_LENGTH(exc->name))<0) gotoerror;
if (PyUnicodeWriter_WriteASCII(writer,"'",1)<0) gotoerror;
Copy link
Member

Choose a reason for hiding this comment

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

For this one, as you're using the entire "exc->name", you can just usePyUnicodeWriter_Format("name=%R", exc->name") instead.

Comment on lines 1896 to 1899
error:
Py_XDECREF(r);
PyUnicodeWriter_Discard(writer);
returnNULL;
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
error:
Py_XDECREF(r);
PyUnicodeWriter_Discard(writer);
returnNULL;
error:
Py_XDECREF(r);
PyUnicodeWriter_Discard(writer);
returnNULL;

Comment on lines 2634 to 2636
try:
importdoes_not_exist# type: ignore[import] # noqa: F401
exceptModuleNotFoundErrorase:
Copy link
Member

Choose a reason for hiding this comment

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

You can usewith self.assertRaises(ImportError) as cm and checkcm.exception attributes.

@@ -1864,6 +1864,41 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
returnres;
}

staticPyObject*
ImportError_repr(PyObject*self)
Copy link
Member

Choose a reason for hiding this comment

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

Are there other exceptions that will benefit from having keywords in their reprs? AFAICT, the keywords are only rendered if they were passed to the constructor as keyword arguments. In particular, we could make a genericrepr helper for that which takes into account them, something like:

staticPyObject*repr_with_keywords(PyObject*exc,constchar*const*kwlist){/* format using kwlist[i]=getattr(exc, kwlist[i])     * with kwlist NULL-terminated */}

Its usage would be

staticchar*kwlist[]= {"name","path",NULL};repr_with_keywords(exc,kwlist)

Copy link
Member

@picnixzpicnixzJul 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

For now, let's hold off this idea as I don't know if it can be useful. But this can be worth investigating instead of storing the given keywords explicitly.

Choose a reason for hiding this comment

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

yes - AFAIK only ImportError and ModuleNotFoundError (which hinnerits this functionality as per the tests) have optional arguments ? but makes sense to me that if other exception types in the future have optionals then we could extract this out.

Copy link
Member

@picnixzpicnixzJul 19, 2025
edited
Loading

Choose a reason for hiding this comment

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

Yes, but let's do it in a follow-up PR so that we can revert the helper commit more easily. Also it depends on whether we would store the keywords passed to the constructor (this is#11580)

@ynir3
Copy link

@picnixz any more comments ? :)

gotoerror;
}
}
if (PyUnicodeWriter_WriteASCII(writer,")",1)<0) gotoerror;
Copy link
Member

Choose a reason for hiding this comment

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

if (writer==NULL) {
gotoerror;
}
if (PyUnicodeWriter_WriteSubstring(writer,r,0,PyUnicode_GET_LENGTH(r)-1)<0) {
Copy link
Member

Choose a reason for hiding this comment

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

Plese wrap lines under 80 chars.

ImportError_repr(PyObject*self)
{
inthasargs=PyTuple_GET_SIZE(((PyBaseExceptionObject*)self)->args)!=0;
PyObject*r=BaseException_repr(self);
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be decrefed after it's been used. By doing so, you can also remove thePy_XDECREF(r) call at the end as it won't be needed anymore. I also suggest calling this function only after the writer has been successfully created.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@picnixzpicnixzpicnixz left review comments

@ynir3ynir3ynir3 left review comments

@encukouencukouAwaiting requested review from encukou

@serhiy-storchakaserhiy-storchakaAwaiting requested review from serhiy-storchaka

@iritkatrieliritkatrielAwaiting requested review from iritkatrieliritkatriel will be requested when the pull request is marked ready for reviewiritkatriel is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@Yoav11@StanFromIreland@ynir3@picnixz@serhiy-storchaka@arhadthedev

[8]ページ先頭

©2009-2025 Movatter.jp