/home/mdboom/Work/builds/cpython/Include/pyport.h
Line | Count | Source |
1 | #ifndef Py_PYPORT_H |
2 | #define Py_PYPORT_H |
3 | |
4 | #include "pyconfig.h" /* include for defines */ |
5 | |
6 | #include <inttypes.h> |
7 | |
8 | #include <limits.h> |
9 | #ifndef UCHAR_MAX |
10 | # error "limits.h must define UCHAR_MAX" |
11 | #endif |
12 | #if UCHAR_MAX != 255 |
13 | # error "Python's source code assumes C's unsigned char is an 8-bit type" |
14 | #endif |
15 | |
16 | |
17 | // Macro to use C++ static_cast<>, reinterpret_cast<> and const_cast<> |
18 | // in the Python C API. |
19 | // |
20 | // In C++, _Py_CAST(type, expr) converts a constant expression to a |
21 | // non constant type using const_cast<type>. For example, |
22 | // _Py_CAST(PyObject*, op) can convert a "const PyObject*" to |
23 | // "PyObject*". |
24 | // |
25 | // The type argument must not be a constant type. |
26 | #ifdef __cplusplus |
27 | #include <cstddef> |
28 | # define _Py_STATIC_CAST(type, expr) static_cast<type>(expr) |
29 | extern "C++" { |
30 | namespace { |
31 | template <typename type> |
32 | inline type _Py_CAST_impl(long int ptr) { |
33 | return reinterpret_cast<type>(ptr); |
34 | } |
35 | template <typename type> |
36 | inline type _Py_CAST_impl(int ptr) { |
37 | return reinterpret_cast<type>(ptr); |
38 | } |
39 | #if __cplusplus >= 201103 |
40 | template <typename type> |
41 | inline type _Py_CAST_impl(std::nullptr_t) { |
42 | return static_cast<type>(nullptr); |
43 | } |
44 | #endif |
45 | |
46 | template <typename type, typename expr_type> |
47 | inline type _Py_CAST_impl(expr_type *expr) { |
48 | return reinterpret_cast<type>(expr); |
49 | } |
50 | |
51 | template <typename type, typename expr_type> |
52 | inline type _Py_CAST_impl(expr_type const *expr) { |
53 | return reinterpret_cast<type>(const_cast<expr_type *>(expr)); |
54 | } |
55 | |
56 | template <typename type, typename expr_type> |
57 | inline type _Py_CAST_impl(expr_type &expr) { |
58 | return static_cast<type>(expr); |
59 | } |
60 | |
61 | template <typename type, typename expr_type> |
62 | inline type _Py_CAST_impl(expr_type const &expr) { |
63 | return static_cast<type>(const_cast<expr_type &>(expr)); |
64 | } |
65 | } |
66 | } |
67 | # define _Py_CAST(type, expr) _Py_CAST_impl<type>(expr) |
68 | |
69 | #else |
70 | # define _Py_STATIC_CAST(type, expr) ((type)(expr2.11M )) |
71 | # define _Py_CAST(type, expr) ((type)(expr49.8M )) |
72 | #endif |
73 | |
74 | // Static inline functions should use _Py_NULL rather than using directly NULL |
75 | // to prevent C++ compiler warnings. On C++11 and newer, _Py_NULL is defined as |
76 | // nullptr. |
77 | #if defined(__cplusplus) && __cplusplus >= 201103 |
78 | # define _Py_NULL nullptr |
79 | #else |
80 | # define _Py_NULL NULL |
81 | #endif |
82 | |
83 | |
84 | /* Defines to build Python and its standard library: |
85 | * |
86 | * - Py_BUILD_CORE: Build Python core. Give access to Python internals, but |
87 | * should not be used by third-party modules. |
88 | * - Py_BUILD_CORE_BUILTIN: Build a Python stdlib module as a built-in module. |
89 | * - Py_BUILD_CORE_MODULE: Build a Python stdlib module as a dynamic library. |
90 | * |
91 | * Py_BUILD_CORE_BUILTIN and Py_BUILD_CORE_MODULE imply Py_BUILD_CORE. |
92 | * |
93 | * On Windows, Py_BUILD_CORE_MODULE exports "PyInit_xxx" symbol, whereas |
94 | * Py_BUILD_CORE_BUILTIN does not. |
95 | */ |
96 | #if defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE) |
97 | # define Py_BUILD_CORE |
98 | #endif |
99 | #if defined(Py_BUILD_CORE_MODULE) && !defined(Py_BUILD_CORE) |
100 | # define Py_BUILD_CORE |
101 | #endif |
102 | |
103 | |
104 | /************************************************************************** |
105 | Symbols and macros to supply platform-independent interfaces to basic |
106 | C language & library operations whose spellings vary across platforms. |
107 | |
108 | Please try to make documentation here as clear as possible: by definition, |
109 | the stuff here is trying to illuminate C's darkest corners. |
110 | |
111 | Config #defines referenced here: |
112 | |
113 | SIGNED_RIGHT_SHIFT_ZERO_FILLS |
114 | Meaning: To be defined iff i>>j does not extend the sign bit when i is a |
115 | signed integral type and i < 0. |
116 | Used in: Py_ARITHMETIC_RIGHT_SHIFT |
117 | |
118 | Py_DEBUG |
119 | Meaning: Extra checks compiled in for debug mode. |
120 | Used in: Py_SAFE_DOWNCAST |
121 | |
122 | **************************************************************************/ |
123 | |
124 | /* typedefs for some C9X-defined synonyms for integral types. |
125 | * |
126 | * The names in Python are exactly the same as the C9X names, except with a |
127 | * Py_ prefix. Until C9X is universally implemented, this is the only way |
128 | * to ensure that Python gets reliable names that don't conflict with names |
129 | * in non-Python code that are playing their own tricks to define the C9X |
130 | * names. |
131 | * |
132 | * NOTE: don't go nuts here! Python has no use for *most* of the C9X |
133 | * integral synonyms. Only define the ones we actually need. |
134 | */ |
135 | |
136 | /* long long is required. Ensure HAVE_LONG_LONG is defined for compatibility. */ |
137 | #ifndef HAVE_LONG_LONG |
138 | #define HAVE_LONG_LONG 1 |
139 | #endif |
140 | #ifndef PY_LONG_LONG |
141 | #define PY_LONG_LONG long long |
142 | /* If LLONG_MAX is defined in limits.h, use that. */ |
143 | #define PY_LLONG_MIN LLONG_MIN |
144 | #define PY_LLONG_MAX LLONG_MAX |
145 | #define PY_ULLONG_MAX ULLONG_MAX |
146 | #endif |
147 | |
148 | #define PY_UINT32_T uint32_t |
149 | #define PY_UINT64_T uint64_t |
150 | |
151 | /* Signed variants of the above */ |
152 | #define PY_INT32_T int32_t |
153 | #define PY_INT64_T int64_t |
154 | |
155 | /* PYLONG_BITS_IN_DIGIT describes the number of bits per "digit" (limb) in the |
156 | * PyLongObject implementation (longintrepr.h). It's currently either 30 or 15, |
157 | * defaulting to 30. The 15-bit digit option may be removed in the future. |
158 | */ |
159 | #ifndef PYLONG_BITS_IN_DIGIT |
160 | #define PYLONG_BITS_IN_DIGIT 30 |
161 | #endif |
162 | |
163 | /* uintptr_t is the C9X name for an unsigned integral type such that a |
164 | * legitimate void* can be cast to uintptr_t and then back to void* again |
165 | * without loss of information. Similarly for intptr_t, wrt a signed |
166 | * integral type. |
167 | */ |
168 | typedef uintptr_t Py_uintptr_t; |
169 | typedef intptr_t Py_intptr_t; |
170 | |
171 | /* Py_ssize_t is a signed integral type such that sizeof(Py_ssize_t) == |
172 | * sizeof(size_t). C99 doesn't define such a thing directly (size_t is an |
173 | * unsigned integral type). See PEP 353 for details. |
174 | * PY_SSIZE_T_MAX is the largest positive value of type Py_ssize_t. |
175 | */ |
176 | #ifdef HAVE_PY_SSIZE_T |
177 | |
178 | #elif HAVE_SSIZE_T |
179 | typedef ssize_t Py_ssize_t; |
180 | # define PY_SSIZE_T_MAX SSIZE_MAX |
181 | #elif SIZEOF_VOID_P == SIZEOF_SIZE_T |
182 | typedef Py_intptr_t Py_ssize_t; |
183 | # define PY_SSIZE_T_MAX INTPTR_MAX |
184 | #else |
185 | # error "Python needs a typedef for Py_ssize_t in pyport.h." |
186 | #endif |
187 | |
188 | /* Smallest negative value of type Py_ssize_t. */ |
189 | #define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1) |
190 | |
191 | /* Py_hash_t is the same size as a pointer. */ |
192 | #define SIZEOF_PY_HASH_T SIZEOF_SIZE_T |
193 | typedef Py_ssize_t Py_hash_t; |
194 | /* Py_uhash_t is the unsigned equivalent needed to calculate numeric hash. */ |
195 | #define SIZEOF_PY_UHASH_T SIZEOF_SIZE_T |
196 | typedef size_t Py_uhash_t; |
197 | |
198 | /* Now PY_SSIZE_T_CLEAN is mandatory. This is just for backward compatibility. */ |
199 | typedef Py_ssize_t Py_ssize_clean_t; |
200 | |
201 | /* Largest possible value of size_t. */ |
202 | #define PY_SIZE_MAX SIZE_MAX |
203 | |
204 | /* Macro kept for backward compatibility: use directly "z" in new code. |
205 | * |
206 | * PY_FORMAT_SIZE_T is a modifier for use in a printf format to convert an |
207 | * argument with the width of a size_t or Py_ssize_t: "z" (C99). |
208 | */ |
209 | #ifndef PY_FORMAT_SIZE_T |
210 | # define PY_FORMAT_SIZE_T "z" |
211 | #endif |
212 | |
213 | /* Py_LOCAL can be used instead of static to get the fastest possible calling |
214 | * convention for functions that are local to a given module. |
215 | * |
216 | * Py_LOCAL_INLINE does the same thing, and also explicitly requests inlining, |
217 | * for platforms that support that. |
218 | * |
219 | * NOTE: You can only use this for functions that are entirely local to a |
220 | * module; functions that are exported via method tables, callbacks, etc, |
221 | * should keep using static. |
222 | */ |
223 | |
224 | #if defined(_MSC_VER) |
225 | /* ignore warnings if the compiler decides not to inline a function */ |
226 | # pragma warning(disable: 4710) |
227 | /* fastest possible local call under MSVC */ |
228 | # define Py_LOCAL(type) static type __fastcall |
229 | # define Py_LOCAL_INLINE(type) static __inline type __fastcall |
230 | #else |
231 | # define Py_LOCAL(type) static type |
232 | # define Py_LOCAL_INLINE(type) static inline type |
233 | #endif |
234 | |
235 | // bpo-28126: Py_MEMCPY is kept for backwards compatibility, |
236 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
237 | # define Py_MEMCPY memcpy |
238 | #endif |
239 | |
240 | #ifdef HAVE_IEEEFP_H |
241 | #include <ieeefp.h> /* needed for 'finite' declaration on some platforms */ |
242 | #endif |
243 | |
244 | #include <math.h> /* Moved here from the math section, before extern "C" */ |
245 | |
246 | /******************************************** |
247 | * WRAPPER FOR <time.h> and/or <sys/time.h> * |
248 | ********************************************/ |
249 | |
250 | #ifdef HAVE_SYS_TIME_H |
251 | #include <sys/time.h> |
252 | #endif |
253 | #include <time.h> |
254 | |
255 | /****************************** |
256 | * WRAPPER FOR <sys/select.h> * |
257 | ******************************/ |
258 | |
259 | /* NB caller must include <sys/types.h> */ |
260 | |
261 | #ifdef HAVE_SYS_SELECT_H |
262 | #include <sys/select.h> |
263 | #endif /* !HAVE_SYS_SELECT_H */ |
264 | |
265 | /******************************* |
266 | * stat() and fstat() fiddling * |
267 | *******************************/ |
268 | |
269 | #ifdef HAVE_SYS_STAT_H |
270 | #include <sys/stat.h> |
271 | #elif defined(HAVE_STAT_H) |
272 | #include <stat.h> |
273 | #endif |
274 | |
275 | #ifndef S_IFMT |
276 | /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */ |
277 | #define S_IFMT 0170000 |
278 | #endif |
279 | |
280 | #ifndef S_IFLNK |
281 | /* Windows doesn't define S_IFLNK but posixmodule.c maps |
282 | * IO_REPARSE_TAG_SYMLINK to S_IFLNK */ |
283 | # define S_IFLNK 0120000 |
284 | #endif |
285 | |
286 | #ifndef S_ISREG |
287 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
288 | #endif |
289 | |
290 | #ifndef S_ISDIR |
291 | #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR) |
292 | #endif |
293 | |
294 | #ifndef S_ISCHR |
295 | #define S_ISCHR(x) (((x) & S_IFMT) == S_IFCHR) |
296 | #endif |
297 | |
298 | #ifdef __cplusplus |
299 | /* Move this down here since some C++ #include's don't like to be included |
300 | inside an extern "C" */ |
301 | extern "C" { |
302 | #endif |
303 | |
304 | |
305 | /* Py_ARITHMETIC_RIGHT_SHIFT |
306 | * C doesn't define whether a right-shift of a signed integer sign-extends |
307 | * or zero-fills. Here a macro to force sign extension: |
308 | * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) |
309 | * Return I >> J, forcing sign extension. Arithmetically, return the |
310 | * floor of I/2**J. |
311 | * Requirements: |
312 | * I should have signed integer type. In the terminology of C99, this can |
313 | * be either one of the five standard signed integer types (signed char, |
314 | * short, int, long, long long) or an extended signed integer type. |
315 | * J is an integer >= 0 and strictly less than the number of bits in the |
316 | * type of I (because C doesn't define what happens for J outside that |
317 | * range either). |
318 | * TYPE used to specify the type of I, but is now ignored. It's been left |
319 | * in for backwards compatibility with versions <= 2.6 or 3.0. |
320 | * Caution: |
321 | * I may be evaluated more than once. |
322 | */ |
323 | #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS |
324 | #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \ |
325 | ((I) < 0 ? -1-((-1-(I)) >> (J)) : (I) >> (J)) |
326 | #else |
327 | #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J)) |
328 | #endif |
329 | |
330 | /* Py_FORCE_EXPANSION(X) |
331 | * "Simply" returns its argument. However, macro expansions within the |
332 | * argument are evaluated. This unfortunate trickery is needed to get |
333 | * token-pasting to work as desired in some cases. |
334 | */ |
335 | #define Py_FORCE_EXPANSION(X) X |
336 | |
337 | /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) |
338 | * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this |
339 | * assert-fails if any information is lost. |
340 | * Caution: |
341 | * VALUE may be evaluated more than once. |
342 | */ |
343 | #ifdef Py_DEBUG |
344 | # define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \ |
345 | (assert(_Py_STATIC_CAST(WIDE, _Py_STATIC_CAST(NARROW, (VALUE))) == (VALUE)), \ |
346 | _Py_STATIC_CAST(NARROW, (VALUE))) |
347 | #else |
348 | # define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) _Py_STATIC_CAST(NARROW, (VALUE)) |
349 | #endif |
350 | |
351 | |
352 | /* Py_DEPRECATED(version) |
353 | * Declare a variable, type, or function deprecated. |
354 | * The macro must be placed before the declaration. |
355 | * Usage: |
356 | * Py_DEPRECATED(3.3) extern int old_var; |
357 | * Py_DEPRECATED(3.4) typedef int T1; |
358 | * Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void); |
359 | */ |
360 | #if defined(__GNUC__) \ |
361 | && ((__GNUC__ >= 4) || (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) |
362 | #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__)) |
363 | #elif defined(_MSC_VER) |
364 | #define Py_DEPRECATED(VERSION) __declspec(deprecated( \ |
365 | "deprecated in " #VERSION)) |
366 | #else |
367 | #define Py_DEPRECATED(VERSION_UNUSED) |
368 | #endif |
369 | |
370 | #if defined(__clang__) |
371 | #define _Py_COMP_DIAG_PUSH _Pragma("clang diagnostic push") |
372 | #define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ |
373 | _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") |
374 | #define _Py_COMP_DIAG_POP _Pragma("clang diagnostic pop") |
375 | #elif defined(__GNUC__) \ |
376 | && ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) |
377 | #define _Py_COMP_DIAG_PUSH _Pragma("GCC diagnostic push") |
378 | #define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ |
379 | _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
380 | #define _Py_COMP_DIAG_POP _Pragma("GCC diagnostic pop") |
381 | #elif defined(_MSC_VER) |
382 | #define _Py_COMP_DIAG_PUSH __pragma(warning(push)) |
383 | #define _Py_COMP_DIAG_IGNORE_DEPR_DECLS __pragma(warning(disable: 4996)) |
384 | #define _Py_COMP_DIAG_POP __pragma(warning(pop)) |
385 | #else |
386 | #define _Py_COMP_DIAG_PUSH |
387 | #define _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
388 | #define _Py_COMP_DIAG_POP |
389 | #endif |
390 | |
391 | /* _Py_HOT_FUNCTION |
392 | * The hot attribute on a function is used to inform the compiler that the |
393 | * function is a hot spot of the compiled program. The function is optimized |
394 | * more aggressively and on many target it is placed into special subsection of |
395 | * the text section so all hot functions appears close together improving |
396 | * locality. |
397 | * |
398 | * Usage: |
399 | * int _Py_HOT_FUNCTION x(void) { return 3; } |
400 | * |
401 | * Issue #28618: This attribute must not be abused, otherwise it can have a |
402 | * negative effect on performance. Only the functions were Python spend most of |
403 | * its time must use it. Use a profiler when running performance benchmark |
404 | * suite to find these functions. |
405 | */ |
406 | #if defined(__GNUC__) \ |
407 | && ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) |
408 | #define _Py_HOT_FUNCTION __attribute__((hot)) |
409 | #else |
410 | #define _Py_HOT_FUNCTION |
411 | #endif |
412 | |
413 | // Ask the compiler to always inline a static inline function. The compiler can |
414 | // ignore it and decides to not inline the function. |
415 | // |
416 | // It can be used to inline performance critical static inline functions when |
417 | // building Python in debug mode with function inlining disabled. For example, |
418 | // MSC disables function inlining when building in debug mode. |
419 | // |
420 | // Marking blindly a static inline function with Py_ALWAYS_INLINE can result in |
421 | // worse performances (due to increased code size for example). The compiler is |
422 | // usually smarter than the developer for the cost/benefit analysis. |
423 | // |
424 | // If Python is built in debug mode (if the Py_DEBUG macro is defined), the |
425 | // Py_ALWAYS_INLINE macro does nothing. |
426 | // |
427 | // It must be specified before the function return type. Usage: |
428 | // |
429 | // static inline Py_ALWAYS_INLINE int random(void) { return 4; } |
430 | #if defined(Py_DEBUG) |
431 | // If Python is built in debug mode, usually compiler optimizations are |
432 | // disabled. In this case, Py_ALWAYS_INLINE can increase a lot the stack |
433 | // memory usage. For example, forcing inlining using gcc -O0 increases the |
434 | // stack usage from 6 KB to 15 KB per Python function call. |
435 | # define Py_ALWAYS_INLINE |
436 | #elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) |
437 | # define Py_ALWAYS_INLINE __attribute__((always_inline)) |
438 | #elif defined(_MSC_VER) |
439 | # define Py_ALWAYS_INLINE __forceinline |
440 | #else |
441 | # define Py_ALWAYS_INLINE |
442 | #endif |
443 | |
444 | // Py_NO_INLINE |
445 | // Disable inlining on a function. For example, it reduces the C stack |
446 | // consumption: useful on LTO+PGO builds which heavily inline code (see |
447 | // bpo-33720). |
448 | // |
449 | // Usage: |
450 | // |
451 | // Py_NO_INLINE static int random(void) { return 4; } |
452 | #if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) |
453 | # define Py_NO_INLINE __attribute__ ((noinline)) |
454 | #elif defined(_MSC_VER) |
455 | # define Py_NO_INLINE __declspec(noinline) |
456 | #else |
457 | # define Py_NO_INLINE |
458 | #endif |
459 | |
460 | /************************************************************************** |
461 | Prototypes that are missing from the standard include files on some systems |
462 | (and possibly only some versions of such systems.) |
463 | |
464 | Please be conservative with adding new ones, document them and enclose them |
465 | in platform-specific #ifdefs. |
466 | **************************************************************************/ |
467 | |
468 | #ifdef SOLARIS |
469 | /* Unchecked */ |
470 | extern int gethostname(char *, int); |
471 | #endif |
472 | |
473 | #ifdef HAVE__GETPTY |
474 | #include <sys/types.h> /* we need to import mode_t */ |
475 | extern char * _getpty(int *, int, mode_t, int); |
476 | #endif |
477 | |
478 | /* On QNX 6, struct termio must be declared by including sys/termio.h |
479 | if TCGETA, TCSETA, TCSETAW, or TCSETAF are used. sys/termio.h must |
480 | be included before termios.h or it will generate an error. */ |
481 | #if defined(HAVE_SYS_TERMIO_H) && !defined(__hpux) |
482 | #include <sys/termio.h> |
483 | #endif |
484 | |
485 | |
486 | /* On 4.4BSD-descendants, ctype functions serves the whole range of |
487 | * wchar_t character set rather than single byte code points only. |
488 | * This characteristic can break some operations of string object |
489 | * including str.upper() and str.split() on UTF-8 locales. This |
490 | * workaround was provided by Tim Robbins of FreeBSD project. |
491 | */ |
492 | |
493 | #if defined(__APPLE__) |
494 | # define _PY_PORT_CTYPE_UTF8_ISSUE |
495 | #endif |
496 | |
497 | #ifdef _PY_PORT_CTYPE_UTF8_ISSUE |
498 | #ifndef __cplusplus |
499 | /* The workaround below is unsafe in C++ because |
500 | * the <locale> defines these symbols as real functions, |
501 | * with a slightly different signature. |
502 | * See issue #10910 |
503 | */ |
504 | #include <ctype.h> |
505 | #include <wctype.h> |
506 | #undef isalnum |
507 | #define isalnum(c) iswalnum(btowc(c)) |
508 | #undef isalpha |
509 | #define isalpha(c) iswalpha(btowc(c)) |
510 | #undef islower |
511 | #define islower(c) iswlower(btowc(c)) |
512 | #undef isspace |
513 | #define isspace(c) iswspace(btowc(c)) |
514 | #undef isupper |
515 | #define isupper(c) iswupper(btowc(c)) |
516 | #undef tolower |
517 | #define tolower(c) towlower(btowc(c)) |
518 | #undef toupper |
519 | #define toupper(c) towupper(btowc(c)) |
520 | #endif |
521 | #endif |
522 | |
523 | |
524 | /* Declarations for symbol visibility. |
525 | |
526 | PyAPI_FUNC(type): Declares a public Python API function and return type |
527 | PyAPI_DATA(type): Declares public Python data and its type |
528 | PyMODINIT_FUNC: A Python module init function. If these functions are |
529 | inside the Python core, they are private to the core. |
530 | If in an extension module, it may be declared with |
531 | external linkage depending on the platform. |
532 | |
533 | As a number of platforms support/require "__declspec(dllimport/dllexport)", |
534 | we support a HAVE_DECLSPEC_DLL macro to save duplication. |
535 | */ |
536 | |
537 | /* |
538 | All windows ports, except cygwin, are handled in PC/pyconfig.h. |
539 | |
540 | Cygwin is the only other autoconf platform requiring special |
541 | linkage handling and it uses __declspec(). |
542 | */ |
543 | #if defined(__CYGWIN__) |
544 | # define HAVE_DECLSPEC_DLL |
545 | #endif |
546 | |
547 | #include "exports.h" |
548 | |
549 | /* only get special linkage if built as shared or platform is Cygwin */ |
550 | #if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__) |
551 | # if defined(HAVE_DECLSPEC_DLL) |
552 | # if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
553 | # define PyAPI_FUNC(RTYPE) Py_EXPORTED_SYMBOL RTYPE |
554 | # define PyAPI_DATA(RTYPE) extern Py_EXPORTED_SYMBOL RTYPE |
555 | /* module init functions inside the core need no external linkage */ |
556 | /* except for Cygwin to handle embedding */ |
557 | # if defined(__CYGWIN__) |
558 | # define PyMODINIT_FUNC Py_EXPORTED_SYMBOL PyObject* |
559 | # else /* __CYGWIN__ */ |
560 | # define PyMODINIT_FUNC PyObject* |
561 | # endif /* __CYGWIN__ */ |
562 | # else /* Py_BUILD_CORE */ |
563 | /* Building an extension module, or an embedded situation */ |
564 | /* public Python functions and data are imported */ |
565 | /* Under Cygwin, auto-import functions to prevent compilation */ |
566 | /* failures similar to those described at the bottom of 4.1: */ |
567 | /* http://docs.python.org/extending/windows.html#a-cookbook-approach */ |
568 | # if !defined(__CYGWIN__) |
569 | # define PyAPI_FUNC(RTYPE) Py_IMPORTED_SYMBOL RTYPE |
570 | # endif /* !__CYGWIN__ */ |
571 | # define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE |
572 | /* module init functions outside the core must be exported */ |
573 | # if defined(__cplusplus) |
574 | # define PyMODINIT_FUNC extern "C" Py_EXPORTED_SYMBOL PyObject* |
575 | # else /* __cplusplus */ |
576 | # define PyMODINIT_FUNC Py_EXPORTED_SYMBOL PyObject* |
577 | # endif /* __cplusplus */ |
578 | # endif /* Py_BUILD_CORE */ |
579 | # endif /* HAVE_DECLSPEC_DLL */ |
580 | #endif /* Py_ENABLE_SHARED */ |
581 | |
582 | /* If no external linkage macros defined by now, create defaults */ |
583 | #ifndef PyAPI_FUNC |
584 | # define PyAPI_FUNC(RTYPE) Py_EXPORTED_SYMBOL RTYPE |
585 | #endif |
586 | #ifndef PyAPI_DATA |
587 | # define PyAPI_DATA(RTYPE) extern Py_EXPORTED_SYMBOL RTYPE |
588 | #endif |
589 | #ifndef PyMODINIT_FUNC |
590 | # if defined(__cplusplus) |
591 | # define PyMODINIT_FUNC extern "C" Py_EXPORTED_SYMBOL PyObject* |
592 | # else /* __cplusplus */ |
593 | # define PyMODINIT_FUNC Py_EXPORTED_SYMBOL PyObject* |
594 | # endif /* __cplusplus */ |
595 | #endif |
596 | |
597 | /* limits.h constants that may be missing */ |
598 | |
599 | #ifndef INT_MAX |
600 | #define INT_MAX 2147483647 |
601 | #endif |
602 | |
603 | #ifndef LONG_MAX |
604 | #if SIZEOF_LONG == 4 |
605 | #define LONG_MAX 0X7FFFFFFFL |
606 | #elif SIZEOF_LONG == 8 |
607 | #define LONG_MAX 0X7FFFFFFFFFFFFFFFL |
608 | #else |
609 | #error "could not set LONG_MAX in pyport.h" |
610 | #endif |
611 | #endif |
612 | |
613 | #ifndef LONG_MIN |
614 | #define LONG_MIN (-LONG_MAX-1) |
615 | #endif |
616 | |
617 | #ifndef LONG_BIT |
618 | #define LONG_BIT (8 * SIZEOF_LONG) |
619 | #endif |
620 | |
621 | #if LONG_BIT != 8 * SIZEOF_LONG |
622 | /* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent |
623 | * 32-bit platforms using gcc. We try to catch that here at compile-time |
624 | * rather than waiting for integer multiplication to trigger bogus |
625 | * overflows. |
626 | */ |
627 | #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)." |
628 | #endif |
629 | |
630 | #ifdef __cplusplus |
631 | } |
632 | #endif |
633 | |
634 | /* |
635 | * Hide GCC attributes from compilers that don't support them. |
636 | */ |
637 | #if (!defined(__GNUC__) || __GNUC__ < 2 || \ |
638 | (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ) |
639 | #define Py_GCC_ATTRIBUTE(x) |
640 | #else |
641 | #define Py_GCC_ATTRIBUTE(x) __attribute__(x) |
642 | #endif |
643 | |
644 | /* |
645 | * Specify alignment on compilers that support it. |
646 | */ |
647 | #if defined(__GNUC__) && __GNUC__ >= 3 |
648 | #define Py_ALIGNED(x) __attribute__((aligned(x))) |
649 | #else |
650 | #define Py_ALIGNED(x) |
651 | #endif |
652 | |
653 | /* Eliminate end-of-loop code not reached warnings from SunPro C |
654 | * when using do{...}while(0) macros |
655 | */ |
656 | #ifdef __SUNPRO_C |
657 | #pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED) |
658 | #endif |
659 | |
660 | #ifndef Py_LL |
661 | #define Py_LL(x) x##LL |
662 | #endif |
663 | |
664 | #ifndef Py_ULL |
665 | #define Py_ULL(x) Py_LL(x##U) |
666 | #endif |
667 | |
668 | #define Py_VA_COPY va_copy |
669 | |
670 | /* |
671 | * Convenient macros to deal with endianness of the platform. WORDS_BIGENDIAN is |
672 | * detected by configure and defined in pyconfig.h. The code in pyconfig.h |
673 | * also takes care of Apple's universal builds. |
674 | */ |
675 | |
676 | #ifdef WORDS_BIGENDIAN |
677 | # define PY_BIG_ENDIAN 1 |
678 | # define PY_LITTLE_ENDIAN 0 |
679 | #else |
680 | # define PY_BIG_ENDIAN 0 |
681 | # define PY_LITTLE_ENDIAN 1 |
682 | #endif |
683 | |
684 | #ifdef __ANDROID__ |
685 | /* The Android langinfo.h header is not used. */ |
686 | # undef HAVE_LANGINFO_H |
687 | # undef CODESET |
688 | #endif |
689 | |
690 | /* Maximum value of the Windows DWORD type */ |
691 | #define PY_DWORD_MAX 4294967295U |
692 | |
693 | /* This macro used to tell whether Python was built with multithreading |
694 | * enabled. Now multithreading is always enabled, but keep the macro |
695 | * for compatibility. |
696 | */ |
697 | #ifndef WITH_THREAD |
698 | # define WITH_THREAD |
699 | #endif |
700 | |
701 | /* Check that ALT_SOABI is consistent with Py_TRACE_REFS: |
702 | ./configure --with-trace-refs should must be used to define Py_TRACE_REFS */ |
703 | #if defined(ALT_SOABI) && defined(Py_TRACE_REFS) |
704 | # error "Py_TRACE_REFS ABI is not compatible with release and debug ABI" |
705 | #endif |
706 | |
707 | #if defined(__ANDROID__) || defined(__VXWORKS__) |
708 | // Use UTF-8 as the locale encoding, ignore the LC_CTYPE locale. |
709 | // See _Py_GetLocaleEncoding(), PyUnicode_DecodeLocale() |
710 | // and PyUnicode_EncodeLocale(). |
711 | # define _Py_FORCE_UTF8_LOCALE |
712 | #endif |
713 | |
714 | #if defined(_Py_FORCE_UTF8_LOCALE) || defined(__APPLE__) |
715 | // Use UTF-8 as the filesystem encoding. |
716 | // See PyUnicode_DecodeFSDefaultAndSize(), PyUnicode_EncodeFSDefault(), |
717 | // Py_DecodeLocale() and Py_EncodeLocale(). |
718 | # define _Py_FORCE_UTF8_FS_ENCODING |
719 | #endif |
720 | |
721 | /* Mark a function which cannot return. Example: |
722 | PyAPI_FUNC(void) _Py_NO_RETURN PyThread_exit_thread(void); |
723 | |
724 | XLC support is intentionally omitted due to bpo-40244 */ |
725 | #ifndef _Py_NO_RETURN |
726 | #if defined(__clang__) || \ |
727 | (defined(__GNUC__) && \ |
728 | ((__GNUC__ >= 3) || \ |
729 | (__GNUC__ == 2) && (__GNUC_MINOR__ >= 5))) |
730 | # define _Py_NO_RETURN __attribute__((__noreturn__)) |
731 | #elif defined(_MSC_VER) |
732 | # define _Py_NO_RETURN __declspec(noreturn) |
733 | #else |
734 | # define _Py_NO_RETURN |
735 | #endif |
736 | #endif |
737 | |
738 | |
739 | // Preprocessor check for a builtin preprocessor function. Always return 0 |
740 | // if __has_builtin() macro is not defined. |
741 | // |
742 | // __has_builtin() is available on clang and GCC 10. |
743 | #ifdef __has_builtin |
744 | # define _Py__has_builtin(x) __has_builtin(x) |
745 | #else |
746 | # define _Py__has_builtin(x) 0 |
747 | #endif |
748 | |
749 | |
750 | /* A convenient way for code to know if sanitizers are enabled. */ |
751 | #if defined(__has_feature) |
752 | # if __has_feature(memory_sanitizer) |
753 | # if !defined(_Py_MEMORY_SANITIZER) |
754 | # define _Py_MEMORY_SANITIZER |
755 | # endif |
756 | # endif |
757 | # if __has_feature(address_sanitizer) |
758 | # if !defined(_Py_ADDRESS_SANITIZER) |
759 | # define _Py_ADDRESS_SANITIZER |
760 | # endif |
761 | # endif |
762 | #elif defined(__GNUC__) |
763 | # if defined(__SANITIZE_ADDRESS__) |
764 | # define _Py_ADDRESS_SANITIZER |
765 | # endif |
766 | #endif |
767 | |
768 | #endif /* Py_PYPORT_H */ |