Skip to content

Commit 721738f

Browse files
committed
Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Patch by Serhiy Storchaka.
2 parents 9351117 + 6f430e4 commit 721738f

10 files changed

Lines changed: 76 additions & 74 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.3.0 Release Candidate 1?
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
Library
1417
-------
1518

Modules/_csv.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ _set_bool(const char *name, int *target, PyObject *src, int dflt)
196196
{
197197
if (src == NULL)
198198
*target = dflt;
199-
else
200-
*target = PyObject_IsTrue(src);
199+
else {
200+
int b = PyObject_IsTrue(src);
201+
if (b < 0)
202+
return -1;
203+
*target = b;
204+
}
201205
return 0;
202206
}
203207

Modules/_io/textio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,11 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
10561056
res = _PyObject_CallMethodId(buffer, &PyId_seekable, NULL);
10571057
if (res == NULL)
10581058
goto error;
1059-
self->seekable = self->telling = PyObject_IsTrue(res);
1059+
r = PyObject_IsTrue(res);
10601060
Py_DECREF(res);
1061+
if (r < 0)
1062+
goto error;
1063+
self->seekable = self->telling = r;
10611064

10621065
self->has_read1 = _PyObject_HasAttrId(buffer, &PyId_read1);
10631066

Modules/_posixsubprocess.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static PyObject *
503503
subprocess_fork_exec(PyObject* self, PyObject *args)
504504
{
505505
PyObject *gc_module = NULL;
506-
PyObject *executable_list, *py_close_fds, *py_fds_to_keep;
506+
PyObject *executable_list, *py_fds_to_keep;
507507
PyObject *env_list, *preexec_fn;
508508
PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
509509
PyObject *preexec_fn_args_tuple = NULL;
@@ -518,15 +518,14 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
518518
Py_ssize_t arg_num;
519519

520520
if (!PyArg_ParseTuple(
521-
args, "OOOOOOiiiiiiiiiiO:fork_exec",
522-
&process_args, &executable_list, &py_close_fds, &py_fds_to_keep,
521+
args, "OOpOOOiiiiiiiiiiO:fork_exec",
522+
&process_args, &executable_list, &close_fds, &py_fds_to_keep,
523523
&cwd_obj, &env_list,
524524
&p2cread, &p2cwrite, &c2pread, &c2pwrite,
525525
&errread, &errwrite, &errpipe_read, &errpipe_write,
526526
&restore_signals, &call_setsid, &preexec_fn))
527527
return NULL;
528528

529-
close_fds = PyObject_IsTrue(py_close_fds);
530529
if (close_fds && errpipe_write < 3) { /* precondition */
531530
PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
532531
return NULL;

Modules/_ssl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,15 +1037,15 @@ PySSL_peercert(PySSLSocket *self, PyObject *args)
10371037
PyObject *retval = NULL;
10381038
int len;
10391039
int verification;
1040-
PyObject *binary_mode = Py_None;
1040+
int binary_mode = 0;
10411041

1042-
if (!PyArg_ParseTuple(args, "|O:peer_certificate", &binary_mode))
1042+
if (!PyArg_ParseTuple(args, "|p:peer_certificate", &binary_mode))
10431043
return NULL;
10441044

10451045
if (!self->peer_cert)
10461046
Py_RETURN_NONE;
10471047

1048-
if (PyObject_IsTrue(binary_mode)) {
1048+
if (binary_mode) {
10491049
/* return cert in DER-encoded format */
10501050

10511051
unsigned char *bytes_buf = NULL;

Modules/itertoolsmodule.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,11 +1105,13 @@ dropwhile_next(dropwhileobject *lz)
11051105
}
11061106
ok = PyObject_IsTrue(good);
11071107
Py_DECREF(good);
1108-
if (!ok) {
1108+
if (ok == 0) {
11091109
lz->start = 1;
11101110
return item;
11111111
}
11121112
Py_DECREF(item);
1113+
if (ok < 0)
1114+
return NULL;
11131115
}
11141116
}
11151117

@@ -1124,7 +1126,7 @@ static PyObject *
11241126
dropwhile_setstate(dropwhileobject *lz, PyObject *state)
11251127
{
11261128
int start = PyObject_IsTrue(state);
1127-
if (start == -1)
1129+
if (start < 0)
11281130
return NULL;
11291131
lz->start = start;
11301132
Py_RETURN_NONE;
@@ -1270,10 +1272,11 @@ takewhile_next(takewhileobject *lz)
12701272
}
12711273
ok = PyObject_IsTrue(good);
12721274
Py_DECREF(good);
1273-
if (ok)
1275+
if (ok == 1)
12741276
return item;
12751277
Py_DECREF(item);
1276-
lz->stop = 1;
1278+
if (ok == 0)
1279+
lz->stop = 1;
12771280
return NULL;
12781281
}
12791282

@@ -1288,7 +1291,7 @@ static PyObject *
12881291
takewhile_reduce_setstate(takewhileobject *lz, PyObject *state)
12891292
{
12901293
int stop = PyObject_IsTrue(state);
1291-
if (stop == -1)
1294+
if (stop < 0)
12921295
return NULL;
12931296
lz->stop = stop;
12941297
Py_RETURN_NONE;
@@ -3536,7 +3539,7 @@ compress_next(compressobject *lz)
35363539
if (ok == 1)
35373540
return datum;
35383541
Py_DECREF(datum);
3539-
if (ok == -1)
3542+
if (ok < 0)
35403543
return NULL;
35413544
}
35423545
}
@@ -3692,9 +3695,11 @@ filterfalse_next(filterfalseobject *lz)
36923695
ok = PyObject_IsTrue(good);
36933696
Py_DECREF(good);
36943697
}
3695-
if (!ok)
3698+
if (ok == 0)
36963699
return item;
36973700
Py_DECREF(item);
3701+
if (ok < 0)
3702+
return NULL;
36983703
}
36993704
}
37003705

Modules/parsermodule.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -382,36 +382,28 @@ parser_sizeof(PyST_Object *st, void *unused)
382382
static PyObject*
383383
parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
384384
{
385-
PyObject *line_option = 0;
386-
PyObject *col_option = 0;
385+
int line_info = 0;
386+
int col_info = 0;
387387
PyObject *res = 0;
388388
int ok;
389389

390390
static char *keywords[] = {"st", "line_info", "col_info", NULL};
391391

392392
if (self == NULL || PyModule_Check(self)) {
393-
ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
394-
&PyST_Type, &self, &line_option,
395-
&col_option);
393+
ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords,
394+
&PyST_Type, &self, &line_info,
395+
&col_info);
396396
}
397397
else
398-
ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
399-
&line_option, &col_option);
398+
ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1],
399+
&line_info, &col_info);
400400
if (ok != 0) {
401-
int lineno = 0;
402-
int col_offset = 0;
403-
if (line_option != NULL) {
404-
lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
405-
}
406-
if (col_option != NULL) {
407-
col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
408-
}
409401
/*
410402
* Convert ST into a tuple representation. Use Guido's function,
411403
* since it's known to work already.
412404
*/
413405
res = node2tuple(((PyST_Object*)self)->st_node,
414-
PyTuple_New, PyTuple_SetItem, lineno, col_offset);
406+
PyTuple_New, PyTuple_SetItem, line_info, col_info);
415407
}
416408
return (res);
417409
}
@@ -426,35 +418,27 @@ parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
426418
static PyObject*
427419
parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
428420
{
429-
PyObject *line_option = 0;
430-
PyObject *col_option = 0;
421+
int line_info = 0;
422+
int col_info = 0;
431423
PyObject *res = 0;
432424
int ok;
433425

434426
static char *keywords[] = {"st", "line_info", "col_info", NULL};
435427

436428
if (self == NULL || PyModule_Check(self))
437-
ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
438-
&PyST_Type, &self, &line_option,
439-
&col_option);
429+
ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords,
430+
&PyST_Type, &self, &line_info,
431+
&col_info);
440432
else
441-
ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
442-
&line_option, &col_option);
433+
ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1],
434+
&line_info, &col_info);
443435
if (ok) {
444-
int lineno = 0;
445-
int col_offset = 0;
446-
if (line_option != 0) {
447-
lineno = PyObject_IsTrue(line_option) ? 1 : 0;
448-
}
449-
if (col_option != NULL) {
450-
col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
451-
}
452436
/*
453437
* Convert ST into a tuple representation. Use Guido's function,
454438
* since it's known to work already.
455439
*/
456440
res = node2tuple(self->st_node,
457-
PyList_New, PyList_SetItem, lineno, col_offset);
441+
PyList_New, PyList_SetItem, line_info, col_info);
458442
}
459443
return (res);
460444
}

Modules/pyexpat.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,14 +1033,11 @@ getting the advantage of providing document type information to the parser.\n\
10331033
static PyObject *
10341034
xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
10351035
{
1036-
PyObject *flagobj = NULL;
1037-
XML_Bool flag = XML_TRUE;
1036+
int flag = 1;
10381037
enum XML_Error rc;
1039-
if (!PyArg_UnpackTuple(args, "UseForeignDTD", 0, 1, &flagobj))
1038+
if (!PyArg_ParseTuple(args, "p:UseForeignDTD", &flag))
10401039
return NULL;
1041-
if (flagobj != NULL)
1042-
flag = PyObject_IsTrue(flagobj) ? XML_TRUE : XML_FALSE;
1043-
rc = XML_UseForeignDTD(self->itself, flag);
1040+
rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
10441041
if (rc != XML_ERROR_NONE) {
10451042
return set_error(self, rc);
10461043
}
@@ -1405,7 +1402,10 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
14051402
}
14061403
assert(PyUnicode_Check(name));
14071404
if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
1408-
if (PyObject_IsTrue(v)) {
1405+
int b = PyObject_IsTrue(v);
1406+
if (b < 0)
1407+
return -1;
1408+
if (b) {
14091409
if (self->buffer == NULL) {
14101410
self->buffer = malloc(self->buffer_size);
14111411
if (self->buffer == NULL) {
@@ -1424,25 +1424,25 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
14241424
return 0;
14251425
}
14261426
if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
1427-
if (PyObject_IsTrue(v))
1428-
self->ns_prefixes = 1;
1429-
else
1430-
self->ns_prefixes = 0;
1427+
int b = PyObject_IsTrue(v);
1428+
if (b < 0)
1429+
return -1;
1430+
self->ns_prefixes = b;
14311431
XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
14321432
return 0;
14331433
}
14341434
if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
1435-
if (PyObject_IsTrue(v))
1436-
self->ordered_attributes = 1;
1437-
else
1438-
self->ordered_attributes = 0;
1435+
int b = PyObject_IsTrue(v);
1436+
if (b < 0)
1437+
return -1;
1438+
self->ordered_attributes = b;
14391439
return 0;
14401440
}
14411441
if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
1442-
if (PyObject_IsTrue(v))
1443-
self->specified_attributes = 1;
1444-
else
1445-
self->specified_attributes = 0;
1442+
int b = PyObject_IsTrue(v);
1443+
if (b < 0)
1444+
return -1;
1445+
self->specified_attributes = b;
14461446
return 0;
14471447
}
14481448

Objects/typeobject.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,15 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
383383
abc.ABCMeta.__new__, so this function doesn't do anything
384384
special to update subclasses.
385385
*/
386-
int res;
386+
int abstract, res;
387387
if (value != NULL) {
388+
abstract = PyObject_IsTrue(value);
389+
if (abstract < 0)
390+
return -1;
388391
res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
389392
}
390393
else {
394+
abstract = 0;
391395
res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
392396
if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
393397
PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
@@ -396,12 +400,10 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
396400
}
397401
if (res == 0) {
398402
PyType_Modified(type);
399-
if (value && PyObject_IsTrue(value)) {
403+
if (abstract)
400404
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
401-
}
402-
else {
405+
else
403406
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
404-
}
405407
}
406408
return res;
407409
}

Python/bltinmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,11 @@ filter_next(filterobject *lz)
429429
ok = PyObject_IsTrue(good);
430430
Py_DECREF(good);
431431
}
432-
if (ok)
432+
if (ok > 0)
433433
return item;
434434
Py_DECREF(item);
435+
if (ok < 0)
436+
return NULL;
435437
}
436438
}
437439

0 commit comments

Comments
 (0)