8000 bpo-29999: repr() of ImportError now contains attributes name and path. · python/cpython@025c980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 025c980

Browse files
bpo-29999: repr() of ImportError now contains attributes name and path.
1 parent a0ce375 commit 025c980

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

Lib/test/test_exceptions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,36 @@ def test_non_str_argument(self):
11261126
exc = ImportError(arg)
11271127
self.assertEqual(str(arg), str(exc))
11281128

1129+
def test_repr(self):
1130+
exc = ImportError()
1131+
self.assertEqual(repr(exc), "ImportError()")
1132+
1133+
exc = ImportError('test')
1134+
self.assertEqual(repr(exc), "ImportError('test')")
1135+
1136+
exc = ImportError('test', 'case')
1137+
self.assertEqual(repr(exc), "ImportError('test', 'case')")
1138+
1139+
exc = ImportError(name='somemodule')
1140+
self.assertEqual(repr(exc), "ImportError(name='somemodule')")
1141+
1142+
exc = ImportError('test', name='somemodule')
1143+
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")
1144+
1145+
exc = ImportError(path='somepath')
1146+
self.assertEqual(repr(exc), "ImportError(path='somepath')")
1147+
1148+
exc = ImportError('test', path='somepath')
1149+
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")
1150+
1151+
exc = ImportError(name='somename', path='somepath')
1152+
self.assertEqual(repr(exc),
1153+
"ImportError(name='somename', path='somepath')")
1154+
1155+
exc = ImportError('test', name='somename', path='somepath')
1156+
self.assertEqual(repr(exc),
1157+
"ImportError('test', name='somename', path='somepath')")
1158+
11291159

11301160
if __name__ == '__main__':
11311161
unittest.main()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29999: repr() of ImportError now contains attributes name and path.
14+
1315
- bpo-29949: Fix memory usage regression of set and frozenset object.
1416

1517
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque

Objects/exceptions.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ BaseException_repr(PyBaseExceptionObject *self)
121121
dot = (const char *) strrchr(name, '.');
122122
if (dot != NULL) name = dot+1;
123123

124-
return PyUnicode_FromFormat("%s%R", name, self->args);
124+
if (PyTuple_GET_SIZE(self->args) == 1)
125+
return PyUnicode_FromFormat("%s(%R)", name,
126+
PyTuple_GET_ITEM(self->args, 0));
127+
else
128+
return PyUnicode_FromFormat("%s%R", name, self->args);
125129
}
126130

127131
/* Pickling support */
@@ -682,6 +686,30 @@ ImportError_str(PyImportErrorObject *self)
682686
}
683687
}
684688

689+
static PyObject *
690+
ImportError_repr(PyImportErrorObject *self)
691+
{
692+
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
693+
PyObject *r = BaseException_repr((PyBaseExceptionObject *)self);
694+
if (r && (self->name || self->path)) {
695+
/* remove ')' */
696+
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1));
697+
if (r && self->name) {
698+
Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R",
699+
r, hasargs ? ", " : "", self->name));
700+
hasargs = 1;
701+
}
702+
if (r && self->path) {
703+
Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R",
704+
r, hasargs ? ", " : "", self->path));
705+
}
706+
if (r) {
707+
Py_SETREF(r, PyUnicode_FromFormat("%U)", r));
708+
}
709+
}
710+
return r;
711+
}
712+
685713
static PyMemberDef ImportError_members[] = {
686714
{"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
687715
PyDoc_STR("exception message")},
@@ -696,12 +724,23 @@ static PyMethodDef ImportError_methods[] = {
696724
{NULL}
697725
};
698726

699-
ComplexExtendsException(PyExc_Exception, ImportError,
700-
ImportError, 0 /* new */,
701-
ImportError_methods, ImportError_members,
702-
0 /* getset */, ImportError_str,
703-
"Import can't find module, or can't find name in "
704-
"module.");
727+
static PyTypeObject _PyExc_ImportError = {
728+
PyVarObject_HEAD_INIT(NULL, 0)
729+
"ImportError",
730+
sizeof(PyImportErrorObject), 0,
731+
(destructor)ImportError_dealloc, 0, 0, 0, 0,
732+
(reprfunc)ImportError_repr, 0, 0, 0, 0, 0,
733+
(reprfunc)ImportError_str, 0, 0, 0,
734+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
735+
PyDoc_STR("Import can't find module, or can't find name in "
736+
"module."),
737+
(traverseproc)ImportError_traverse,
738+
(inquiry)ImportError_clear, 0, 0, 0, 0, ImportError_methods,
739+
ImportError_members, 0, &_PyExc_Exception,
740+
0, 0, 0, offsetof(PyImportErrorObject, dict),
741+
(initproc)ImportError_init,
742+
};
743+
PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError;
705744

706745
/*
707746
* ModuleNotFoundError extends ImportError

0 commit comments

Comments
 (0)
0