]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
backport:
authorNeal Norwitz <nnorwitz@gmail.com>
Sun, 2 Feb 2003 19:59:59 +0000 (19:59 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sun, 2 Feb 2003 19:59:59 +0000 (19:59 +0000)
revision 2.196
date: 2002/12/07 21:39:16;  author: tim_one;  state: Exp;  lines: +27 -28
slot_nb_nonzero():  Another leak uncovered by the sandbox datetime
tests.  I found the logic too confusing to follow here, so rewrote more
than was likely absolutely necessary.

Objects/typeobject.c

index fa255985c8689d1b4eff66a11e5b7b723149b7c3..ad30f8e05c102854c8fec47e4d1231799c6fcd16 100644 (file)
@@ -3113,26 +3113,29 @@ SLOT0(slot_nb_absolute, "__abs__")
 static int
 slot_nb_nonzero(PyObject *self)
 {
-       PyObject *func, *res;
+       PyObject *func, *args;
        static PyObject *nonzero_str, *len_str;
+       int result = -1;
 
        func = lookup_maybe(self, "__nonzero__", &nonzero_str);
        if (func == NULL) {
                if (PyErr_Occurred())
                        return -1;
                func = lookup_maybe(self, "__len__", &len_str);
-               if (func == NULL) {
-                       if (PyErr_Occurred())
-                               return -1;
-                       else
-                               return 1;
+               if (func == NULL)
+                       return PyErr_Occurred() ? -1 : 1;
+       }
+       args = PyTuple_New(0);
+       if (args != NULL) {
+               PyObject *temp = PyObject_Call(func, args, NULL);
+               Py_DECREF(args);
+               if (temp != NULL) {
+                       result = PyObject_IsTrue(temp);
+                       Py_DECREF(temp);
                }
        }
-       res = PyObject_CallObject(func, NULL);
        Py_DECREF(func);
-       if (res == NULL)
-               return -1;
-       return PyObject_IsTrue(res);
+       return result;
 }
 
 SLOT0(slot_nb_invert, "__invert__")