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