Skip to content

Commit c19c3a0

Browse files
authored
bpo-45476: Add _Py_RVALUE() macro (GH-29860)
Add a new _Py_RVALUE() macro to prevent using an expression as an l-value. Replace a "(void)" cast with the _Py_RVALUE() macro in the following macros: * PyCell_SET() * PyList_SET_ITEM() * PyTuple_SET_ITEM() * _PyGCHead_SET_FINALIZED() * _PyGCHead_SET_NEXT() * asdl_seq_SET() * asdl_seq_SET_UNTYPED() Add also parentheses around macro arguments in PyCell_SET() and PyTuple_SET_ITEM() macros.
1 parent f97ec09 commit c19c3a0

File tree

6 files changed

+11
-7
lines changed

6 files changed

+11
-7
lines changed

Include/cpython/cellobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
2222
PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *);
2323

2424
#define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref)
25-
#define PyCell_SET(op, v) ((void)(((PyCellObject *)(op))->ob_ref = v))
25+
#define PyCell_SET(op, v) _Py_RVALUE(((PyCellObject *)(op))->ob_ref = (v))
2626

2727
#ifdef __cplusplus
2828
}

Include/cpython/listobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
3030
#define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
3131

3232
#define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i])
33-
#define PyList_SET_ITEM(op, i, v) ((void)(_PyList_CAST(op)->ob_item[i] = (v)))
33+
#define PyList_SET_ITEM(op, i, v) _Py_RVALUE(_PyList_CAST(op)->ob_item[i] = (v))
3434
#define PyList_GET_SIZE(op) Py_SIZE(_PyList_CAST(op))

Include/cpython/tupleobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
2323
#define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
2424

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

2828
PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);

Include/internal/pycore_asdl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *a
9191
(S)->typed_elements[_asdl_i] = (V); \
9292
} while (0)
9393
#else
94-
# define asdl_seq_SET(S, I, V) ((void)((S)->typed_elements[I] = (V)))
94+
# define asdl_seq_SET(S, I, V) _Py_RVALUE((S)->typed_elements[I] = (V))
9595
#endif
9696

9797
#ifdef Py_DEBUG
@@ -103,7 +103,7 @@ asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *a
103103
(S)->elements[_asdl_i] = (V); \
104104
} while (0)
105105
#else
106-
# define asdl_seq_SET_UNTYPED(S, I, V) ((void)((S)->elements[I] = (V)))
106+
# define asdl_seq_SET_UNTYPED(S, I, V) _Py_RVALUE((S)->elements[I] = (V))
107107
#endif
108108

109109
#ifdef __cplusplus

Include/internal/pycore_gc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct {
4343
// Lowest bit of _gc_next is used for flags only in GC.
4444
// But it is always 0 for normal code.
4545
#define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next)
46-
#define _PyGCHead_SET_NEXT(g, p) ((void)((g)->_gc_next = (uintptr_t)(p)))
46+
#define _PyGCHead_SET_NEXT(g, p) _Py_RVALUE((g)->_gc_next = (uintptr_t)(p))
4747

4848
// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
4949
#define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
@@ -56,7 +56,7 @@ typedef struct {
5656
#define _PyGCHead_FINALIZED(g) \
5757
(((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
5858
#define _PyGCHead_SET_FINALIZED(g) \
59-
((void)((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED))
59+
_Py_RVALUE((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
6060

6161
#define _PyGC_FINALIZED(o) \
6262
_PyGCHead_FINALIZED(_Py_AS_GC(o))

Include/pymacro.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,8 @@
129129
Py_FatalError("Unreachable C code path reached")
130130
#endif
131131

132+
// Prevent using an expression as a l-value.
133+
// For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
134+
#define _Py_RVALUE(EXPR) ((void)0, (EXPR))
135+
132136
#endif /* Py_PYMACRO_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy