fix to work on python <= 2.1
[python/dscho.git] / Include / pyport.h
blobc358cc2ac9b18267961c36c15deb166ad5ddeb9d
1 #ifndef Py_PYPORT_H
2 #define Py_PYPORT_H
4 #include "pyconfig.h" /* include for defines */
6 /**************************************************************************
7 Symbols and macros to supply platform-independent interfaces to basic
8 C language & library operations whose spellings vary across platforms.
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.
13 Config #defines referenced here:
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
20 Py_DEBUG
21 Meaning: Extra checks compiled in for debug mode.
22 Used in: Py_SAFE_DOWNCAST
24 HAVE_UINTPTR_T
25 Meaning: The C9X type uintptr_t is supported by the compiler
26 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
32 **************************************************************************/
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
45 /* typedefs for some C9X-defined synonyms for integral types.
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.
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.
57 #ifdef HAVE_LONG_LONG
58 #ifndef PY_LONG_LONG
59 #define PY_LONG_LONG long long
60 #endif
61 #endif /* HAVE_LONG_LONG */
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.
68 #ifdef HAVE_UINTPTR_T
69 typedef uintptr_t Py_uintptr_t;
70 typedef intptr_t Py_intptr_t;
72 #elif SIZEOF_VOID_P <= SIZEOF_INT
73 typedef unsigned int Py_uintptr_t;
74 typedef int Py_intptr_t;
76 #elif SIZEOF_VOID_P <= SIZEOF_LONG
77 typedef unsigned long Py_uintptr_t;
78 typedef long Py_intptr_t;
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;
84 #else
85 # error "Python needs a typedef for Py_uintptr_t in pyport.h."
86 #endif /* HAVE_UINTPTR_T */
88 #ifdef HAVE_STDLIB_H
89 #include <stdlib.h>
90 #endif
92 #include <math.h> /* Moved here from the math section, before extern "C" */
94 /********************************************
95 * WRAPPER FOR <time.h> and/or <sys/time.h> *
96 ********************************************/
98 #ifdef TIME_WITH_SYS_TIME
99 #include <sys/time.h>
100 #include <time.h>
101 #else /* !TIME_WITH_SYS_TIME */
102 #ifdef HAVE_SYS_TIME_H
103 #include <sys/time.h>
104 #else /* !HAVE_SYS_TIME_H */
105 #include <time.h>
106 #endif /* !HAVE_SYS_TIME_H */
107 #endif /* !TIME_WITH_SYS_TIME */
110 /******************************
111 * WRAPPER FOR <sys/select.h> *
112 ******************************/
114 /* NB caller must include <sys/types.h> */
116 #ifdef HAVE_SYS_SELECT_H
118 #include <sys/select.h>
120 #endif /* !HAVE_SYS_SELECT_H */
122 /*******************************
123 * stat() and fstat() fiddling *
124 *******************************/
126 /* We expect that stat and fstat exist on most systems.
127 * It's confirmed on Unix, Mac and Windows.
128 * If you don't have them, add
129 * #define DONT_HAVE_STAT
130 * and/or
131 * #define DONT_HAVE_FSTAT
132 * to your pyconfig.h. Python code beyond this should check HAVE_STAT and
133 * HAVE_FSTAT instead.
134 * Also
135 * #define DONT_HAVE_SYS_STAT_H
136 * if <sys/stat.h> doesn't exist on your platform, and
137 * #define HAVE_STAT_H
138 * if <stat.h> does (don't look at me -- ths mess is inherited).
140 #ifndef DONT_HAVE_STAT
141 #define HAVE_STAT
142 #endif
144 #ifndef DONT_HAVE_FSTAT
145 #define HAVE_FSTAT
146 #endif
148 #ifdef RISCOS
149 #include <sys/types.h>
150 #include "unixstuff.h"
151 #endif
153 #ifndef DONT_HAVE_SYS_STAT_H
154 #if defined(PYOS_OS2) && defined(PYCC_GCC)
155 #include <sys/types.h>
156 #endif
157 #include <sys/stat.h>
158 #elif defined(HAVE_STAT_H)
159 #include <stat.h>
160 #endif
162 #if defined(PYCC_VACPP)
163 /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
164 #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
165 #endif
167 #ifndef S_ISREG
168 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
169 #endif
171 #ifndef S_ISDIR
172 #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
173 #endif
176 #ifdef __cplusplus
177 /* Move this down here since some C++ #include's don't like to be included
178 inside an extern "C" */
179 extern "C" {
180 #endif
183 /* Py_ARITHMETIC_RIGHT_SHIFT
184 * C doesn't define whether a right-shift of a signed integer sign-extends
185 * or zero-fills. Here a macro to force sign extension:
186 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
187 * Return I >> J, forcing sign extension.
188 * Requirements:
189 * I is of basic signed type TYPE (char, short, int, long, or long long).
190 * TYPE is one of char, short, int, long, or long long, although long long
191 * must not be used except on platforms that support it.
192 * J is an integer >= 0 and strictly less than the number of bits in TYPE
193 * (because C doesn't define what happens for J outside that range either).
194 * Caution:
195 * I may be evaluated more than once.
197 #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
198 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
199 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
200 #else
201 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
202 #endif
204 /* Py_FORCE_EXPANSION(X)
205 * "Simply" returns its argument. However, macro expansions within the
206 * argument are evaluated. This unfortunate trickery is needed to get
207 * token-pasting to work as desired in some cases.
209 #define Py_FORCE_EXPANSION(X) X
211 /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
212 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
213 * assert-fails if any information is lost.
214 * Caution:
215 * VALUE may be evaluated more than once.
217 #ifdef Py_DEBUG
218 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
219 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
220 #else
221 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
222 #endif
224 /* Py_IS_INFINITY(X)
225 * Return 1 if float or double arg is an infinity, else 0.
226 * Caution:
227 * X is evaluated more than once.
228 * This implementation may set the underflow flag if |X| is very small;
229 * it really can't be implemented correctly (& easily) before C99.
231 #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
233 /* HUGE_VAL is supposed to expand to a positive double infinity. Python
234 * uses Py_HUGE_VAL instead because some platforms are broken in this
235 * respect. We used to embed code in pyport.h to try to worm around that,
236 * but different platforms are broken in conflicting ways. If you're on
237 * a platform where HUGE_VAL is defined incorrectly, fiddle your Python
238 * config to #define Py_HUGE_VAL to something that works on your platform.
240 #ifndef Py_HUGE_VAL
241 #define Py_HUGE_VAL HUGE_VAL
242 #endif
244 /* Py_OVERFLOWED(X)
245 * Return 1 iff a libm function overflowed. Set errno to 0 before calling
246 * a libm function, and invoke this macro after, passing the function
247 * result.
248 * Caution:
249 * This isn't reliable. C99 no longer requires libm to set errno under
250 * any exceptional condition, but does require +- HUGE_VAL return
251 * values on overflow. A 754 box *probably* maps HUGE_VAL to a
252 * double infinity, and we're cool if that's so, unless the input
253 * was an infinity and an infinity is the expected result. A C89
254 * system sets errno to ERANGE, so we check for that too. We're
255 * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or
256 * if the returned result is a NaN, or if a C89 box returns HUGE_VAL
257 * in non-overflow cases.
258 * X is evaluated more than once.
259 * Some platforms have better way to spell this, so expect some #ifdef'ery.
261 #ifdef __FreeBSD__
262 #define Py_OVERFLOWED(X) isinf(X)
263 #else
264 #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \
265 (X) == Py_HUGE_VAL || \
266 (X) == -Py_HUGE_VAL))
267 #endif
269 /* Py_SET_ERANGE_ON_OVERFLOW(x)
270 * If a libm function did not set errno, but it looks like the result
271 * overflowed, set errno to ERANGE. Set errno to 0 before calling a libm
272 * function, and invoke this macro after, passing the function result.
273 * Caution:
274 * This isn't reliable. See Py_OVERFLOWED comments.
275 * X is evaluated more than once.
277 #define Py_SET_ERANGE_IF_OVERFLOW(X) \
278 do { \
279 if (errno == 0 && ((X) == Py_HUGE_VAL || \
280 (X) == -Py_HUGE_VAL)) \
281 errno = ERANGE; \
282 } while(0)
284 /* Py_ADJUST_ERANGE1(x)
285 * Py_ADJUST_ERANGE2(x, y)
286 * Set errno to 0 before calling a libm function, and invoke one of these
287 * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
288 * for functions returning complex results). This makes two kinds of
289 * adjustments to errno: (A) If it looks like the platform libm set
290 * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
291 * platform libm overflowed but didn't set errno, force errno to ERANGE. In
292 * effect, we're trying to force a useful implementation of C89 errno
293 * behavior.
294 * Caution:
295 * This isn't reliable. See Py_OVERFLOWED comments.
296 * X and Y may be evaluated more than once.
298 #define Py_ADJUST_ERANGE1(X) \
299 do { \
300 if (errno == 0) { \
301 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
302 errno = ERANGE; \
304 else if (errno == ERANGE && (X) == 0.0) \
305 errno = 0; \
306 } while(0)
308 #define Py_ADJUST_ERANGE2(X, Y) \
309 do { \
310 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
311 (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
312 if (errno == 0) \
313 errno = ERANGE; \
315 else if (errno == ERANGE) \
316 errno = 0; \
317 } while(0)
319 /* Py_DEPRECATED(version)
320 * Declare a variable, type, or function deprecated.
321 * Usage:
322 * extern int old_var Py_DEPRECATED(2.3);
323 * typedef int T1 Py_DEPRECATED(2.4);
324 * extern int x() Py_DEPRECATED(2.5);
326 #if defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)
327 #define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
328 #else
329 #define Py_DEPRECATED(VERSION_UNUSED)
330 #endif
332 /**************************************************************************
333 Prototypes that are missing from the standard include files on some systems
334 (and possibly only some versions of such systems.)
336 Please be conservative with adding new ones, document them and enclose them
337 in platform-specific #ifdefs.
338 **************************************************************************/
340 #ifdef SOLARIS
341 /* Unchecked */
342 extern int gethostname(char *, int);
343 #endif
345 #ifdef __BEOS__
346 /* Unchecked */
347 /* It's in the libs, but not the headers... - [cjh] */
348 int shutdown( int, int );
349 #endif
351 #ifdef HAVE__GETPTY
352 #include <sys/types.h> /* we need to import mode_t */
353 extern char * _getpty(int *, int, mode_t, int);
354 #endif
356 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
357 #if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
358 /* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
359 functions, even though they are included in libutil. */
360 #include <termios.h>
361 extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
362 extern int forkpty(int *, char *, struct termios *, struct winsize *);
363 #endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
364 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
367 /* These are pulled from various places. It isn't obvious on what platforms
368 they are necessary, nor what the exact prototype should look like (which
369 is likely to vary between platforms!) If you find you need one of these
370 declarations, please move them to a platform-specific block and include
371 proper prototypes. */
372 #if 0
374 /* From Modules/resource.c */
375 extern int getrusage();
376 extern int getpagesize();
378 /* From Python/sysmodule.c and Modules/posixmodule.c */
379 extern int fclose(FILE *);
381 /* From Modules/posixmodule.c */
382 extern int fdatasync(int);
383 /* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
384 extern int rename(const char *, const char *);
385 extern int pclose(FILE *);
386 extern int lstat(const char *, struct stat *);
387 extern int symlink(const char *, const char *);
388 extern int fsync(int fd);
390 #endif /* 0 */
393 /************************
394 * WRAPPER FOR <math.h> *
395 ************************/
397 #ifndef HAVE_HYPOT
398 extern double hypot(double, double);
399 #endif
401 /* Declarations for symbol visibility.
403 PyAPI_FUNC(type): Declares a public Python API function and return type
404 PyAPI_DATA(type): Declares public Python data and its type
405 PyMODINIT_FUNC: A Python module init function. If these functions are
406 inside the Python core, they are private to the core.
407 If in an extension module, it may be declared with
408 external linkage depending on the platform.
410 As a number of platforms support/require "__declspec(dllimport/dllexport)",
411 we support a HAVE_DECLSPEC_DLL macro to save duplication.
415 All windows ports, except cygwin, are handled in PC/pyconfig.h.
417 BeOS and cygwin are the only other autoconf platform requiring special
418 linkage handling and both of these use __declspec().
420 #if defined(__CYGWIN__) || defined(__BEOS__)
421 # define HAVE_DECLSPEC_DLL
422 #endif
424 #if defined(Py_ENABLE_SHARED) /* only get special linkage if built as shared */
425 # if defined(HAVE_DECLSPEC_DLL)
426 # ifdef Py_BUILD_CORE
427 # define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
428 # define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
429 /* module init functions inside the core need no external linkage */
430 # define PyMODINIT_FUNC void
431 # else /* Py_BUILD_CORE */
432 /* Building an extension module, or an embedded situation */
433 /* public Python functions and data are imported */
434 /* Under Cygwin, auto-import functions to prevent compilation */
435 /* failures similar to http://python.org/doc/FAQ.html#3.24 */
436 # if !defined(__CYGWIN__)
437 # define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
438 # endif /* !__CYGWIN__ */
439 # define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
440 /* module init functions outside the core must be exported */
441 # if defined(__cplusplus)
442 # define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
443 # else /* __cplusplus */
444 # define PyMODINIT_FUNC __declspec(dllexport) void
445 # endif /* __cplusplus */
446 # endif /* Py_BUILD_CORE */
447 # endif /* HAVE_DECLSPEC */
448 #endif /* Py_ENABLE_SHARED */
450 /* If no external linkage macros defined by now, create defaults */
451 #ifndef PyAPI_FUNC
452 # define PyAPI_FUNC(RTYPE) RTYPE
453 #endif
454 #ifndef PyAPI_DATA
455 # define PyAPI_DATA(RTYPE) extern RTYPE
456 #endif
457 #ifndef PyMODINIT_FUNC
458 # if defined(__cplusplus)
459 # define PyMODINIT_FUNC extern "C" void
460 # else /* __cplusplus */
461 # define PyMODINIT_FUNC void
462 # endif /* __cplusplus */
463 #endif
465 /* Deprecated DL_IMPORT and DL_EXPORT macros */
466 #if defined(Py_ENABLE_SHARED) && defined (HAVE_DECLSPEC_DLL)
467 # if defined(Py_BUILD_CORE)
468 # define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
469 # define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
470 # else
471 # define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
472 # define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
473 # endif
474 #endif
475 #ifndef DL_EXPORT
476 # define DL_EXPORT(RTYPE) RTYPE
477 #endif
478 #ifndef DL_IMPORT
479 # define DL_IMPORT(RTYPE) RTYPE
480 #endif
481 /* End of deprecated DL_* macros */
483 /* If the fd manipulation macros aren't defined,
484 here is a set that should do the job */
486 #if 0 /* disabled and probably obsolete */
488 #ifndef FD_SETSIZE
489 #define FD_SETSIZE 256
490 #endif
492 #ifndef FD_SET
494 typedef long fd_mask;
496 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
497 #ifndef howmany
498 #define howmany(x, y) (((x)+((y)-1))/(y))
499 #endif /* howmany */
501 typedef struct fd_set {
502 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
503 } fd_set;
505 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
506 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
507 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
508 #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
510 #endif /* FD_SET */
512 #endif /* fd manipulation macros */
515 /* limits.h constants that may be missing */
517 #ifndef INT_MAX
518 #define INT_MAX 2147483647
519 #endif
521 #ifndef LONG_MAX
522 #if SIZEOF_LONG == 4
523 #define LONG_MAX 0X7FFFFFFFL
524 #elif SIZEOF_LONG == 8
525 #define LONG_MAX 0X7FFFFFFFFFFFFFFFL
526 #else
527 #error "could not set LONG_MAX in pyport.h"
528 #endif
529 #endif
531 #ifndef LONG_MIN
532 #define LONG_MIN (-LONG_MAX-1)
533 #endif
535 #ifndef LONG_BIT
536 #define LONG_BIT (8 * SIZEOF_LONG)
537 #endif
539 #if LONG_BIT != 8 * SIZEOF_LONG
540 /* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent
541 * 32-bit platforms using gcc. We try to catch that here at compile-time
542 * rather than waiting for integer multiplication to trigger bogus
543 * overflows.
545 #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
546 #endif
548 #ifdef __cplusplus
550 #endif
553 * Hide GCC attributes from compilers that don't support them.
555 #if (!defined(__GNUC__) || __GNUC__ < 2 || \
556 (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ) && \
557 !defined(RISCOS)
558 #define Py_GCC_ATTRIBUTE(x)
559 #else
560 #define Py_GCC_ATTRIBUTE(x) __attribute__(x)
561 #endif
563 #endif /* Py_PYPORT_H */