Skip to content

Commit 0ef96c2

Browse files
authored
bpo-30459: Cast the result of PyCell_SET to void (GH-23654)
1 parent c266736 commit 0ef96c2

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Doc/whatsnew/3.10.rst

+7
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ Porting to Python 3.10
611611
:ref:`Python Path Configuration. <init-path-config>`.
612612
(Contributed by Victor Stinner in :issue:`42260`.)
613613

614+
* :c:func:`PyList_SET_ITEM`, :c:func:`PyTuple_SET_ITEM` and
615+
:c:func:`PyCell_SET` macros can no longer be used as l-value or r-value.
616+
For example, ``x = PyList_SET_ITEM(a, b, c)`` and
617+
``PyList_SET_ITEM(a, b, c) = x`` now fail with a compiler error. It prevents
618+
bugs like ``if (PyList_SET_ITEM (a, b, c) < 0) ...`` test.
619+
(Contributed by Zackery Spytz and Victor Stinner in :issue:`30459`.)
620+
614621
Deprecated
615622
----------
616623

Include/cellobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
2020
PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *);
2121

2222
#define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref)
23-
#define PyCell_SET(op, v) (((PyCellObject *)(op))->ob_ref = v)
23+
#define PyCell_SET(op, v) ((void)(((PyCellObject *)(op))->ob_ref = v))
2424

2525
#ifdef __cplusplus
2626
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
Cast the result of :c:func:`PyList_SET_ITEM` and :c:func:`PyTuple_SET_ITEM`
2-
to void.
1+
:c:func:`PyList_SET_ITEM`, :c:func:`PyTuple_SET_ITEM` and :c:func:`PyCell_SET`
2+
macros can no longer be used as l-value or r-value. For example,
3+
``x = PyList_SET_ITEM(a, b, c)`` and ``PyList_SET_ITEM(a, b, c) = x`` now fail
4+
with a compiler error. It prevents bugs like
5+
``if (PyList_SET_ITEM (a, b, c) < 0) ...`` test.
6+
Patch by Zackery Spytz and Victor Stinner.

0 commit comments

Comments
 (0)