pyport.h 32 KB

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