Skip to content

gh-117518: Add PyTuple_GetItemRef() function #117519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Doc/c-api/tuple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,21 @@ Tuple Objects
no error checking is performed.


.. c:function:: PyObject* PyTuple_GetItemRef(PyObject *p, Py_ssize_t pos)

Return a :term:`strong reference` to the object at position *pos* in the
tuple pointed to by *p*.

If *pos* is negative or out of bounds, return ``NULL`` and set an
:exc:`IndexError` exception.

.. versionadded:: 3.13


.. c:function:: PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)

Return the object at position *pos* in the tuple pointed to by *p*. If *pos* is
negative or out of bounds, return ``NULL`` and set an :exc:`IndexError` exception.
Similar to :c:func:`PyTuple_GetItemRef`, but return a :term:`borrowed
reference`.


.. c:function:: PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,11 @@ New Features
* Add :c:func:`PyType_GetModuleByDef` to the limited C API
(Contributed by Victor Stinner in :gh:`116936`.)

* Add :c:func:`PyTuple_GetItemRef` function, similar to
:c:func:`PyTuple_GetItem` but return a :term:`strong reference` instead of a
:term:`borrowed reference`.
(Contributed by Victor Stinner in :gh:`117518`.)


Porting to Python 3.13
----------------------
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Include/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ PyAPI_DATA(PyTypeObject) PyTupleIter_Type;
PyAPI_FUNC(PyObject *) PyTuple_New(Py_ssize_t size);
PyAPI_FUNC(Py_ssize_t) PyTuple_Size(PyObject *);
PyAPI_FUNC(PyObject *) PyTuple_GetItem(PyObject *, Py_ssize_t);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
PyAPI_FUNC(PyObject *) PyTuple_GetItemRef(PyObject *, Py_ssize_t);
#endif
PyAPI_FUNC(int) PyTuple_SetItem(PyObject *, Py_ssize_t, PyObject *);
PyAPI_FUNC(PyObject *) PyTuple_GetSlice(PyObject *, Py_ssize_t, Py_ssize_t);
PyAPI_FUNC(PyObject *) PyTuple_Pack(Py_ssize_t, ...);
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :c:func:`PyTuple_GetItemRef` function, similar to
:c:func:`PyTuple_GetItem` but return a :term:`strong reference` instead of a
:term:`borrowed reference`. Patch by Victor Stinner.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2506,3 +2506,5 @@
added = '3.13'
[function.PyType_GetModuleByDef]
added = '3.13'
[function.PyTuple_GetItemRef]
added = '3.13'
6 changes: 3 additions & 3 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ divide_nearest(PyObject *m, PyObject *n)
temp = _PyLong_DivmodNear(m, n);
if (temp == NULL)
return NULL;
result = Py_NewRef(PyTuple_GET_ITEM(temp, 0));
result = PyTuple_GetItemRef(temp, 0);
Py_DECREF(temp);

return result;
Expand Down Expand Up @@ -1978,7 +1978,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
goto BadDivmod;
}

num = Py_NewRef(PyTuple_GET_ITEM(tuple, 0)); /* leftover seconds */
num = PyTuple_GetItemRef(tuple, 0); /* leftover seconds */
Py_DECREF(tuple);

tuple = checked_divmod(num, st->seconds_per_day);
Expand All @@ -1996,7 +1996,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
goto BadDivmod;
}

num = Py_NewRef(PyTuple_GET_ITEM(tuple, 0)); /* leftover days */
num = PyTuple_GetItemRef(tuple, 0); /* leftover days */
d = PyLong_AsInt(num);
if (d == -1 && PyErr_Occurred()) {
goto Done;
Expand Down
4 changes: 2 additions & 2 deletions Modules/_testcapi/heaptype.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ test_from_spec_invalid_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(
"TypeError args are not a one-tuple");
goto finally;
}
message = Py_NewRef(PyTuple_GET_ITEM(args, 0));
message = PyTuple_GetItemRef(args, 0);
meta_error_string = PyUnicode_FromString("metaclass conflict:");
if (meta_error_string == NULL) {
goto finally;
Expand Down Expand Up @@ -1028,7 +1028,7 @@ HeapCCollection_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
}

for (Py_ssize_t i = 0; i < size; i++) {
data[i] = Py_NewRef(PyTuple_GET_ITEM(args, i));
data[i] = PyTuple_GetItemRef(args, i);
}

result = self;
Expand Down
10 changes: 5 additions & 5 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
if (size == 0)
return 0;
if (size == 1) {
Py_XSETREF(self->code, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
Py_XSETREF(self->code, PyTuple_GetItemRef(args, 0));
}
else { /* size > 1 */
Py_XSETREF(self->code, Py_NewRef(args));
Expand Down Expand Up @@ -1546,7 +1546,7 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
Py_XSETREF(self->name_from, Py_XNewRef(name_from));

if (PyTuple_GET_SIZE(args) == 1) {
msg = Py_NewRef(PyTuple_GET_ITEM(args, 0));
msg = PyTuple_GetItemRef(args, 0);
}
Py_XSETREF(self->msg, msg);

Expand Down Expand Up @@ -2034,8 +2034,8 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
if (!args)
return NULL;

PyTuple_SET_ITEM(args, 0, Py_NewRef(PyTuple_GET_ITEM(self->args, 0)));
PyTuple_SET_ITEM(args, 1, Py_NewRef(PyTuple_GET_ITEM(self->args, 1)));
PyTuple_SET_ITEM(args, 0, PyTuple_GetItemRef(self->args, 0));
PyTuple_SET_ITEM(args, 1, PyTuple_GetItemRef(self->args, 1));
PyTuple_SET_ITEM(args, 2, Py_NewRef(self->filename));

if (self->filename2) {
Expand Down Expand Up @@ -2389,7 +2389,7 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
return -1;

if (lenargs >= 1) {
Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
Py_XSETREF(self->msg, PyTuple_GetItemRef(args, 0));
}
if (lenargs == 2) {
info = PyTuple_GET_ITEM(args, 1);
Expand Down
2 changes: 1 addition & 1 deletion Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ struct_unpack_single(const char *ptr, struct unpacker *x)
return NULL;

if (PyTuple_GET_SIZE(v) == 1) {
PyObject *res = Py_NewRef(PyTuple_GET_ITEM(v, 0));
PyObject *res = PyTuple_GetItemRef(v, 0);
Py_DECREF(v);
return res;
}
Expand Down
9 changes: 9 additions & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ PyTuple_GetItem(PyObject *op, Py_ssize_t i)
return ((PyTupleObject *)op) -> ob_item[i];
}


PyObject *
PyTuple_GetItemRef(PyObject *op, Py_ssize_t index)
{
PyObject *item = PyTuple_GetItem(op, index);
return Py_XNewRef(item);
}


int
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
{
Expand Down
4 changes: 2 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6453,8 +6453,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
Py_DECREF(newargs);
return -1;
}
*args = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
*kwargs = Py_NewRef(PyTuple_GET_ITEM(newargs, 1));
*args = PyTuple_GetItemRef(newargs, 0);
*kwargs = PyTuple_GetItemRef(newargs, 1);
Py_DECREF(newargs);

/* XXX We should perhaps allow None to be passed here. */
Expand Down
2 changes: 1 addition & 1 deletion Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ union_getitem(PyObject *self, PyObject *item)
res = make_union(newargs);
}
else {
res = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
res = PyTuple_GetItemRef(newargs, 0);
for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
Py_SETREF(res, PyNumber_Or(res, arg));
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,8 @@ dummy_func(
DEOPT_IF(!PyTuple_CheckExact(seq));
DEOPT_IF(PyTuple_GET_SIZE(seq) != 2);
STAT_INC(UNPACK_SEQUENCE, hit);
val0 = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
val1 = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
val0 = PyTuple_GetItemRef(seq, 0);
val1 = PyTuple_GetItemRef(seq, 1);
DECREF_INPUTS();
}

Expand Down Expand Up @@ -2702,7 +2702,7 @@ dummy_func(
PyTupleObject *seq = it->it_seq;
assert(seq);
assert(it->it_index < PyTuple_GET_SIZE(seq));
next = Py_NewRef(PyTuple_GET_ITEM(seq, it->it_index++));
next = PyTuple_GetItemRef((PyObject*)seq, it->it_index++);
}

macro(FOR_ITER_TUPLE) =
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,8 +2031,8 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
}
assert(PyTuple_CheckExact(pair));
assert(PyTuple_GET_SIZE(pair) == 2);
*match = Py_NewRef(PyTuple_GET_ITEM(pair, 0));
*rest = Py_NewRef(PyTuple_GET_ITEM(pair, 1));
*match = PyTuple_GetItemRef(pair, 0);
*rest = PyTuple_GetItemRef(pair, 1);
Py_DECREF(pair);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ _PyCodec_EncodeInternal(PyObject *object,
"encoder must return a tuple (object, integer)");
goto onError;
}
v = Py_NewRef(PyTuple_GET_ITEM(result,0));
v = PyTuple_GetItemRef(result, 0);
/* We don't check or use the second (integer) entry. */

Py_DECREF(args);
Expand Down Expand Up @@ -455,7 +455,7 @@ _PyCodec_DecodeInternal(PyObject *object,
"decoder must return a tuple (object,integer)");
goto onError;
}
v = Py_NewRef(PyTuple_GET_ITEM(result,0));
v = PyTuple_GetItemRef(result, 0);
/* We don't check or use the second (integer) entry. */

Py_DECREF(args);
Expand Down Expand Up @@ -550,7 +550,7 @@ PyObject *codec_getitem_checked(const char *encoding,
if (codec == NULL)
return NULL;

v = Py_NewRef(PyTuple_GET_ITEM(codec, index));
v = PyTuple_GetItemRef(codec, index);
Py_DECREF(codec);
return v;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
}
PyObject *u;
if (PyTuple_CheckExact(k)) {
u = Py_NewRef(PyTuple_GET_ITEM(k, 1));
u = PyTuple_GetItemRef(k, 1);
Py_DECREF(k);
}
else {
Expand Down
6 changes: 3 additions & 3 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
if (!skip) {
PyObject *current_arg;
if (i < nargs) {
current_arg = Py_NewRef(PyTuple_GET_ITEM(args, i));
current_arg = PyTuple_GetItemRef(args, i);
}
else if (nkwargs && i >= pos) {
if (PyDict_GetItemStringRef(kwargs, kwlist[i], &current_arg) < 0) {
Expand Down
Loading