blob: 5bd641c9eda34b9d0c458a63b1eeb4b164545909 [file] [log] [blame]
Tim Peters7d3a5112000-07-08 04:17:21 +00001/***********************************************************
2Copyright (c) 2000, BeOpen.com.
3All rights reserved.
4
5See the file "Misc/COPYRIGHT" for information on usage and
6redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7******************************************************************/
8
9#ifndef Py_PYPORT_H
Vladimir Marangozov14a4d882000-07-10 04:59:49 +000010#define Py_PYPORT_H
Tim Peters7d3a5112000-07-08 04:17:21 +000011
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000012#include "config.h" /* include for defines */
13
Tim Peters7d3a5112000-07-08 04:17:21 +000014/**************************************************************************
15Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +000016C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +000017
18Please try to make documentation here as clear as possible: by definition,
19the stuff here is trying to illuminate C's darkest corners.
20
21Config #defines referenced here:
22
23SIGNED_RIGHT_SHIFT_ZERO_FILLS
24Meaning: To be defined iff i>>j does not extend the sign bit when i is a
25 signed integral type and i < 0.
26Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000027
Tim Peters8315ea52000-07-23 19:28:35 +000028Py_DEBUG
29Meaning: Extra checks compiled in for debug mode.
30Used in: Py_SAFE_DOWNCAST
Tim Peters7d3a5112000-07-08 04:17:21 +000031**************************************************************************/
32
33
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000034#define ANY void /* For API compatibility only. Obsolete, do not use. */
35
36#ifdef HAVE_STDLIB_H
37#include <stdlib.h>
38#endif
39
Vladimir Marangozov2c57e072000-08-11 11:48:33 +000040#include <math.h> /* Moved here from the math section, before extern "C" */
41
42/********************************************
43 * WRAPPER FOR <time.h> and/or <sys/time.h> *
44 ********************************************/
45
46#ifdef TIME_WITH_SYS_TIME
47#include <sys/time.h>
48#include <time.h>
49#else /* !TIME_WITH_SYS_TIME */
50#ifdef HAVE_SYS_TIME_H
51#include <sys/time.h>
52#else /* !HAVE_SYS_TIME_H */
53#include <time.h>
54#endif /* !HAVE_SYS_TIME_H */
55#endif /* !TIME_WITH_SYS_TIME */
56
57
58/******************************
59 * WRAPPER FOR <sys/select.h> *
60 ******************************/
61
62/* NB caller must include <sys/types.h> */
63
64#ifdef HAVE_SYS_SELECT_H
65
66#include <sys/select.h>
67
68#else /* !HAVE_SYS_SELECT_H */
69
70#ifdef USE_GUSI1
71/* If we don't have sys/select the definition may be in unistd.h */
72#include <GUSI.h>
73#endif
74
75#endif /* !HAVE_SYS_SELECT_H */
76
77
Tim Peters7d3a5112000-07-08 04:17:21 +000078#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000079/* Move this down here since some C++ #include's don't like to be included
80 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +000081extern "C" {
82#endif
83
Vladimir Marangozov2c57e072000-08-11 11:48:33 +000084
Tim Peters7d3a5112000-07-08 04:17:21 +000085/* Py_ARITHMETIC_RIGHT_SHIFT
86 * C doesn't define whether a right-shift of a signed integer sign-extends
87 * or zero-fills. Here a macro to force sign extension:
88 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
89 * Return I >> J, forcing sign extension.
90 * Requirements:
91 * I is of basic signed type TYPE (char, short, int, long, or long long).
92 * TYPE is one of char, short, int, long, or long long, although long long
93 * must not be used except on platforms that support it.
94 * J is an integer >= 0 and strictly less than the number of bits in TYPE
95 * (because C doesn't define what happens for J outside that range either).
96 * Caution:
97 * I may be evaluated more than once.
98 */
99#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
100#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
101 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
102#else
103#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
104#endif
105
Tim Peters39dce292000-08-15 03:34:48 +0000106/* Py_FORCE_EXPANSION(X)
Tim Peters1be46842000-07-23 18:10:18 +0000107 * "Simply" returns its argument. However, macro expansions within the
108 * argument are evaluated. This unfortunate trickery is needed to get
109 * token-pasting to work as desired in some cases.
110 */
111#define Py_FORCE_EXPANSION(X) X
112
Tim Peters8315ea52000-07-23 19:28:35 +0000113/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
114 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
115 * assert-fails if any information is lost.
116 * Caution:
117 * VALUE may be evaluated more than once.
118 */
119#ifdef Py_DEBUG
120#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
121 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
122#else
123#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
124#endif
125
Tim Peters39dce292000-08-15 03:34:48 +0000126/* Py_IS_INFINITY(X)
127 * Return 1 if float or double arg is an infinity, else 0.
128 * Caution:
129 * X is evaluated more than once.
130 * This implementation may set the underflow flag if |X| is very small;
131 * it really can't be implemented correctly (& easily) before C99.
132 */
133 #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000134
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000135/**************************************************************************
136Prototypes that are missing from the standard include files on some systems
137(and possibly only some versions of such systems.)
138
139Please be conservative with adding new ones, document them and enclose them
140in platform-specific #ifdefs.
141**************************************************************************/
142
143#ifdef SOLARIS
144/* Unchecked */
145extern int gethostname(char *, int);
146#endif
147
148#ifdef __BEOS__
149/* Unchecked */
150/* It's in the libs, but not the headers... - [cjh] */
151int shutdown( int, int );
152#endif
153
154#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000155#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000156extern char * _getpty(int *, int, mode_t, int);
157#endif
158
159#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
160#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
161/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
162 functions, even though they are included in libutil. */
163#include <termios.h>
164extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
165extern int forkpty(int *, char *, struct termios *, struct winsize *);
166#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
167#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
168
169
170/* These are pulled from various places. It isn't obvious on what platforms
171 they are necessary, nor what the exact prototype should look like (which
172 is likely to vary between platforms!) If you find you need one of these
173 declarations, please move them to a platform-specific block and include
174 proper prototypes. */
175#if 0
176
177/* From Modules/resource.c */
178extern int getrusage();
179extern int getpagesize();
180
181/* From Python/sysmodule.c and Modules/posixmodule.c */
182extern int fclose(FILE *);
183
184/* From Modules/posixmodule.c */
185extern int fdatasync(int);
186/* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
187extern int rename(const char *, const char *);
188extern int pclose(FILE *);
189extern int lstat(const char *, struct stat *);
190extern int symlink(const char *, const char *);
191extern int fsync(int fd);
192
193#endif /* 0 */
194
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000195
196/************************
197 * WRAPPER FOR <math.h> *
198 ************************/
199
200/* On the 68K Mac, when using CFM (Code Fragment Manager),
201 <math.h> requires special treatment -- we need to surround it with
202 #pragma lib_export off / on...
203 This is because MathLib.o is a static library, and exporting its
204 symbols doesn't quite work...
205 XXX Not sure now... Seems to be something else going on as well... */
206
207#ifndef HAVE_HYPOT
208extern double hypot(double, double);
209#ifdef MWERKS_BEFORE_PRO4
210#define hypot we_dont_want_faulty_hypot_decl
211#endif
212#endif
213
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000214#ifndef HAVE_HYPOT
215#ifdef __MWERKS__
216#undef hypot
217#endif
218#endif
219
220#if defined(USE_MSL) && defined(__MC68K__)
221/* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
222** when you take the address of math functions. If I interpret the
223** ANSI C standard correctly this is illegal, but I haven't been able
224** to convince the MetroWerks folks of this...
225*/
226#undef acos
227#undef asin
228#undef atan
229#undef atan2
230#undef ceil
231#undef cos
232#undef cosh
233#undef exp
234#undef fabs
235#undef floor
236#undef fmod
237#undef log
238#undef log10
239#undef pow
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000240#undef sin
241#undef sinh
242#undef sqrt
243#undef tan
244#undef tanh
245#define acos acosd
246#define asin asind
247#define atan atand
248#define atan2 atan2d
249#define ceil ceild
250#define cos cosd
251#define cosh coshd
252#define exp expd
253#define fabs fabsd
254#define floor floord
255#define fmod fmodd
256#define log logd
257#define log10 log10d
258#define pow powd
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000259#define sin sind
260#define sinh sinhd
261#define sqrt sqrtd
262#define tan tand
263#define tanh tanhd
264#endif
265
266
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000267/************************************
268 * MALLOC COMPATIBILITY FOR pymem.h *
269 ************************************/
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000270
271#ifndef DL_IMPORT /* declarations for DLL import */
272#define DL_IMPORT(RTYPE) RTYPE
273#endif
274
275#ifndef NULL
276#define NULL ((void *)0)
277#endif
278
279#ifdef MALLOC_ZERO_RETURNS_NULL
280/* XXX Always allocate one extra byte, since some malloc's return NULL
281 XXX for malloc(0) or realloc(p, 0). */
282#define _PyMem_EXTRA 1
283#else
284#define _PyMem_EXTRA 0
285#endif
286
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000287
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000288/* If the fd manipulation macros aren't defined,
289 here is a set that should do the job */
290
Guido van Rossum367e46a2000-08-01 18:28:44 +0000291#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000292
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000293#ifndef FD_SETSIZE
294#define FD_SETSIZE 256
295#endif
296
297#ifndef FD_SET
298
299typedef long fd_mask;
300
301#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
302#ifndef howmany
303#define howmany(x, y) (((x)+((y)-1))/(y))
304#endif /* howmany */
305
306typedef struct fd_set {
307 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
308} fd_set;
309
310#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
311#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
312#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
313#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
314
315#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000316
317#endif /* fd manipulation macros */
318
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000319
Tim Peters7d3a5112000-07-08 04:17:21 +0000320#ifdef __cplusplus
321}
322#endif
323
324#endif /* Py_PYPORT_H */