Skip to content

Commit f87289b

Browse files
committed
Issue python#15229: An OSError subclass whose __init__ doesn't call back
OSError.__init__ could produce incomplete instances, leading to crashes when calling str() on them.
1 parent 4c99071 commit f87289b

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎Lib/test/test_pep3151.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class SubOSErrorCombinedInitFirst(SubOSErrorWithInit, SubOSErrorWithNew):
2929
class SubOSErrorCombinedNewFirst(SubOSErrorWithNew, SubOSErrorWithInit):
3030
pass
3131

32+
class SubOSErrorWithStandaloneInit(OSError):
33+
def __init__(self):
34+
pass
35+
3236

3337
class HierarchyTest(unittest.TestCase):
3438

@@ -193,6 +197,12 @@ def test_init_new_overriden(self):
193197
self.assertEqual(e.baz, "baz")
194198
self.assertEqual(e.args, ("some message",))
195199

200+
def test_init_standalone(self):
201+
# __init__ doesn't propagate to OSError.__init__ (see issue #15229)
202+
e = SubOSErrorWithStandaloneInit()
203+
self.assertEqual(e.args, ())
204+
self.assertEqual(str(e), '')
205+
196206

197207
def test_main():
198208
support.run_unittest(__name__)

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15229: An OSError subclass whose __init__ doesn't call back
14+
OSError.__init__ could produce incomplete instances, leading to crashes
15+
when calling str() on them.
1316

1417
Library
1518
-------

‎Objects/exceptions.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
834834
#endif
835835

836836
/* Steals the reference to args */
837+
Py_CLEAR(self->args);
837838
self->args = args;
838839
args = NULL;
839840

@@ -916,6 +917,11 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
916917
))
917918
goto error;
918919
}
920+
else {
921+
self->args = PyTuple_New(0);
922+
if (self->args == NULL)
923+
goto error;
924+
}
919925

920926
return (PyObject *) self;
921927

0 commit comments

Comments
 (0)