-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
base: main
Are you sure you want to change the base?
Conversation
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 |
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 |
You need to sign the CLA. Requesting @encukou who wanted to finish this per #1011 (comment) |
Objects/exceptions.c
Outdated
|
||
if (r && (exc->name || exc->path)) { | ||
/* remove ')' */ | ||
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1)); |
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.
Let's use PyUnicodeWriter
instead to avoids re-creating strings:
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) goto error;
if (PyUnicodeWriter_WriteSubstring(writer, r, 0, l - 1) < 0) goto error;
if (exc->name) {
if (hasargs) {
if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) goto error;
}
...
}
...
return PyUnicodeWriter_Finish(writer);
error: /* cleanup */
Objects/exceptions.c
Outdated
PyObject *r = BaseException_repr(self); | ||
PyImportErrorObject *exc = PyImportErrorObject_CAST(self); | ||
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); | ||
if (writer == NULL) goto error; |
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.
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.
@picnixz are you referring to braces are required everywhere, even where C permits them to be omitted
?
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.
Yes
Objects/exceptions.c
Outdated
if (PyUnicodeWriter_WriteASCII(writer, "name='", 6) < 0) goto error; | ||
if (PyUnicodeWriter_WriteSubstring(writer, exc->name, 0, PyUnicode_GET_LENGTH(exc->name)) < 0) goto error; | ||
if (PyUnicodeWriter_WriteASCII(writer, "'", 1) < 0) goto error; |
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.
For this one, as you're using the entire "exc->name", you can just use PyUnicodeWriter_Format("name=%R", exc->name")
instead.
Objects/exceptions.c
Outdated
error: | ||
Py_XDECREF(r); | ||
PyUnicodeWriter_Discard(writer); | ||
return NULL; |
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.
error: | |
Py_XDECREF(r); | |
PyUnicodeWriter_Discard(writer); | |
return NULL; | |
error: | |
Py_XDECREF(r); | |
PyUnicodeWriter_Discard(writer); | |
return NULL; |
Lib/test/test_exceptions.py
Outdated
try: | ||
import does_not_exist # type: ignore[import] # noqa: F401 | ||
except ModuleNotFoundError as 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.
You can use with self.assertRaises(ImportError) as cm
and check cm.exception
attributes.
@@ -1864,6 +1864,41 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) | |||
return res; | |||
} | |||
|
|||
static PyObject * | |||
ImportError_repr(PyObject *self) |
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.
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 generic repr
helper for that which takes into account them, something like:
static PyObject *
repr_with_keywords(PyObject *exc, const char * const *kwlist)
{
/* format using kwlist[i]=getattr(exc, kwlist[i])
* with kwlist NULL-terminated */
}
Its usage would be
static char *kwlist[] = {"name", "path", NULL};
repr_with_keywords(exc, kwlist)
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.
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.
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.
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.
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.
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)
@picnixz any more comments ? :) |
Objects/exceptions.c
Outdated
goto error; | ||
} | ||
} | ||
if (PyUnicodeWriter_WriteASCII(writer, ")", 1) < 0) goto error; |
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.
Objects/exceptions.c
Outdated
if (writer == NULL) { | ||
goto error; | ||
} | ||
if (PyUnicodeWriter_WriteSubstring(writer, r, 0, PyUnicode_GET_LENGTH(r)-1) < 0) { |
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.
Plese wrap lines under 80 chars.
Objects/exceptions.c
Outdated
ImportError_repr(PyObject *self) | ||
{ | ||
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0; | ||
PyObject *r = BaseException_repr(self); |
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.
This needs to be decrefed after it's been used. By doing so, you can also remove the Py_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.
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.
yup makes sense, thanks!
This PR was created during Europython 2025's spring weekend.
This is a patch on #1011. authored by serhiy-storchaka and arhadthedev