Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f430e4

Browse files
committed
Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Patch by Serhiy Storchaka.
1 parent dd7c552 commit 6f430e4

11 files changed

Lines changed: 87 additions & 39 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
14+
errors correctly. Patch by Serhiy Storchaka.
15+
1316
- Issue #13119: sys.stdout and sys.stderr are now using "\r\n" newline on
1417
Windows, as Python 2.
1518

Modules/_csv.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ _set_bool(const char *name, int *target, PyObject *src, int dflt)
166166
{
167167
if (src == NULL)
168168
*target = dflt;
169-
else
170-
*target = PyObject_IsTrue(src);
169+
else {
170+
int b = PyObject_IsTrue(src);
171+
if (b < 0)
172+
return -1;
173+
*target = b;
174+
}
171175
return 0;
172176
}
173177

Modules/_io/textio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,11 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
10461046
res = PyObject_CallMethod(buffer, "seekable", NULL);
10471047
if (res == NULL)
10481048
goto error;
1049-
self->seekable = self->telling = PyObject_IsTrue(res);
1049+
r = PyObject_IsTrue(res);
10501050
Py_DECREF(res);
1051+
if (r < 0)
1052+
goto error;
1053+
self->seekable = self->telling = r;
10511054

10521055
self->has_read1 = PyObject_HasAttrString(buffer, "read1");
10531056

Modules/_posixsubprocess.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
525525
return NULL;
526526

527527
close_fds = PyObject_IsTrue(py_close_fds);
528+
if (close_fds < 0)
529+
return NULL;
528530
if (close_fds && errpipe_write < 3) { /* precondition */
529531
PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
530532
return NULL;

Modules/_ssl.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,14 +883,18 @@ PySSL_peercert(PySSLSocket *self, PyObject *args)
883883
int len;
884884
int verification;
885885
PyObject *binary_mode = Py_None;
886+
int b;
886887

887888
if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
888889
return NULL;
889890

890891
if (!self->peer_cert)
891892
Py_RETURN_NONE;
892893

893-
if (PyObject_IsTrue(binary_mode)) {
894+
b = PyObject_IsTrue(binary_mode);
895+
if (b < 0)
896+
return NULL;
897+
if (b) {
894898
/* return cert in DER-encoded format */
895899

896900
unsigned char *bytes_buf = NULL;

Modules/itertoolsmodule.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,13 @@ dropwhile_next(dropwhileobject *lz)
903903
}
904904
ok = PyObject_IsTrue(good);
905905
Py_DECREF(good);
906-
if (!ok) {
906+
if (ok == 0) {
907907
lz->start = 1;
908908
return item;
909909
}
910910
Py_DECREF(item);
911+
if (ok < 0)
912+
return NULL;
911913
}
912914
}
913915

@@ -1043,10 +1045,11 @@ takewhile_next(takewhileobject *lz)
10431045
}
10441046
ok = PyObject_IsTrue(good);
10451047
Py_DECREF(good);
1046-
if (ok)
1048+
if (ok > 0)
10471049
return item;
10481050
Py_DECREF(item);
1049-
lz->stop = 1;
1051+
if (ok == 0)
1052+
lz->stop = 1;
10501053
return NULL;
10511054
}
10521055

@@ -2959,9 +2962,11 @@ filterfalse_next(filterfalseobject *lz)
29592962
ok = PyObject_IsTrue(good);
29602963
Py_DECREF(good);
29612964
}
2962-
if (!ok)
2965+
if (ok == 0)
29632966
return item;
29642967
Py_DECREF(item);
2968+
if (ok < 0)
2969+
return NULL;
29652970
}
29662971
}
29672972

Modules/parsermodule.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,14 @@ parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
401401
int lineno = 0;
402402
int col_offset = 0;
403403
if (line_option != NULL) {
404-
lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
404+
lineno = PyObject_IsTrue(line_option);
405+
if (lineno < 0)
406+
return NULL;
405407
}
406408
if (col_option != NULL) {
407-
col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
409+
col_offset = PyObject_IsTrue(col_option);
410+
if (col_offset < 0)
411+
return NULL;
408412
}
409413
/*
410414
* Convert ST into a tuple representation. Use Guido's function,
@@ -444,10 +448,14 @@ parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
444448
int lineno = 0;
445449
int col_offset = 0;
446450
if (line_option != 0) {
447-
lineno = PyObject_IsTrue(line_option) ? 1 : 0;
451+
lineno = PyObject_IsTrue(line_option);
452+
if (lineno < 0)
453+
return NULL;
448454
}
449-
if (col_option != NULL) {
450-
col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
455+
if (col_option != 0) {
456+
col_offset = PyObject_IsTrue(col_option);
457+
if (col_offset < 0)
458+
return NULL;
451459
}
452460
/*
453461
* Convert ST into a tuple representation. Use Guido's function,

Modules/pyexpat.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,16 @@ static PyObject *
10331033
xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
10341034
{
10351035
PyObject *flagobj = NULL;
1036-
XML_Bool flag = XML_TRUE;
1036+
int flag = 1;
10371037
enum XML_Error rc;
1038-
if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
1038+
if (!PyArg_ParseTuple(args, "O:UseForeignDTD", &flagobj))
10391039
return NULL;
1040-
if (flagobj != NULL)
1041-
flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1042-
rc = XML_UseForeignDTD(self->itself, flag);
1040+
if (flagobj != NULL) {
1041+
flag = PyObject_IsTrue(flagobj);
1042+
if (flag < 0)
1043+
return NULL;
1044+
}
1045+
rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
10431046
if (rc != XML_ERROR_NONE) {
10441047
return set_error(self, rc);
10451048
}
@@ -1397,7 +1400,10 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
13971400
}
13981401
assert(PyUnicode_Check(name));
13991402
if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
1400-
if (PyObject_IsTrue(v)) {
1403+
int b = PyObject_IsTrue(v);
1404+
if (b < 0)
1405+
return -1;
1406+
if (b) {
14011407
if (self->buffer == NULL) {
14021408
self->buffer = malloc(self->buffer_size);
14031409
if (self->buffer == NULL) {
@@ -1416,25 +1422,25 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
14161422
return 0;
14171423
}
14181424
if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
1419-
if (PyObject_IsTrue(v))
1420-
self->ns_prefixes = 1;
1421-
else
1422-
self->ns_prefixes = 0;
1425+
int b = PyObject_IsTrue(v);
1426+
if (b < 0)
1427+
return -1;
1428+
self->ns_prefixes = b;
14231429
XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
14241430
return 0;
14251431
}
14261432
if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
1427-
if (PyObject_IsTrue(v))
1428-
self->ordered_attributes = 1;
1429-
else
1430-
self->ordered_attributes = 0;
1433+
int b = PyObject_IsTrue(v);
1434+
if (b < 0)
1435+
return -1;
1436+
self->ordered_attributes = b;
14311437
return 0;
14321438
}
14331439
if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
1434-
if (PyObject_IsTrue(v))
1435-
self->specified_attributes = 1;
1436-
else
1437-
self->specified_attributes = 0;
1440+
int b = PyObject_IsTrue(v);
1441+
if (b < 0)
1442+
return -1;
1443+
self->specified_attributes = b;
14381444
return 0;
14391445
}
14401446

Objects/typeobject.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,15 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
340340
abc.ABCMeta.__new__, so this function doesn't do anything
341341
special to update subclasses.
342342
*/
343-
int res;
343+
int abstract, res;
344344
if (value != NULL) {
345+
abstract = PyObject_IsTrue(value);
346+
if (abstract < 0)
347+
return -1;
345348
res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
346349
}
347350
else {
351+
abstract = 0;
348352
res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
349353
if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
350354
PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
@@ -353,12 +357,10 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
353357
}
354358
if (res == 0) {
355359
PyType_Modified(type);
356-
if (value && PyObject_IsTrue(value)) {
360+
if (abstract)
357361
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
358-
}
359-
else {
362+
else
360363
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
361-
}
362364
}
363365
return res;
364366
}

Python/bltinmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,11 @@ filter_next(filterobject *lz)
428428
ok = PyObject_IsTrue(good);
429429
Py_DECREF(good);
430430
}
431-
if (ok)
431+
if (ok > 0)
432432
return item;
433433
Py_DECREF(item);
434+
if (ok < 0)
435+
return NULL;
434436
}
435437
}
436438

0 commit comments

Comments
 (0)