Skip to content

Commit 6a5ae97

Browse files
committed
gh-111178: Fix function signatures in tupleobject.c
1 parent 91e64be commit 6a5ae97

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

Objects/tupleobject.c

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ PyTuple_Pack(Py_ssize_t n, ...)
181181
/* Methods */
182182

183183
static void
184-
tupledealloc(PyTupleObject *op)
184+
tuple_dealloc(PyObject *self)
185185
{
186+
PyTupleObject *op = _PyTuple_CAST(self);
186187
if (Py_SIZE(op) == 0) {
187188
/* The empty tuple is statically allocated. */
188189
if (op == &_Py_SINGLETON(tuple_empty)) {
@@ -199,7 +200,7 @@ tupledealloc(PyTupleObject *op)
199200
}
200201

201202
PyObject_GC_UnTrack(op);
202-
Py_TRASHCAN_BEGIN(op, tupledealloc)
203+
Py_TRASHCAN_BEGIN(op, tuple_dealloc)
203204

204205
Py_ssize_t i = Py_SIZE(op);
205206
while (--i >= 0) {
@@ -214,29 +215,29 @@ tupledealloc(PyTupleObject *op)
214215
}
215216

216217
static PyObject *
217-
tuplerepr(PyTupleObject *v)
218+
tuple_repr(PyObject *self)
218219
{
219-
Py_ssize_t i, n;
220-
_PyUnicodeWriter writer;
221-
222-
n = Py_SIZE(v);
223-
if (n == 0)
220+
PyTupleObject *v = _PyTuple_CAST(self);
221+
Py_ssize_t n = PyTuple_GET_SIZE(v);
222+
if (n == 0) {
224223
return PyUnicode_FromString("()");
224+
}
225225

226226
/* While not mutable, it is still possible to end up with a cycle in a
227227
tuple through an object that stores itself within a tuple (and thus
228228
infinitely asks for the repr of itself). This should only be
229229
possible within a type. */
230-
i = Py_ReprEnter((PyObject *)v);
231-
if (i != 0) {
232-
return i > 0 ? PyUnicode_FromString("(...)") : NULL;
230+
int res = Py_ReprEnter((PyObject *)v);
231+
if (res != 0) {
232+
return res > 0 ? PyUnicode_FromString("(...)") : NULL;
233233
}
234234

235+
_PyUnicodeWriter writer;
235236
_PyUnicodeWriter_Init(&writer);
236237
writer.overallocate = 1;
237-
if (Py_SIZE(v) > 1) {
238+
if (n > 1) {
238239
/* "(" + "1" + ", 2" * (len - 1) + ")" */
239-
writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
240+
writer.min_length = 1 + 1 + (2 + 1) * (n - 1) + 1;
240241
}
241242
else {
242243
/* "(1,)" */
@@ -247,7 +248,7 @@ tuplerepr(PyTupleObject *v)
247248
goto error;
248249

249250
/* Do repr() on each element. */
250-
for (i = 0; i < n; ++i) {
251+
for (Py_ssize_t i = 0; i < n; ++i) {
251252
PyObject *s;
252253

253254
if (i > 0) {
@@ -316,8 +317,9 @@ tuplerepr(PyTupleObject *v)
316317
/* Tests have shown that it's not worth to cache the hash value, see
317318
https://bugs.python.org/issue9685 */
318319
static Py_hash_t
319-
tuplehash(PyTupleObject *v)
320+
tuple_hash(PyObject *op)
320321
{
322+
PyTupleObject *v = _PyTuple_CAST(op);
321323
Py_ssize_t i, len = Py_SIZE(v);
322324
PyObject **item = v->ob_item;
323325

@@ -342,25 +344,27 @@ tuplehash(PyTupleObject *v)
342344
}
343345

344346
static Py_ssize_t
345-
tuplelength(PyTupleObject *a)
347+
tuple_length(PyObject *self)
346348
{
349+
PyTupleObject *a = _PyTuple_CAST(self);
347350
return Py_SIZE(a);
348351
}
349352

350353
static int
351-
tuplecontains(PyTupleObject *a, PyObject *el)
354+
tuple_contains(PyObject *self, PyObject *el)
352355
{
353-
Py_ssize_t i;
354-
int cmp;
355-
356-
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
356+
PyTupleObject *a = _PyTuple_CAST(self);
357+
int cmp = 0;
358+
for (Py_ssize_t i = 0; cmp == 0 && i < Py_SIZE(a); ++i) {
357359
cmp = PyObject_RichCompareBool(PyTuple_GET_ITEM(a, i), el, Py_EQ);
360+
}
358361
return cmp;
359362
}
360363

361364
static PyObject *
362-
tupleitem(PyTupleObject *a, Py_ssize_t i)
365+
tuple_item(PyObject *op, Py_ssize_t i)
363366
{
367+
PyTupleObject *a = _PyTuple_CAST(op);
364368
if (i < 0 || i >= Py_SIZE(a)) {
365369
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
366370
return NULL;
@@ -432,7 +436,7 @@ _PyTuple_FromArraySteal(PyObject *const *src, Py_ssize_t n)
432436
}
433437

434438
static PyObject *
435-
tupleslice(PyTupleObject *a, Py_ssize_t ilow,
439+
tuple_slice(PyTupleObject *a, Py_ssize_t ilow,
436440
Py_ssize_t ihigh)
437441
{
438442
if (ilow < 0)
@@ -454,16 +458,13 @@ PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
454458
PyErr_BadInternalCall();
455459
return NULL;
456460
}
457-
return tupleslice((PyTupleObject *)op, i, j);
461+
return tuple_slice((PyTupleObject *)op, i, j);
458462
}
459463

460464
static PyObject *
461-
tupleconcat(PyTupleObject *a, PyObject *bb)
465+
tuple_concat(PyObject *aa, PyObject *bb)
462466
{
463-
Py_ssize_t size;
464-
Py_ssize_t i;
465-
PyObject **src, **dest;
466-
PyTupleObject *np;
467+
PyTupleObject *a = _PyTuple_CAST(aa);
467468
if (Py_SIZE(a) == 0 && PyTuple_CheckExact(bb)) {
468469
return Py_NewRef(bb);
469470
}
@@ -479,34 +480,38 @@ tupleconcat(PyTupleObject *a, PyObject *bb)
479480
return Py_NewRef(a);
480481
}
481482
assert((size_t)Py_SIZE(a) + (size_t)Py_SIZE(b) < PY_SSIZE_T_MAX);
482-
size = Py_SIZE(a) + Py_SIZE(b);
483+
Py_ssize_t size = Py_SIZE(a) + Py_SIZE(b);
483484
if (size == 0) {
484485
return tuple_get_empty();
485486
}
486487

487-
np = tuple_alloc(size);
488+
PyTupleObject *np = tuple_alloc(size);
488489
if (np == NULL) {
489490
return NULL;
490491
}
491-
src = a->ob_item;
492-
dest = np->ob_item;
493-
for (i = 0; i < Py_SIZE(a); i++) {
492+
493+
PyObject **src = a->ob_item;
494+
PyObject **dest = np->ob_item;
495+
for (Py_ssize_t i = 0; i < Py_SIZE(a); i++) {
494496
PyObject *v = src[i];
495497
dest[i] = Py_NewRef(v);
496498
}
499+
497500
src = b->ob_item;
498501
dest = np->ob_item + Py_SIZE(a);
499-
for (i = 0; i < Py_SIZE(b); i++) {
502+
for (Py_ssize_t i = 0; i < Py_SIZE(b); i++) {
500503
PyObject *v = src[i];
501504
dest[i] = Py_NewRef(v);
502505
}
506+
503507
_PyObject_GC_TRACK(np);
504508
return (PyObject *)np;
505509
}
506510

507511
static PyObject *
508-
tuplerepeat(PyTupleObject *a, Py_ssize_t n)
512+
tuple_repeat(PyObject *self, Py_ssize_t n)
509513
{
514+
PyTupleObject *a = _PyTuple_CAST(self);
510515
const Py_ssize_t input_size = Py_SIZE(a);
511516
if (input_size == 0 || n == 1) {
512517
if (PyTuple_CheckExact(a)) {
@@ -621,17 +626,16 @@ tuple_count(PyTupleObject *self, PyObject *value)
621626
}
622627

623628
static int
624-
tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
629+
tuple_traverse(PyObject *self, visitproc visit, void *arg)
625630
{
626-
Py_ssize_t i;
627-
628-
for (i = Py_SIZE(o); --i >= 0; )
631+
PyTupleObject *o = _PyTuple_CAST(self);
632+
for (Py_ssize_t i = Py_SIZE(o); --i >= 0; )
629633
Py_VISIT(o->ob_item[i]);
630634
return 0;
631635
}
632636

633637
static PyObject *
634-
tuplerichcompare(PyObject *v, PyObject *w, int op)
638+
tuple_richcompare(PyObject *v, PyObject *w, int op)
635639
{
636640
PyTupleObject *vt, *wt;
637641
Py_ssize_t i;
@@ -770,26 +774,27 @@ tuple_subtype_new(PyTypeObject *type, PyObject *iterable)
770774
}
771775

772776
static PySequenceMethods tuple_as_sequence = {
773-
(lenfunc)tuplelength, /* sq_length */
774-
(binaryfunc)tupleconcat, /* sq_concat */
775-
(ssizeargfunc)tuplerepeat, /* sq_repeat */
776-
(ssizeargfunc)tupleitem, /* sq_item */
777+
tuple_length, /* sq_length */
778+
tuple_concat, /* sq_concat */
779+
tuple_repeat, /* sq_repeat */
780+
tuple_item, /* sq_item */
777781
0, /* sq_slice */
778782
0, /* sq_ass_item */
779783
0, /* sq_ass_slice */
780-
(objobjproc)tuplecontains, /* sq_contains */
784+
tuple_contains, /* sq_contains */
781785
};
782786

783787
static PyObject*
784-
tuplesubscript(PyTupleObject* self, PyObject* item)
788+
tuple_subscript(PyObject *op, PyObject* item)
785789
{
790+
PyTupleObject *self = _PyTuple_CAST(op);
786791
if (_PyIndex_Check(item)) {
787792
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
788793
if (i == -1 && PyErr_Occurred())
789794
return NULL;
790795
if (i < 0)
791796
i += PyTuple_GET_SIZE(self);
792-
return tupleitem(self, i);
797+
return tuple_item(op, i);
793798
}
794799
else if (PySlice_Check(item)) {
795800
Py_ssize_t start, stop, step, slicelength, i;
@@ -843,7 +848,7 @@ static PyObject *
843848
tuple___getnewargs___impl(PyTupleObject *self)
844849
/*[clinic end generated code: output=25e06e3ee56027e2 input=1aeb4b286a21639a]*/
845850
{
846-
return Py_BuildValue("(N)", tupleslice(self, 0, Py_SIZE(self)));
851+
return Py_BuildValue("(N)", tuple_slice(self, 0, Py_SIZE(self)));
847852
}
848853

849854
static PyMethodDef tuple_methods[] = {
@@ -855,8 +860,8 @@ static PyMethodDef tuple_methods[] = {
855860
};
856861

857862
static PyMappingMethods tuple_as_mapping = {
858-
(lenfunc)tuplelength,
859-
(binaryfunc)tuplesubscript,
863+
tuple_length,
864+
tuple_subscript,
860865
0
861866
};
862867

@@ -867,16 +872,16 @@ PyTypeObject PyTuple_Type = {
867872
"tuple",
868873
sizeof(PyTupleObject) - sizeof(PyObject *),
869874
sizeof(PyObject *),
870-
(destructor)tupledealloc, /* tp_dealloc */
875+
tuple_dealloc, /* tp_dealloc */
871876
0, /* tp_vectorcall_offset */
872877
0, /* tp_getattr */
873878
0, /* tp_setattr */
874879
0, /* tp_as_async */
875-
(reprfunc)tuplerepr, /* tp_repr */
880+
tuple_repr, /* tp_repr */
876881
0, /* tp_as_number */
877882
&tuple_as_sequence, /* tp_as_sequence */
878883
&tuple_as_mapping, /* tp_as_mapping */
879-
(hashfunc)tuplehash, /* tp_hash */
884+
tuple_hash, /* tp_hash */
880885
0, /* tp_call */
881886
0, /* tp_str */
882887
PyObject_GenericGetAttr, /* tp_getattro */
@@ -886,9 +891,9 @@ PyTypeObject PyTuple_Type = {
886891
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS |
887892
_Py_TPFLAGS_MATCH_SELF | Py_TPFLAGS_SEQUENCE, /* tp_flags */
888893
tuple_new__doc__, /* tp_doc */
889-
(traverseproc)tupletraverse, /* tp_traverse */
894+
tuple_traverse, /* tp_traverse */
890895
0, /* tp_clear */
891-
tuplerichcompare, /* tp_richcompare */
896+
tuple_richcompare, /* tp_richcompare */
892897
0, /* tp_weaklistoffset */
893898
tuple_iter, /* tp_iter */
894899
0, /* tp_iternext */

0 commit comments

Comments
 (0)