[issue15604] PyObject_IsTrue failure checks

Serhiy Storchaka report at bugs.python.org
Tue Aug 14 23:45:23 CEST 2012


Serhiy Storchaka added the comment:

Patches updated to reflect Antoine's comments.

----------
Added file: http://bugs.python.org/file26813/istrue_check-3.3_2.patch
Added file: http://bugs.python.org/file26814/istrue_check-3.2_2.patch
Added file: http://bugs.python.org/file26815/istrue_check-2.7_2.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15604>
_______________________________________
-------------- next part --------------
diff -r e2e85ed7f8ba Modules/_csv.c
--- a/Modules/_csv.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/_csv.c	Wed Aug 15 00:36:48 2012 +0300
@@ -196,8 +196,12 @@
 {
     if (src == NULL)
         *target = dflt;
-    else
-        *target = PyObject_IsTrue(src);
+    else {
+        int b = PyObject_IsTrue(src);
+        if (b < 0)
+            return -1;
+        *target = b;
+    }
     return 0;
 }
 
diff -r e2e85ed7f8ba Modules/_io/textio.c
--- a/Modules/_io/textio.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/_io/textio.c	Wed Aug 15 00:36:48 2012 +0300
@@ -1056,8 +1056,11 @@
     res = _PyObject_CallMethodId(buffer, &PyId_seekable, NULL);
     if (res == NULL)
         goto error;
-    self->seekable = self->telling = PyObject_IsTrue(res);
+    r = PyObject_IsTrue(res);
     Py_DECREF(res);
+    if (r < 0)
+        goto error;
+    self->seekable = self->telling = r;
 
     self->has_read1 = _PyObject_HasAttrId(buffer, &PyId_read1);
 
diff -r e2e85ed7f8ba Modules/_posixsubprocess.c
--- a/Modules/_posixsubprocess.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/_posixsubprocess.c	Wed Aug 15 00:36:48 2012 +0300
@@ -503,7 +503,7 @@
 subprocess_fork_exec(PyObject* self, PyObject *args)
 {
     PyObject *gc_module = NULL;
-    PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
+    PyObject *executable_list, *py_fds_to_keep;
     PyObject *env_list, *preexec_fn;
     PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
     PyObject *preexec_fn_args_tuple = NULL;
@@ -518,15 +518,14 @@
     Py_ssize_t arg_num;
 
     if (!PyArg_ParseTuple(
-            args, "OOOOOOiiiiiiiiiiO:fork_exec",
-            &process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
+            args, "OOpOOOiiiiiiiiiiO:fork_exec",
+            &process_args, &executable_list, &close_fds, &py_fds_to_keep,
             &cwd_obj, &env_list,
             &p2cread, &p2cwrite, &c2pread, &c2pwrite,
             &errread, &errwrite, &errpipe_read, &errpipe_write,
             &restore_signals, &call_setsid, &preexec_fn))
         return NULL;
 
-    close_fds = PyObject_IsTrue(py_close_fds);
     if (close_fds && errpipe_write < 3) {  /* precondition */
         PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
         return NULL;
diff -r e2e85ed7f8ba Modules/_ssl.c
--- a/Modules/_ssl.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/_ssl.c	Wed Aug 15 00:36:48 2012 +0300
@@ -1037,15 +1037,15 @@
     PyObject *retval = NULL;
     int len;
     int verification;
-    PyObject *binary_mode = Py_None;
+    int binary_mode = 0;
 
-    if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
+    if (!PyArg_ParseTuple(args, "|p:peer_certificate", &binary_mode))
         return NULL;
 
     if (!self->peer_cert)
         Py_RETURN_NONE;
 
-    if (PyObject_IsTrue(binary_mode)) {
+    if (binary_mode) {
         /* return cert in DER-encoded format */
 
         unsigned char *bytes_buf = NULL;
diff -r e2e85ed7f8ba Modules/itertoolsmodule.c
--- a/Modules/itertoolsmodule.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/itertoolsmodule.c	Wed Aug 15 00:36:48 2012 +0300
@@ -1105,11 +1105,13 @@
         }
         ok = PyObject_IsTrue(good);
         Py_DECREF(good);
-        if (!ok) {
+        if (ok == 0) {
             lz->start = 1;
             return item;
         }
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
@@ -1124,7 +1126,7 @@
 dropwhile_setstate(dropwhileobject *lz, PyObject *state)
 {
     int start = PyObject_IsTrue(state);
-    if (start == -1)
+    if (start < 0)
         return NULL;
     lz->start = start;
     Py_RETURN_NONE;
@@ -1270,10 +1272,11 @@
     }
     ok = PyObject_IsTrue(good);
     Py_DECREF(good);
-    if (ok)
+    if (ok == 1)
         return item;
     Py_DECREF(item);
-    lz->stop = 1;
+    if (ok == 0)
+        lz->stop = 1;
     return NULL;
 }
 
@@ -1288,7 +1291,7 @@
 takewhile_reduce_setstate(takewhileobject *lz, PyObject *state)
 {
     int stop = PyObject_IsTrue(state);
-    if (stop == -1)
+    if (stop < 0)
         return NULL;
     lz->stop = stop;
     Py_RETURN_NONE;
@@ -3536,7 +3539,7 @@
         if (ok == 1)
             return datum;
         Py_DECREF(datum);
-        if (ok == -1)
+        if (ok < 0)
             return NULL;
     }
 }
@@ -3692,9 +3695,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (!ok)
+        if (ok == 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
diff -r e2e85ed7f8ba Modules/parsermodule.c
--- a/Modules/parsermodule.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/parsermodule.c	Wed Aug 15 00:36:48 2012 +0300
@@ -382,36 +382,28 @@
 static PyObject*
 parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
 {
-    PyObject *line_option = 0;
-    PyObject *col_option = 0;
+    int line_info = 0;
+    int col_info = 0;
     PyObject *res = 0;
     int ok;
 
     static char *keywords[] = {"st", "line_info", "col_info", NULL};
 
     if (self == NULL || PyModule_Check(self)) {
-        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
-                                         &PyST_Type, &self, &line_option,
-                                         &col_option);
+        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
+                                         &PyST_Type, &self, &line_info,
+                                         &col_info);
     }
     else
-        ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
-                                         &line_option, &col_option);
+        ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
+                                         &line_info, &col_info);
     if (ok != 0) {
-        int lineno = 0;
-        int col_offset = 0;
-        if (line_option != NULL) {
-            lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
-        }
-        if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
-        }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
          *  since it's known to work already.
          */
         res = node2tuple(((PyST_Object*)self)->st_node,
-                         PyTuple_New, PyTuple_SetItem, lineno, col_offset);
+                         PyTuple_New, PyTuple_SetItem, line_info, col_info);
     }
     return (res);
 }
@@ -426,35 +418,27 @@
 static PyObject*
 parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
 {
-    PyObject *line_option = 0;
-    PyObject *col_option = 0;
+    int line_info = 0;
+    int col_info = 0;
     PyObject *res = 0;
     int ok;
 
     static char *keywords[] = {"st", "line_info", "col_info", NULL};
 
     if (self == NULL || PyModule_Check(self))
-        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
-                                         &PyST_Type, &self, &line_option,
-                                         &col_option);
+        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
+                                         &PyST_Type, &self, &line_info,
+                                         &col_info);
     else
-        ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
-                                         &line_option, &col_option);
+        ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
+                                         &line_info, &col_info);
     if (ok) {
-        int lineno = 0;
-        int col_offset = 0;
-        if (line_option != 0) {
-            lineno = PyObject_IsTrue(line_option) ? 1 : 0;
-        }
-        if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
-        }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
          *  since it's known to work already.
          */
         res = node2tuple(self->st_node,
-                         PyList_New, PyList_SetItem, lineno, col_offset);
+                         PyList_New, PyList_SetItem, line_info, col_info);
     }
     return (res);
 }
diff -r e2e85ed7f8ba Modules/pyexpat.c
--- a/Modules/pyexpat.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Modules/pyexpat.c	Wed Aug 15 00:36:48 2012 +0300
@@ -1033,14 +1033,11 @@
 static PyObject *
 xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
 {
-    PyObject *flagobj = NULL;
-    XML_Bool flag = XML_TRUE;
+    int flag = 1;
     enum XML_Error rc;
-    if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
+    if (!PyArg_ParseTuple(args, "p:UseForeignDTD", &flag))
         return NULL;
-    if (flagobj != NULL)
-        flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
-    rc = XML_UseForeignDTD(self->itself, flag);
+    rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
     if (rc != XML_ERROR_NONE) {
         return set_error(self, rc);
     }
@@ -1405,7 +1402,10 @@
     }
     assert(PyUnicode_Check(name));
     if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
-        if (PyObject_IsTrue(v)) {
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        if (b) {
             if (self->buffer == NULL) {
                 self->buffer = malloc(self->buffer_size);
                 if (self->buffer == NULL) {
@@ -1424,25 +1424,25 @@
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ns_prefixes = 1;
-        else
-            self->ns_prefixes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ns_prefixes = b;
         XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ordered_attributes = 1;
-        else
-            self->ordered_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ordered_attributes = b;
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->specified_attributes = 1;
-        else
-            self->specified_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->specified_attributes = b;
         return 0;
     }
 
diff -r e2e85ed7f8ba Objects/typeobject.c
--- a/Objects/typeobject.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Objects/typeobject.c	Wed Aug 15 00:36:48 2012 +0300
@@ -396,12 +396,16 @@
     }
     if (res == 0) {
         PyType_Modified(type);
-        if (value && PyObject_IsTrue(value)) {
+        int abstract = 0;
+        if (value) {
+            abstract = PyObject_IsTrue(value);
+            if (abstract < 0)
+                return -1;
+        }
+        if (abstract)
             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
-        }
-        else {
+        else
             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
-        }
     }
     return res;
 }
diff -r e2e85ed7f8ba Python/bltinmodule.c
--- a/Python/bltinmodule.c	Tue Aug 14 18:42:54 2012 +0300
+++ b/Python/bltinmodule.c	Wed Aug 15 00:36:48 2012 +0300
@@ -429,9 +429,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok)
+        if (ok > 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
-------------- next part --------------
diff -r 84899daa4309 Modules/_csv.c
--- a/Modules/_csv.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/_csv.c	Wed Aug 15 00:36:56 2012 +0300
@@ -166,8 +166,12 @@
 {
     if (src == NULL)
         *target = dflt;
-    else
-        *target = PyObject_IsTrue(src);
+    else {
+        int b = PyObject_IsTrue(src);
+        if (b < 0)
+            return -1;
+        *target = b;
+    }
     return 0;
 }
 
diff -r 84899daa4309 Modules/_io/textio.c
--- a/Modules/_io/textio.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/_io/textio.c	Wed Aug 15 00:36:56 2012 +0300
@@ -1046,8 +1046,11 @@
     res = PyObject_CallMethod(buffer, "seekable", NULL);
     if (res == NULL)
         goto error;
-    self->seekable = self->telling = PyObject_IsTrue(res);
+    r = PyObject_IsTrue(res);
     Py_DECREF(res);
+    if (r < 0)
+        goto error;
+    self->seekable = self->telling = r;
 
     self->has_read1 = PyObject_HasAttrString(buffer, "read1");
 
diff -r 84899daa4309 Modules/_posixsubprocess.c
--- a/Modules/_posixsubprocess.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/_posixsubprocess.c	Wed Aug 15 00:36:56 2012 +0300
@@ -525,6 +525,8 @@
         return NULL;
 
     close_fds = PyObject_IsTrue(py_close_fds);
+    if (close_fds < 0)
+        return NULL;
     if (close_fds && errpipe_write < 3) {  /* precondition */
         PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
         return NULL;
diff -r 84899daa4309 Modules/_ssl.c
--- a/Modules/_ssl.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/_ssl.c	Wed Aug 15 00:36:56 2012 +0300
@@ -883,6 +883,7 @@
     int len;
     int verification;
     PyObject *binary_mode = Py_None;
+    int b;
 
     if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
         return NULL;
@@ -890,7 +891,10 @@
     if (!self->peer_cert)
         Py_RETURN_NONE;
 
-    if (PyObject_IsTrue(binary_mode)) {
+    b = PyObject_IsTrue(binary_mode);
+    if (b < 0)
+        return NULL;
+    if (b) {
         /* return cert in DER-encoded format */
 
         unsigned char *bytes_buf = NULL;
diff -r 84899daa4309 Modules/itertoolsmodule.c
--- a/Modules/itertoolsmodule.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/itertoolsmodule.c	Wed Aug 15 00:36:56 2012 +0300
@@ -903,11 +903,13 @@
         }
         ok = PyObject_IsTrue(good);
         Py_DECREF(good);
-        if (!ok) {
+        if (ok == 0) {
             lz->start = 1;
             return item;
         }
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
@@ -1043,10 +1045,11 @@
     }
     ok = PyObject_IsTrue(good);
     Py_DECREF(good);
-    if (ok)
+    if (ok > 0)
         return item;
     Py_DECREF(item);
-    lz->stop = 1;
+    if (ok == 0)
+        lz->stop = 1;
     return NULL;
 }
 
@@ -2959,9 +2962,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (!ok)
+        if (ok == 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
diff -r 84899daa4309 Modules/parsermodule.c
--- a/Modules/parsermodule.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/parsermodule.c	Wed Aug 15 00:36:56 2012 +0300
@@ -401,10 +401,14 @@
         int lineno = 0;
         int col_offset = 0;
         if (line_option != NULL) {
-            lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
+            lineno = PyObject_IsTrue(line_option);
+            if (lineno < 0)
+                return NULL;
         }
         if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
+            col_offset = PyObject_IsTrue(col_option);
+            if (col_offset < 0)
+                return NULL;
         }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
@@ -444,10 +448,14 @@
         int lineno = 0;
         int col_offset = 0;
         if (line_option != 0) {
-            lineno = PyObject_IsTrue(line_option) ? 1 : 0;
+            lineno = PyObject_IsTrue(line_option);
+            if (lineno < 0)
+                return NULL;
         }
-        if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
+        if (col_option != 0) {
+            col_offset = PyObject_IsTrue(col_option);
+            if (col_offset < 0)
+                return NULL;
         }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
diff -r 84899daa4309 Modules/pyexpat.c
--- a/Modules/pyexpat.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Modules/pyexpat.c	Wed Aug 15 00:36:56 2012 +0300
@@ -1033,13 +1033,16 @@
 xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
 {
     PyObject *flagobj = NULL;
-    XML_Bool flag = XML_TRUE;
+    int flag = 1;
     enum XML_Error rc;
-    if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
+    if (!PyArg_ParseTuple(args, "O:UseForeignDTD", &flagobj))
         return NULL;
-    if (flagobj != NULL)
-        flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
-    rc = XML_UseForeignDTD(self->itself, flag);
+    if (flagobj != NULL) {
+        flag = PyObject_IsTrue(flagobj);
+        if (flag < 0)
+            return NULL;
+    }
+    rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
     if (rc != XML_ERROR_NONE) {
         return set_error(self, rc);
     }
@@ -1397,7 +1400,10 @@
     }
     assert(PyUnicode_Check(name));
     if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
-        if (PyObject_IsTrue(v)) {
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        if (b) {
             if (self->buffer == NULL) {
                 self->buffer = malloc(self->buffer_size);
                 if (self->buffer == NULL) {
@@ -1416,25 +1422,25 @@
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ns_prefixes = 1;
-        else
-            self->ns_prefixes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ns_prefixes = b;
         XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ordered_attributes = 1;
-        else
-            self->ordered_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ordered_attributes = b;
         return 0;
     }
     if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->specified_attributes = 1;
-        else
-            self->specified_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->specified_attributes = b;
         return 0;
     }
 
diff -r 84899daa4309 Objects/typeobject.c
--- a/Objects/typeobject.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Objects/typeobject.c	Wed Aug 15 00:36:56 2012 +0300
@@ -353,12 +353,16 @@
     }
     if (res == 0) {
         PyType_Modified(type);
-        if (value && PyObject_IsTrue(value)) {
+        int abstract = 0;
+        if (value) {
+            abstract = PyObject_IsTrue(value);
+            if (abstract < 0)
+                return -1;
+        }
+        if (abstract)
             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
-        }
-        else {
+        else
             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
-        }
     }
     return res;
 }
diff -r 84899daa4309 Python/bltinmodule.c
--- a/Python/bltinmodule.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Python/bltinmodule.c	Wed Aug 15 00:36:56 2012 +0300
@@ -428,9 +428,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok)
+        if (ok > 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
diff -r 84899daa4309 Python/import.c
--- a/Python/import.c	Tue Aug 14 18:41:40 2012 +0300
+++ b/Python/import.c	Wed Aug 15 00:36:56 2012 +0300
@@ -1338,7 +1338,10 @@
                 name, pathname);
         if (cpathname) {
             PyObject *ro = PySys_GetObject("dont_write_bytecode");
-            if (ro == NULL || !PyObject_IsTrue(ro))
+            int b = (ro == NULL) ? 0 : PyObject_IsTrue(ro);
+            if (b < 0)
+                goto error_exit;
+            if (!b)
                 write_compiled_module(co, cpathname, &st);
         }
     }
@@ -2504,7 +2507,13 @@
     }
 
     if (fromlist != NULL) {
-        if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
+        int b = (fromlist == Py_None) ? 0 : PyObject_IsTrue(fromlist);
+        if (b < 0) {
+            Py_DECREF(tail);
+            Py_DECREF(head);
+            goto error_exit;
+        }
+        if (!b)
             fromlist = NULL;
     }
 
-------------- next part --------------
diff -r db1b4aab53eb Modules/_csv.c
--- a/Modules/_csv.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/_csv.c	Wed Aug 15 00:37:12 2012 +0300
@@ -208,8 +208,12 @@
 {
     if (src == NULL)
         *target = dflt;
-    else
-        *target = PyObject_IsTrue(src);
+    else {
+        int b = PyObject_IsTrue(src);
+        if (b < 0)
+            return -1;
+        *target = b;
+    }
     return 0;
 }
 
diff -r db1b4aab53eb Modules/_io/textio.c
--- a/Modules/_io/textio.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/_io/textio.c	Wed Aug 15 00:37:12 2012 +0300
@@ -1013,8 +1013,11 @@
     res = PyObject_CallMethod(buffer, "seekable", NULL);
     if (res == NULL)
         goto error;
-    self->seekable = self->telling = PyObject_IsTrue(res);
+    r = PyObject_IsTrue(res);
     Py_DECREF(res);
+    if (r < 0)
+        goto error;
+    self->seekable = self->telling = r;
 
     self->encoding_start_of_stream = 0;
     if (self->seekable && self->encoder) {
diff -r db1b4aab53eb Modules/_ssl.c
--- a/Modules/_ssl.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/_ssl.c	Wed Aug 15 00:37:12 2012 +0300
@@ -1005,6 +1005,7 @@
     int len;
     int verification;
     PyObject *binary_mode = Py_None;
+    int b;
 
     if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
         return NULL;
@@ -1012,7 +1013,10 @@
     if (!self->peer_cert)
         Py_RETURN_NONE;
 
-    if (PyObject_IsTrue(binary_mode)) {
+    b = PyObject_IsTrue(binary_mode);
+    if (b < 0)
+        return NULL;
+    if (b) {
         /* return cert in DER-encoded format */
 
         unsigned char *bytes_buf = NULL;
diff -r db1b4aab53eb Modules/cStringIO.c
--- a/Modules/cStringIO.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/cStringIO.c	Wed Aug 15 00:37:12 2012 +0300
@@ -127,12 +127,16 @@
 static PyObject *
 IO_getval(IOobject *self, PyObject *args) {
     PyObject *use_pos=Py_None;
+    int b;
     Py_ssize_t s;
 
     if (!IO__opencheck(self)) return NULL;
     if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
 
-    if (PyObject_IsTrue(use_pos)) {
+    b = PyObject_IsTrue(use_pos);
+    if (b < 0)
+        return NULL;
+    if (b) {
               s=self->pos;
               if (s > self->string_size) s=self->string_size;
     }
diff -r db1b4aab53eb Modules/itertoolsmodule.c
--- a/Modules/itertoolsmodule.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/itertoolsmodule.c	Wed Aug 15 00:37:12 2012 +0300
@@ -903,11 +903,13 @@
         }
         ok = PyObject_IsTrue(good);
         Py_DECREF(good);
-        if (!ok) {
+        if (ok == 0) {
             lz->start = 1;
             return item;
         }
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
@@ -1043,10 +1045,11 @@
     }
     ok = PyObject_IsTrue(good);
     Py_DECREF(good);
-    if (ok)
+    if (ok > 0)
         return item;
     Py_DECREF(item);
-    lz->stop = 1;
+    if (ok == 0)
+        lz->stop = 1;
     return NULL;
 }
 
@@ -3001,9 +3004,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok)
+        if (ok > 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
@@ -3144,9 +3149,11 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (!ok)
+        if (ok == 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
diff -r db1b4aab53eb Modules/parsermodule.c
--- a/Modules/parsermodule.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/parsermodule.c	Wed Aug 15 00:37:12 2012 +0300
@@ -350,10 +350,14 @@
         int lineno = 0;
         int col_offset = 0;
         if (line_option != NULL) {
-            lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
+            lineno = PyObject_IsTrue(line_option);
+            if (lineno < 0)
+                return NULL;
         }
         if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
+            col_offset = PyObject_IsTrue(col_option);
+            if (col_offset < 0)
+                return NULL;
         }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
@@ -401,10 +405,14 @@
         int lineno = 0;
         int col_offset = 0;
         if (line_option != 0) {
-            lineno = PyObject_IsTrue(line_option) ? 1 : 0;
+            lineno = PyObject_IsTrue(line_option);
+            if (lineno < 0)
+                return NULL;
         }
-        if (col_option != NULL) {
-            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
+        if (col_option != 0) {
+            col_offset = PyObject_IsTrue(col_option);
+            if (col_offset < 0)
+                return NULL;
         }
         /*
          *  Convert ST into a tuple representation.  Use Guido's function,
diff -r db1b4aab53eb Modules/pyexpat.c
--- a/Modules/pyexpat.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Modules/pyexpat.c	Wed Aug 15 00:37:12 2012 +0300
@@ -1174,13 +1174,16 @@
 xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
 {
     PyObject *flagobj = NULL;
-    XML_Bool flag = XML_TRUE;
+    int flag = 1;
     enum XML_Error rc;
-    if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
+    if (!PyArg_ParseTuple(args, "O:UseForeignDTD", &flagobj))
         return NULL;
-    if (flagobj != NULL)
-        flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
-    rc = XML_UseForeignDTD(self->itself, flag);
+    if (flagobj != NULL) {
+        flag = PyObject_IsTrue(flagobj);
+        if (flag < 0)
+            return NULL;
+    }
+    rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
     if (rc != XML_ERROR_NONE) {
         return set_error(self, rc);
     }
@@ -1549,7 +1552,10 @@
         return -1;
     }
     if (strcmp(name, "buffer_text") == 0) {
-        if (PyObject_IsTrue(v)) {
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        if (b) {
             if (self->buffer == NULL) {
                 self->buffer = malloc(self->buffer_size);
                 if (self->buffer == NULL) {
@@ -1568,39 +1574,39 @@
         return 0;
     }
     if (strcmp(name, "namespace_prefixes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ns_prefixes = 1;
-        else
-            self->ns_prefixes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ns_prefixes = b;
         XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
         return 0;
     }
     if (strcmp(name, "ordered_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->ordered_attributes = 1;
-        else
-            self->ordered_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->ordered_attributes = b;
         return 0;
     }
     if (strcmp(name, "returns_unicode") == 0) {
-        if (PyObject_IsTrue(v)) {
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
 #ifndef Py_USING_UNICODE
+        if (b) {
             PyErr_SetString(PyExc_ValueError,
                             "Unicode support not available");
             return -1;
-#else
-            self->returns_unicode = 1;
+        }
 #endif
-        }
-        else
-            self->returns_unicode = 0;
+        self->returns_unicode = b;
         return 0;
     }
     if (strcmp(name, "specified_attributes") == 0) {
-        if (PyObject_IsTrue(v))
-            self->specified_attributes = 1;
-        else
-            self->specified_attributes = 0;
+        int b = PyObject_IsTrue(v);
+        if (b < 0)
+            return -1;
+        self->specified_attributes = b;
         return 0;
     }
 
diff -r db1b4aab53eb Objects/typeobject.c
--- a/Objects/typeobject.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Objects/typeobject.c	Wed Aug 15 00:37:12 2012 +0300
@@ -340,12 +340,16 @@
     }
     if (res == 0) {
         PyType_Modified(type);
-        if (value && PyObject_IsTrue(value)) {
+        int abstract = 0;
+        if (value) {
+            abstract = PyObject_IsTrue(value);
+            if (abstract < 0)
+                return -1;
+        }
+        if (abstract)
             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
-        }
-        else {
+        else
             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
-        }
     }
     return res;
 }
diff -r db1b4aab53eb Python/bltinmodule.c
--- a/Python/bltinmodule.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Python/bltinmodule.c	Wed Aug 15 00:37:12 2012 +0300
@@ -313,7 +313,7 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok) {
+        if (ok > 0) {
             if (j < len)
                 PyList_SET_ITEM(result, j, item);
             else {
@@ -324,8 +324,11 @@
             }
             ++j;
         }
-        else
+        else {
             Py_DECREF(item);
+            if (ok < 0)
+                goto Fail_result_it;
+        }
     }
 
 
@@ -2784,12 +2787,15 @@
         }
         ok = PyObject_IsTrue(good);
         Py_DECREF(good);
-        if (ok) {
+        if (ok > 0) {
             if (PyTuple_SetItem(result, j++, item) < 0)
                 goto Fail_1;
         }
-        else
+        else {
             Py_DECREF(item);
+            if (ok < 0)
+                goto Fail_1;
+        }
     }
 
     if (_PyTuple_Resize(&result, j) < 0)
@@ -2851,7 +2857,7 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok) {
+        if (ok > 0) {
             Py_ssize_t reslen;
             if (!PyString_Check(item)) {
                 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
@@ -2917,6 +2923,8 @@
                     }
         }
         Py_DECREF(item);
+        if (ok < 0)
+            goto Fail_1;
     }
 
     if (j < outlen)
@@ -2977,7 +2985,7 @@
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok) {
+        if (ok > 0) {
             Py_ssize_t reslen;
             if (!PyUnicode_Check(item)) {
                 PyErr_SetString(PyExc_TypeError,
@@ -3032,6 +3040,8 @@
                     }
         }
         Py_DECREF(item);
+        if (ok < 0)
+            goto Fail_1;
     }
 
     if (j < outlen)
diff -r db1b4aab53eb Python/import.c
--- a/Python/import.c	Tue Aug 07 11:57:47 2012 -0700
+++ b/Python/import.c	Wed Aug 15 00:37:12 2012 +0300
@@ -1043,7 +1043,10 @@
                 name, pathname);
         if (cpathname) {
             PyObject *ro = PySys_GetObject("dont_write_bytecode");
-            if (ro == NULL || !PyObject_IsTrue(ro))
+            int b = (ro == NULL) ? 0 : PyObject_IsTrue(ro);
+            if (b < 0)
+                goto error_exit;
+            if (!b)
                 write_compiled_module(co, cpathname, &st);
         }
     }
@@ -2200,7 +2203,13 @@
     }
 
     if (fromlist != NULL) {
-        if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
+        int b = (fromlist == Py_None) ? 0 : PyObject_IsTrue(fromlist);
+        if (b < 0) {
+            Py_DECREF(tail);
+            Py_DECREF(head);
+            goto error_exit;
+        }
+        if (!b)
             fromlist = NULL;
     }
 


More information about the Python-bugs-list mailing list