pyport.h 25 KB

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