pyport.h 31 KB

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