pyport.h 30 KB

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