This commit was manufactured by cvs2svn to create tag 'r222'.
[python/dscho.git] / Include / pyport.h
blobe103c8a0f2d68030759092f67ab1af1371369d21
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: LONG_LONG
32 **************************************************************************/
35 /* For backward compatibility only. Obsolete, do not use. */
36 #define ANY void
37 #ifdef HAVE_PROTOTYPES
38 #define Py_PROTO(x) x
39 #else
40 #define Py_PROTO(x) ()
41 #endif
42 #ifndef Py_FPROTO
43 #define Py_FPROTO(x) Py_PROTO(x)
44 #endif
46 /* typedefs for some C9X-defined synonyms for integral types.
48 * The names in Python are exactly the same as the C9X names, except with a
49 * Py_ prefix. Until C9X is universally implemented, this is the only way
50 * to ensure that Python gets reliable names that don't conflict with names
51 * in non-Python code that are playing their own tricks to define the C9X
52 * names.
54 * NOTE: don't go nuts here! Python has no use for *most* of the C9X
55 * integral synonyms. Only define the ones we actually need.
58 #ifdef HAVE_LONG_LONG
59 #ifndef LONG_LONG
60 #define LONG_LONG long long
61 #endif
62 #endif /* HAVE_LONG_LONG */
64 /* uintptr_t is the C9X name for an unsigned integral type such that a
65 * legitimate void* can be cast to uintptr_t and then back to void* again
66 * without loss of information. Similarly for intptr_t, wrt a signed
67 * integral type.
69 #ifdef HAVE_UINTPTR_T
70 typedef uintptr_t Py_uintptr_t;
71 typedef intptr_t Py_intptr_t;
73 #elif SIZEOF_VOID_P <= SIZEOF_INT
74 typedef unsigned int Py_uintptr_t;
75 typedef int Py_intptr_t;
77 #elif SIZEOF_VOID_P <= SIZEOF_LONG
78 typedef unsigned long Py_uintptr_t;
79 typedef long Py_intptr_t;
81 #elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
82 typedef unsigned LONG_LONG Py_uintptr_t;
83 typedef LONG_LONG Py_intptr_t;
85 #else
86 # error "Python needs a typedef for Py_uintptr_t in pyport.h."
87 #endif /* HAVE_UINTPTR_T */
89 #ifdef HAVE_STDLIB_H
90 #include <stdlib.h>
91 #endif
93 #include <math.h> /* Moved here from the math section, before extern "C" */
95 /********************************************
96 * WRAPPER FOR <time.h> and/or <sys/time.h> *
97 ********************************************/
99 #ifdef TIME_WITH_SYS_TIME
100 #include <sys/time.h>
101 #include <time.h>
102 #else /* !TIME_WITH_SYS_TIME */
103 #ifdef HAVE_SYS_TIME_H
104 #include <sys/time.h>
105 #else /* !HAVE_SYS_TIME_H */
106 #include <time.h>
107 #endif /* !HAVE_SYS_TIME_H */
108 #endif /* !TIME_WITH_SYS_TIME */
111 /******************************
112 * WRAPPER FOR <sys/select.h> *
113 ******************************/
115 /* NB caller must include <sys/types.h> */
117 #ifdef HAVE_SYS_SELECT_H
119 #include <sys/select.h>
121 #else /* !HAVE_SYS_SELECT_H */
123 #ifdef USE_GUSI1
124 /* If we don't have sys/select the definition may be in unistd.h */
125 #include <GUSI.h>
126 #endif
128 #endif /* !HAVE_SYS_SELECT_H */
130 /*******************************
131 * stat() and fstat() fiddling *
132 *******************************/
134 /* We expect that stat and fstat exist on most systems.
135 * It's confirmed on Unix, Mac and Windows.
136 * If you don't have them, add
137 * #define DONT_HAVE_STAT
138 * and/or
139 * #define DONT_HAVE_FSTAT
140 * to your pyconfig.h. Python code beyond this should check HAVE_STAT and
141 * HAVE_FSTAT instead.
142 * Also
143 * #define DONT_HAVE_SYS_STAT_H
144 * if <sys/stat.h> doesn't exist on your platform, and
145 * #define HAVE_STAT_H
146 * if <stat.h> does (don't look at me -- ths mess is inherited).
148 #ifndef DONT_HAVE_STAT
149 #define HAVE_STAT
150 #endif
152 #ifndef DONT_HAVE_FSTAT
153 #define HAVE_FSTAT
154 #endif
156 #ifdef RISCOS
157 #include <sys/types.h>
158 #endif
160 #ifndef DONT_HAVE_SYS_STAT_H
161 #include <sys/stat.h>
162 #elif defined(HAVE_STAT_H)
163 #include <stat.h>
164 #endif
166 #if defined(PYCC_VACPP)
167 /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */
168 #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG)
169 #endif
171 #ifndef S_ISREG
172 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
173 #endif
175 #ifndef S_ISDIR
176 #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
177 #endif
180 #ifdef __cplusplus
181 /* Move this down here since some C++ #include's don't like to be included
182 inside an extern "C" */
183 extern "C" {
184 #endif
187 /* Py_ARITHMETIC_RIGHT_SHIFT
188 * C doesn't define whether a right-shift of a signed integer sign-extends
189 * or zero-fills. Here a macro to force sign extension:
190 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
191 * Return I >> J, forcing sign extension.
192 * Requirements:
193 * I is of basic signed type TYPE (char, short, int, long, or long long).
194 * TYPE is one of char, short, int, long, or long long, although long long
195 * must not be used except on platforms that support it.
196 * J is an integer >= 0 and strictly less than the number of bits in TYPE
197 * (because C doesn't define what happens for J outside that range either).
198 * Caution:
199 * I may be evaluated more than once.
201 #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
202 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
203 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
204 #else
205 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
206 #endif
208 /* Py_FORCE_EXPANSION(X)
209 * "Simply" returns its argument. However, macro expansions within the
210 * argument are evaluated. This unfortunate trickery is needed to get
211 * token-pasting to work as desired in some cases.
213 #define Py_FORCE_EXPANSION(X) X
215 /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
216 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
217 * assert-fails if any information is lost.
218 * Caution:
219 * VALUE may be evaluated more than once.
221 #ifdef Py_DEBUG
222 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
223 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
224 #else
225 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
226 #endif
228 /* Py_IS_INFINITY(X)
229 * Return 1 if float or double arg is an infinity, else 0.
230 * Caution:
231 * X is evaluated more than once.
232 * This implementation may set the underflow flag if |X| is very small;
233 * it really can't be implemented correctly (& easily) before C99.
235 #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
237 /* According to
238 * http://www.cray.com/swpubs/manuals/SN-2194_2.0/html-SN-2194_2.0/x3138.htm
239 * on some Cray systems HUGE_VAL is incorrectly (according to the C std)
240 * defined to be the largest positive finite rather than infinity. We need
241 * the std-conforming infinity meaning (provided the platform has one!).
243 * Then, according to a bug report on SourceForge, defining Py_HUGE_VAL as
244 * INFINITY caused internal compiler errors under BeOS using some version
245 * of gcc. Explicitly casting INFINITY to double made that problem go away.
247 #ifdef INFINITY
248 #define Py_HUGE_VAL ((double)INFINITY)
249 #else
250 #define Py_HUGE_VAL HUGE_VAL
251 #endif
253 /* Py_OVERFLOWED(X)
254 * Return 1 iff a libm function overflowed. Set errno to 0 before calling
255 * a libm function, and invoke this macro after, passing the function
256 * result.
257 * Caution:
258 * This isn't reliable. C99 no longer requires libm to set errno under
259 * any exceptional condition, but does require +- HUGE_VAL return
260 * values on overflow. A 754 box *probably* maps HUGE_VAL to a
261 * double infinity, and we're cool if that's so, unless the input
262 * was an infinity and an infinity is the expected result. A C89
263 * system sets errno to ERANGE, so we check for that too. We're
264 * out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or
265 * if the returned result is a NaN, or if a C89 box returns HUGE_VAL
266 * in non-overflow cases.
267 * X is evaluated more than once.
269 #define Py_OVERFLOWED(X) ((X) != 0.0 && (errno == ERANGE || \
270 (X) == Py_HUGE_VAL || \
271 (X) == -Py_HUGE_VAL))
273 /* Py_SET_ERANGE_ON_OVERFLOW(x)
274 * If a libm function did not set errno, but it looks like the result
275 * overflowed, set errno to ERANGE. Set errno to 0 before calling a libm
276 * function, and invoke this macro after, passing the function result.
277 * Caution:
278 * This isn't reliable. See Py_OVERFLOWED comments.
279 * X is evaluated more than once.
281 #define Py_SET_ERANGE_IF_OVERFLOW(X) \
282 do { \
283 if (errno == 0 && ((X) == Py_HUGE_VAL || \
284 (X) == -Py_HUGE_VAL)) \
285 errno = ERANGE; \
286 } while(0)
288 /* Py_ADJUST_ERANGE1(x)
289 * Py_ADJUST_ERANGE2(x, y)
290 * Set errno to 0 before calling a libm function, and invoke one of these
291 * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
292 * for functions returning complex results). This makes two kinds of
293 * adjustments to errno: (A) If it looks like the platform libm set
294 * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
295 * platform libm overflowed but didn't set errno, force errno to ERANGE. In
296 * effect, we're trying to force a useful implementation of C89 errno
297 * behavior.
298 * Caution:
299 * This isn't reliable. See Py_OVERFLOWED comments.
300 * X and Y may be evaluated more than once.
302 #define Py_ADJUST_ERANGE1(X) \
303 do { \
304 if (errno == 0) { \
305 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
306 errno = ERANGE; \
308 else if (errno == ERANGE && (X) == 0.0) \
309 errno = 0; \
310 } while(0)
312 #define Py_ADJUST_ERANGE2(X, Y) \
313 do { \
314 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
315 (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
316 if (errno == 0) \
317 errno = ERANGE; \
319 else if (errno == ERANGE) \
320 errno = 0; \
321 } while(0)
323 /**************************************************************************
324 Prototypes that are missing from the standard include files on some systems
325 (and possibly only some versions of such systems.)
327 Please be conservative with adding new ones, document them and enclose them
328 in platform-specific #ifdefs.
329 **************************************************************************/
331 #ifdef SOLARIS
332 /* Unchecked */
333 extern int gethostname(char *, int);
334 #endif
336 #ifdef __BEOS__
337 /* Unchecked */
338 /* It's in the libs, but not the headers... - [cjh] */
339 int shutdown( int, int );
340 #endif
342 #ifdef HAVE__GETPTY
343 #include <sys/types.h> /* we need to import mode_t */
344 extern char * _getpty(int *, int, mode_t, int);
345 #endif
347 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
348 #if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
349 /* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
350 functions, even though they are included in libutil. */
351 #include <termios.h>
352 extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
353 extern int forkpty(int *, char *, struct termios *, struct winsize *);
354 #endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
355 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
358 /* These are pulled from various places. It isn't obvious on what platforms
359 they are necessary, nor what the exact prototype should look like (which
360 is likely to vary between platforms!) If you find you need one of these
361 declarations, please move them to a platform-specific block and include
362 proper prototypes. */
363 #if 0
365 /* From Modules/resource.c */
366 extern int getrusage();
367 extern int getpagesize();
369 /* From Python/sysmodule.c and Modules/posixmodule.c */
370 extern int fclose(FILE *);
372 /* From Modules/posixmodule.c */
373 extern int fdatasync(int);
374 /* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
375 extern int rename(const char *, const char *);
376 extern int pclose(FILE *);
377 extern int lstat(const char *, struct stat *);
378 extern int symlink(const char *, const char *);
379 extern int fsync(int fd);
381 #endif /* 0 */
384 /************************
385 * WRAPPER FOR <math.h> *
386 ************************/
388 #ifndef HAVE_HYPOT
389 extern double hypot(double, double);
390 #endif
393 /************************************
394 * MALLOC COMPATIBILITY FOR pymem.h *
395 ************************************/
397 #ifndef DL_IMPORT /* declarations for DLL import */
398 #define DL_IMPORT(RTYPE) RTYPE
399 #endif
401 #ifdef MALLOC_ZERO_RETURNS_NULL
402 /* Allocate an extra byte if the platform malloc(0) returns NULL.
403 Caution: this bears no relation to whether realloc(p, 0) returns NULL
404 when p != NULL. Even on platforms where malloc(0) does not return NULL,
405 realloc(p, 0) may act like free(p) and return NULL. Examples include
406 Windows, and Python's own obmalloc.c (as of 2-Mar-2002). For whatever
407 reason, our docs promise that PyMem_Realloc(p, 0) won't act like
408 free(p) or return NULL, so realloc() calls may have to be hacked
409 too, but MALLOC_ZERO_RETURNS_NULL's state is irrelevant to realloc (it
410 needs a different hack).
412 #define _PyMem_EXTRA 1
413 #else
414 #define _PyMem_EXTRA 0
415 #endif
418 /* If the fd manipulation macros aren't defined,
419 here is a set that should do the job */
421 #if 0 /* disabled and probably obsolete */
423 #ifndef FD_SETSIZE
424 #define FD_SETSIZE 256
425 #endif
427 #ifndef FD_SET
429 typedef long fd_mask;
431 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
432 #ifndef howmany
433 #define howmany(x, y) (((x)+((y)-1))/(y))
434 #endif /* howmany */
436 typedef struct fd_set {
437 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
438 } fd_set;
440 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
441 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
442 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
443 #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
445 #endif /* FD_SET */
447 #endif /* fd manipulation macros */
450 /* limits.h constants that may be missing */
452 #ifndef INT_MAX
453 #define INT_MAX 2147483647
454 #endif
456 #ifndef LONG_MAX
457 #if SIZEOF_LONG == 4
458 #define LONG_MAX 0X7FFFFFFFL
459 #elif SIZEOF_LONG == 8
460 #define LONG_MAX 0X7FFFFFFFFFFFFFFFL
461 #else
462 #error "could not set LONG_MAX in pyport.h"
463 #endif
464 #endif
466 #ifndef LONG_MIN
467 #define LONG_MIN (-LONG_MAX-1)
468 #endif
470 #ifndef LONG_BIT
471 #define LONG_BIT (8 * SIZEOF_LONG)
472 #endif
474 #if LONG_BIT != 8 * SIZEOF_LONG
475 /* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent
476 * 32-bit platforms using gcc. We try to catch that here at compile-time
477 * rather than waiting for integer multiplication to trigger bogus
478 * overflows.
480 #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
481 #endif
484 * Rename some functions for the Borland compiler
486 #ifdef __BORLANDC__
487 # include <io.h>
488 # define _chsize chsize
489 # define _setmode setmode
490 #endif
492 #ifdef __cplusplus
494 #endif
497 * Hide GCC attributes from compilers that don't support them.
499 #if (!defined(__GNUC__) || __GNUC__ < 2 || \
500 (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || \
501 defined(NEXT) ) && \
502 !defined(RISCOS)
503 #define __attribute__(__x)
504 #endif
506 #endif /* Py_PYPORT_H */