[Python-checkins] cpython (merge 3.5 -> 3.6): Issue #21578: Fixed misleading error message when ImportError called with
serhiy.storchaka
python-checkins at python.org
Tue Sep 27 13:52:18 EDT 2016
https://hg.python.org/cpython/rev/95549f4970d0
changeset: 104104:95549f4970d0
branch: 3.6
parent: 104101:52f8eb2fa6a6
parent: 104103:9b8f0db1944f
user: Serhiy Storchaka <storchaka at gmail.com>
date: Tue Sep 27 20:51:04 2016 +0300
summary:
Issue #21578: Fixed misleading error message when ImportError called with
invalid keyword args.
files:
Lib/test/test_exceptions.py | 16 ++++++++
Misc/NEWS | 3 +
Objects/exceptions.c | 50 +++++++++++++------------
3 files changed, 45 insertions(+), 24 deletions(-)
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1096,6 +1096,22 @@
self.assertEqual(exc.name, 'somename')
self.assertEqual(exc.path, 'somepath')
+ msg = "'invalid' is an invalid keyword argument for this function"
+ with self.assertRaisesRegex(TypeError, msg):
+ ImportError('test', invalid='keyword')
+
+ with self.assertRaisesRegex(TypeError, msg):
+ ImportError('test', name='name', invalid='keyword')
+
+ with self.assertRaisesRegex(TypeError, msg):
+ ImportError('test', path='path', invalid='keyword')
+
+ with self.assertRaisesRegex(TypeError, msg):
+ ImportError(invalid='keyword')
+
+ with self.assertRaisesRegex(TypeError, msg):
+ ImportError('test', invalid='keyword', another=True)
+
def test_non_str_argument(self):
# Issue #15778
with check_warnings(('', BytesWarning), quiet=True):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #21578: Fixed misleading error message when ImportError called with
+ invalid keyword args.
+
- Issue #28203: Fix incorrect type in complex(1.0, {2:3}) error message.
Patch by Soumya Sharma.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -612,36 +612,38 @@
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 (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
+ return -1;
+
+ empty_tuple = PyTuple_New(0);
+ if (!empty_tuple)
+ return -1;
+ if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
+ &name, &path)) {
+ Py_DECREF(empty_tuple);
+ return -1;
}
-
- if (kwds) {
- GET_KWD(name);
- GET_KWD(path);
+ Py_DECREF(empty_tuple);
+
+ if (name) {
+ Py_INCREF(name);
+ Py_XSETREF(self->name, name);
}
-
- if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
- return -1;
- if (PyTuple_GET_SIZE(args) != 1)
- return 0;
- if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
- return -1;
-
- Py_INCREF(msg);
- Py_XSETREF(self->msg, msg);
+ 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;
}
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list