[Python-checkins] cpython: Issue #28289: ImportError.__init__ now resets not specified attributes.

serhiy.storchaka python-checkins at python.org
Thu Sep 29 13:48:07 EDT 2016


https://hg.python.org/cpython/rev/dcb39d3ba67a
changeset:   104161:dcb39d3ba67a
parent:      104159:a2c5179bce01
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Sep 28 07:53:32 2016 +0300
summary:
  Issue #28289: ImportError.__init__ now resets not specified attributes.

files:
  Lib/test/test_exceptions.py |  14 ++++++++++++++
  Misc/NEWS                   |   2 ++
  Objects/exceptions.c        |  16 +++++++---------
  3 files changed, 23 insertions(+), 9 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
@@ -1112,6 +1112,20 @@
         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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 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.
 
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -631,19 +631,17 @@
     }
     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;
 }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list