Skip to content

Commit cb764d9

Browse files
serhiy-storchakaynir3
authored andcommitted
bpo-29999: repr() of ImportError now contains attributes name and path.
1 parent 03017a8 commit cb764d9

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

Lib/test/test_exceptions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,36 @@ def after_with():
25952595
with ExitFails():
25962596
1/0
25972597
self.lineno_after_raise(after_with, 1, 1)
2598+
def test_repr(self):
2599+
exc = ImportError()
2600+
self.assertEqual(repr(exc), "ImportError()")
2601+
2602+
exc = ImportError('test')
2603+
self.assertEqual(repr(exc), "ImportError('test')")
2604+
2605+
exc = ImportError('test', 'case')
2606+
self.assertEqual(repr(exc), "ImportError('test', 'case')")
2607+
2608+
exc = ImportError(name='somemodule')
2609+
self.assertEqual(repr(exc), "ImportError(name='somemodule')")
2610+
2611+
exc = ImportError('test', name='somemodule')
2612+
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")
2613+
2614+
exc = ImportError(path='somepath')
2615+
self.assertEqual(repr(exc), "ImportError(path='somepath')")
2616+
2617+
exc = ImportError('test', path='somepath')
2618+
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")
2619+
2620+
exc = ImportError(name='somename', path='somepath')
2621+
self.assertEqual(repr(exc),
2622+
"ImportError(name='somename', path='somepath')")
2623+
2624+
exc = ImportError('test', name='somename', path='somepath')
2625+
self.assertEqual(repr(exc),
2626+
"ImportError('test', name='somename', path='somepath')")
2627+
25982628

25992629
if __name__ == '__main__':
26002630
unittest.main()

Objects/exceptions.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,30 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
18641864
return res;
18651865
}
18661866

1867+
static PyObject *
1868+
ImportError_repr(PyImportErrorObject *self)
1869+
{
1870+
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
1871+
PyObject *r = BaseException_repr((PyBaseExceptionObject *)self);
1872+
if (r && (self->name || self->path)) {
1873+
/* remove ')' */
1874+
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1));
1875+
if (r && self->name) {
1876+
Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R",
1877+
r, hasargs ? ", " : "", self->name));
1878+
hasargs = 1;
1879+
}
1880+
if (r && self->path) {
1881+
Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R",
1882+
r, hasargs ? ", " : "", self->path));
1883+
}
1884+
if (r) {
1885+
Py_SETREF(r, PyUnicode_FromFormat("%U)", r));
1886+
}
1887+
}
1888+
return r;
1889+
}
1890+
18671891
static PyMemberDef ImportError_members[] = {
18681892
{"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
18691893
PyDoc_STR("exception message")},
@@ -1881,12 +1905,23 @@ static PyMethodDef ImportError_methods[] = {
18811905
{NULL}
18821906
};
18831907

1884-
ComplexExtendsException(PyExc_Exception, ImportError,
1885-
ImportError, 0 /* new */,
1886-
ImportError_methods, ImportError_members,
1887-
0 /* getset */, ImportError_str,
1888-
"Import can't find module, or can't find name in "
1889-
"module.");
1908+
static PyTypeObject _PyExc_ImportError = {
1909+
PyVarObject_HEAD_INIT(NULL, 0)
1910+
"ImportError",
1911+
sizeof(PyImportErrorObject), 0,
1912+
(destructor)ImportError_dealloc, 0, 0, 0, 0,
1913+
(reprfunc)ImportError_repr, 0, 0, 0, 0, 0,
1914+
(reprfunc)ImportError_str, 0, 0, 0,
1915+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1916+
PyDoc_STR("Import can't find module, or can't find name in "
1917+
"module."),
1918+
(traverseproc)ImportError_traverse,
1919+
(inquiry)ImportError_clear, 0, 0, 0, 0, ImportError_methods,
1920+
ImportError_members, 0, &_PyExc_Exception,
1921+
0, 0, 0, offsetof(PyImportErrorObject, dict),
1922+
(initproc)ImportError_init,
1923+
};
1924+
PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError;
18901925

18911926
/*
18921927
* ModuleNotFoundError extends ImportError

0 commit comments

Comments
 (0)