]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #28289: ImportError.__init__ now resets not specified attributes.
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 28 Sep 2016 04:53:32 +0000 (07:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 28 Sep 2016 04:53:32 +0000 (07:53 +0300)
Lib/test/test_exceptions.py
Misc/NEWS
Objects/exceptions.c

index 48379222c3765920fbe727cdb8683077c1955655..34265a517f2caf86d6f30d0eb9e2a3f219b3707b 100644 (file)
@@ -1112,6 +1112,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):
index c733eba4732609154a65370f151bab58d74e6dc4..8938b863acd491a95b595231dc01b88f536c6fc0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #28289: ImportError.__init__ now resets not specified attributes.
+
 - Issue #21578: Fixed misleading error message when ImportError called with
   invalid keyword args.
 
index f63f06a145bcb151944e8e0dccc9d8b89f1c2ab2..bc0264a5e9577c208a5bd07020cb3b4cf2d66283 100644 (file)
@@ -631,19 +631,17 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
     }
     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;
 }