- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.3k
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
Changes from 29 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
              3e57fba
              3a30ef3
              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,4 @@ | ||
| 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. | ||
| Patch by Serhiy Storchaka, Oleg Iarygin, and Yoav Nir | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -1864,6 +1864,59 @@ 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; | ||
| PyImportErrorObject *exc = PyImportErrorObject_CAST(self); | ||
| PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); | ||
|         
                  Yoav11 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| if (writer == NULL) { | ||
| goto error; | ||
| } | ||
| PyObject *r = BaseException_repr(self); | ||
| if (r == NULL) { | ||
| goto error; | ||
| } | ||
| if (PyUnicodeWriter_WriteSubstring( | ||
| writer, r, 0, PyUnicode_GET_LENGTH(r) - 1) < 0) | ||
| { | ||
| Py_XDECREF(r); | ||
|         
                  Yoav11 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| goto error; | ||
| } | ||
| Py_XDECREF(r); | ||
|         
                  Yoav11 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| if (exc->name) { | ||
| if (hasargs) { | ||
| if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) { | ||
| goto error; | ||
| } | ||
| } | ||
| if (PyUnicodeWriter_Format(writer, "name=%R", exc->name) < 0) { | ||
| goto error; | ||
| } | ||
| hasargs = 1; | ||
| } | ||
| if (exc->path) { | ||
| if (hasargs) { | ||
| if (PyUnicodeWriter_WriteASCII(writer, ", ", 2) < 0) { | ||
| goto error; | ||
| } | ||
| } | ||
| if (PyUnicodeWriter_Format(writer, "path=%R", exc->path) < 0) { | ||
| goto error; | ||
| } | ||
| } | ||
|  | ||
| if (PyUnicodeWriter_WriteChar(writer, ')') < 0) { | ||
| goto error; | ||
| } | ||
|  | ||
| return PyUnicodeWriter_Finish(writer); | ||
|  | ||
| error: | ||
| PyUnicodeWriter_Discard(writer); | ||
| return NULL; | ||
| } | ||
|  | ||
| static PyMemberDef ImportError_members[] = { | ||
| {"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0, | ||
| PyDoc_STR("exception message")}, | ||
|  | @@ -1881,12 +1934,26 @@ 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) | ||
| .tp_name = "ImportError", | ||
| .tp_basicsize = sizeof(PyImportErrorObject), | ||
| .tp_dealloc = ImportError_dealloc, | ||
| .tp_repr = ImportError_repr, | ||
| .tp_str = ImportError_str, | ||
| .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, | ||
| .tp_doc = PyDoc_STR( | ||
| "Import can't find module, " | ||
| "or can't find name in module."), | ||
| .tp_traverse = ImportError_traverse, | ||
| .tp_clear = ImportError_clear, | ||
| .tp_methods = ImportError_methods, | ||
| .tp_members = ImportError_members, | ||
| .tp_base = &_PyExc_Exception, | ||
| .tp_dictoffset = offsetof(PyImportErrorObject, dict), | ||
| .tp_init = ImportError_init, | ||
| }; | ||
| PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError; | ||
|  | ||
| /* | ||
| * ModuleNotFoundError extends ImportError | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.