Skip to content

Commit 7ad6f74

Browse files
authored
gh-87347: Add parenthesis around macro arguments (#93915)
Add unit test on Py_MEMBER_SIZE() and some other macros.
1 parent 61f24e7 commit 7ad6f74

30 files changed

+172
-144
lines changed

Include/abstract.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extern "C" {
8888
-1 on failure.
8989
9090
This is the equivalent of the Python statement: del o.attr_name. */
91-
#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
91+
#define PyObject_DelAttrString(O, A) PyObject_SetAttrString((O), (A), NULL)
9292

9393

9494
/* Implemented as a macro:
@@ -98,7 +98,7 @@ extern "C" {
9898
Delete attribute named attr_name, for object o. Returns -1
9999
on failure. This is the equivalent of the Python
100100
statement: del o.attr_name. */
101-
#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
101+
#define PyObject_DelAttr(O, A) PyObject_SetAttr((O), (A), NULL)
102102

103103

104104
/* Implemented elsewhere:
@@ -722,7 +722,7 @@ PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
722722
/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
723723
by PySequence_Fast, and that i is within bounds. */
724724
#define PySequence_Fast_GET_ITEM(o, i)\
725-
(PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
725+
(PyList_Check(o) ? PyList_GET_ITEM((o), (i)) : PyTuple_GET_ITEM((o), (i)))
726726

727727
/* Return a pointer to the underlying item array for
728728
an object returned by PySequence_Fast */
@@ -802,7 +802,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
802802
failure.
803803
804804
This is equivalent to the Python statement: del o[key]. */
805-
#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
805+
#define PyMapping_DelItemString(O, K) PyObject_DelItemString((O), (K))
806806

807807
/* Implemented as a macro:
808808
@@ -812,7 +812,7 @@ PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
812812
Returns -1 on failure.
813813
814814
This is equivalent to the Python statement: del o[key]. */
815-
#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
815+
#define PyMapping_DelItem(O, K) PyObject_DelItem((O), (K))
816816

817817
/* On success, return 1 if the mapping object 'o' has the key 'key',
818818
and 0 otherwise.

Include/ceval.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
3131

3232
/* Deprecated since PyEval_CallObjectWithKeywords is deprecated */
3333
#define PyEval_CallObject(callable, arg) \
34-
PyEval_CallObjectWithKeywords(callable, arg, _PyObject_CAST(_Py_NULL))
34+
PyEval_CallObjectWithKeywords((callable), (arg), _PyObject_CAST(_Py_NULL))
3535

3636
Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallFunction(
3737
PyObject *callable, const char *format, ...);

Include/cpython/abstract.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
176176
/* Assume tp_as_sequence and sq_item exist and that 'i' does not
177177
need to be corrected for a negative index. */
178178
#define PySequence_ITEM(o, i)\
179-
( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
179+
( Py_TYPE(o)->tp_as_sequence->sq_item((o), (i)) )
180180

181181
#define PY_ITERSEARCH_COUNT 1
182182
#define PY_ITERSEARCH_INDEX 2

Include/cpython/classobject.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ PyAPI_FUNC(PyObject *) PyMethod_Self(PyObject *);
2929
/* Macros for direct access to these values. Type checks are *not*
3030
done, so use with care. */
3131
#define PyMethod_GET_FUNCTION(meth) \
32-
(((PyMethodObject *)meth) -> im_func)
32+
(((PyMethodObject *)(meth)) -> im_func)
3333
#define PyMethod_GET_SELF(meth) \
34-
(((PyMethodObject *)meth) -> im_self)
34+
(((PyMethodObject *)(meth)) -> im_self)
3535

3636
typedef struct {
3737
PyObject_HEAD
@@ -48,7 +48,7 @@ PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);
4848
/* Macros for direct access to these values. Type checks are *not*
4949
done, so use with care. */
5050
#define PyInstanceMethod_GET_FUNCTION(meth) \
51-
(((PyInstanceMethodObject *)meth) -> func)
51+
(((PyInstanceMethodObject *)(meth)) -> func)
5252

5353
#ifdef __cplusplus
5454
}

Include/cpython/code.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ typedef uint16_t _Py_CODEUNIT;
2929
#endif
3030

3131
// Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing:
32-
#define _Py_SET_OPCODE(word, opcode) (((unsigned char *)&(word))[0] = (opcode))
32+
#define _Py_SET_OPCODE(word, opcode) \
33+
do { ((unsigned char *)&(word))[0] = (opcode); } while (0)
3334

3435
// To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
3536
// defined in this macro:

Include/cpython/listobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ static inline Py_ssize_t PyList_GET_SIZE(PyObject *op) {
3636
}
3737
#define PyList_GET_SIZE(op) PyList_GET_SIZE(_PyObject_CAST(op))
3838

39-
#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[index])
39+
#define PyList_GET_ITEM(op, index) (_PyList_CAST(op)->ob_item[(index)])
4040

4141
static inline void
4242
PyList_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
4343
PyListObject *list = _PyList_CAST(op);
4444
list->ob_item[index] = value;
4545
}
4646
#define PyList_SET_ITEM(op, index, value) \
47-
PyList_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
47+
PyList_SET_ITEM(_PyObject_CAST(op), (index), _PyObject_CAST(value))

Include/cpython/modsupport.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
3434
#define _PyArg_NoPositional(funcname, args) \
3535
((args) == NULL || _PyArg_NoPositional((funcname), (args)))
3636

37-
#define _Py_ANY_VARARGS(n) (n == PY_SSIZE_T_MAX)
37+
#define _Py_ANY_VARARGS(n) ((n) == PY_SSIZE_T_MAX)
3838

3939
PyAPI_FUNC(void) _PyArg_BadArgument(const char *, const char *, const char *, PyObject *);
4040
PyAPI_FUNC(int) _PyArg_CheckPositional(const char *, Py_ssize_t,
@@ -100,7 +100,7 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
100100

101101
#define _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, parser, minpos, maxpos, minkw, buf) \
102102
(((minkw) == 0 && (kwargs) == NULL && (kwnames) == NULL && \
103-
(minpos) <= (nargs) && (nargs) <= (maxpos) && args != NULL) ? (args) : \
103+
(minpos) <= (nargs) && (nargs) <= (maxpos) && (args) != NULL) ? (args) : \
104104
_PyArg_UnpackKeywords((args), (nargs), (kwargs), (kwnames), (parser), \
105105
(minpos), (maxpos), (minkw), (buf)))
106106

Include/cpython/object.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct _Py_Identifier {
4545
// For now we are keeping _Py_IDENTIFIER for continued use
4646
// in non-builtin extensions (and naughty PyPI modules).
4747

48-
#define _Py_static_string_init(value) { .string = value, .index = -1 }
48+
#define _Py_static_string_init(value) { .string = (value), .index = -1 }
4949
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
5050
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
5151

@@ -385,9 +385,9 @@ _PyObject_DebugTypeStats(FILE *out);
385385
#endif
386386

387387
#define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
388-
_PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__)
388+
_PyObject_ASSERT_FROM((obj), expr, (msg), __FILE__, __LINE__, __func__)
389389
#define _PyObject_ASSERT(obj, expr) \
390-
_PyObject_ASSERT_WITH_MSG(obj, expr, NULL)
390+
_PyObject_ASSERT_WITH_MSG((obj), expr, NULL)
391391

392392
#define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
393393
_PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
@@ -493,8 +493,8 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
493493
} while (0);
494494

495495
#define Py_TRASHCAN_BEGIN(op, dealloc) \
496-
Py_TRASHCAN_BEGIN_CONDITION(op, \
497-
_PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc))
496+
Py_TRASHCAN_BEGIN_CONDITION((op), \
497+
_PyTrash_cond(_PyObject_CAST(op), (destructor)(dealloc)))
498498

499499
/* The following two macros, Py_TRASHCAN_SAFE_BEGIN and
500500
* Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and
@@ -505,7 +505,7 @@ Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro;
505505
#define Py_TRASHCAN_SAFE_BEGIN(op) \
506506
do { \
507507
UsingDeprecatedTrashcanMacro cond=1; \
508-
Py_TRASHCAN_BEGIN_CONDITION(op, cond);
508+
Py_TRASHCAN_BEGIN_CONDITION((op), cond);
509509
#define Py_TRASHCAN_SAFE_END(op) \
510510
Py_TRASHCAN_END; \
511511
} while(0);

Include/cpython/odictobject.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ PyAPI_FUNC(int) PyODict_SetItem(PyObject *od, PyObject *key, PyObject *item);
2727
PyAPI_FUNC(int) PyODict_DelItem(PyObject *od, PyObject *key);
2828

2929
/* wrappers around PyDict* functions */
30-
#define PyODict_GetItem(od, key) PyDict_GetItem(_PyObject_CAST(od), key)
30+
#define PyODict_GetItem(od, key) PyDict_GetItem(_PyObject_CAST(od), (key))
3131
#define PyODict_GetItemWithError(od, key) \
32-
PyDict_GetItemWithError(_PyObject_CAST(od), key)
33-
#define PyODict_Contains(od, key) PyDict_Contains(_PyObject_CAST(od), key)
32+
PyDict_GetItemWithError(_PyObject_CAST(od), (key))
33+
#define PyODict_Contains(od, key) PyDict_Contains(_PyObject_CAST(od), (key))
3434
#define PyODict_Size(od) PyDict_Size(_PyObject_CAST(od))
3535
#define PyODict_GetItemString(od, key) \
36-
PyDict_GetItemString(_PyObject_CAST(od), key)
36+
PyDict_GetItemString(_PyObject_CAST(od), (key))
3737

3838
#endif
3939

Include/cpython/pyerrors.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat(
176176
const char *format,
177177
...);
178178

179-
#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, message)
179+
#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message))

Include/cpython/pythonrun.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ PyAPI_FUNC(PyObject *) Py_CompileStringObject(
6666
PyCompilerFlags *flags,
6767
int optimize);
6868

69-
#define Py_CompileString(str, p, s) Py_CompileStringExFlags(str, p, s, NULL, -1)
70-
#define Py_CompileStringFlags(str, p, s, f) Py_CompileStringExFlags(str, p, s, f, -1)
69+
#define Py_CompileString(str, p, s) Py_CompileStringExFlags((str), (p), (s), NULL, -1)
70+
#define Py_CompileStringFlags(str, p, s, f) Py_CompileStringExFlags((str), (p), (s), (f), -1)
7171

7272

7373
PyAPI_FUNC(const char *) _Py_SourceAsString(
@@ -96,23 +96,23 @@ PyAPI_FUNC(PyObject *) PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g,
9696
PyAPI_FUNC(PyObject *) PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, PyCompilerFlags *flags);
9797

9898
/* Use macros for a bunch of old variants */
99-
#define PyRun_String(str, s, g, l) PyRun_StringFlags(str, s, g, l, NULL)
100-
#define PyRun_AnyFile(fp, name) PyRun_AnyFileExFlags(fp, name, 0, NULL)
99+
#define PyRun_String(str, s, g, l) PyRun_StringFlags((str), (s), (g), (l), NULL)
100+
#define PyRun_AnyFile(fp, name) PyRun_AnyFileExFlags((fp), (name), 0, NULL)
101101
#define PyRun_AnyFileEx(fp, name, closeit) \
102-
PyRun_AnyFileExFlags(fp, name, closeit, NULL)
102+
PyRun_AnyFileExFlags((fp), (name), (closeit), NULL)
103103
#define PyRun_AnyFileFlags(fp, name, flags) \
104-
PyRun_AnyFileExFlags(fp, name, 0, flags)
105-
#define PyRun_SimpleString(s) PyRun_SimpleStringFlags(s, NULL)
106-
#define PyRun_SimpleFile(f, p) PyRun_SimpleFileExFlags(f, p, 0, NULL)
107-
#define PyRun_SimpleFileEx(f, p, c) PyRun_SimpleFileExFlags(f, p, c, NULL)
108-
#define PyRun_InteractiveOne(f, p) PyRun_InteractiveOneFlags(f, p, NULL)
109-
#define PyRun_InteractiveLoop(f, p) PyRun_InteractiveLoopFlags(f, p, NULL)
104+
PyRun_AnyFileExFlags((fp), (name), 0, (flags))
105+
#define PyRun_SimpleString(s) PyRun_SimpleStringFlags((s), NULL)
106+
#define PyRun_SimpleFile(f, p) PyRun_SimpleFileExFlags((f), (p), 0, NULL)
107+
#define PyRun_SimpleFileEx(f, p, c) PyRun_SimpleFileExFlags((f), (p), (c), NULL)
108+
#define PyRun_InteractiveOne(f, p) PyRun_InteractiveOneFlags((f), (p), NULL)
109+
#define PyRun_InteractiveLoop(f, p) PyRun_InteractiveLoopFlags((f), (p), NULL)
110110
#define PyRun_File(fp, p, s, g, l) \
111-
PyRun_FileExFlags(fp, p, s, g, l, 0, NULL)
111+
PyRun_FileExFlags((fp), (p), (s), (g), (l), 0, NULL)
112112
#define PyRun_FileEx(fp, p, s, g, l, c) \
113-
PyRun_FileExFlags(fp, p, s, g, l, c, NULL)
113+
PyRun_FileExFlags((fp), (p), (s), (g), (l), (c), NULL)
114114
#define PyRun_FileFlags(fp, p, s, g, l, flags) \
115-
PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
115+
PyRun_FileExFlags((fp), (p), (s), (g), (l), 0, (flags))
116116

117117

118118
/* Stuff with no proper home (yet) */

Include/cpython/tupleobject.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static inline Py_ssize_t PyTuple_GET_SIZE(PyObject *op) {
2525
}
2626
#define PyTuple_GET_SIZE(op) PyTuple_GET_SIZE(_PyObject_CAST(op))
2727

28-
#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[index])
28+
#define PyTuple_GET_ITEM(op, index) (_PyTuple_CAST(op)->ob_item[(index)])
2929

3030
/* Function *only* to be used to fill in brand new tuples */
3131
static inline void
@@ -34,6 +34,6 @@ PyTuple_SET_ITEM(PyObject *op, Py_ssize_t index, PyObject *value) {
3434
tuple->ob_item[index] = value;
3535
}
3636
#define PyTuple_SET_ITEM(op, index, value) \
37-
PyTuple_SET_ITEM(_PyObject_CAST(op), index, _PyObject_CAST(value))
37+
PyTuple_SET_ITEM(_PyObject_CAST(op), (index), _PyObject_CAST(value))
3838

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

Include/cpython/warnings.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ PyAPI_FUNC(int) PyErr_WarnExplicitFormat(
1717
const char *format, ...);
1818

1919
// DEPRECATED: Use PyErr_WarnEx() instead.
20-
#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)
20+
#define PyErr_Warn(category, msg) PyErr_WarnEx((category), (msg), 1)

0 commit comments

Comments
 (0)