-
-
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?
Changes from 13 commits
cb764d9
db41c7d
1caeb91
7f29270
c80e056
126be20
4a97f5c
852da8c
6ab7362
12c2293
a53da37
5b6d626
951e0a1
8fbfd18
458bcd0
fa2f884
35a6a29
6e416c6
aa75f6a
8b5e4ed
28545e5
4f63193
62a479b
25f3d42
a45ed75
69e7305
9c09cd0
ca0e2a4
b0d7f4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:meth:`repr` of :class:`ImportError` now contains attributes name and path. | ||
Patch by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1864,6 +1864,32 @@ 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 commentThe 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 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 commentThe 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 commentThe 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 commentThe 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) |
||
{ | ||
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0; | ||
PyObject *r = BaseException_repr(self); | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyImportErrorObject *exc = PyImportErrorObject_CAST(self); | ||
|
||
if (r && (exc->name || exc->path)) { | ||
/* remove ')' */ | ||
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1)); | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (r && exc->name) { | ||
Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R", | ||
r, hasargs ? ", " : "", exc->name)); | ||
hasargs = 1; | ||
} | ||
if (r && exc->path) { | ||
Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R", | ||
r, hasargs ? ", " : "", exc->path)); | ||
} | ||
if (r) { | ||
Py_SETREF(r, PyUnicode_FromFormat("%U)", r)); | ||
} | ||
} | ||
return r; | ||
} | ||
|
||
static PyMemberDef ImportError_members[] = { | ||
{"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0, | ||
PyDoc_STR("exception message")}, | ||
|
@@ -1881,12 +1907,23 @@ static PyMethodDef ImportError_methods[] = { | |
{NULL} | ||
}; | ||
|
||
ComplexExtendsException(PyExc_Exception, ImportError, | ||
ImportError, 0 /* new */, | ||
ImportError_methods, ImportError_members, | ||
0 /* getset */, ImportError_str, | ||
"Import can't find module, or can't find name in " | ||
"module."); | ||
static PyTypeObject _PyExc_ImportError = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
"ImportError", | ||
sizeof(PyImportErrorObject), 0, | ||
(destructor)ImportError_dealloc, 0, 0, 0, 0, | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ImportError_repr, 0, 0, 0, 0, 0, | ||
ImportError_str, 0, 0, 0, | ||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, | ||
PyDoc_STR("Import can't find module, or can't find name in " | ||
"module."), | ||
(traverseproc)ImportError_traverse, | ||
(inquiry)ImportError_clear, 0, 0, 0, 0, ImportError_methods, | ||
ImportError_members, 0, &_PyExc_Exception, | ||
0, 0, 0, offsetof(PyImportErrorObject, dict), | ||
(initproc)ImportError_init, | ||
}; | ||
PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError; | ||
|
||
/* | ||
* ModuleNotFoundError extends ImportError | ||
|
Uh oh!
There was an error while loading. Please reload this page.