blob: 53352f16f665d595ab70ceac370e6454a24903fd [file] [log] [blame]
Tim Peters7d3a5112000-07-08 04:17:21 +00001#ifndef Py_PYPORT_H
Vladimir Marangozov14a4d882000-07-10 04:59:49 +00002#define Py_PYPORT_H
Tim Peters7d3a5112000-07-08 04:17:21 +00003
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +00004#include "pyconfig.h" /* include for defines */
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00005
Martin v. Löwis40e9aed2006-10-02 15:20:37 +00006#ifdef HAVE_STDINT_H
7#include <stdint.h>
8#endif
9
Tim Peters7d3a5112000-07-08 04:17:21 +000010/**************************************************************************
11Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +000012C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +000013
14Please try to make documentation here as clear as possible: by definition,
15the stuff here is trying to illuminate C's darkest corners.
16
17Config #defines referenced here:
18
19SIGNED_RIGHT_SHIFT_ZERO_FILLS
20Meaning: To be defined iff i>>j does not extend the sign bit when i is a
21 signed integral type and i < 0.
22Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000023
Tim Peters8315ea52000-07-23 19:28:35 +000024Py_DEBUG
25Meaning: Extra checks compiled in for debug mode.
26Used in: Py_SAFE_DOWNCAST
Barry Warsawfd847b22000-08-18 04:48:18 +000027
28HAVE_UINTPTR_T
29Meaning: The C9X type uintptr_t is supported by the compiler
30Used in: Py_uintptr_t
31
32HAVE_LONG_LONG
33Meaning: The compiler supports the C type "long long"
Martin v. Löwisb9a0f912003-03-29 10:06:18 +000034Used in: PY_LONG_LONG
Barry Warsawfd847b22000-08-18 04:48:18 +000035
Tim Peters7d3a5112000-07-08 04:17:21 +000036**************************************************************************/
37
38
Vladimir Marangozove2bf7e62000-09-08 12:55:35 +000039/* For backward compatibility only. Obsolete, do not use. */
Vladimir Marangozove2bf7e62000-09-08 12:55:35 +000040#ifdef HAVE_PROTOTYPES
41#define Py_PROTO(x) x
42#else
43#define Py_PROTO(x) ()
44#endif
Marc-André Lemburg77317ca2000-10-05 17:25:45 +000045#ifndef Py_FPROTO
46#define Py_FPROTO(x) Py_PROTO(x)
47#endif
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000048
Barry Warsawfd847b22000-08-18 04:48:18 +000049/* 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öwisb9a0f912003-03-29 10:06:18 +000062#ifndef PY_LONG_LONG
63#define PY_LONG_LONG long long
Martin v. Löwis9201e7f2007-06-09 10:10:26 +000064#if defined(LLONG_MAX)
Martin v. Löwis2a71a472007-06-13 03:42:19 +000065/* If LLONG_MAX is defined in limits.h, use that. */
Martin v. Löwis6371cd82007-06-09 07:42:52 +000066#define PY_LLONG_MIN LLONG_MIN
67#define PY_LLONG_MAX LLONG_MAX
68#define PY_ULLONG_MAX ULLONG_MAX
Martin v. Löwis2a71a472007-06-13 03:42:19 +000069#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öwis9201e7f2007-06-09 10:10:26 +000072#define PY_LLONG_MIN (-PY_LLONG_MAX-1)
Martin v. Löwis2a71a472007-06-13 03:42:19 +000073#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öwis9201e7f2007-06-09 10:10:26 +000079#endif /* LLONG_MAX */
Barry Warsawfd847b22000-08-18 04:48:18 +000080#endif
81#endif /* HAVE_LONG_LONG */
82
Mark Dickinsonefc82f72009-03-20 15:51:55 +000083/* 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 Warsawfd847b22000-08-18 04:48:18 +0000134/* 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 Peters79248aa2001-08-29 21:37:10 +0000136 * without loss of information. Similarly for intptr_t, wrt a signed
137 * integral type.
Barry Warsawfd847b22000-08-18 04:48:18 +0000138 */
139#ifdef HAVE_UINTPTR_T
Tim Peters79248aa2001-08-29 21:37:10 +0000140typedef uintptr_t Py_uintptr_t;
141typedef intptr_t Py_intptr_t;
142
Barry Warsawfd847b22000-08-18 04:48:18 +0000143#elif SIZEOF_VOID_P <= SIZEOF_INT
Tim Peters79248aa2001-08-29 21:37:10 +0000144typedef unsigned int Py_uintptr_t;
145typedef int Py_intptr_t;
146
Barry Warsawfd847b22000-08-18 04:48:18 +0000147#elif SIZEOF_VOID_P <= SIZEOF_LONG
Tim Peters79248aa2001-08-29 21:37:10 +0000148typedef unsigned long Py_uintptr_t;
149typedef long Py_intptr_t;
150
Barry Warsawfd847b22000-08-18 04:48:18 +0000151#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000152typedef unsigned PY_LONG_LONG Py_uintptr_t;
153typedef PY_LONG_LONG Py_intptr_t;
Tim Peters79248aa2001-08-29 21:37:10 +0000154
Barry Warsawfd847b22000-08-18 04:48:18 +0000155#else
156# error "Python needs a typedef for Py_uintptr_t in pyport.h."
157#endif /* HAVE_UINTPTR_T */
158
Tim Petersae1d0c92006-03-17 03:29:34 +0000159/* 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öwis18e16552006-02-15 17:27:45 +0000163#ifdef HAVE_SSIZE_T
164typedef ssize_t Py_ssize_t;
165#elif SIZEOF_VOID_P == SIZEOF_SIZE_T
Martin v. Löwis2d65b552006-02-18 18:26:55 +0000166typedef Py_intptr_t Py_ssize_t;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000167#else
168# error "Python needs a typedef for Py_ssize_t in pyport.h."
169#endif
Tim Petersae1d0c92006-03-17 03:29:34 +0000170
Gregory P. Smith9d534572008-06-11 07:41:16 +0000171/* 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 Petersae1d0c92006-03-17 03:29:34 +0000182/* Largest positive value of type Py_ssize_t. */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000183#define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
Tim Peters3b1c01d2006-04-05 18:43:30 +0000184/* Smallest negative value of type Py_ssize_t. */
Martin v. Löwisc48c8db2006-04-05 18:21:17 +0000185#define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000186
Christian Heimes951cc0f2008-01-31 23:08:23 +0000187#if SIZEOF_PID_T > SIZEOF_LONG
188# error "Python doesn't support sizeof(pid_t) > sizeof(long)"
189#endif
190
Tim Petersae1d0c92006-03-17 03:29:34 +0000191/* 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. Smithdd96db62008-06-09 04:58:54 +0000200 * PyString_FromFormat
Tim Petersae1d0c92006-03-17 03:29:34 +0000201 * PyErr_Format
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000202 * PyString_FromFormatV
Tim Petersae1d0c92006-03-17 03:29:34 +0000203 *
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 Norwitz02743ca2006-09-22 08:47:23 +0000215# if SIZEOF_SIZE_T == SIZEOF_INT && !defined(__APPLE__)
Neal Norwitz4a8fbdb2006-09-22 08:16:26 +0000216# define PY_FORMAT_SIZE_T ""
Neal Norwitz02743ca2006-09-22 08:47:23 +0000217# elif SIZEOF_SIZE_T == SIZEOF_LONG
218# define PY_FORMAT_SIZE_T "l"
Tim Petersae1d0c92006-03-17 03:29:34 +0000219# 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 Lundh7a830892006-05-27 10:39:48 +0000226/* 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 Lundh95e2a912006-05-26 11:38:15 +0000228 *
Fredrik Lundh7a830892006-05-27 10:39:48 +0000229 * 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 Lundh57640f52006-05-26 11:54:04 +0000237 *
Fredrik Lundh95e2a912006-05-26 11:38:15 +0000238 * 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 Lundhb8b3c8e2006-05-26 11:29:39 +0000242
243#undef USE_INLINE /* XXX - set via configure? */
244
245#if defined(_MSC_VER)
Fredrik Lundh57640f52006-05-26 11:54:04 +0000246#if defined(PY_LOCAL_AGGRESSIVE)
247/* enable more aggressive optimization for visual studio */
248#pragma optimize("agtw", on)
249#endif
Fredrik Lundh95e2a912006-05-26 11:38:15 +0000250/* ignore warnings if the compiler decides not to inline a function */
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000251#pragma warning(disable: 4710)
252/* fastest possible local call under MSVC */
Fredrik Lundh7a830892006-05-27 10:39:48 +0000253#define Py_LOCAL(type) static type __fastcall
254#define Py_LOCAL_INLINE(type) static __inline type __fastcall
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000255#elif defined(USE_INLINE)
Fredrik Lundh7a830892006-05-27 10:39:48 +0000256#define Py_LOCAL(type) static type
257#define Py_LOCAL_INLINE(type) static inline type
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000258#else
259#define Py_LOCAL(type) static type
Fredrik Lundh7a830892006-05-27 10:39:48 +0000260#define Py_LOCAL_INLINE(type) static type
Fredrik Lundhb8b3c8e2006-05-26 11:29:39 +0000261#endif
262
Fredrik Lundh80f8e802006-05-28 12:06:46 +0000263/* 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-Kamp7e018902000-07-31 15:28:04 +0000284#include <stdlib.h>
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000285
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000286#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 Marangozov2c57e072000-08-11 11:48:33 +0000314#endif /* !HAVE_SYS_SELECT_H */
315
Tim Peters60f42b52001-01-18 03:03:16 +0000316/*******************************
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 Peters76f373d2001-07-26 21:34:59 +0000326 * to your pyconfig.h. Python code beyond this should check HAVE_STAT and
Tim Peters60f42b52001-01-18 03:03:16 +0000327 * HAVE_FSTAT instead.
328 * Also
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000329 * #define HAVE_SYS_STAT_H
330 * if <sys/stat.h> exists on your platform, and
Tim Peters60f42b52001-01-18 03:03:16 +0000331 * #define HAVE_STAT_H
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000332 * if <stat.h> does.
Tim Peters60f42b52001-01-18 03:03:16 +0000333 */
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 Rossume2ae77b2001-10-24 20:42:55 +0000342#ifdef RISCOS
343#include <sys/types.h>
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000344#include "unixstuff.h"
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000345#endif
346
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000347#ifdef HAVE_SYS_STAT_H
Andrew MacIntyre5e090fc2002-02-26 11:20:01 +0000348#if defined(PYOS_OS2) && defined(PYCC_GCC)
349#include <sys/types.h>
350#endif
Tim Peters60f42b52001-01-18 03:03:16 +0000351#include <sys/stat.h>
352#elif defined(HAVE_STAT_H)
353#include <stat.h>
354#endif
355
Martin v. Löwisf9836ba2001-08-08 10:28:06 +0000356#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 Marangozov2c57e072000-08-11 11:48:33 +0000369
Tim Peters7d3a5112000-07-08 04:17:21 +0000370#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000371/* Move this down here since some C++ #include's don't like to be included
372 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +0000373extern "C" {
374#endif
375
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000376
Tim Peters7d3a5112000-07-08 04:17:21 +0000377/* 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)
381 * Return I >> J, forcing sign extension.
382 * Requirements:
383 * I is of basic signed type TYPE (char, short, int, long, or long long).
384 * TYPE is one of char, short, int, long, or long long, although long long
385 * must not be used except on platforms that support it.
386 * J is an integer >= 0 and strictly less than the number of bits in TYPE
387 * (because C doesn't define what happens for J outside that range either).
388 * Caution:
389 * I may be evaluated more than once.
390 */
391#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
392#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
393 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
394#else
395#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
396#endif
397
Tim Peters39dce292000-08-15 03:34:48 +0000398/* Py_FORCE_EXPANSION(X)
Tim Peters1be46842000-07-23 18:10:18 +0000399 * "Simply" returns its argument. However, macro expansions within the
400 * argument are evaluated. This unfortunate trickery is needed to get
401 * token-pasting to work as desired in some cases.
402 */
403#define Py_FORCE_EXPANSION(X) X
404
Tim Peters8315ea52000-07-23 19:28:35 +0000405/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
406 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
407 * assert-fails if any information is lost.
408 * Caution:
409 * VALUE may be evaluated more than once.
410 */
411#ifdef Py_DEBUG
412#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
413 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
414#else
415#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
416#endif
417
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000418/* Py_SET_ERRNO_ON_MATH_ERROR(x)
Tim Petersa40c7932001-09-05 22:36:56 +0000419 * If a libm function did not set errno, but it looks like the result
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000420 * overflowed or not-a-number, set errno to ERANGE or EDOM. Set errno
421 * to 0 before calling a libm function, and invoke this macro after,
422 * passing the function result.
Tim Petersa40c7932001-09-05 22:36:56 +0000423 * Caution:
424 * This isn't reliable. See Py_OVERFLOWED comments.
425 * X is evaluated more than once.
426 */
Guido van Rossum539c6622005-09-14 17:49:54 +0000427#if defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(__hpux) && defined(__ia64))
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000428#define _Py_SET_EDOM_FOR_NAN(X) if (isnan(X)) errno = EDOM;
429#else
430#define _Py_SET_EDOM_FOR_NAN(X) ;
431#endif
432#define Py_SET_ERRNO_ON_MATH_ERROR(X) \
Tim Petersa40c7932001-09-05 22:36:56 +0000433 do { \
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000434 if (errno == 0) { \
435 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
436 errno = ERANGE; \
437 else _Py_SET_EDOM_FOR_NAN(X) \
438 } \
Tim Petersa40c7932001-09-05 22:36:56 +0000439 } while(0)
Tim Peters57f282a2001-09-05 05:38:10 +0000440
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000441/* Py_SET_ERANGE_ON_OVERFLOW(x)
442 * An alias of Py_SET_ERRNO_ON_MATH_ERROR for backward-compatibility.
443 */
444#define Py_SET_ERANGE_IF_OVERFLOW(X) Py_SET_ERRNO_ON_MATH_ERROR(X)
445
Tim Petersdc5a5082002-03-09 04:58:24 +0000446/* Py_ADJUST_ERANGE1(x)
447 * Py_ADJUST_ERANGE2(x, y)
448 * Set errno to 0 before calling a libm function, and invoke one of these
449 * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
450 * for functions returning complex results). This makes two kinds of
451 * adjustments to errno: (A) If it looks like the platform libm set
452 * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
453 * platform libm overflowed but didn't set errno, force errno to ERANGE. In
454 * effect, we're trying to force a useful implementation of C89 errno
455 * behavior.
456 * Caution:
457 * This isn't reliable. See Py_OVERFLOWED comments.
458 * X and Y may be evaluated more than once.
459 */
460#define Py_ADJUST_ERANGE1(X) \
461 do { \
462 if (errno == 0) { \
463 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
464 errno = ERANGE; \
465 } \
466 else if (errno == ERANGE && (X) == 0.0) \
467 errno = 0; \
468 } while(0)
469
470#define Py_ADJUST_ERANGE2(X, Y) \
471 do { \
472 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
473 (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
474 if (errno == 0) \
475 errno = ERANGE; \
476 } \
477 else if (errno == ERANGE) \
478 errno = 0; \
479 } while(0)
480
Neal Norwitz80292642002-12-19 15:12:26 +0000481/* Py_DEPRECATED(version)
Neal Norwitz93344ab2002-12-19 15:24:11 +0000482 * Declare a variable, type, or function deprecated.
Neal Norwitz80292642002-12-19 15:12:26 +0000483 * Usage:
484 * extern int old_var Py_DEPRECATED(2.3);
485 * typedef int T1 Py_DEPRECATED(2.4);
486 * extern int x() Py_DEPRECATED(2.5);
487 */
Neal Norwitz3dd8be42006-03-20 06:33:01 +0000488#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
489 (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
Neal Norwitz80292642002-12-19 15:12:26 +0000490#define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
491#else
Tim Peters4643bd92002-12-28 21:56:08 +0000492#define Py_DEPRECATED(VERSION_UNUSED)
Neal Norwitz80292642002-12-19 15:12:26 +0000493#endif
494
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000495/**************************************************************************
496Prototypes that are missing from the standard include files on some systems
497(and possibly only some versions of such systems.)
498
499Please be conservative with adding new ones, document them and enclose them
500in platform-specific #ifdefs.
501**************************************************************************/
502
503#ifdef SOLARIS
504/* Unchecked */
505extern int gethostname(char *, int);
506#endif
507
508#ifdef __BEOS__
509/* Unchecked */
510/* It's in the libs, but not the headers... - [cjh] */
Tim Peters60f42b52001-01-18 03:03:16 +0000511int shutdown( int, int );
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000512#endif
513
514#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000515#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000516extern char * _getpty(int *, int, mode_t, int);
517#endif
518
Martin v. Löwis8c255e42008-05-23 15:06:50 +0000519/* On QNX 6, struct termio must be declared by including sys/termio.h
520 if TCGETA, TCSETA, TCSETAW, or TCSETAF are used. sys/termio.h must
521 be included before termios.h or it will generate an error. */
522#ifdef HAVE_SYS_TERMIO_H
523#include <sys/termio.h>
524#endif
525
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000526#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
527#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
528/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
529 functions, even though they are included in libutil. */
530#include <termios.h>
531extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
Christian Heimes951cc0f2008-01-31 23:08:23 +0000532extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000533#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
534#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
535
536
537/* These are pulled from various places. It isn't obvious on what platforms
538 they are necessary, nor what the exact prototype should look like (which
539 is likely to vary between platforms!) If you find you need one of these
540 declarations, please move them to a platform-specific block and include
541 proper prototypes. */
542#if 0
543
544/* From Modules/resource.c */
545extern int getrusage();
546extern int getpagesize();
547
548/* From Python/sysmodule.c and Modules/posixmodule.c */
549extern int fclose(FILE *);
550
551/* From Modules/posixmodule.c */
552extern int fdatasync(int);
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000553#endif /* 0 */
554
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000555
Hye-Shik Changf3032612006-03-22 08:52:43 +0000556/* On 4.4BSD-descendants, ctype functions serves the whole range of
557 * wchar_t character set rather than single byte code points only.
558 * This characteristic can break some operations of string object
559 * including str.upper() and str.split() on UTF-8 locales. This
560 * workaround was provided by Tim Robbins of FreeBSD project.
561 */
Hye-Shik Changb5047fd2004-08-04 06:33:51 +0000562
563#ifdef __FreeBSD__
564#include <osreldate.h>
565#if __FreeBSD_version > 500039
566#include <ctype.h>
567#include <wctype.h>
568#undef isalnum
569#define isalnum(c) iswalnum(btowc(c))
570#undef isalpha
571#define isalpha(c) iswalpha(btowc(c))
572#undef islower
573#define islower(c) iswlower(btowc(c))
574#undef isspace
575#define isspace(c) iswspace(btowc(c))
576#undef isupper
577#define isupper(c) iswupper(btowc(c))
578#undef tolower
579#define tolower(c) towlower(btowc(c))
580#undef toupper
581#define toupper(c) towupper(btowc(c))
582#endif
583#endif
584
585
Mark Hammond8235ea12002-07-19 06:55:41 +0000586/* Declarations for symbol visibility.
587
588 PyAPI_FUNC(type): Declares a public Python API function and return type
Tim Peters4643bd92002-12-28 21:56:08 +0000589 PyAPI_DATA(type): Declares public Python data and its type
Mark Hammond8235ea12002-07-19 06:55:41 +0000590 PyMODINIT_FUNC: A Python module init function. If these functions are
Tim Peters4643bd92002-12-28 21:56:08 +0000591 inside the Python core, they are private to the core.
592 If in an extension module, it may be declared with
Mark Hammond8235ea12002-07-19 06:55:41 +0000593 external linkage depending on the platform.
594
595 As a number of platforms support/require "__declspec(dllimport/dllexport)",
596 we support a HAVE_DECLSPEC_DLL macro to save duplication.
597*/
598
Tim Peters4643bd92002-12-28 21:56:08 +0000599/*
Michael W. Hudsonf163d102003-02-10 19:36:46 +0000600 All windows ports, except cygwin, are handled in PC/pyconfig.h.
601
602 BeOS and cygwin are the only other autoconf platform requiring special
603 linkage handling and both of these use __declspec().
Mark Hammond8235ea12002-07-19 06:55:41 +0000604*/
605#if defined(__CYGWIN__) || defined(__BEOS__)
606# define HAVE_DECLSPEC_DLL
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000607#endif
Mark Hammond8235ea12002-07-19 06:55:41 +0000608
Jason Tishler30765592003-09-04 11:04:06 +0000609/* only get special linkage if built as shared or platform is Cygwin */
610#if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__)
Mark Hammond8235ea12002-07-19 06:55:41 +0000611# if defined(HAVE_DECLSPEC_DLL)
612# ifdef Py_BUILD_CORE
613# define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
Mark Hammonda2905272002-07-29 13:42:14 +0000614# define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000615 /* module init functions inside the core need no external linkage */
Jason Tishler6bc06ec2003-09-04 11:59:50 +0000616 /* except for Cygwin to handle embedding (FIXME: BeOS too?) */
617# if defined(__CYGWIN__)
618# define PyMODINIT_FUNC __declspec(dllexport) void
619# else /* __CYGWIN__ */
620# define PyMODINIT_FUNC void
621# endif /* __CYGWIN__ */
Mark Hammond8235ea12002-07-19 06:55:41 +0000622# else /* Py_BUILD_CORE */
623 /* Building an extension module, or an embedded situation */
624 /* public Python functions and data are imported */
Jason Tishlerfb8595d2003-01-06 12:41:26 +0000625 /* Under Cygwin, auto-import functions to prevent compilation */
626 /* failures similar to http://python.org/doc/FAQ.html#3.24 */
627# if !defined(__CYGWIN__)
628# define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
629# endif /* !__CYGWIN__ */
Mark Hammonda2905272002-07-29 13:42:14 +0000630# define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000631 /* module init functions outside the core must be exported */
632# if defined(__cplusplus)
633# define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
634# else /* __cplusplus */
635# define PyMODINIT_FUNC __declspec(dllexport) void
636# endif /* __cplusplus */
637# endif /* Py_BUILD_CORE */
638# endif /* HAVE_DECLSPEC */
639#endif /* Py_ENABLE_SHARED */
640
641/* If no external linkage macros defined by now, create defaults */
642#ifndef PyAPI_FUNC
643# define PyAPI_FUNC(RTYPE) RTYPE
644#endif
645#ifndef PyAPI_DATA
Mark Hammonda2905272002-07-29 13:42:14 +0000646# define PyAPI_DATA(RTYPE) extern RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000647#endif
648#ifndef PyMODINIT_FUNC
649# if defined(__cplusplus)
650# define PyMODINIT_FUNC extern "C" void
651# else /* __cplusplus */
652# define PyMODINIT_FUNC void
653# endif /* __cplusplus */
654#endif
655
656/* Deprecated DL_IMPORT and DL_EXPORT macros */
657#if defined(Py_ENABLE_SHARED) && defined (HAVE_DECLSPEC_DLL)
658# if defined(Py_BUILD_CORE)
659# define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
660# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
661# else
662# define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
663# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
664# endif
665#endif
666#ifndef DL_EXPORT
667# define DL_EXPORT(RTYPE) RTYPE
668#endif
669#ifndef DL_IMPORT
670# define DL_IMPORT(RTYPE) RTYPE
671#endif
672/* End of deprecated DL_* macros */
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000673
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000674/* If the fd manipulation macros aren't defined,
675 here is a set that should do the job */
676
Guido van Rossum367e46a2000-08-01 18:28:44 +0000677#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000678
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000679#ifndef FD_SETSIZE
680#define FD_SETSIZE 256
681#endif
682
683#ifndef FD_SET
684
685typedef long fd_mask;
686
687#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
688#ifndef howmany
689#define howmany(x, y) (((x)+((y)-1))/(y))
690#endif /* howmany */
691
692typedef struct fd_set {
693 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
694} fd_set;
695
696#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
697#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
698#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
699#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
700
701#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000702
703#endif /* fd manipulation macros */
704
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000705
Fred Draked5fadf72000-09-26 05:46:01 +0000706/* limits.h constants that may be missing */
707
708#ifndef INT_MAX
709#define INT_MAX 2147483647
710#endif
711
712#ifndef LONG_MAX
713#if SIZEOF_LONG == 4
714#define LONG_MAX 0X7FFFFFFFL
715#elif SIZEOF_LONG == 8
716#define LONG_MAX 0X7FFFFFFFFFFFFFFFL
717#else
718#error "could not set LONG_MAX in pyport.h"
719#endif
720#endif
721
722#ifndef LONG_MIN
723#define LONG_MIN (-LONG_MAX-1)
724#endif
725
Tim Petersd57731f2000-10-05 01:42:25 +0000726#ifndef LONG_BIT
727#define LONG_BIT (8 * SIZEOF_LONG)
728#endif
729
730#if LONG_BIT != 8 * SIZEOF_LONG
731/* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent
732 * 32-bit platforms using gcc. We try to catch that here at compile-time
733 * rather than waiting for integer multiplication to trigger bogus
734 * overflows.
735 */
Andrew M. Kuchling234fb632001-01-12 15:06:28 +0000736#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
Tim Petersd57731f2000-10-05 01:42:25 +0000737#endif
738
Tim Peters7d3a5112000-07-08 04:17:21 +0000739#ifdef __cplusplus
740}
741#endif
742
Neil Schemenauer15691082001-10-23 02:20:37 +0000743/*
744 * Hide GCC attributes from compilers that don't support them.
745 */
Guido van Rossumbd67d6f2001-10-27 21:16:16 +0000746#if (!defined(__GNUC__) || __GNUC__ < 2 || \
Jack Jansen4892f242002-02-01 15:46:29 +0000747 (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ) && \
Guido van Rossumbd67d6f2001-10-27 21:16:16 +0000748 !defined(RISCOS)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +0000749#define Py_GCC_ATTRIBUTE(x)
750#else
751#define Py_GCC_ATTRIBUTE(x) __attribute__(x)
Neil Schemenauer15691082001-10-23 02:20:37 +0000752#endif
753
Martin v. Löwisaac13162006-10-19 10:58:46 +0000754/*
755 * Add PyArg_ParseTuple format where available.
756 */
757#ifdef HAVE_ATTRIBUTE_FORMAT_PARSETUPLE
758#define Py_FORMAT_PARSETUPLE(func,p1,p2) __attribute__((format(func,p1,p2)))
759#else
760#define Py_FORMAT_PARSETUPLE(func,p1,p2)
761#endif
762
Jeffrey Yasskind89f5b22009-01-09 16:47:07 +0000763/*
764 * Specify alignment on compilers that support it.
765 */
766#if defined(__GNUC__) && __GNUC__ >= 3
767#define Py_ALIGNED(x) __attribute__((aligned(x)))
768#else
769#define Py_ALIGNED(x)
770#endif
771
Nicholas Bastin9ba301e2004-07-15 15:54:05 +0000772/* Eliminate end-of-loop code not reached warnings from SunPro C
773 * when using do{...}while(0) macros
774 */
775#ifdef __SUNPRO_C
776#pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
777#endif
778
Martin v. Löwisa43190b2006-05-22 09:15:18 +0000779/*
780 * Older Microsoft compilers don't support the C99 long long literal suffixes,
781 * so these will be defined in PC/pyconfig.h for those compilers.
782 */
783#ifndef Py_LL
784#define Py_LL(x) x##LL
785#endif
786
787#ifndef Py_ULL
788#define Py_ULL(x) Py_LL(x##U)
789#endif
790
Tim Peters7d3a5112000-07-08 04:17:21 +0000791#endif /* Py_PYPORT_H */