Issue 15229: stringification of subclasses of OSError can cause crash (original) (raw)

If you subclass OSError without calling OSError.init() then you can get a crash. For example

Python 3.3.0b1 (default:cfbe51e66749, Jun 30 2012, 20:50:54) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

class MyError(OSError): ... def init(self): ... pass ... [61116 refs] repr(MyError()) Assertion failed: obj, file ..\Objects\unicodeobject.c, line 2646

Note that str(MyError()) results in a crash without hitting a failed assertion.

The problem does not occur in Python 3.2, and it does not affect subclasses of Exception.

Here is a quick patch (needs a test):

diff --git a/Objects/exceptions.c b/Objects/exceptions.c --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -834,6 +834,7 @@ oserror_init(PyOSErrorObject *self, PyOb #endif

 /* Steals the reference to args */

@@ -916,6 +917,11 @@ OSError_new(PyTypeObject *type, PyObject )) goto error; }