]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15229: An OSError subclass whose __init__ doesn't call back
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Jun 2012 21:37:47 +0000 (23:37 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Jun 2012 21:37:47 +0000 (23:37 +0200)
OSError.__init__ could produce incomplete instances, leading to crashes
when calling str() on them.

Lib/test/test_pep3151.py
Misc/NEWS
Objects/exceptions.c

index 8af9e0c5f650b5c32a99e03003dcd10be262e165..2792c10f9892c68e369ee6d8645e02444b9e902d 100644 (file)
@@ -29,6 +29,10 @@ class SubOSErrorCombinedInitFirst(SubOSErrorWithInit, SubOSErrorWithNew):
 class SubOSErrorCombinedNewFirst(SubOSErrorWithNew, SubOSErrorWithInit):
     pass
 
+class SubOSErrorWithStandaloneInit(OSError):
+    def __init__(self):
+        pass
+
 
 class HierarchyTest(unittest.TestCase):
 
@@ -193,6 +197,12 @@ class ExplicitSubclassingTest(unittest.TestCase):
         self.assertEqual(e.baz, "baz")
         self.assertEqual(e.args, ("some message",))
 
+    def test_init_standalone(self):
+        # __init__ doesn't propagate to OSError.__init__ (see issue #15229)
+        e = SubOSErrorWithStandaloneInit()
+        self.assertEqual(e.args, ())
+        self.assertEqual(str(e), '')
+
 
 def test_main():
     support.run_unittest(__name__)
index bca959b550b9f9e0c975a2f97edafe563d80c289..44a3d14db9f2a5b2e0c9f75281fd23c809fca48d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
 Core and Builtins
 -----------------
 
+- Issue #15229: An OSError subclass whose __init__ doesn't call back
+  OSError.__init__ could produce incomplete instances, leading to crashes
+  when calling str() on them.
 
 Library
 -------
index f70669849994e47a3a3008b765f456d079a29651..5c85f10444a00dea2650d99120060882b7a6be4a 100644 (file)
@@ -834,6 +834,7 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
 #endif
 
     /* Steals the reference to args */
+    Py_CLEAR(self->args);
     self->args = args;
     args = NULL;
 
@@ -916,6 +917,11 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
             ))
             goto error;
     }
+    else {
+        self->args = PyTuple_New(0);
+        if (self->args == NULL)
+            goto error;
+    }
 
     return (PyObject *) self;