Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/python3.14/pymacro.h
1 #ifndef Py_PYMACRO_H 2 #define Py_PYMACRO_H 3 4 // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are 5 // defined, <sys/cdefs.h> disables C11 support and <assert.h> does not define 6 // the static_assert() macro. 7 // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290 8 // 9 // macOS <= 10.10 doesn't define static_assert in assert.h at all despite 10 // having C11 compiler support. 11 // 12 // static_assert is defined in glibc from version 2.16. Compiler support for 13 // the C11 _Static_assert keyword is in gcc >= 4.6. 14 // 15 // MSVC makes static_assert a keyword in C11-17, contrary to the standards. 16 // 17 // In C++11 and C2x, static_assert is a keyword, redefining is undefined 18 // behaviour. So only define if building as C, not C++ (if __cplusplus is 19 // not defined), and only for C11-17. 20 #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \ 21 && !defined(__cplusplus) && defined(__STDC_VERSION__) \ 22 && __STDC_VERSION__ >= 201112L && __STDC_VERSION__ <= 201710L 23 # define static_assert _Static_assert 24 #endif 25 26 27 // _Py_ALIGNED_DEF(N, T): Define a variable/member with increased alignment 28 // 29 // `N`: the desired minimum alignment, an integer literal, number of bytes 30 // `T`: the type of the defined variable 31 // (or a type with at least the defined variable's alignment) 32 // 33 // May not be used on a struct definition. 34 // 35 // Standards/compiler support for `alignas` alternatives: 36 // - `alignas` is a keyword in C23 and C++11. 37 // - `_Alignas` is a keyword in C11 38 // - GCC & clang has __attribute__((aligned)) 39 // (use that for older standards in pedantic mode) 40 // - MSVC has __declspec(align) 41 // - `_Alignas` is common C compiler extension 42 // Older compilers may name `alignas` differently; to allow compilation on such 43 // unsupported platforms, we don't redefine _Py_ALIGNED_DEF if it's already 44 // defined. Note that defining it wrong (including defining it to nothing) will 45 // cause ABI incompatibilities. 46 // 47 // Behavior of `alignas` alternatives: 48 // - `alignas` & `_Alignas`: 49 // - Can be used multiple times; the greatest alignment applies. 50 // - It is an *error* if the combined effect of all `alignas` modifiers would 51 // decrease the alignment. 52 // - Takes types or numbers. 53 // - May not be used on a struct definition, unless also defining a variable. 54 // - `__declspec(align)`: 55 // - Has no effect if it would decrease alignment. 56 // - Only takes an integer literal. 57 // - May be used on struct or variable definitions. 58 // However, when defining both the struct and the variable at once, 59 // `declspec(aligned)` causes compiler warning 5274 and possible ABI 60 // incompatibility. 61 // - ` __attribute__((aligned))`: 62 // - Has no effect if it would decrease alignment. 63 // - Takes types or numbers 64 // - May be used on struct or variable definitions. 65 #ifndef _Py_ALIGNED_DEF 66 # ifdef __cplusplus 67 # if __cplusplus >= 201103L 68 # define _Py_ALIGNED_DEF(N, T) alignas(N) alignas(T) T 69 # elif defined(__GNUC__) || defined(__clang__) 70 # define _Py_ALIGNED_DEF(N, T) __attribute__((aligned(N))) T 71 # elif defined(_MSC_VER) 72 # define _Py_ALIGNED_DEF(N, T) __declspec(align(N)) T 73 # else 74 # define _Py_ALIGNED_DEF(N, T) alignas(N) alignas(T) T 75 # endif 76 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L 77 # define _Py_ALIGNED_DEF(N, T) alignas(N) alignas(T) T 78 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 79 # define _Py_ALIGNED_DEF(N, T) _Alignas(N) _Alignas(T) T 80 # elif (defined(__GNUC__) || defined(__clang__)) 81 # define _Py_ALIGNED_DEF(N, T) __attribute__((aligned(N))) T 82 # elif defined(_MSC_VER) 83 # define _Py_ALIGNED_DEF(N, T) __declspec(align(N)) T 84 # else 85 # define _Py_ALIGNED_DEF(N, T) _Alignas(N) _Alignas(T) T 86 # endif 87 #endif 88 89 /* Minimum value between x and y */ 90 #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) 91 92 /* Maximum value between x and y */ 93 #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) 94 95 /* Absolute value of the number x */ 96 #define Py_ABS(x) ((x) < 0 ? -(x) : (x)) 97 /* Safer implementation that avoids an undefined behavior for the minimal 98 value of the signed integer type if its absolute value is larger than 99 the maximal value of the signed integer type (in the two's complement 100 representations, which is common). 101 */ 102 #define _Py_ABS_CAST(T, x) ((x) >= 0 ? ((T) (x)) : ((T) (((T) -((x) + 1)) + 1u))) 103 104 #define _Py_XSTRINGIFY(x) #x 105 106 /* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced 107 with "123" by the preprocessor. Defines are also replaced by their value. 108 For example Py_STRINGIFY(__LINE__) is replaced by the line number, not 109 by "__LINE__". */ 110 #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x) 111 112 /* Get the size of a structure member in bytes */ 113 #define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) 114 115 /* Argument must be a char or an int in [-128, 127] or [0, 255]. */ 116 #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) 117 118 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ 119 && !defined(__cplusplus) && !defined(_MSC_VER)) 120 # define Py_BUILD_ASSERT_EXPR(cond) \ 121 ((void)sizeof(struct { int dummy; _Static_assert(cond, #cond); }), \ 122 0) 123 #else 124 /* Assert a build-time dependency, as an expression. 125 * 126 * Your compile will fail if the condition isn't true, or can't be evaluated 127 * by the compiler. This can be used in an expression: its value is 0. 128 * 129 * Example: 130 * 131 * #define foo_to_char(foo) \ 132 * ((char *)(foo) \ 133 * + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0)) 134 * 135 * Written by Rusty Russell, public domain, http://ccodearchive.net/ 136 */ 137 # define Py_BUILD_ASSERT_EXPR(cond) \ 138 (sizeof(char [1 - 2*!(cond)]) - 1) 139 #endif 140 141 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) \ 142 || (defined(__cplusplus) && __cplusplus >= 201103L)) 143 // Use static_assert() on C11 and newer 144 # define Py_BUILD_ASSERT(cond) \ 145 do { \ 146 static_assert((cond), #cond); \ 147 } while (0) 148 #else 149 # define Py_BUILD_ASSERT(cond) \ 150 do { \ 151 (void)Py_BUILD_ASSERT_EXPR(cond); \ 152 } while(0) 153 #endif 154 155 /* Get the number of elements in a visible array 156 157 This does not work on pointers, or arrays declared as [], or function 158 parameters. With correct compiler support, such usage will cause a build 159 error (see Py_BUILD_ASSERT_EXPR). 160 161 Written by Rusty Russell, public domain, http://ccodearchive.net/ 162 163 Requires at GCC 3.1+ */ 164 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ 165 (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4))) 166 /* Two gcc extensions. 167 &a[0] degrades to a pointer: a different type from an array */ 168 #define Py_ARRAY_LENGTH(array) \ 169 (sizeof(array) / sizeof((array)[0]) \ 170 + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ 171 typeof(&(array)[0])))) 172 #else 173 #define Py_ARRAY_LENGTH(array) \ 174 (sizeof(array) / sizeof((array)[0])) 175 #endif 176 177 178 /* Define macros for inline documentation. */ 179 #define PyDoc_VAR(name) static const char name[] 180 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) 181 #ifdef WITH_DOC_STRINGS 182 #define PyDoc_STR(str) str 183 #else 184 #define PyDoc_STR(str) "" 185 #endif 186 187 /* Below "a" is a power of 2. */ 188 /* Round down size "n" to be a multiple of "a". */ 189 #define _Py_SIZE_ROUND_DOWN(n, a) ((size_t)(n) & ~(size_t)((a) - 1)) 190 /* Round up size "n" to be a multiple of "a". */ 191 #define _Py_SIZE_ROUND_UP(n, a) (((size_t)(n) + \ 192 (size_t)((a) - 1)) & ~(size_t)((a) - 1)) 193 /* Round pointer "p" down to the closest "a"-aligned address <= "p". */ 194 #define _Py_ALIGN_DOWN(p, a) ((void *)((uintptr_t)(p) & ~(uintptr_t)((a) - 1))) 195 /* Round pointer "p" up to the closest "a"-aligned address >= "p". */ 196 #define _Py_ALIGN_UP(p, a) ((void *)(((uintptr_t)(p) + \ 197 (uintptr_t)((a) - 1)) & ~(uintptr_t)((a) - 1))) 198 /* Check if pointer "p" is aligned to "a"-bytes boundary. */ 199 #define _Py_IS_ALIGNED(p, a) (!((uintptr_t)(p) & (uintptr_t)((a) - 1))) 200 201 /* Use this for unused arguments in a function definition to silence compiler 202 * warnings. Example: 203 * 204 * int func(int a, int Py_UNUSED(b)) { return a; } 205 */ 206 #if defined(__GNUC__) || defined(__clang__) 207 # define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) 208 #elif defined(_MSC_VER) 209 // Disable warning C4100: unreferenced formal parameter, 210 // declare the parameter, 211 // restore old compiler warnings. 212 # define Py_UNUSED(name) \ 213 __pragma(warning(push)) \ 214 __pragma(warning(suppress: 4100)) \ 215 _unused_ ## name \ 216 __pragma(warning(pop)) 217 #else 218 # define Py_UNUSED(name) _unused_ ## name 219 #endif 220 221 #if defined(RANDALL_WAS_HERE) 222 # define Py_UNREACHABLE() \ 223 Py_FatalError( \ 224 "If you're seeing this, the code is in what I thought was\n" \ 225 "an unreachable state.\n\n" \ 226 "I could give you advice for what to do, but honestly, why\n" \ 227 "should you trust me? I clearly screwed this up. I'm writing\n" \ 228 "a message that should never appear, yet I know it will\n" \ 229 "probably appear someday.\n\n" \ 230 "On a deep level, I know I'm not up to this task.\n" \ 231 "I'm so sorry.\n" \ 232 "https://xkcd.com/2200") 233 #elif defined(Py_DEBUG) 234 # define Py_UNREACHABLE() \ 235 Py_FatalError( \ 236 "We've reached an unreachable state. Anything is possible.\n" \ 237 "The limits were in our heads all along. Follow your dreams.\n" \ 238 "https://xkcd.com/2200") 239 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)) 240 # define Py_UNREACHABLE() __builtin_unreachable() 241 #elif defined(__clang__) || defined(__INTEL_COMPILER) 242 # define Py_UNREACHABLE() __builtin_unreachable() 243 #elif defined(_MSC_VER) 244 # define Py_UNREACHABLE() __assume(0) 245 #else 246 # define Py_UNREACHABLE() \ 247 Py_FatalError("Unreachable C code path reached") 248 #endif 249 250 #define _Py_CONTAINER_OF(ptr, type, member) \ 251 (type*)((char*)ptr - offsetof(type, member)) 252 253 // Prevent using an expression as a l-value. 254 // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error. 255 #define _Py_RVALUE(EXPR) ((void)0, (EXPR)) 256 257 // Return non-zero if the type is signed, return zero if it's unsigned. 258 // Use "<= 0" rather than "< 0" to prevent the compiler warning: 259 // "comparison of unsigned expression in '< 0' is always false". 260 #define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0) 261 262 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000 // 3.14 263 // Version helpers. These are primarily macros, but have exported equivalents. 264 PyAPI_FUNC(uint32_t) Py_PACK_FULL_VERSION(int x, int y, int z, int level, int serial); 265 PyAPI_FUNC(uint32_t) Py_PACK_VERSION(int x, int y); 266 #define Py_PACK_FULL_VERSION _Py_PACK_FULL_VERSION 267 #define Py_PACK_VERSION(X, Y) Py_PACK_FULL_VERSION(X, Y, 0, 0, 0) 268 #endif // Py_LIMITED_API < 3.14 269 270 271 #endif /* Py_PYMACRO_H */