]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 15 Aug 2012 21:18:25 +0000 (23:18 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 15 Aug 2012 21:18:25 +0000 (23:18 +0200)
Patch by Serhiy Storchaka.

Misc/NEWS
Modules/_csv.c
Modules/_io/textio.c
Modules/_posixsubprocess.c
Modules/_ssl.c
Modules/itertoolsmodule.c
Modules/parsermodule.c
Modules/pyexpat.c
Objects/typeobject.c
Python/bltinmodule.c
Python/import.c

index 1e1e0c550aca6b1fb20423219ceadec1f565dbcc..4f3fc5103f29c9e3d0bf7655d1f344f226a16bf0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
+  errors correctly.  Patch by Serhiy Storchaka.
+
 - Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
   Windows, as Python 2.
 
index a749cec0a538ad4afff8511ad068d03f9d11c3b5..6c564d727bbbb7acf2176a0351eff5b63dbd6baa 100644 (file)
@@ -166,8 +166,12 @@ _set_bool(const char *name, int *target, PyObject *src, int dflt)
 {
     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;
 }
 
index 2d516ee3ccdbb706a9ac60b6552103aec2ba2af2..490484254b3b31033582ea761856c2a74bea2c7b 100644 (file)
@@ -1046,8 +1046,11 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
     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");
 
index 21ad966378de69e625151f6bbc76778aa26687b4..44e1613f10f97f258bc450ba24842ca17afbbb15 100644 (file)
@@ -525,6 +525,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
         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;
index 3fa2d6ac36f2ec1d3ef26e12b72e0f3eb789a24e..0a841183a5fcb2c0fdb2c4c4701dbb41b78b7b13 100644 (file)
@@ -883,6 +883,7 @@ PySSL_peercert(PySSLSocket *self, PyObject *args)
     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 @@ PySSL_peercert(PySSLSocket *self, PyObject *args)
     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;
index 8b6fa855a87394fc0c6f03d4f33fa20774c34916..77e76fe588042df8727ad3e9fb96b2c1405898c6 100644 (file)
@@ -903,11 +903,13 @@ dropwhile_next(dropwhileobject *lz)
         }
         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 @@ takewhile_next(takewhileobject *lz)
     }
     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 @@ filterfalse_next(filterfalseobject *lz)
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (!ok)
+        if (ok == 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
index 84516ec8da7ae64e1682c347f2b8b868472f03f1..b8732dce87fb5b58d9af79b6dcce759b575fcb44 100644 (file)
@@ -401,10 +401,14 @@ parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
         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 @@ parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
         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,
index bf81e2af01a335e114536f27092c2e48f3dc2192..c965ff4aa0c253942f0513f2fed8f6b7b6ce8dda 100644 (file)
@@ -1033,13 +1033,16 @@ 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, "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 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
     }
     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 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
         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;
     }
 
index 54a990e01127e1654a4afd69126ab4a3def66774..adb69cb627dbd6bdd6c1df16305cdedc802b59b2 100644 (file)
@@ -340,11 +340,15 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
        abc.ABCMeta.__new__, so this function doesn't do anything
        special to update subclasses.
     */
-    int res;
+    int abstract, res;
     if (value != NULL) {
+        abstract = PyObject_IsTrue(value);
+        if (abstract < 0)
+            return -1;
         res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
     }
     else {
+        abstract = 0;
         res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
         if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
             PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
@@ -353,12 +357,10 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
     }
     if (res == 0) {
         PyType_Modified(type);
-        if (value && PyObject_IsTrue(value)) {
+        if (abstract)
             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
-        }
-        else {
+        else
             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
-        }
     }
     return res;
 }
index 232e3dc497091de093a96ed5b305c15209a2791c..0e904909d1d721cfb43ed5bc629832d6c96bd0a5 100644 (file)
@@ -428,9 +428,11 @@ filter_next(filterobject *lz)
             ok = PyObject_IsTrue(good);
             Py_DECREF(good);
         }
-        if (ok)
+        if (ok > 0)
             return item;
         Py_DECREF(item);
+        if (ok < 0)
+            return NULL;
     }
 }
 
index 598b7e09d909ab26c4e52ddf7eec93a9f341dabb..beb0eecb5a01a88570dfd66ef67d764e49d08810 100644 (file)
@@ -1338,7 +1338,10 @@ load_source_module(char *name, char *pathname, FILE *fp)
                 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 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
     }
 
     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;
     }