]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45476: Add _Py_RVALUE() macro (GH-29860)
authorVictor Stinner <vstinner@python.org>
Tue, 30 Nov 2021 11:14:45 +0000 (12:14 +0100)
committerGitHub <noreply@github.com>
Tue, 30 Nov 2021 11:14:45 +0000 (12:14 +0100)
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.

Include/cpython/cellobject.h
Include/cpython/listobject.h
Include/cpython/tupleobject.h
Include/internal/pycore_asdl.h
Include/internal/pycore_gc.h
Include/pymacro.h

index 8dc7b8f4cf6f888da05378c5b6fe1872da50e40b..e07f9d1de79423cf8f9a67580d434a486a8b7f96 100644 (file)
@@ -22,7 +22,7 @@ PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
 PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *);
 
 #define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref)
-#define PyCell_SET(op, v) ((void)(((PyCellObject *)(op))->ob_ref = v))
+#define PyCell_SET(op, v) _Py_RVALUE(((PyCellObject *)(op))->ob_ref = (v))
 
 #ifdef __cplusplus
 }
index e3239152c497c2c87663501e8b4de4919dae833a..51687d866ce9717c4b6c097db9ddaf31b925ad05 100644 (file)
@@ -30,5 +30,5 @@ PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
 #define _PyList_CAST(op) (assert(PyList_Check(op)), (PyListObject *)(op))
 
 #define PyList_GET_ITEM(op, i) (_PyList_CAST(op)->ob_item[i])
-#define PyList_SET_ITEM(op, i, v) ((void)(_PyList_CAST(op)->ob_item[i] = (v)))
+#define PyList_SET_ITEM(op, i, v) _Py_RVALUE(_PyList_CAST(op)->ob_item[i] = (v))
 #define PyList_GET_SIZE(op)    Py_SIZE(_PyList_CAST(op))
index 7cada8848c49fc48424f9d35cc1027038911afa5..fc37c4e6de1cabd69bb06a8c09c6f0e25e54093d 100644 (file)
@@ -23,6 +23,6 @@ PyAPI_FUNC(void) _PyTuple_MaybeUntrack(PyObject *);
 #define PyTuple_GET_ITEM(op, i) (_PyTuple_CAST(op)->ob_item[i])
 
 /* Macro, *only* to be used to fill in brand new tuples */
-#define PyTuple_SET_ITEM(op, i, v) ((void)(_PyTuple_CAST(op)->ob_item[i] = v))
+#define PyTuple_SET_ITEM(op, i, v) _Py_RVALUE(_PyTuple_CAST(op)->ob_item[i] = (v))
 
 PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
index 2929e030872d943500e299053ef1c4f389fe6287..d00a7f04e6d0026b5b89739351210e9ee13b1b8f 100644 (file)
@@ -91,7 +91,7 @@ asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *a
         (S)->typed_elements[_asdl_i] = (V); \
     } while (0)
 #else
-#  define asdl_seq_SET(S, I, V) ((void)((S)->typed_elements[I] = (V)))
+#  define asdl_seq_SET(S, I, V) _Py_RVALUE((S)->typed_elements[I] = (V))
 #endif
 
 #ifdef Py_DEBUG
@@ -103,7 +103,7 @@ asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *a
         (S)->elements[_asdl_i] = (V); \
     } while (0)
 #else
-#  define asdl_seq_SET_UNTYPED(S, I, V) ((void)((S)->elements[I] = (V)))
+#  define asdl_seq_SET_UNTYPED(S, I, V) _Py_RVALUE((S)->elements[I] = (V))
 #endif
 
 #ifdef __cplusplus
index 129d53921dc49b876fb08b9386f92c5193cb332d..a23dca805491d09e2fd5fe4fc11e03d9d965f514 100644 (file)
@@ -43,7 +43,7 @@ typedef struct {
 // Lowest bit of _gc_next is used for flags only in GC.
 // But it is always 0 for normal code.
 #define _PyGCHead_NEXT(g)        ((PyGC_Head*)(g)->_gc_next)
-#define _PyGCHead_SET_NEXT(g, p) ((void)((g)->_gc_next = (uintptr_t)(p)))
+#define _PyGCHead_SET_NEXT(g, p) _Py_RVALUE((g)->_gc_next = (uintptr_t)(p))
 
 // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
 #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
@@ -56,7 +56,7 @@ typedef struct {
 #define _PyGCHead_FINALIZED(g) \
     (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
 #define _PyGCHead_SET_FINALIZED(g) \
-    ((void)((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED))
+    _Py_RVALUE((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
 
 #define _PyGC_FINALIZED(o) \
     _PyGCHead_FINALIZED(_Py_AS_GC(o))
index 202b936d964f00dec2f7cb8a3fc993f1bd6cfc85..2728496976de7e5ab635906d05e7dc8250cae5da 100644 (file)
     Py_FatalError("Unreachable C code path reached")
 #endif
 
+// Prevent using an expression as a l-value.
+// For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
+#define _Py_RVALUE(EXPR) ((void)0, (EXPR))
+
 #endif /* Py_PYMACRO_H */