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

Skip to content

Commit 47dee11

Browse files
Issue #21578: Fixed misleading error message when ImportError called with
invalid keyword args.
1 parent c0b7037 commit 47dee11

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

Lib/test/test_exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,23 @@ 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+
msg = "'invalid|another' is an invalid keyword argument for this function"
1113+
with self.assertRaisesRegex(TypeError, msg):
1114+
ImportError('test', invalid='keyword', another=True)
1115+
10991116
def test_non_str_argument(self):
11001117
# Issue #15778
11011118
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 @@ Release date: TBA
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 error message from
1417
``complex(1.0, {2:3})``. Patch by Soumya Sharma.
1518

Objects/exceptions.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -618,36 +618,38 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
618618
static int
619619
ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
620620
{
621+
static char *kwlist[] = {"name", "path", 0};
622+
PyObject *empty_tuple;
621623
PyObject *msg = NULL;
622624
PyObject *name = NULL;
623625
PyObject *path = NULL;
624626

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

642-
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
630+
empty_tuple = PyTuple_New(0);
631+
if (!empty_tuple)
643632
return -1;
644-
if (PyTuple_GET_SIZE(args) != 1)
645-
return 0;
646-
if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
633+
if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
634+
&name, &path)) {
635+
Py_DECREF(empty_tuple);
647636
return -1;
637+
}
638+
Py_DECREF(empty_tuple);
648639

649-
Py_INCREF(msg);
650-
Py_XSETREF(self->msg, msg);
640+
if (name) {
641+
Py_INCREF(name);
642+
Py_XSETREF(self->name, name);
643+
}
644+
if (path) {
645+
Py_INCREF(path);
646+
Py_XSETREF(self->path, path);
647+
}
648+
if (PyTuple_GET_SIZE(args) == 1) {
649+
msg = PyTuple_GET_ITEM(args, 0);
650+
Py_INCREF(msg);
651+
Py_XSETREF(self->msg, msg);
652+
}
651653

652654
return 0;
653655
}

0 commit comments

Comments
 (0)