File indexing completed on 2025-11-19 09:50:54
0001 #ifndef Py_PYMACRO_H
0002 #define Py_PYMACRO_H
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \
0021 && !defined(__cplusplus) && defined(__STDC_VERSION__) \
0022 && __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L
0023 # define static_assert _Static_assert
0024 #endif
0025
0026
0027 #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
0028
0029
0030 #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
0031
0032
0033 #define Py_ABS(x) ((x) < 0 ? -(x) : (x))
0034
0035 #define _Py_XSTRINGIFY(x) #x
0036
0037
0038
0039
0040
0041 #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
0042
0043
0044 #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
0045
0046
0047 #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
0048
0049 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
0050 && !defined(__cplusplus) && !defined(_MSC_VER))
0051 # define Py_BUILD_ASSERT_EXPR(cond) \
0052 ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \
0053 0)
0054 #else
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 # define Py_BUILD_ASSERT_EXPR(cond) \
0069 (sizeof(char [1 - 2*!(cond)]) - 1)
0070 #endif
0071
0072 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \
0073 || (defined(__cplusplus) && __cplusplus >= 201103L))
0074
0075 # define Py_BUILD_ASSERT(cond) \
0076 do { \
0077 static_assert((cond), #cond); \
0078 } while (0)
0079 #else
0080 # define Py_BUILD_ASSERT(cond) \
0081 do { \
0082 (void)Py_BUILD_ASSERT_EXPR(cond); \
0083 } while(0)
0084 #endif
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
0096 (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
0097
0098
0099 #define Py_ARRAY_LENGTH(array) \
0100 (sizeof(array) / sizeof((array)[0]) \
0101 + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
0102 typeof(&(array)[0]))))
0103 #else
0104 #define Py_ARRAY_LENGTH(array) \
0105 (sizeof(array) / sizeof((array)[0]))
0106 #endif
0107
0108
0109
0110 #define PyDoc_VAR(name) static const char name[]
0111 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
0112 #ifdef WITH_DOC_STRINGS
0113 #define PyDoc_STR(str) str
0114 #else
0115 #define PyDoc_STR(str) ""
0116 #endif
0117
0118
0119
0120 #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1))
0121
0122 #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \
0123 (size_t)((a) - 1)) & ~(size_t)((a) - 1))
0124
0125 #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1)))
0126
0127 #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \
0128 (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1)))
0129
0130 #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1)))
0131
0132
0133
0134
0135
0136
0137 #if defined(__GNUC__) || defined(__clang__)
0138 # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
0139 #elif defined(_MSC_VER)
0140
0141
0142
0143 # define Py_UNUSED(name) \
0144 __pragma(warning(push)) \
0145 __pragma(warning(suppress: 4100)) \
0146 _unused_ ## name \
0147 __pragma(warning(pop))
0148 #else
0149 # define Py_UNUSED(name) _unused_ ## name
0150 #endif
0151
0152 #if defined(RANDALL_WAS_HERE)
0153 # define Py_UNREACHABLE() \
0154 Py_FatalError( \
0155 "If you're seeing this, the code is in what I thought was\n" \
0156 "an unreachable state.\n\n" \
0157 "I could give you advice for what to do, but honestly, why\n" \
0158 "should you trust me? I clearly screwed this up. I'm writing\n" \
0159 "a message that should never appear, yet I know it will\n" \
0160 "probably appear someday.\n\n" \
0161 "On a deep level, I know I'm not up to this task.\n" \
0162 "I'm so sorry.\n" \
0163 "https://xkcd.com/2200")
0164 #elif defined(Py_DEBUG)
0165 # define Py_UNREACHABLE() \
0166 Py_FatalError( \
0167 "We've reached an unreachable state. Anything is possible.\n" \
0168 "The limits were in our heads all along. Follow your dreams.\n" \
0169 "https://xkcd.com/2200")
0170 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
0171 # define Py_UNREACHABLE() __builtin_unreachable()
0172 #elif defined(__clang__) || defined(__INTEL_COMPILER)
0173 # define Py_UNREACHABLE() __builtin_unreachable()
0174 #elif defined(_MSC_VER)
0175 # define Py_UNREACHABLE() __assume(0)
0176 #else
0177 # define Py_UNREACHABLE() \
0178 Py_FatalError("Unreachable C code path reached")
0179 #endif
0180
0181 #define _Py_CONTAINER_OF(ptr, type, member) \
0182 (type*)((char*)ptr - offsetof(type, member))
0183
0184
0185
0186 #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
0187
0188
0189
0190
0191 #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
0192
0193 #endif