diff -r dad879edefd2 Lib/test/test_exceptions.py --- a/Lib/test/test_exceptions.py Tue Sep 27 22:03:51 2016 +0300 +++ b/Lib/test/test_exceptions.py Tue Sep 27 23:45:52 2016 +0300 @@ -1113,6 +1113,20 @@ class ImportErrorTests(unittest.TestCase with self.assertRaisesRegex(TypeError, msg): ImportError('test', invalid='keyword', another=True) + def test_reset_attributes(self): + exc = ImportError('test', name='name', path='path') + self.assertEqual(exc.args, ('test',)) + self.assertEqual(exc.msg, 'test') + self.assertEqual(exc.name, 'name') + self.assertEqual(exc.path, 'path') + + # Reset not specified attributes + exc.__init__() + self.assertEqual(exc.args, ()) + self.assertEqual(exc.msg, None) + self.assertEqual(exc.name, None) + self.assertEqual(exc.path, None) + def test_non_str_argument(self): # Issue #15778 with check_warnings(('', BytesWarning), quiet=True): diff -r dad879edefd2 Objects/exceptions.c --- a/Objects/exceptions.c Tue Sep 27 22:03:51 2016 +0300 +++ b/Objects/exceptions.c Tue Sep 27 23:45:52 2016 +0300 @@ -637,19 +637,17 @@ ImportError_init(PyImportErrorObject *se } Py_DECREF(empty_tuple); - if (name) { - Py_INCREF(name); - Py_XSETREF(self->name, name); - } - if (path) { - Py_INCREF(path); - Py_XSETREF(self->path, path); - } + Py_XINCREF(name); + Py_XSETREF(self->name, name); + + Py_XINCREF(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); } + Py_XSETREF(self->msg, msg); return 0; }