classification
Title: stringification of subclasses of OSError can cause crash
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, python-dev, sbt
Priority: high Keywords:

Created on 2012-06-30 20:09 by sbt, last changed 2012-06-30 21:39 by pitrou. This issue is now closed.

Messages (4)
msg164423 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-06-30 20:09
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.
msg164426 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-06-30 20:46
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 */
+    Py_CLEAR(self->args);
     self->args = args;
     args = NULL;
 
@@ -916,6 +917,11 @@ OSError_new(PyTypeObject *type, PyObject
             ))
             goto error;
     }
+    else {
+        self->args = PyTuple_New(0);
+        if (self->args == NULL)
+            goto error;
+    }
 
     return (PyObject *) self;
msg164427 - (view) Author: Roundup Robot (python-dev) Date: 2012-06-30 21:38
New changeset 1cbab581bf1e by Antoine Pitrou in branch 'default':
Issue #15229: An OSError subclass whose __init__ doesn't call back
http://hg.python.org/cpython/rev/1cbab581bf1e
msg164428 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-06-30 21:39
It should be fixed now.
History
Date User Action Args
2012-06-30 21:39:08pitrousetstatus: open -> closed
resolution: fixed
messages: + msg164428

stage: needs patch -> resolved
2012-06-30 21:38:49python-devsetnosy: + python-dev
messages: + msg164427
2012-06-30 20:46:12pitrousetmessages: + msg164426
2012-06-30 20:30:46pitrousetpriority: normal -> high
nosy: + pitrou
2012-06-30 20:09:41sbtcreate