pyport.h 28 KB

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