]> granicus.if.org Git - python/commitdiff
Fix SystemError and a wasps nest of ref counting issues.
authorRaymond Hettinger <python@rcn.com>
Fri, 12 Jun 2009 18:40:16 +0000 (18:40 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 12 Jun 2009 18:40:16 +0000 (18:40 +0000)
Lib/test/test_range.py
Misc/NEWS
Objects/rangeobject.c

index 43fadf01d1f014e81fe998269510d84a523352ac..7650103bd0c2b70ca4d25f28c7881cb063b92b1c 100644 (file)
@@ -71,6 +71,13 @@ class RangeTest(unittest.TestCase):
                 self.assertEquals(list(pickle.loads(pickle.dumps(r, proto))),
                                   list(r))
 
+    def test_odd_bug(self):
+        # This used to raise a "SystemError: NULL result without error"
+        # because the range validation step was eating the exception
+        # before NULL was returned.
+        with self.assertRaises(TypeError):
+            range([], 1, -1)
+
 def test_main():
     test.support.run_unittest(RangeTest)
 
index f12c6d105b81fd97ede7675585fde708a3597e38..627dcf4f2a9fac2a98afee9bba61a28e97cc6428 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.1 Release Candidate 2?
 Core and Builtins
 -----------------
 
+- Fixed SystemError triggered by "range([], 1, -1)".
+
 - Issue #5924: On Windows, a large PYTHONPATH environment variable
   (more than 255 characters) would be completely ignored.
 
index 01114bb6adb37fc765105ffc81663a95eab2924d..f8174caa4268983dc13f35dc3f2fe010e46f7f4d 100644 (file)
@@ -59,26 +59,42 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
 
     if (PyTuple_Size(args) <= 1) {
         if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop))
-            goto Fail;
+            return NULL;
         stop = PyNumber_Index(stop);
         if (!stop)
-            goto Fail;
+            return NULL;
         start = PyLong_FromLong(0);
+        if (!start) {
+            Py_DECREF(stop);
+            return NULL;
+        }
         step = PyLong_FromLong(1);
-        if (!start || !step)
-            goto Fail;
+        if (!step) {
+            Py_DECREF(stop);
+            Py_DECREF(start);
+            return NULL;
+        }
     }
     else {
         if (!PyArg_UnpackTuple(args, "range", 2, 3,
                                &start, &stop, &step))
-            goto Fail;
+            return NULL;
 
         /* Convert borrowed refs to owned refs */
         start = PyNumber_Index(start);
+        if (!start)
+            return NULL;
         stop = PyNumber_Index(stop);
-        step = validate_step(step);
-        if (!start || !stop || !step)
-            goto Fail;
+        if (!stop) {
+            Py_DECREF(start);
+            return NULL;
+        }
+        step = validate_step(step);    /* Caution, this can clear exceptions */
+        if (!step) {
+            Py_DECREF(start);
+            Py_DECREF(stop);
+            return NULL;
+        }
     }
 
     obj = PyObject_New(rangeobject, &PyRange_Type);