[Python-3000-checkins] r57886 - in python/branches/py3k: Lib/test/test_raise.py Objects/exceptions.c Python/ceval.c

collin.winter python-3000-checkins at python.org
Sat Sep 1 22:26:44 CEST 2007


Author: collin.winter
Date: Sat Sep  1 22:26:44 2007
New Revision: 57886

Modified:
   python/branches/py3k/Lib/test/test_raise.py
   python/branches/py3k/Objects/exceptions.c
   python/branches/py3k/Python/ceval.c
Log:
Fix refleaks exposed by test_raise.

Modified: python/branches/py3k/Lib/test/test_raise.py
==============================================================================
--- python/branches/py3k/Lib/test/test_raise.py	(original)
+++ python/branches/py3k/Lib/test/test_raise.py	Sat Sep  1 22:26:44 2007
@@ -37,6 +37,18 @@
         else:
             self.fail("No exception raised")
 
+    def test_erroneous_exception(self):
+        class MyException(Exception):
+            def __init__(self):
+                raise RuntimeError()
+
+        try:
+            raise MyException
+        except RuntimeError:
+            pass
+        else:
+            self.fail("No exception raised")
+
 
 class TestCause(unittest.TestCase):
     def test_invalid_cause(self):
@@ -64,6 +76,18 @@
         else:
             self.fail("No exception raised")
 
+    def test_erroneous_cause(self):
+        class MyException(Exception):
+            def __init__(self):
+                raise RuntimeError()
+
+        try:
+            raise IndexError from MyException
+        except RuntimeError:
+            pass
+        else:
+            self.fail("No exception raised")
+
 
 class TestTraceback(unittest.TestCase):
     def test_sets_traceback(self):

Modified: python/branches/py3k/Objects/exceptions.c
==============================================================================
--- python/branches/py3k/Objects/exceptions.c	(original)
+++ python/branches/py3k/Objects/exceptions.c	Sat Sep  1 22:26:44 2007
@@ -28,7 +28,7 @@
         return NULL;
     /* the dict is created on the fly in PyObject_GenericSetAttr */
     self->dict = NULL;
-    self->traceback = NULL;
+    self->traceback = self->cause = self->context = NULL;
 
     self->args = PyTuple_New(0);
     if (!self->args) {
@@ -58,6 +58,8 @@
     Py_CLEAR(self->dict);
     Py_CLEAR(self->args);
     Py_CLEAR(self->traceback);
+    Py_CLEAR(self->cause);
+    Py_CLEAR(self->context);
     return 0;
 }
 
@@ -75,6 +77,8 @@
     Py_VISIT(self->dict);
     Py_VISIT(self->args);
     Py_VISIT(self->traceback);
+    Py_VISIT(self->cause);
+    Py_VISIT(self->context);
     return 0;
 }
 

Modified: python/branches/py3k/Python/ceval.c
==============================================================================
--- python/branches/py3k/Python/ceval.c	(original)
+++ python/branches/py3k/Python/ceval.c	Sat Sep  1 22:26:44 2007
@@ -2967,7 +2967,6 @@
 		/* Not something you can raise.  You get an exception
 		   anyway, just not what you specified :-) */
         Py_DECREF(exc);
-        Py_XDECREF(cause);
 		PyErr_SetString(PyExc_TypeError,
                                 "exceptions must derive from BaseException");
 		goto raise_error;
@@ -2980,12 +2979,12 @@
             fixed_cause = PyObject_CallObject(cause, NULL);
             if (fixed_cause == NULL)
                 goto raise_error;
+            Py_DECREF(cause);
         }
         else if (PyExceptionInstance_Check(cause)) {
             fixed_cause = cause;
         }
         else {
-            Py_DECREF(cause);
             PyErr_SetString(PyExc_TypeError,
                             "exception causes must derive from BaseException");
             goto raise_error;
@@ -3000,6 +2999,7 @@
 	Py_XDECREF(value);
 	Py_XDECREF(type);
 	Py_XDECREF(tb);
+	Py_XDECREF(cause);
 	return WHY_EXCEPTION;
 }
 


More information about the Python-3000-checkins mailing list