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