From e5b8c63cce18a89b5fa5cb64bc373233d9b845df Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 1 Sep 2022 13:51:51 -0400 Subject: [PATCH] Use PyTuple_Pack as-is in more places --- Modules/_json.c | 10 ++++---- Objects/dictobject.c | 18 +++++---------- Objects/enumobject.c | 22 +++++------------- Objects/longobject.c | 22 +++++------------- Objects/odictobject.c | 14 ++++++------ Objects/stringlib/partition.h | 43 ++++++++++++++++++++--------------- 6 files changed, 54 insertions(+), 75 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 1c39b46937d792..fcac1d6b68fde4 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -353,14 +353,12 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) { Py_DECREF(rval); return NULL; } - tpl = PyTuple_New(2); + tpl = PyTuple_Pack(2, rval, pyidx); + Py_DECREF(pyidx); + Py_DECREF(rval); if (tpl == NULL) { - Py_DECREF(pyidx); - Py_DECREF(rval); return NULL; } - PyTuple_SET_ITEM(tpl, 0, rval); - PyTuple_SET_ITEM(tpl, 1, pyidx); return tpl; } @@ -1530,7 +1528,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir if (*first) { *first = false; - } + } else { if (_PyUnicodeWriter_WriteStr(writer, s->item_separator) < 0) { Py_DECREF(keystr); diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 0cb95d52360ef1..fdc09596243e24 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4263,12 +4263,12 @@ dictiter_iternextitem(dictiterobject *di) } di->di_pos = i+1; di->len--; - Py_INCREF(key); - Py_INCREF(value); result = di->di_result; if (Py_REFCNT(result) == 1) { PyObject *oldkey = PyTuple_GET_ITEM(result, 0); PyObject *oldvalue = PyTuple_GET_ITEM(result, 1); + Py_INCREF(key); + Py_INCREF(value); PyTuple_SET_ITEM(result, 0, key); /* steals reference */ PyTuple_SET_ITEM(result, 1, value); /* steals reference */ Py_INCREF(result); @@ -4281,11 +4281,7 @@ dictiter_iternextitem(dictiterobject *di) } } else { - result = PyTuple_New(2); - if (result == NULL) - return NULL; - PyTuple_SET_ITEM(result, 0, key); /* steals reference */ - PyTuple_SET_ITEM(result, 1, value); /* steals reference */ + result = PyTuple_Pack(2, key, value); } return result; @@ -4397,10 +4393,10 @@ dictreviter_iternext(dictiterobject *di) return value; } else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) { - Py_INCREF(key); - Py_INCREF(value); result = di->di_result; if (Py_REFCNT(result) == 1) { + Py_INCREF(key); + Py_INCREF(value); PyObject *oldkey = PyTuple_GET_ITEM(result, 0); PyObject *oldvalue = PyTuple_GET_ITEM(result, 1); PyTuple_SET_ITEM(result, 0, key); /* steals reference */ @@ -4415,12 +4411,10 @@ dictreviter_iternext(dictiterobject *di) } } else { - result = PyTuple_New(2); + result = PyTuple_Pack(2, key, value); if (result == NULL) { return NULL; } - PyTuple_SET_ITEM(result, 0, key); /* steals reference */ - PyTuple_SET_ITEM(result, 1, value); /* steals reference */ } return result; } diff --git a/Objects/enumobject.c b/Objects/enumobject.c index d84bac6f4c9af2..6cf0fa0f5469a7 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -207,14 +207,9 @@ enum_next_long(enumobject *en, PyObject* next_item) } return result; } - result = PyTuple_New(2); - if (result == NULL) { - Py_DECREF(next_index); - Py_DECREF(next_item); - return NULL; - } - PyTuple_SET_ITEM(result, 0, next_index); - PyTuple_SET_ITEM(result, 1, next_item); + result = PyTuple_Pack(2, next_index, next_item); + Py_DECREF(next_index); + Py_DECREF(next_item); return result; } @@ -257,14 +252,9 @@ enum_next(enumobject *en) } return result; } - result = PyTuple_New(2); - if (result == NULL) { - Py_DECREF(next_index); - Py_DECREF(next_item); - return NULL; - } - PyTuple_SET_ITEM(result, 0, next_index); - PyTuple_SET_ITEM(result, 1, next_item); + result = PyTuple_Pack(2, next_index, next_item); + Py_DECREF(next_index); + Py_DECREF(next_item); return result; } diff --git a/Objects/longobject.c b/Objects/longobject.c index 90ed02b8c27a19..2e2f871e955f79 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4249,15 +4249,9 @@ long_divmod(PyObject *a, PyObject *b) if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { return NULL; } - z = PyTuple_New(2); - if (z != NULL) { - PyTuple_SET_ITEM(z, 0, (PyObject *) div); - PyTuple_SET_ITEM(z, 1, (PyObject *) mod); - } - else { - Py_DECREF(div); - Py_DECREF(mod); - } + z = PyTuple_Pack(2, div, mod); + Py_DECREF(div); + Py_DECREF(mod); return z; } @@ -5544,13 +5538,9 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b) goto error; } - result = PyTuple_New(2); - if (result == NULL) - goto error; - - /* PyTuple_SET_ITEM steals references */ - PyTuple_SET_ITEM(result, 0, (PyObject *)quo); - PyTuple_SET_ITEM(result, 1, (PyObject *)rem); + result = PyTuple_Pack(2, quo, rem); + Py_DECREF(quo); + Py_DECREF(rem); return result; error: diff --git a/Objects/odictobject.c b/Objects/odictobject.c index bd2a7677fe1cf4..2178b47b7982db 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -1768,18 +1768,18 @@ odictiter_iternext(odictiterobject *di) if (!_PyObject_GC_IS_TRACKED(result)) { _PyObject_GC_TRACK(result); } - } - else { - result = PyTuple_New(2); + PyTuple_SET_ITEM(result, 0, key); /* steals reference */ + PyTuple_SET_ITEM(result, 1, value); /* steals reference */ + } else { + result = PyTuple_Pack(2, key, value); + Py_DECREF(key); + Py_DECREF(value); + if (result == NULL) { - Py_DECREF(key); - Py_DECREF(value); goto done; } } - PyTuple_SET_ITEM(result, 0, key); /* steals reference */ - PyTuple_SET_ITEM(result, 1, value); /* steals reference */ return result; done: diff --git a/Objects/stringlib/partition.h b/Objects/stringlib/partition.h index bcc217697b2e9c..9d49a180cbbedb 100644 --- a/Objects/stringlib/partition.h +++ b/Objects/stringlib/partition.h @@ -1,5 +1,6 @@ /* stringlib: partition implementation */ +#include #ifndef STRINGLIB_FASTSEARCH_H # error must include "stringlib/fastsearch.h" before including this module #endif @@ -23,40 +24,47 @@ STRINGLIB(partition)(PyObject* str_obj, return NULL; } - out = PyTuple_New(3); - if (!out) - return NULL; - pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH); if (pos < 0) { #if STRINGLIB_MUTABLE - PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len)); - PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0)); - PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0)); + out = PyTuple_Pack( + 3, + STRINGLIB_NEW(str, str_len), + STRINGLIB_NEW(NULL, 0), + STRINGLIB_NEW(NULL, 0) + ); + + if (out == NULL) { + return NULL; + } + + for (int i = 0; i < 3; ++i) { + Py_DECREF(PyTuple_GET_ITEM(out, i)); + } if (PyErr_Occurred()) { Py_DECREF(out); return NULL; } #else - Py_INCREF(str_obj); - PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY(); assert(empty != NULL); - Py_INCREF(empty); - PyTuple_SET_ITEM(out, 1, empty); - Py_INCREF(empty); - PyTuple_SET_ITEM(out, 2, empty); + out = PyTuple_Pack(3, str_obj, empty, empty); #endif return out; } - PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); - Py_INCREF(sep_obj); - PyTuple_SET_ITEM(out, 1, sep_obj); + PyObject *a = STRINGLIB_NEW(str, pos); pos += sep_len; - PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); + PyObject *b = STRINGLIB_NEW(str + pos, str_len - pos); + out = PyTuple_Pack(3, a, sep_obj, b); + Py_DECREF(a); + Py_DECREF(b); + + if (out == NULL) { + return NULL; + } if (PyErr_Occurred()) { Py_DECREF(out); @@ -122,4 +130,3 @@ STRINGLIB(rpartition)(PyObject* str_obj, return out; } -