pyport.h 31 KB

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