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