From f87289bb58e71f5df73cbd594cbc0c0515e91c4b Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 30 Jun 2012 23:37:47 +0200 Subject: 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. --- Lib/test/test_pep3151.py | 10 ++++++++++ Misc/NEWS | 3 +++ Objects/exceptions.c | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/Lib/test/test_pep3151.py b/Lib/test/test_pep3151.py index 8af9e0c..2792c10 100644 --- a/Lib/test/test_pep3151.py +++ b/Lib/test/test_pep3151.py @@ -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__) diff --git a/Misc/NEWS b/Misc/NEWS index bca959b..44a3d14 100644 --- 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 ------- diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f706698..5c85f10 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -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; -- cgit v0.12