[Python-checkins] gh-87347: Add parenthesis around macro arguments (#93915)

vstinner webhook-mailer at python.org
Mon Jun 20 10:04:58 EDT 2022


https://github.com/python/cpython/commit/7ad6f74fcf9db1ccfeaf0986064870d8d3887300
commit: 7ad6f74fcf9db1ccfeaf0986064870d8d3887300
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-06-20T16:04:52+02:00
summary:

gh-87347: Add parenthesis around macro arguments (#93915)

Add unit test on Py_MEMBER_SIZE() and some other macros.

files:
M Include/abstract.h
M Include/ceval.h
M Include/cpython/abstract.h
M Include/cpython/classobject.h
M Include/cpython/code.h
M Include/cpython/listobject.h
M Include/cpython/modsupport.h
M Include/cpython/object.h
M Include/cpython/odictobject.h
M Include/cpython/pyerrors.h
M Include/cpython/pythonrun.h
M Include/cpython/tupleobject.h
M Include/cpython/warnings.h
M Include/datetime.h
M Include/import.h
M Include/internal/pycore_asdl.h
M Include/internal/pycore_code.h
M Include/internal/pycore_dict.h
M Include/internal/pycore_hashtable.h
M Include/internal/pycore_initconfig.h
M Include/internal/pycore_object.h
M Include/internal/pycore_pystate.h
M Include/internal/pycore_runtime_init.h
M Include/modsupport.h
M Include/object.h
M Include/objimpl.h
M Include/pyerrors.h
M Include/pymem.h
M Include/structseq.h
M Modules/_testcapimodule.c

diff --git a/Include/abstract.h b/Include/abstract.h
index 197b20e4f23b7..576024e09c410 100644
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -88,7 +88,7 @@ extern "C" {
    -1 on failure.
 
    This is the equivalent of the Python statement: del o.attr_name. */
-#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
+#define PyObject_DelAttrString(O, A) PyObject_SetAttrString((O), (A), NULL)
 
 
 /* Implemented as a macro:
@@ -98,7 +98,7 @@ extern "C" {
    Delete attribute named attr_name, for object o. Returns -1
    on failure.  This is the equivalent of the Python
    statement: del o.attr_name. */
-#define  PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
+#define  PyObject_DelAttr(O, A) PyObject_SetAttr((O), (A), NULL)
 
 
 /* Implemented elsewhere:
@@ -722,7 +722,7 @@ PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
 /* Return the 'i'-th element of the sequence 'o', assuming that o was returned
    by PySequence_Fast, and that i is within bounds. */
 #define PySequence_Fast_GET_ITEM(o, i)\
-     (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
+     (PyList_Check(o) ? PyList_GET_ITEM((o), (i)) : PyTuple_GET_ITEM((o), (i)))
 
 /* Return a pointer to the underlying item array for
    an object returned by PySequence_Fast */
@@ -802,7 +802,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
    failure.
 
    This is equivalent to the Python statement: del o[key]. */
-#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
+#define PyMapping_DelItemString(O, K) PyObject_DelItemString((O), (K))
 
 /* Implemented as a macro:
 
@@ -812,7 +812,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
    Returns -1 on failure.
 
    This is equivalent to the Python statement: del o[key]. */
-#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
+#define PyMapping_DelItem(O, K) PyObject_DelItem((O), (K))
 
 /* On success, return 1 if the mapping object 'o' has the key 'key',
    and 0 otherwise.
diff --git a/Include/ceval.h b/Include/ceval.h
index a5387bd1f1211..ad4d909d6f2b1 100644
--- a/Include/ceval.h
+++ b/Include/ceval.h
@@ -31,7 +31,7 @@ Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
 
 /* Deprecated since PyEval_CallObjectWithKeywords is deprecated */
 #define PyEval_CallObject(callable, arg) \
-    PyEval_CallObjectWithKeywords(callable, arg, _PyObject_CAST(_Py_NULL))
+    PyEval_CallObjectWithKeywords((callable), (arg), _PyObject_CAST(_Py_NULL))
 
 Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallFunction(
     PyObject *callable, const char *format, ...);
diff --git a/Include/cpython/abstract.h b/Include/cpython/abstract.h
index d276669312ee2..7038918f01888 100644
--- a/Include/cpython/abstract.h
+++ b/Include/cpython/abstract.h
@@ -176,7 +176,7 @@ PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
 /* Assume tp_as_sequence and sq_item exist and that 'i' does not
    need to be corrected for a negative index. */
 #define PySequence_ITEM(o, i)\
-    ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
+    ( Py_TYPE(o)->tp_as_sequence->sq_item((o), (i)) )
 
 #define PY_ITERSEARCH_COUNT    1
 #define PY_ITERSEARCH_INDEX    2
diff --git a/Include/cpython/classobject.h b/Include/cpython/classobject.h
index d748e2cab18b8..051041965002a 100644
--- a/Include/cpython/classobject.h
+++ b/Include/cpython/classobject.h
@@ -29,9 +29,9 @@ PyAPI_FUNC(PyObject *) PyMethod_Self(PyObject *);
 /* Macros for direct access to these values. Type checks are *not*
    done, so use with care. */
 #define PyMethod_GET_FUNCTION(meth) \
-        (((PyMethodObject *)meth) -> im_func)
+        (((PyMethodObject *)(meth)) -> im_func)
 #define PyMethod_GET_SELF(meth) \
-        (((PyMethodObject *)meth) -> im_self)
+        (((PyMethodObject *)(meth)) -> im_self)
 
 typedef struct {
     PyObject_HEAD
@@ -48,7 +48,7 @@ PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);
 /* Macros for direct access to these values. Type checks are *not*
    done, so use with care. */
 #define PyInstanceMethod_GET_FUNCTION(meth) \
-        (((PyInstanceMethodObject *)meth) -> func)
+        (((PyInstanceMethodObject *)(meth)) -> func)
 
 #ifdef __cplusplus
 }
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 238bf9450a995..ebc0df9323bf6 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -29,7 +29,8 @@ typedef uint16_t _Py_CODEUNIT;
 #endif
 
 // Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing:
-#define _Py_SET_OPCODE(word, opcode) (((unsigned char *)&(word))[0] = (opcode))
+#define _Py_SET_OPCODE(word, opcode) \
+    do { ((unsigned char *)&(word))[0] = (opcode); } while (0)
 
 // To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
 // defined in this macro:
diff --git a/Include/cpython/listobject.h b/Include/cpython/listobject.h
index b094560cdcfc9..8fa82122d8d24 100644
--- a/Include/cpython/listobject.h
+++ b/Include/cpython/listobject.h
@@ -36,7 +36,7 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
 }
 #define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
 
-#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
+#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[(index)])
 
 static inline void
 PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
@@ -44,4 +44,4 @@ PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
     list->ob_item[index] = value;
 }
 #define PyList_SET_ITEM(op, index, value) \
-    PyList_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
+    PyList_SET_ITEM(_PyObject_CAST(op), (index), _PyObject_CAST(value))
diff --git a/Include/cpython/modsupport.h b/Include/cpython/modsupport.h
index 74ef999b7aebf..591dcb132961f 100644
--- a/Include/cpython/modsupport.h
+++ b/Include/cpython/modsupport.h
@@ -34,7 +34,7 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
 #define _PyArg_NoPositional(funcname, args) \
     ((args) == NULL || _PyArg_NoPositional((funcname), (args)))
 
-#define _Py_ANY_VARARGS(n) (n == PY_SSIZE_T_MAX)
+#define _Py_ANY_VARARGS(n) ((n) == PY_SSIZE_T_MAX)
 
 PyAPI_FUNC(void) _PyArg_BadArgument(const char *, const char *, const char *, PyObject *);
 PyAPI_FUNC(int) _PyArg_CheckPositional(const char *, Py_ssize_t,
@@ -100,7 +100,7 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
 
 #define _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, parser, minpos, maxpos, minkw, buf) \
     (((minkw) == 0 && (kwargs) == NULL && (kwnames) == NULL && \
-      (minpos) <= (nargs) && (nargs) <= (maxpos) && args != NULL) ? (args) : \
+      (minpos) <= (nargs) && (nargs) <= (maxpos) && (args) != NULL) ? (args) : \
      _PyArg_UnpackKeywords((args), (nargs), (kwargs), (kwnames), (parser), \
                            (minpos), (maxpos), (minkw), (buf)))
 
diff --git a/Include/cpython/object.h b/Include/cpython/object.h
index b018dabf9d862..614d6c18ee0b4 100644
--- a/Include/cpython/object.h
+++ b/Include/cpython/object.h
@@ -45,7 +45,7 @@ typedef struct _Py_Identifier {
 // For now we are keeping _Py_IDENTIFIER for continued use
 // in non-builtin extensions (and naughty PyPI modules).
 
-#define _Py_static_string_init(value) { .string = value, .index = -1 }
+#define _Py_static_string_init(value) { .string = (value), .index = -1 }
 #define _Py_static_string(varname, value)  static _Py_Identifier varname = _Py_static_string_init(value)
 #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
 
@@ -385,9 +385,9 @@ _PyObject_DebugTypeStats(FILE *out);
 #endif
 
 #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
-    _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
+    _PyObject_ASSERT_FROM((obj), expr, (msg), __FILE__, __LINE__, __func__)
 #define _PyObject_ASSERT(obj, expr) \
-    _PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
+    _PyObject_ASSERT_WITH_MSG((obj), expr, NULL)
 
 #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
     _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
@@ -493,8 +493,8 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
     } while (0);
 
 #define Py_TRASHCAN_BEGIN(op, dealloc) \
-    Py_TRASHCAN_BEGIN_CONDITION(op, \
-        _PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc))
+    Py_TRASHCAN_BEGIN_CONDITION((op), \
+        _PyTrash_cond(_PyObject_CAST(op), (destructor)(dealloc)))
 
 /* The following two macros, Py_TRASHCAN_SAFE_BEGIN and
  * Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and
@@ -505,7 +505,7 @@ Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro;
 #define Py_TRASHCAN_SAFE_BEGIN(op) \
     do { \
         UsingDeprecatedTrashcanMacro cond=1; \
-        Py_TRASHCAN_BEGIN_CONDITION(op, cond);
+        Py_TRASHCAN_BEGIN_CONDITION((op), cond);
 #define Py_TRASHCAN_SAFE_END(op) \
         Py_TRASHCAN_END; \
     } while(0);
diff --git a/Include/cpython/odictobject.h b/Include/cpython/odictobject.h
index ce54d4836c3d2..3822d554868c1 100644
--- a/Include/cpython/odictobject.h
+++ b/Include/cpython/odictobject.h
@@ -27,13 +27,13 @@ PyAPI_FUNC(int) PyODict_SetItem(PyObject *od, PyObject *key, PyObject *item);
 PyAPI_FUNC(int) PyODict_DelItem(PyObject *od, PyObject *key);
 
 /* wrappers around PyDict* functions */
-#define PyODict_GetItem(od, key) PyDict_GetItem(_PyObject_CAST(od), key)
+#define PyODict_GetItem(od, key) PyDict_GetItem(_PyObject_CAST(od), (key))
 #define PyODict_GetItemWithError(od, key) \
-    PyDict_GetItemWithError(_PyObject_CAST(od), key)
-#define PyODict_Contains(od, key) PyDict_Contains(_PyObject_CAST(od), key)
+    PyDict_GetItemWithError(_PyObject_CAST(od), (key))
+#define PyODict_Contains(od, key) PyDict_Contains(_PyObject_CAST(od), (key))
 #define PyODict_Size(od) PyDict_Size(_PyObject_CAST(od))
 #define PyODict_GetItemString(od, key) \
-    PyDict_GetItemString(_PyObject_CAST(od), key)
+    PyDict_GetItemString(_PyObject_CAST(od), (key))
 
 #endif
 
diff --git a/Include/cpython/pyerrors.h b/Include/cpython/pyerrors.h
index 47d80e3242302..f33d3caaa2082 100644
--- a/Include/cpython/pyerrors.h
+++ b/Include/cpython/pyerrors.h
@@ -176,4 +176,4 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat(
     const char *format,
     ...);
 
-#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, message)
+#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message))
diff --git a/Include/cpython/pythonrun.h b/Include/cpython/pythonrun.h
index 2e72d0820d34f..fb61765537402 100644
--- a/Include/cpython/pythonrun.h
+++ b/Include/cpython/pythonrun.h
@@ -66,8 +66,8 @@ PyAPI_FUNC(PyObject *) Py_CompileStringObject(
     PyCompilerFlags *flags,
     int optimize);
 
-#define Py_CompileString(str, p, s) Py_CompileStringExFlags(str, p, s, NULL, -1)
-#define Py_CompileStringFlags(str, p, s, f) Py_CompileStringExFlags(str, p, s, f, -1)
+#define Py_CompileString(str, p, s) Py_CompileStringExFlags((str), (p), (s), NULL, -1)
+#define Py_CompileStringFlags(str, p, s, f) Py_CompileStringExFlags((str), (p), (s), (f), -1)
 
 
 PyAPI_FUNC(const char *) _Py_SourceAsString(
@@ -96,23 +96,23 @@ PyAPI_FUNC(PyObject *) PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g,
 PyAPI_FUNC(PyObject *) PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, PyCompilerFlags *flags);
 
 /* Use macros for a bunch of old variants */
-#define PyRun_String(str, s, g, l) PyRun_StringFlags(str, s, g, l, NULL)
-#define PyRun_AnyFile(fp, name) PyRun_AnyFileExFlags(fp, name, 0, NULL)
+#define PyRun_String(str, s, g, l) PyRun_StringFlags((str), (s), (g), (l), NULL)
+#define PyRun_AnyFile(fp, name) PyRun_AnyFileExFlags((fp), (name), 0, NULL)
 #define PyRun_AnyFileEx(fp, name, closeit) \
-    PyRun_AnyFileExFlags(fp, name, closeit, NULL)
+    PyRun_AnyFileExFlags((fp), (name), (closeit), NULL)
 #define PyRun_AnyFileFlags(fp, name, flags) \
-    PyRun_AnyFileExFlags(fp, name, 0, flags)
-#define PyRun_SimpleString(s) PyRun_SimpleStringFlags(s, NULL)
-#define PyRun_SimpleFile(f, p) PyRun_SimpleFileExFlags(f, p, 0, NULL)
-#define PyRun_SimpleFileEx(f, p, c) PyRun_SimpleFileExFlags(f, p, c, NULL)
-#define PyRun_InteractiveOne(f, p) PyRun_InteractiveOneFlags(f, p, NULL)
-#define PyRun_InteractiveLoop(f, p) PyRun_InteractiveLoopFlags(f, p, NULL)
+    PyRun_AnyFileExFlags((fp), (name), 0, (flags))
+#define PyRun_SimpleString(s) PyRun_SimpleStringFlags((s), NULL)
+#define PyRun_SimpleFile(f, p) PyRun_SimpleFileExFlags((f), (p), 0, NULL)
+#define PyRun_SimpleFileEx(f, p, c) PyRun_SimpleFileExFlags((f), (p), (c), NULL)
+#define PyRun_InteractiveOne(f, p) PyRun_InteractiveOneFlags((f), (p), NULL)
+#define PyRun_InteractiveLoop(f, p) PyRun_InteractiveLoopFlags((f), (p), NULL)
 #define PyRun_File(fp, p, s, g, l) \
-    PyRun_FileExFlags(fp, p, s, g, l, 0, NULL)
+    PyRun_FileExFlags((fp), (p), (s), (g), (l), 0, NULL)
 #define PyRun_FileEx(fp, p, s, g, l, c) \
-    PyRun_FileExFlags(fp, p, s, g, l, c, NULL)
+    PyRun_FileExFlags((fp), (p), (s), (g), (l), (c), NULL)
 #define PyRun_FileFlags(fp, p, s, g, l, flags) \
-    PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
+    PyRun_FileExFlags((fp), (p), (s), (g), (l), 0, (flags))
 
 
 /* Stuff with no proper home (yet) */
diff --git a/Include/cpython/tupleobject.h b/Include/cpython/tupleobject.h
index 30303fa2a26a1..f6a1f076e0333 100644
--- a/Include/cpython/tupleobject.h
+++ b/Include/cpython/tupleobject.h
@@ -25,7 +25,7 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
 }
 #define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
 
-#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index])
+#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[(index)])
 
 /* Function *only* to be used to fill in brand new tuples */
 static inline void
@@ -34,6 +34,6 @@ PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
     tuple->ob_item[index] = value;
 }
 #define PyTuple_SET_ITEM(op, index, value) \
-    PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
+    PyTuple_SET_ITEM(_PyObject_CAST(op), (index), _PyObject_CAST(value))
 
 PyAPI_FUNC(void) _PyTuple_DebugMallocStats(FILE *out);
diff --git a/Include/cpython/warnings.h b/Include/cpython/warnings.h
index 2ef8e3ce9435f..4e3eb88e8ff44 100644
--- a/Include/cpython/warnings.h
+++ b/Include/cpython/warnings.h
@@ -17,4 +17,4 @@ PyAPI_FUNC(int) PyErr_WarnExplicitFormat(
     const char *format, ...);
 
 // DEPRECATED: Use PyErr_WarnEx() instead.
-#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)
+#define PyErr_Warn(category, msg) PyErr_WarnEx((category), (msg), 1)
diff --git a/Include/datetime.h b/Include/datetime.h
index bb565201a164d..b78cc0e8e2e5a 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -119,39 +119,39 @@ typedef struct
 // o is a pointer to a time or a datetime object.
 #define _PyDateTime_HAS_TZINFO(o)  (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)
 
-#define PyDateTime_GET_YEAR(o)     ((((PyDateTime_Date*)o)->data[0] << 8) | \
-                     ((PyDateTime_Date*)o)->data[1])
-#define PyDateTime_GET_MONTH(o)    (((PyDateTime_Date*)o)->data[2])
-#define PyDateTime_GET_DAY(o)      (((PyDateTime_Date*)o)->data[3])
-
-#define PyDateTime_DATE_GET_HOUR(o)        (((PyDateTime_DateTime*)o)->data[4])
-#define PyDateTime_DATE_GET_MINUTE(o)      (((PyDateTime_DateTime*)o)->data[5])
-#define PyDateTime_DATE_GET_SECOND(o)      (((PyDateTime_DateTime*)o)->data[6])
+#define PyDateTime_GET_YEAR(o)     ((((PyDateTime_Date*)(o))->data[0] << 8) | \
+                     ((PyDateTime_Date*)(o))->data[1])
+#define PyDateTime_GET_MONTH(o)    (((PyDateTime_Date*)(o))->data[2])
+#define PyDateTime_GET_DAY(o)      (((PyDateTime_Date*)(o))->data[3])
+
+#define PyDateTime_DATE_GET_HOUR(o)        (((PyDateTime_DateTime*)(o))->data[4])
+#define PyDateTime_DATE_GET_MINUTE(o)      (((PyDateTime_DateTime*)(o))->data[5])
+#define PyDateTime_DATE_GET_SECOND(o)      (((PyDateTime_DateTime*)(o))->data[6])
 #define PyDateTime_DATE_GET_MICROSECOND(o)              \
-    ((((PyDateTime_DateTime*)o)->data[7] << 16) |       \
-     (((PyDateTime_DateTime*)o)->data[8] << 8)  |       \
-      ((PyDateTime_DateTime*)o)->data[9])
-#define PyDateTime_DATE_GET_FOLD(o)        (((PyDateTime_DateTime*)o)->fold)
-#define PyDateTime_DATE_GET_TZINFO(o)      (_PyDateTime_HAS_TZINFO(o) ? \
+    ((((PyDateTime_DateTime*)(o))->data[7] << 16) |       \
+     (((PyDateTime_DateTime*)(o))->data[8] << 8)  |       \
+      ((PyDateTime_DateTime*)(o))->data[9])
+#define PyDateTime_DATE_GET_FOLD(o)        (((PyDateTime_DateTime*)(o))->fold)
+#define PyDateTime_DATE_GET_TZINFO(o)      (_PyDateTime_HAS_TZINFO((o)) ? \
     ((PyDateTime_DateTime *)(o))->tzinfo : Py_None)
 
 /* Apply for time instances. */
-#define PyDateTime_TIME_GET_HOUR(o)        (((PyDateTime_Time*)o)->data[0])
-#define PyDateTime_TIME_GET_MINUTE(o)      (((PyDateTime_Time*)o)->data[1])
-#define PyDateTime_TIME_GET_SECOND(o)      (((PyDateTime_Time*)o)->data[2])
+#define PyDateTime_TIME_GET_HOUR(o)        (((PyDateTime_Time*)(o))->data[0])
+#define PyDateTime_TIME_GET_MINUTE(o)      (((PyDateTime_Time*)(o))->data[1])
+#define PyDateTime_TIME_GET_SECOND(o)      (((PyDateTime_Time*)(o))->data[2])
 #define PyDateTime_TIME_GET_MICROSECOND(o)              \
-    ((((PyDateTime_Time*)o)->data[3] << 16) |           \
-     (((PyDateTime_Time*)o)->data[4] << 8)  |           \
-      ((PyDateTime_Time*)o)->data[5])
-#define PyDateTime_TIME_GET_FOLD(o)        (((PyDateTime_Time*)o)->fold)
+    ((((PyDateTime_Time*)(o))->data[3] << 16) |           \
+     (((PyDateTime_Time*)(o))->data[4] << 8)  |           \
+      ((PyDateTime_Time*)(o))->data[5])
+#define PyDateTime_TIME_GET_FOLD(o)        (((PyDateTime_Time*)(o))->fold)
 #define PyDateTime_TIME_GET_TZINFO(o)      (_PyDateTime_HAS_TZINFO(o) ? \
     ((PyDateTime_Time *)(o))->tzinfo : Py_None)
 
 /* Apply for time delta instances */
-#define PyDateTime_DELTA_GET_DAYS(o)         (((PyDateTime_Delta*)o)->days)
-#define PyDateTime_DELTA_GET_SECONDS(o)      (((PyDateTime_Delta*)o)->seconds)
+#define PyDateTime_DELTA_GET_DAYS(o)         (((PyDateTime_Delta*)(o))->days)
+#define PyDateTime_DELTA_GET_SECONDS(o)      (((PyDateTime_Delta*)(o))->seconds)
 #define PyDateTime_DELTA_GET_MICROSECONDS(o)            \
-    (((PyDateTime_Delta*)o)->microseconds)
+    (((PyDateTime_Delta*)(o))->microseconds)
 
 
 /* Define structure for C API. */
@@ -203,60 +203,60 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
 #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
 
 /* Macros for type checking when not building the Python core. */
-#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
-#define PyDate_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateType)
+#define PyDate_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateType)
+#define PyDate_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateType)
 
-#define PyDateTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateTimeType)
-#define PyDateTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DateTimeType)
+#define PyDateTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DateTimeType)
+#define PyDateTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DateTimeType)
 
-#define PyTime_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TimeType)
-#define PyTime_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TimeType)
+#define PyTime_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TimeType)
+#define PyTime_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TimeType)
 
-#define PyDelta_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DeltaType)
-#define PyDelta_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->DeltaType)
+#define PyDelta_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->DeltaType)
+#define PyDelta_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->DeltaType)
 
-#define PyTZInfo_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->TZInfoType)
-#define PyTZInfo_CheckExact(op) Py_IS_TYPE(op, PyDateTimeAPI->TZInfoType)
+#define PyTZInfo_Check(op) PyObject_TypeCheck((op), PyDateTimeAPI->TZInfoType)
+#define PyTZInfo_CheckExact(op) Py_IS_TYPE((op), PyDateTimeAPI->TZInfoType)
 
 
 /* Macros for accessing constructors in a simplified fashion. */
 #define PyDate_FromDate(year, month, day) \
-    PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
+    PyDateTimeAPI->Date_FromDate((year), (month), (day), PyDateTimeAPI->DateType)
 
 #define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
-    PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
-        min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
+    PyDateTimeAPI->DateTime_FromDateAndTime((year), (month), (day), (hour), \
+        (min), (sec), (usec), Py_None, PyDateTimeAPI->DateTimeType)
 
 #define PyDateTime_FromDateAndTimeAndFold(year, month, day, hour, min, sec, usec, fold) \
-    PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(year, month, day, hour, \
-        min, sec, usec, Py_None, fold, PyDateTimeAPI->DateTimeType)
+    PyDateTimeAPI->DateTime_FromDateAndTimeAndFold((year), (month), (day), (hour), \
+        (min), (sec), (usec), Py_None, (fold), PyDateTimeAPI->DateTimeType)
 
 #define PyTime_FromTime(hour, minute, second, usecond) \
-    PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
+    PyDateTimeAPI->Time_FromTime((hour), (minute), (second), (usecond), \
         Py_None, PyDateTimeAPI->TimeType)
 
 #define PyTime_FromTimeAndFold(hour, minute, second, usecond, fold) \
-    PyDateTimeAPI->Time_FromTimeAndFold(hour, minute, second, usecond, \
-        Py_None, fold, PyDateTimeAPI->TimeType)
+    PyDateTimeAPI->Time_FromTimeAndFold((hour), (minute), (second), (usecond), \
+        Py_None, (fold), PyDateTimeAPI->TimeType)
 
 #define PyDelta_FromDSU(days, seconds, useconds) \
-    PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
+    PyDateTimeAPI->Delta_FromDelta((days), (seconds), (useconds), 1, \
         PyDateTimeAPI->DeltaType)
 
 #define PyTimeZone_FromOffset(offset) \
-    PyDateTimeAPI->TimeZone_FromTimeZone(offset, NULL)
+    PyDateTimeAPI->TimeZone_FromTimeZone((offset), NULL)
 
 #define PyTimeZone_FromOffsetAndName(offset, name) \
-    PyDateTimeAPI->TimeZone_FromTimeZone(offset, name)
+    PyDateTimeAPI->TimeZone_FromTimeZone((offset), (name))
 
 /* Macros supporting the DB API. */
 #define PyDateTime_FromTimestamp(args) \
     PyDateTimeAPI->DateTime_FromTimestamp( \
-        (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
+        (PyObject*) (PyDateTimeAPI->DateTimeType), (args), NULL)
 
 #define PyDate_FromTimestamp(args) \
     PyDateTimeAPI->Date_FromTimestamp( \
-        (PyObject*) (PyDateTimeAPI->DateType), args)
+        (PyObject*) (PyDateTimeAPI->DateType), (args))
 
 #endif   /* !defined(_PY_DATETIME_IMPL) */
 
diff --git a/Include/import.h b/Include/import.h
index a87677bb10c7f..5d5f3425b8e71 100644
--- a/Include/import.h
+++ b/Include/import.h
@@ -67,7 +67,7 @@ PyAPI_FUNC(PyObject *) PyImport_ImportModuleLevelObject(
 #endif
 
 #define PyImport_ImportModuleEx(n, g, l, f) \
-    PyImport_ImportModuleLevel(n, g, l, f, 0)
+    PyImport_ImportModuleLevel((n), (g), (l), (f), 0)
 
 PyAPI_FUNC(PyObject *) PyImport_GetImporter(PyObject *path);
 PyAPI_FUNC(PyObject *) PyImport_Import(PyObject *name);
diff --git a/Include/internal/pycore_asdl.h b/Include/internal/pycore_asdl.h
index 5b01c7a66599e..afeada88d13e2 100644
--- a/Include/internal/pycore_asdl.h
+++ b/Include/internal/pycore_asdl.h
@@ -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) _Py_RVALUE((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) _Py_RVALUE((S)->elements[I] = (V))
+#  define asdl_seq_SET_UNTYPED(S, I, V) _Py_RVALUE((S)->elements[(I)] = (V))
 #endif
 
 #ifdef __cplusplus
diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index bb82d9fb9c52e..7e21c5abea250 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -252,16 +252,16 @@ extern int _PyStaticCode_InternStrings(PyCodeObject *co);
 #ifdef Py_STATS
 
 
-#define STAT_INC(opname, name) _py_stats.opcode_stats[opname].specialization.name++
-#define STAT_DEC(opname, name) _py_stats.opcode_stats[opname].specialization.name--
-#define OPCODE_EXE_INC(opname) _py_stats.opcode_stats[opname].execution_count++
+#define STAT_INC(opname, name) _py_stats.opcode_stats[(opname)].specialization.name++
+#define STAT_DEC(opname, name) _py_stats.opcode_stats[(opname)].specialization.name--
+#define OPCODE_EXE_INC(opname) _py_stats.opcode_stats[(opname)].execution_count++
 #define CALL_STAT_INC(name) _py_stats.call_stats.name++
 #define OBJECT_STAT_INC(name) _py_stats.object_stats.name++
 #define OBJECT_STAT_INC_COND(name, cond) \
     do { if (cond) _py_stats.object_stats.name++; } while (0)
-#define EVAL_CALL_STAT_INC(name) _py_stats.call_stats.eval_calls[name]++
+#define EVAL_CALL_STAT_INC(name) _py_stats.call_stats.eval_calls[(name)]++
 #define EVAL_CALL_STAT_INC_IF_FUNCTION(name, callable) \
-    do { if (PyFunction_Check(callable)) _py_stats.call_stats.eval_calls[name]++; } while (0)
+    do { if (PyFunction_Check(callable)) _py_stats.call_stats.eval_calls[(name)]++; } while (0)
 
 // Used by the _opcode extension which is built as a shared library
 PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
diff --git a/Include/internal/pycore_dict.h b/Include/internal/pycore_dict.h
index 24d2a711878ce..c831c4ccbd0cb 100644
--- a/Include/internal/pycore_dict.h
+++ b/Include/internal/pycore_dict.h
@@ -154,9 +154,11 @@ struct _dictvalues {
             2 : sizeof(int32_t))
 #endif
 #define DK_ENTRIES(dk) \
-    (assert(dk->dk_kind == DICT_KEYS_GENERAL), (PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
+    (assert((dk)->dk_kind == DICT_KEYS_GENERAL), \
+     (PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
 #define DK_UNICODE_ENTRIES(dk) \
-    (assert(dk->dk_kind != DICT_KEYS_GENERAL), (PyDictUnicodeEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
+    (assert((dk)->dk_kind != DICT_KEYS_GENERAL), \
+     (PyDictUnicodeEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
 #define DK_IS_UNICODE(dk) ((dk)->dk_kind != DICT_KEYS_GENERAL)
 
 extern uint64_t _pydict_global_version;
diff --git a/Include/internal/pycore_hashtable.h b/Include/internal/pycore_hashtable.h
index 18757abc28c19..2aa23a24c2830 100644
--- a/Include/internal/pycore_hashtable.h
+++ b/Include/internal/pycore_hashtable.h
@@ -18,9 +18,9 @@ typedef struct {
     _Py_slist_item_t *head;
 } _Py_slist_t;
 
-#define _Py_SLIST_ITEM_NEXT(ITEM) (((_Py_slist_item_t *)ITEM)->next)
+#define _Py_SLIST_ITEM_NEXT(ITEM) (((_Py_slist_item_t *)(ITEM))->next)
 
-#define _Py_SLIST_HEAD(SLIST) (((_Py_slist_t *)SLIST)->head)
+#define _Py_SLIST_HEAD(SLIST) (((_Py_slist_t *)(SLIST))->head)
 
 
 /* _Py_hashtable: table entry */
diff --git a/Include/internal/pycore_initconfig.h b/Include/internal/pycore_initconfig.h
index a2f4cd182e8e9..69f88d7d1d46b 100644
--- a/Include/internal/pycore_initconfig.h
+++ b/Include/internal/pycore_initconfig.h
@@ -36,13 +36,13 @@ struct pyruntimestate;
         ._type = _PyStatus_TYPE_EXIT, \
         .exitcode = (EXITCODE)}
 #define _PyStatus_IS_ERROR(err) \
-    (err._type == _PyStatus_TYPE_ERROR)
+    ((err)._type == _PyStatus_TYPE_ERROR)
 #define _PyStatus_IS_EXIT(err) \
-    (err._type == _PyStatus_TYPE_EXIT)
+    ((err)._type == _PyStatus_TYPE_EXIT)
 #define _PyStatus_EXCEPTION(err) \
-    (err._type != _PyStatus_TYPE_OK)
+    ((err)._type != _PyStatus_TYPE_OK)
 #define _PyStatus_UPDATE_FUNC(err) \
-    do { err.func = _PyStatus_GET_FUNC(); } while (0)
+    do { (err).func = _PyStatus_GET_FUNC(); } while (0)
 
 PyObject* _PyErr_SetFromPyStatus(PyStatus status);
 
diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h
index cc50418e2ef40..d4f66b65cc9ff 100644
--- a/Include/internal/pycore_object.h
+++ b/Include/internal/pycore_object.h
@@ -17,7 +17,7 @@ extern "C" {
 #define _PyObject_IMMORTAL_INIT(type) \
     { \
         .ob_refcnt = 999999999, \
-        .ob_type = type, \
+        .ob_type = (type), \
     }
 #define _PyVarObject_IMMORTAL_INIT(type, size) \
     { \
@@ -29,7 +29,8 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
     const char *func,
     const char *message);
 
-#define _Py_FatalRefcountError(message) _Py_FatalRefcountErrorFunc(__func__, message)
+#define _Py_FatalRefcountError(message) \
+    _Py_FatalRefcountErrorFunc(__func__, (message))
 
 static inline void
 _Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct)
@@ -275,7 +276,7 @@ extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
 
 // Access macro to the members which are floating "behind" the object
 #define _PyHeapType_GET_MEMBERS(etype) \
-    ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
+    ((PyMemberDef *)(((char *)(etype)) + Py_TYPE(etype)->tp_basicsize))
 
 PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, PyObject *);
 
@@ -296,7 +297,7 @@ PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, PyObject *);
 #if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
 #define _PyCFunction_TrampolineCall(meth, self, args) \
     _PyCFunctionWithKeywords_TrampolineCall( \
-        (*(PyCFunctionWithKeywords)(void(*)(void))meth), self, args, NULL)
+        (*(PyCFunctionWithKeywords)(void(*)(void))(meth)), (self), (args), NULL)
 extern PyObject* _PyCFunctionWithKeywords_TrampolineCall(
     PyCFunctionWithKeywords meth, PyObject *, PyObject *, PyObject *);
 #else
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index e2d7c5bee28b9..b4a39b62b76a1 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -97,7 +97,7 @@ _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
 
 // Call Py_FatalError() if tstate is NULL
 #define _Py_EnsureTstateNotNULL(tstate) \
-    _Py_EnsureFuncTstateNotNULL(__func__, tstate)
+    _Py_EnsureFuncTstateNotNULL(__func__, (tstate))
 
 
 /* Get the current interpreter state.
diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h
index b4ce4e325a4d8..a7a436011d9b4 100644
--- a/Include/internal/pycore_runtime_init.h
+++ b/Include/internal/pycore_runtime_init.h
@@ -84,13 +84,13 @@ extern "C" {
 
 #define _PyBytes_SIMPLE_INIT(CH, LEN) \
     { \
-        _PyVarObject_IMMORTAL_INIT(&PyBytes_Type, LEN), \
+        _PyVarObject_IMMORTAL_INIT(&PyBytes_Type, (LEN)), \
         .ob_shash = -1, \
-        .ob_sval = { CH }, \
+        .ob_sval = { (CH) }, \
     }
 #define _PyBytes_CHAR_INIT(CH) \
     { \
-        _PyBytes_SIMPLE_INIT(CH, 1) \
+        _PyBytes_SIMPLE_INIT((CH), 1) \
     }
 
 #define _PyUnicode_ASCII_BASE_INIT(LITERAL, ASCII) \
@@ -101,13 +101,13 @@ extern "C" {
         .state = { \
             .kind = 1, \
             .compact = 1, \
-            .ascii = ASCII, \
+            .ascii = (ASCII), \
         }, \
     }
 #define _PyASCIIObject_INIT(LITERAL) \
     { \
-        ._ascii = _PyUnicode_ASCII_BASE_INIT(LITERAL, 1), \
-        ._data = LITERAL \
+        ._ascii = _PyUnicode_ASCII_BASE_INIT((LITERAL), 1), \
+        ._data = (LITERAL) \
     }
 #define INIT_STR(NAME, LITERAL) \
     ._ ## NAME = _PyASCIIObject_INIT(LITERAL)
@@ -116,9 +116,9 @@ extern "C" {
 #define _PyUnicode_LATIN1_INIT(LITERAL) \
     { \
         ._latin1 = { \
-            ._base = _PyUnicode_ASCII_BASE_INIT(LITERAL, 0), \
+            ._base = _PyUnicode_ASCII_BASE_INIT((LITERAL), 0), \
         }, \
-        ._data = LITERAL, \
+        ._data = (LITERAL), \
     }
 
 /* The following is auto-generated by Tools/scripts/generate_global_objects.py. */
diff --git a/Include/modsupport.h b/Include/modsupport.h
index c42a3f17a2bfd..4e369bd56b4d2 100644
--- a/Include/modsupport.h
+++ b/Include/modsupport.h
@@ -56,8 +56,8 @@ PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char
 PyAPI_FUNC(int) PyModule_AddType(PyObject *module, PyTypeObject *type);
 #endif /* Py_LIMITED_API */
 
-#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c)
-#define PyModule_AddStringMacro(m, c) PyModule_AddStringConstant(m, #c, c)
+#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant((m), #c, (c))
+#define PyModule_AddStringMacro(m, c) PyModule_AddStringConstant((m), #c, (c))
 
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
 /* New in 3.5 */
@@ -132,10 +132,10 @@ PyAPI_FUNC(PyObject *) PyModule_Create2(PyModuleDef*, int apiver);
 
 #ifdef Py_LIMITED_API
 #define PyModule_Create(module) \
-        PyModule_Create2(module, PYTHON_ABI_VERSION)
+        PyModule_Create2((module), PYTHON_ABI_VERSION)
 #else
 #define PyModule_Create(module) \
-        PyModule_Create2(module, PYTHON_API_VERSION)
+        PyModule_Create2((module), PYTHON_API_VERSION)
 #endif
 
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
@@ -146,10 +146,10 @@ PyAPI_FUNC(PyObject *) PyModule_FromDefAndSpec2(PyModuleDef *def,
 
 #ifdef Py_LIMITED_API
 #define PyModule_FromDefAndSpec(module, spec) \
-    PyModule_FromDefAndSpec2(module, spec, PYTHON_ABI_VERSION)
+    PyModule_FromDefAndSpec2((module), (spec), PYTHON_ABI_VERSION)
 #else
 #define PyModule_FromDefAndSpec(module, spec) \
-    PyModule_FromDefAndSpec2(module, spec, PYTHON_API_VERSION)
+    PyModule_FromDefAndSpec2((module), (spec), PYTHON_API_VERSION)
 #endif /* Py_LIMITED_API */
 
 #endif /* New in 3.5 */
diff --git a/Include/object.h b/Include/object.h
index a3c6bd4fa984d..a12b754c8ed8f 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -80,10 +80,10 @@ whose size is determined when the object is allocated.
 
 #define PyObject_HEAD_INIT(type)        \
     { _PyObject_EXTRA_INIT              \
-    1, type },
+    1, (type) },
 
 #define PyVarObject_HEAD_INIT(type, size)       \
-    { PyObject_HEAD_INIT(type) size },
+    { PyObject_HEAD_INIT(type) (size) },
 
 /* PyObject_VAR_HEAD defines the initial segment of all variable-size
  * container objects.  These end with a declaration of an array with 1
@@ -152,7 +152,7 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
     return Py_TYPE(ob) == type;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), type)
+#  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
 #endif
 
 
@@ -160,7 +160,7 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
     ob->ob_refcnt = refcnt;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
+#  define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
 #endif
 
 
@@ -176,7 +176,7 @@ static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
     ob->ob_size = size;
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), size)
+#  define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
 #endif
 
 
@@ -268,7 +268,7 @@ static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
     return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
 }
 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
-#  define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), type)
+#  define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type))
 #endif
 
 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
@@ -779,7 +779,7 @@ PyType_HasFeature(PyTypeObject *type, unsigned long feature)
     return ((flags & feature) != 0);
 }
 
-#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)
+#define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag))
 
 static inline int PyType_Check(PyObject *op) {
     return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 4fa670e71ab46..140918198dae0 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -135,14 +135,14 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
 
 // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
 // PyObject_MALLOC() with _PyObject_SIZE().
-#define PyObject_NEW(type, typeobj) PyObject_New(type, typeobj)
+#define PyObject_NEW(type, typeobj) PyObject_New((type), (typeobj))
 
 #define PyObject_NewVar(type, typeobj, n) \
                 ( (type *) _PyObject_NewVar((typeobj), (n)) )
 
 // Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
 // directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
-#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)
+#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar((type), (typeobj), (n))
 
 
 /*
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index 4944feecd8a6b..d5ac6af5b32c6 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -65,7 +65,7 @@ PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
 #define PyExceptionInstance_Class(x) _PyObject_CAST(Py_TYPE(x))
 
 #define _PyBaseExceptionGroup_Check(x)                   \
-    PyObject_TypeCheck(x, (PyTypeObject *)PyExc_BaseExceptionGroup)
+    PyObject_TypeCheck((x), (PyTypeObject *)PyExc_BaseExceptionGroup)
 
 /* Predefined exceptions */
 
diff --git a/Include/pymem.h b/Include/pymem.h
index c15ad10dfcf83..e882645757bfd 100644
--- a/Include/pymem.h
+++ b/Include/pymem.h
@@ -82,13 +82,13 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
 // Deprecated aliases only kept for backward compatibility.
 // PyMem_Del and PyMem_DEL are defined with no parameter to be able to use
 // them as function pointers (ex: dealloc = PyMem_Del).
-#define PyMem_MALLOC(n)           PyMem_Malloc(n)
-#define PyMem_NEW(type, n)        PyMem_New(type, n)
-#define PyMem_REALLOC(p, n)       PyMem_Realloc(p, n)
-#define PyMem_RESIZE(p, type, n)  PyMem_Resize(p, type, n)
-#define PyMem_FREE(p)             PyMem_Free(p)
-#define PyMem_Del                 PyMem_Free
-#define PyMem_DEL                 PyMem_Free
+#define PyMem_MALLOC(n)           PyMem_Malloc((n))
+#define PyMem_NEW(type, n)        PyMem_New(type, (n))
+#define PyMem_REALLOC(p, n)       PyMem_Realloc((p), (n))
+#define PyMem_RESIZE(p, type, n)  PyMem_Resize((p), type, (n))
+#define PyMem_FREE(p)             PyMem_Free((p))
+#define PyMem_Del(p)              PyMem_Free((p))
+#define PyMem_DEL(p)              PyMem_Free((p))
 
 
 #ifndef Py_LIMITED_API
diff --git a/Include/structseq.h b/Include/structseq.h
index 4f5c09f7ba05e..9687115561195 100644
--- a/Include/structseq.h
+++ b/Include/structseq.h
@@ -35,9 +35,9 @@ PyAPI_FUNC(PyObject *) PyStructSequence_New(PyTypeObject* type);
 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)
+#define PyStructSequence_SET_ITEM(op, i, v) PyTuple_SET_ITEM((op), (i), (v))
 
-#define PyStructSequence_GET_ITEM(op, i) PyTuple_GET_ITEM(op, i)
+#define PyStructSequence_GET_ITEM(op, i) PyTuple_GET_ITEM((op), (i))
 #endif
 
 PyAPI_FUNC(void) PyStructSequence_SetItem(PyObject*, Py_ssize_t, PyObject*);
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 8bf4de758fe93..f427a49047176 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -6070,12 +6070,35 @@ settrace_to_record(PyObject *self, PyObject *list)
     Py_RETURN_NONE;
 }
 
+
 static PyObject *
 test_macros(PyObject *self, PyObject *Py_UNUSED(args))
 {
-    // Py_MIN(), Py_MAX()
+    struct MyStruct {
+        int x;
+    };
+    wchar_t array[3];
+
+    // static_assert(), Py_BUILD_ASSERT()
+    static_assert(1 == 1, "bug");
+    Py_BUILD_ASSERT(1 == 1);
+
+
+    // Py_MIN(), Py_MAX(), Py_ABS()
     assert(Py_MIN(5, 11) == 5);
     assert(Py_MAX(5, 11) == 11);
+    assert(Py_ABS(-5) == 5);
+
+    // Py_STRINGIFY()
+    assert(strcmp(Py_STRINGIFY(123), "123") == 0);
+
+    // Py_MEMBER_SIZE(), Py_ARRAY_LENGTH()
+    assert(Py_MEMBER_SIZE(struct MyStruct, x) == sizeof(int));
+    assert(Py_ARRAY_LENGTH(array) == 3);
+
+    // Py_CHARMASK()
+    int c = 0xab00 | 7;
+    assert(Py_CHARMASK(c) == 7);
 
     // _Py_IS_TYPE_SIGNED()
     assert(_Py_IS_TYPE_SIGNED(int));
@@ -6084,6 +6107,7 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
     Py_RETURN_NONE;
 }
 
+
 static PyObject *negative_dictoffset(PyObject *, PyObject *);
 static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
 static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);



More information about the Python-checkins mailing list