summaryrefslogtreecommitdiffstats
path: root/Objects/exceptions.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-09-27 17:45:35 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-09-27 17:45:35 (GMT)
commit47dee11ba76a12d22277562b9ccea51259a5ecc0 (patch)
tree949a4eaa4034f1ed15e84e46178b666d07cff6e2 /Objects/exceptions.c
parentc0b7037d4fc0f85af858cfa56df4dca25fb8896f (diff)
downloadcpython-47dee11ba76a12d22277562b9ccea51259a5ecc0.zip
cpython-47dee11ba76a12d22277562b9ccea51259a5ecc0.tar.gz
cpython-47dee11ba76a12d22277562b9ccea51259a5ecc0.tar.bz2
Issue #21578: Fixed misleading error message when ImportError called with
invalid keyword args.
Diffstat (limited to 'Objects/exceptions.c')
-rw-r--r--Objects/exceptions.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 0749e90..981ead2 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -618,36 +618,38 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
static int
ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
{
+ static char *kwlist[] = {"name", "path", 0};
+ PyObject *empty_tuple;
PyObject *msg = NULL;
PyObject *name = NULL;
PyObject *path = NULL;
-/* Macro replacement doesn't allow ## to start the first line of a macro,
- so we move the assignment and NULL check into the if-statement. */
-#define GET_KWD(kwd) { \
- kwd = PyDict_GetItemString(kwds, #kwd); \
- if (kwd) { \
- Py_INCREF(kwd); \
- Py_XSETREF(self->kwd, kwd); \
- if (PyDict_DelItemString(kwds, #kwd)) \
- return -1; \
- } \
- }
-
- if (kwds) {
- GET_KWD(name);
- GET_KWD(path);
- }
+ if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
+ return -1;
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
+ empty_tuple = PyTuple_New(0);
+ if (!empty_tuple)
return -1;
- if (PyTuple_GET_SIZE(args) != 1)
- return 0;
- if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
+ if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
+ &name, &path)) {
+ Py_DECREF(empty_tuple);
return -1;
+ }
+ Py_DECREF(empty_tuple);
- Py_INCREF(msg);
- Py_XSETREF(self->msg, msg);
+ if (name) {
+ Py_INCREF(name);
+ Py_XSETREF(self->name, name);
+ }
+ if (path) {
+ Py_INCREF(path);
+ Py_XSETREF(self->path, path);
+ }
+ if (PyTuple_GET_SIZE(args) == 1) {
+ msg = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(msg);
+ Py_XSETREF(self->msg, msg);
+ }
return 0;
}