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

Add unit test on Py_MEMBER_SIZE() and some other macros.
This commit is contained in:
Victor Stinner 2022-06-20 16:04:52 +02:00 committed by GitHub
parent 61f24e7885
commit 7ad6f74fcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 171 additions and 143 deletions

View File

@ -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 */

View File

@ -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, ...);

View File

@ -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

View File

@ -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
}

View File

@ -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:

View File

@ -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))

View File

@ -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)))

View File

@ -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);

View File

@ -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

View File

@ -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))

View File

@ -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) */

View File

@ -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);

View File

@ -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)

View File

@ -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_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_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) */

View File

@ -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);

View 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) _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

View File

@ -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);

View File

@ -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;

View File

@ -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 */

View File

@ -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);

View File

@ -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

View File

@ -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.

View File

@ -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. */

View File

@ -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 */

View File

@ -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);

View File

@ -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))
/*

View File

@ -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 */

View File

@ -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

View File

@ -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*);

View File

@ -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*);