Skip to content
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: 10 additions & 3 deletions Doc/c-api/tuple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Tuple Objects

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

Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments.
Like :c:func:`PyTuple_GetItem`, but does no checking of its arguments (in
release mode).


.. c:function:: PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Expand All @@ -83,13 +84,16 @@ Tuple Objects

.. c:function:: void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)

Like :c:func:`PyTuple_SetItem`, but does no error checking, and should *only* be
used to fill in brand new tuples.
Like :c:func:`PyTuple_SetItem`, but does no error checking (in release
mode), and should *only* be used to fill in brand new tuples.

.. note::

This function "steals" a reference to *o*.

.. versionchanged:: 3.8
Arguments are now checked in debug mode.


.. c:function:: int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)

Expand Down Expand Up @@ -197,6 +201,9 @@ type.

Macro equivalent of :c:func:`PyStructSequence_GetItem`.

.. versionchanged:: 3.8
Arguments are now checked in debug mode.


.. c:function:: void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)

Expand Down
18 changes: 15 additions & 3 deletions Include/cpython/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ typedef struct {
PyAPI_FUNC(int) _PyTuple_Resize(PyObject **, Py_ssize_t);
PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);

/* Macros trading safety for speed */
/* Macros and inline functions trading safety for speed */

/* Cast argument to PyTupleObject* type. */
#define _PyTuple_CAST(op) (assert(PyTuple_Check(op)), (PyTupleObject *)(op))

#define PyTuple_GET_SIZE(op) Py_SIZE(_PyTuple_CAST(op))

#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
static inline PyObject* _PyTuple_GET_ITEM(PyTupleObject *op, Py_ssize_t i)
{
assert(0 <= i && i < Py_SIZE(op));
return op->ob_item[i];
}

#define PyTuple_GET_ITEM(op, i) _PyTuple_GET_ITEM(_PyTuple_CAST(op), i)

static inline void _PyTuple_SET_ITEM(PyTupleObject *op, Py_ssize_t i, PyObject *item)
{
assert(0 <= i && i < Py_SIZE(op));
op->ob_item[i] = item;
}

/* Macro, *only* to be used to fill in brand new tuples */
#define PyTuple_SET_ITEM(op, i, v) (_PyTuple_CAST(op)->ob_item[i] = v)
#define PyTuple_SET_ITEM(op, i, v) _PyTuple_SET_ITEM(_PyTuple_CAST(op), i, v)

PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);

Expand Down
11 changes: 8 additions & 3 deletions Include/structseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ PyAPI_FUNC(PyObject *) PyStructSequence_New(PyTypeObject* type);
#ifndef Py_LIMITED_API
typedef PyTupleObject PyStructSequence;

/* Macro, *only* to be used to fill in brand new objects */
#define PyStructSequence_SET_ITEM(op, i, v) PyTuple_SET_ITEM(op, i, v)
/* Macro, *only* to be used to fill in brand new objects.

#define PyStructSequence_GET_ITEM(op, i) PyTuple_GET_ITEM(op, i)
PyTuple_SET_ITEM() checks the size and so cannot be used here, since a
structseq with unnamed fields can be smaller size than the real size. */
#define PyStructSequence_SET_ITEM(op, i, v) (_PyTuple_CAST(op)->ob_item[i] = v)

/* PyTuple_GET_ITEM() checks the size and so cannot be used here, since a
structseq with unnamed fields can be smaller size than the real size. */
#define PyStructSequence_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
#endif

PyAPI_FUNC(void) PyStructSequence_SetItem(PyObject*, Py_ssize_t, PyObject*);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:c:func:`PyTuple_GET_ITEM` and :c:func:`PyTuple_SET_ITEM` now check the
index using an assertion.