gh-111178: Fix function signatures in tupleobject.c (#124804)

This commit is contained in:
Victor Stinner 2024-10-02 13:37:04 +02:00 committed by GitHub
parent 1d3700f943
commit 595a5631d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 64 additions and 58 deletions

View File

@ -181,8 +181,9 @@ PyTuple_Pack(Py_ssize_t n, ...)
/* Methods */ /* Methods */
static void static void
tupledealloc(PyTupleObject *op) tuple_dealloc(PyObject *self)
{ {
PyTupleObject *op = _PyTuple_CAST(self);
if (Py_SIZE(op) == 0) { if (Py_SIZE(op) == 0) {
/* The empty tuple is statically allocated. */ /* The empty tuple is statically allocated. */
if (op == &_Py_SINGLETON(tuple_empty)) { if (op == &_Py_SINGLETON(tuple_empty)) {
@ -199,7 +200,7 @@ tupledealloc(PyTupleObject *op)
} }
PyObject_GC_UnTrack(op); PyObject_GC_UnTrack(op);
Py_TRASHCAN_BEGIN(op, tupledealloc) Py_TRASHCAN_BEGIN(op, tuple_dealloc)
Py_ssize_t i = Py_SIZE(op); Py_ssize_t i = Py_SIZE(op);
while (--i >= 0) { while (--i >= 0) {
@ -214,29 +215,29 @@ tupledealloc(PyTupleObject *op)
} }
static PyObject * static PyObject *
tuplerepr(PyTupleObject *v) tuple_repr(PyObject *self)
{ {
Py_ssize_t i, n; PyTupleObject *v = _PyTuple_CAST(self);
_PyUnicodeWriter writer; Py_ssize_t n = PyTuple_GET_SIZE(v);
if (n == 0) {
n = Py_SIZE(v);
if (n == 0)
return PyUnicode_FromString("()"); return PyUnicode_FromString("()");
}
/* While not mutable, it is still possible to end up with a cycle in a /* While not mutable, it is still possible to end up with a cycle in a
tuple through an object that stores itself within a tuple (and thus tuple through an object that stores itself within a tuple (and thus
infinitely asks for the repr of itself). This should only be infinitely asks for the repr of itself). This should only be
possible within a type. */ possible within a type. */
i = Py_ReprEnter((PyObject *)v); int res = Py_ReprEnter((PyObject *)v);
if (i != 0) { if (res != 0) {
return i > 0 ? PyUnicode_FromString("(...)") : NULL; return res > 0 ? PyUnicode_FromString("(...)") : NULL;
} }
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer); _PyUnicodeWriter_Init(&writer);
writer.overallocate = 1; writer.overallocate = 1;
if (Py_SIZE(v) > 1) { if (n > 1) {
/* "(" + "1" + ", 2" * (len - 1) + ")" */ /* "(" + "1" + ", 2" * (len - 1) + ")" */
writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1; writer.min_length = 1 + 1 + (2 + 1) * (n - 1) + 1;
} }
else { else {
/* "(1,)" */ /* "(1,)" */
@ -247,7 +248,7 @@ tuplerepr(PyTupleObject *v)
goto error; goto error;
/* Do repr() on each element. */ /* Do repr() on each element. */
for (i = 0; i < n; ++i) { for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *s; PyObject *s;
if (i > 0) { if (i > 0) {
@ -316,13 +317,14 @@ error:
/* Tests have shown that it's not worth to cache the hash value, see /* Tests have shown that it's not worth to cache the hash value, see
https://bugs.python.org/issue9685 */ https://bugs.python.org/issue9685 */
static Py_hash_t static Py_hash_t
tuplehash(PyTupleObject *v) tuple_hash(PyObject *op)
{ {
Py_ssize_t i, len = Py_SIZE(v); PyTupleObject *v = _PyTuple_CAST(op);
Py_ssize_t len = Py_SIZE(v);
PyObject **item = v->ob_item; PyObject **item = v->ob_item;
Py_uhash_t acc = _PyHASH_XXPRIME_5; Py_uhash_t acc = _PyHASH_XXPRIME_5;
for (i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
Py_uhash_t lane = PyObject_Hash(item[i]); Py_uhash_t lane = PyObject_Hash(item[i]);
if (lane == (Py_uhash_t)-1) { if (lane == (Py_uhash_t)-1) {
return -1; return -1;
@ -342,25 +344,27 @@ tuplehash(PyTupleObject *v)
} }
static Py_ssize_t static Py_ssize_t
tuplelength(PyTupleObject *a) tuple_length(PyObject *self)
{ {
PyTupleObject *a = _PyTuple_CAST(self);
return Py_SIZE(a); return Py_SIZE(a);
} }
static int static int
tuplecontains(PyTupleObject *a, PyObject *el) tuple_contains(PyObject *self, PyObject *el)
{ {
Py_ssize_t i; PyTupleObject *a = _PyTuple_CAST(self);
int cmp; int cmp = 0;
for (Py_ssize_t i = 0; cmp == 0 && i < Py_SIZE(a); ++i) {
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ); cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
}
return cmp; return cmp;
} }
static PyObject * static PyObject *
tupleitem(PyTupleObject *a, Py_ssize_t i) tuple_item(PyObject *op, Py_ssize_t i)
{ {
PyTupleObject *a = _PyTuple_CAST(op);
if (i < 0 || i >= Py_SIZE(a)) { if (i < 0 || i >= Py_SIZE(a)) {
PyErr_SetString(PyExc_IndexError, "tuple index out of range"); PyErr_SetString(PyExc_IndexError, "tuple index out of range");
return NULL; return NULL;
@ -432,7 +436,7 @@ _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n)
} }
static PyObject * static PyObject *
tupleslice(PyTupleObject *a, Py_ssize_t ilow, tuple_slice(PyTupleObject *a, Py_ssize_t ilow,
Py_ssize_t ihigh) Py_ssize_t ihigh)
{ {
if (ilow < 0) if (ilow < 0)
@ -454,16 +458,13 @@ PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return NULL; return NULL;
} }
return tupleslice((PyTupleObject *)op, i, j); return tuple_slice((PyTupleObject *)op, i, j);
} }
static PyObject * static PyObject *
tupleconcat(PyTupleObject *a, PyObject *bb) tuple_concat(PyObject *aa, PyObject *bb)
{ {
Py_ssize_t size; PyTupleObject *a = _PyTuple_CAST(aa);
Py_ssize_t i;
PyObject **src, **dest;
PyTupleObject *np;
if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) { if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
return Py_NewRef(bb); return Py_NewRef(bb);
} }
@ -479,34 +480,38 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
return Py_NewRef(a); return Py_NewRef(a);
} }
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX); assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
size = Py_SIZE(a) + Py_SIZE(b); Py_ssize_t size = Py_SIZE(a) + Py_SIZE(b);
if (size == 0) { if (size == 0) {
return tuple_get_empty(); return tuple_get_empty();
} }
np = tuple_alloc(size); PyTupleObject *np = tuple_alloc(size);
if (np == NULL) { if (np == NULL) {
return NULL; return NULL;
} }
src = a->ob_item;
dest = np->ob_item; PyObject **src = a->ob_item;
for (i = 0; i < Py_SIZE(a); i++) { PyObject **dest = np->ob_item;
for (Py_ssize_t i = 0; i < Py_SIZE(a); i++) {
PyObject *v = src[i]; PyObject *v = src[i];
dest[i] = Py_NewRef(v); dest[i] = Py_NewRef(v);
} }
src = b->ob_item; src = b->ob_item;
dest = np->ob_item + Py_SIZE(a); dest = np->ob_item + Py_SIZE(a);
for (i = 0; i < Py_SIZE(b); i++) { for (Py_ssize_t i = 0; i < Py_SIZE(b); i++) {
PyObject *v = src[i]; PyObject *v = src[i];
dest[i] = Py_NewRef(v); dest[i] = Py_NewRef(v);
} }
_PyObject_GC_TRACK(np); _PyObject_GC_TRACK(np);
return (PyObject *)np; return (PyObject *)np;
} }
static PyObject * static PyObject *
tuplerepeat(PyTupleObject *a, Py_ssize_t n) tuple_repeat(PyObject *self, Py_ssize_t n)
{ {
PyTupleObject *a = _PyTuple_CAST(self);
const Py_ssize_t input_size = Py_SIZE(a); const Py_ssize_t input_size = Py_SIZE(a);
if (input_size == 0 || n == 1) { if (input_size == 0 || n == 1) {
if (PyTuple_CheckExact(a)) { if (PyTuple_CheckExact(a)) {
@ -621,17 +626,17 @@ tuple_count(PyTupleObject *self, PyObject *value)
} }
static int static int
tupletraverse(PyTupleObject *o, visitproc visit, void *arg) tuple_traverse(PyObject *self, visitproc visit, void *arg)
{ {
Py_ssize_t i; PyTupleObject *o = _PyTuple_CAST(self);
for (Py_ssize_t i = Py_SIZE(o); --i >= 0; ) {
for (i = Py_SIZE(o); --i >= 0; )
Py_VISIT(o->ob_item[i]); Py_VISIT(o->ob_item[i]);
}
return 0; return 0;
} }
static PyObject * static PyObject *
tuplerichcompare(PyObject *v, PyObject *w, int op) tuple_richcompare(PyObject *v, PyObject *w, int op)
{ {
PyTupleObject *vt, *wt; PyTupleObject *vt, *wt;
Py_ssize_t i; Py_ssize_t i;
@ -770,26 +775,27 @@ tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
} }
static PySequenceMethods tuple_as_sequence = { static PySequenceMethods tuple_as_sequence = {
(lenfunc)tuplelength, /* sq_length */ tuple_length, /* sq_length */
(binaryfunc)tupleconcat, /* sq_concat */ tuple_concat, /* sq_concat */
(ssizeargfunc)tuplerepeat, /* sq_repeat */ tuple_repeat, /* sq_repeat */
(ssizeargfunc)tupleitem, /* sq_item */ tuple_item, /* sq_item */
0, /* sq_slice */ 0, /* sq_slice */
0, /* sq_ass_item */ 0, /* sq_ass_item */
0, /* sq_ass_slice */ 0, /* sq_ass_slice */
(objobjproc)tuplecontains, /* sq_contains */ tuple_contains, /* sq_contains */
}; };
static PyObject* static PyObject*
tuplesubscript(PyTupleObject* self, PyObject* item) tuple_subscript(PyObject *op, PyObject* item)
{ {
PyTupleObject *self = _PyTuple_CAST(op);
if (_PyIndex_Check(item)) { if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred()) if (i == -1 && PyErr_Occurred())
return NULL; return NULL;
if (i < 0) if (i < 0)
i += PyTuple_GET_SIZE(self); i += PyTuple_GET_SIZE(self);
return tupleitem(self, i); return tuple_item(op, i);
} }
else if (PySlice_Check(item)) { else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength, i; Py_ssize_t start, stop, step, slicelength, i;
@ -843,7 +849,7 @@ static PyObject *
tuple___getnewargs___impl(PyTupleObject *self) tuple___getnewargs___impl(PyTupleObject *self)
/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/ /*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
{ {
return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self))); return Py_BuildValue("(N)", tuple_slice(self, 0, Py_SIZE(self)));
} }
static PyMethodDef tuple_methods[] = { static PyMethodDef tuple_methods[] = {
@ -855,8 +861,8 @@ static PyMethodDef tuple_methods[] = {
}; };
static PyMappingMethods tuple_as_mapping = { static PyMappingMethods tuple_as_mapping = {
(lenfunc)tuplelength, tuple_length,
(binaryfunc)tuplesubscript, tuple_subscript,
0 0
}; };
@ -867,16 +873,16 @@ PyTypeObject PyTuple_Type = {
"tuple", "tuple",
sizeof(PyTupleObject) - sizeof(PyObject *), sizeof(PyTupleObject) - sizeof(PyObject *),
sizeof(PyObject *), sizeof(PyObject *),
(destructor)tupledealloc, /* tp_dealloc */ tuple_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */ 0, /* tp_vectorcall_offset */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_as_async */ 0, /* tp_as_async */
(reprfunc)tuplerepr, /* tp_repr */ tuple_repr, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
&tuple_as_sequence, /* tp_as_sequence */ &tuple_as_sequence, /* tp_as_sequence */
&tuple_as_mapping, /* tp_as_mapping */ &tuple_as_mapping, /* tp_as_mapping */
(hashfunc)tuplehash, /* tp_hash */ tuple_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
@ -886,9 +892,9 @@ PyTypeObject PyTuple_Type = {
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS |
_Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */ _Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
tuple_new__doc__, /* tp_doc */ tuple_new__doc__, /* tp_doc */
(traverseproc)tupletraverse, /* tp_traverse */ tuple_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
tuplerichcompare, /* tp_richcompare */ tuple_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
tuple_iter, /* tp_iter */ tuple_iter, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */