Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 389ef9d

Browse files
Issue #21578: Fixed misleading error message when ImportError called with
invalid keyword args.
2 parents a12e784 + 47dee11 commit 389ef9d

3 files changed

Lines changed: 43 additions & 22 deletions

File tree

Lib/test/test_exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,22 @@ def test_attributes(self):
10961096
self.assertEqual(exc.name, 'somename')
10971097
self.assertEqual(exc.path, 'somepath')
10981098

1099+
msg = "'invalid' is an invalid keyword argument for this function"
1100+
with self.assertRaisesRegex(TypeError, msg):
1101+
ImportError('test', invalid='keyword')
1102+
1103+
with self.assertRaisesRegex(TypeError, msg):
1104+
ImportError('test', name='name', invalid='keyword')
1105+
1106+
with self.assertRaisesRegex(TypeError, msg):
1107+
ImportError('test', path='path', invalid='keyword')
1108+
1109+
with self.assertRaisesRegex(TypeError, msg):
1110+
ImportError(invalid='keyword')
1111+
1112+
with self.assertRaisesRegex(TypeError, msg):
1113+
ImportError('test', invalid='keyword', another=True)
1114+
10991115
def test_non_str_argument(self):
11001116
# Issue #15778
11011117
with check_warnings(('', BytesWarning), quiet=True):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 beta 2
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #21578: Fixed misleading error message when ImportError called with
14+
invalid keyword args.
15+
1316
- Issue #28203: Fix incorrect type in complex(1.0, {2:3}) error message.
1417
Patch by Soumya Sharma.
1518

Objects/exceptions.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -612,36 +612,38 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
612612
static int
613613
ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
614614
{
615+
static char *kwlist[] = {"name", "path", 0};
616+
PyObject *empty_tuple;
615617
PyObject *msg = NULL;
616618
PyObject *name = NULL;
617619
PyObject *path = NULL;
618620

619-
/* Macro replacement doesn't allow ## to start the first line of a macro,
620-
so we move the assignment and NULL check into the if-statement. */
621-
#define GET_KWD(kwd) { \
622-
kwd = PyDict_GetItemString(kwds, #kwd); \
623-
if (kwd) { \
624-
Py_INCREF(kwd); \
625-
Py_XSETREF(self->kwd, kwd); \
626-
if (PyDict_DelItemString(kwds, #kwd)) \
627-
return -1; \
628-
} \
629-
}
630-
631-
if (kwds) {
632-
GET_KWD(name);
633-
GET_KWD(path);
634-
}
621+
if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
622+
return -1;
635623

636-
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
624+
empty_tuple = PyTuple_New(0);
625+
if (!empty_tuple)
637626
return -1;
638-
if (PyTuple_GET_SIZE(args) != 1)
639-
return 0;
640-
if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
627+
if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
628+
&name, &path)) {
629+
Py_DECREF(empty_tuple);
641630
return -1;
631+
}
632+
Py_DECREF(empty_tuple);
642633

643-
Py_INCREF(msg);
644-
Py_XSETREF(self->msg, msg);
634+
if (name) {
635+
Py_INCREF(name);
636+
Py_XSETREF(self->name, name);
637+
}
638+
if (path) {
639+
Py_INCREF(path);
640+
Py_XSETREF(self->path, path);
641+
}
642+
if (PyTuple_GET_SIZE(args) == 1) {
643+
msg = PyTuple_GET_ITEM(args, 0);
644+
Py_INCREF(msg);
645+
Py_XSETREF(self->msg, msg);
646+
}
645647

646648
return 0;
647649
}

0 commit comments

Comments
 (0)