Skip to content

[WIP] bpo-42294: Add PyTuple_GetItemRef() function #23207

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 2 commits 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
13 changes: 11 additions & 2 deletions Doc/c-api/tuple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ 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 out of bounds, return ``NULL`` and set
an :exc:`IndexError` exception.

.. versionadded:: 3.10


.. 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
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
4 changes: 4 additions & 0 deletions Doc/data/refcounts.dat
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,10 @@ PyTuple_GetItem:PyObject*::0:
PyTuple_GetItem:PyObject*:p:0:
PyTuple_GetItem:Py_ssize_t:pos::

PyTuple_GetItemRef:PyObject*::+1:
PyTuple_GetItemRef:PyObject*:p:0:
PyTuple_GetItemRef:Py_ssize_t:pos::

PyTuple_GET_SIZE:Py_ssize_t:::
PyTuple_GET_SIZE:PyObject*:p:0:

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ New Features
slot.
(Contributed by Hai Shi in :issue:`41832`.)

* Added :c:func:`PyTuple_GetItemRef` function: similar to
:c:func:`PyTuple_GetItem`, but return a :term:`strong reference`.
(Contributed by Victor Stinner in :issue:`42262`.)


Porting to Python 3.10
----------------------
Expand Down
1 change: 1 addition & 0 deletions Include/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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);
PyAPI_FUNC(PyObject *) PyTuple_GetItemRef(PyObject *, Py_ssize_t);
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added :c:func:`PyTuple_GetItemRef` function: similar to
:c:func:`PyTuple_GetItem`, but return a :term:`strong reference`.
Patch by Victor Stinner.
21 changes: 3 additions & 18 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3264,34 +3264,19 @@ iso_calendar_date_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
static PyObject *
iso_calendar_date_year(PyDateTime_IsoCalendarDate *self, void *unused)
{
PyObject *year = PyTuple_GetItem((PyObject *)self, 0);
if (year == NULL) {
return NULL;
}
Py_INCREF(year);
return year;
return PyTuple_GetItemRef((PyObject *)self, 0);
}

static PyObject *
iso_calendar_date_week(PyDateTime_IsoCalendarDate *self, void *unused)
{
PyObject *week = PyTuple_GetItem((PyObject *)self, 1);
if (week == NULL) {
return NULL;
}
Py_INCREF(week);
return week;
return PyTuple_GetItemRef((PyObject *)self, 1);
}

static PyObject *
iso_calendar_date_weekday(PyDateTime_IsoCalendarDate *self, void *unused)
{
PyObject *weekday = PyTuple_GetItem((PyObject *)self, 2);
if (weekday == NULL) {
return NULL;
}
Py_INCREF(weekday);
return weekday;
return PyTuple_GetItemRef((PyObject *)self, 2);
}

static PyGetSetDef iso_calendar_date_getset[] = {
Expand Down
16 changes: 5 additions & 11 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)

PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
{
PyObject* item = PyTuple_GetItem(self->data, idx);
Py_XINCREF(item);
return item;
return PyTuple_GetItemRef(self->data, idx);
}

static int
Expand Down Expand Up @@ -111,18 +109,16 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
{
Py_ssize_t _idx;
Py_ssize_t nitems, i;
PyObject* item;

if (PyLong_Check(idx)) {
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
if (_idx == -1 && PyErr_Occurred())
return NULL;
if (_idx < 0)
_idx += PyTuple_GET_SIZE(self->data);
item = PyTuple_GetItem(self->data, _idx);
Py_XINCREF(item);
return item;
} else if (PyUnicode_Check(idx)) {
return PyTuple_GetItemRef(self->data, _idx);
}
else if (PyUnicode_Check(idx)) {
nitems = PyTuple_Size(self->description);

for (i = 0; i < nitems; i++) {
Expand All @@ -135,9 +131,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
}
if (eq) {
/* found item */
item = PyTuple_GetItem(self->data, i);
Py_INCREF(item);
return item;
return PyTuple_GetItemRef(self->data, i);
}
}

Expand Down
10 changes: 9 additions & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,15 @@ PyTuple_GetItem(PyObject *op, Py_ssize_t i)
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
return NULL;
}
return ((PyTupleObject *)op) -> ob_item[i];
return PyTuple_GET_ITEM(op, i);
}

PyObject *
PyTuple_GetItemRef(PyObject *op, Py_ssize_t i)
{
PyObject *item = PyTuple_GetItem(op, i);
// On a newly created tuple, an item can still be NULL
return Py_XNewRef(item);
}

int
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ EXPORT_FUNC(PyThreadState_Swap)
EXPORT_FUNC(PyTraceBack_Here)
EXPORT_FUNC(PyTraceBack_Print)
EXPORT_FUNC(PyTuple_GetItem)
EXPORT_FUNC(PyTuple_GetItemRef)
EXPORT_FUNC(PyTuple_GetSlice)
EXPORT_FUNC(PyTuple_New)
EXPORT_FUNC(PyTuple_Pack)
Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
/* Tuple access macros */

#ifndef Py_DEBUG
#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
# define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
#else
#define GETITEM(v, i) PyTuple_GetItem((v), (i))
# define GETITEM(v, i) PyTuple_GetItem((v), (i))
#endif

/* Code access macros */
Expand Down