blob: f44c43e24775bd463cae168fac628a87c678d973 [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)
Mark Dickinsonf35f8042009-03-20 23:16:14 +0000381 * Return I >> J, forcing sign extension. Arithmetically, return the
382 * floor of I/2**J.
Tim Peters7d3a5112000-07-08 04:17:21 +0000383 * Requirements:
Mark Dickinsonf35f8042009-03-20 23:16:14 +0000384 * I should have signed integer type. In the terminology of C99, this can
385 * be either one of the five standard signed integer types (signed char,
386 * short, int, long, long long) or an extended signed integer type.
387 * J is an integer >= 0 and strictly less than the number of bits in the
388 * type of I (because C doesn't define what happens for J outside that
389 * range either).
390 * TYPE used to specify the type of I, but is now ignored. It's been left
391 * in for backwards compatibility with versions <= 2.6 or 3.0.
Tim Peters7d3a5112000-07-08 04:17:21 +0000392 * Caution:
393 * I may be evaluated more than once.
394 */
395#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
396#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
Mark Dickinsonf35f8042009-03-20 23:16:14 +0000397 ((I) < 0 ? -1-((-1-(I)) >> (J)) : (I) >> (J))
Tim Peters7d3a5112000-07-08 04:17:21 +0000398#else
399#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
400#endif
401
Tim Peters39dce292000-08-15 03:34:48 +0000402/* Py_FORCE_EXPANSION(X)
Tim Peters1be46842000-07-23 18:10:18 +0000403 * "Simply" returns its argument. However, macro expansions within the
404 * argument are evaluated. This unfortunate trickery is needed to get
405 * token-pasting to work as desired in some cases.
406 */
407#define Py_FORCE_EXPANSION(X) X
408
Tim Peters8315ea52000-07-23 19:28:35 +0000409/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
410 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
411 * assert-fails if any information is lost.
412 * Caution:
413 * VALUE may be evaluated more than once.
414 */
415#ifdef Py_DEBUG
416#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
417 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
418#else
419#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
420#endif
421
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000422/* Py_SET_ERRNO_ON_MATH_ERROR(x)
Tim Petersa40c7932001-09-05 22:36:56 +0000423 * If a libm function did not set errno, but it looks like the result
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000424 * overflowed or not-a-number, set errno to ERANGE or EDOM. Set errno
425 * to 0 before calling a libm function, and invoke this macro after,
426 * passing the function result.
Tim Petersa40c7932001-09-05 22:36:56 +0000427 * Caution:
428 * This isn't reliable. See Py_OVERFLOWED comments.
429 * X is evaluated more than once.
430 */
Guido van Rossum539c6622005-09-14 17:49:54 +0000431#if defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(__hpux) && defined(__ia64))
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000432#define _Py_SET_EDOM_FOR_NAN(X) if (isnan(X)) errno = EDOM;
433#else
434#define _Py_SET_EDOM_FOR_NAN(X) ;
435#endif
436#define Py_SET_ERRNO_ON_MATH_ERROR(X) \
Tim Petersa40c7932001-09-05 22:36:56 +0000437 do { \
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000438 if (errno == 0) { \
439 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
440 errno = ERANGE; \
441 else _Py_SET_EDOM_FOR_NAN(X) \
442 } \
Tim Petersa40c7932001-09-05 22:36:56 +0000443 } while(0)
Tim Peters57f282a2001-09-05 05:38:10 +0000444
Hye-Shik Chang77d9a3e2004-03-22 08:43:55 +0000445/* Py_SET_ERANGE_ON_OVERFLOW(x)
446 * An alias of Py_SET_ERRNO_ON_MATH_ERROR for backward-compatibility.
447 */
448#define Py_SET_ERANGE_IF_OVERFLOW(X) Py_SET_ERRNO_ON_MATH_ERROR(X)
449
Tim Petersdc5a5082002-03-09 04:58:24 +0000450/* Py_ADJUST_ERANGE1(x)
451 * Py_ADJUST_ERANGE2(x, y)
452 * Set errno to 0 before calling a libm function, and invoke one of these
453 * macros after, passing the function result(s) (Py_ADJUST_ERANGE2 is useful
454 * for functions returning complex results). This makes two kinds of
455 * adjustments to errno: (A) If it looks like the platform libm set
456 * errno=ERANGE due to underflow, clear errno. (B) If it looks like the
457 * platform libm overflowed but didn't set errno, force errno to ERANGE. In
458 * effect, we're trying to force a useful implementation of C89 errno
459 * behavior.
460 * Caution:
461 * This isn't reliable. See Py_OVERFLOWED comments.
462 * X and Y may be evaluated more than once.
463 */
464#define Py_ADJUST_ERANGE1(X) \
465 do { \
466 if (errno == 0) { \
467 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
468 errno = ERANGE; \
469 } \
470 else if (errno == ERANGE && (X) == 0.0) \
471 errno = 0; \
472 } while(0)
473
474#define Py_ADJUST_ERANGE2(X, Y) \
475 do { \
476 if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
477 (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
478 if (errno == 0) \
479 errno = ERANGE; \
480 } \
481 else if (errno == ERANGE) \
482 errno = 0; \
483 } while(0)
484
Neal Norwitz80292642002-12-19 15:12:26 +0000485/* Py_DEPRECATED(version)
Neal Norwitz93344ab2002-12-19 15:24:11 +0000486 * Declare a variable, type, or function deprecated.
Neal Norwitz80292642002-12-19 15:12:26 +0000487 * Usage:
488 * extern int old_var Py_DEPRECATED(2.3);
489 * typedef int T1 Py_DEPRECATED(2.4);
490 * extern int x() Py_DEPRECATED(2.5);
491 */
Neal Norwitz3dd8be42006-03-20 06:33:01 +0000492#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
493 (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
Neal Norwitz80292642002-12-19 15:12:26 +0000494#define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
495#else
Tim Peters4643bd92002-12-28 21:56:08 +0000496#define Py_DEPRECATED(VERSION_UNUSED)
Neal Norwitz80292642002-12-19 15:12:26 +0000497#endif
498
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000499/**************************************************************************
500Prototypes that are missing from the standard include files on some systems
501(and possibly only some versions of such systems.)
502
503Please be conservative with adding new ones, document them and enclose them
504in platform-specific #ifdefs.
505**************************************************************************/
506
507#ifdef SOLARIS
508/* Unchecked */
509extern int gethostname(char *, int);
510#endif
511
512#ifdef __BEOS__
513/* Unchecked */
514/* It's in the libs, but not the headers... - [cjh] */
Tim Peters60f42b52001-01-18 03:03:16 +0000515int shutdown( int, int );
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000516#endif
517
518#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000519#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000520extern char * _getpty(int *, int, mode_t, int);
521#endif
522
Martin v. Löwis8c255e42008-05-23 15:06:50 +0000523/* On QNX 6, struct termio must be declared by including sys/termio.h
524 if TCGETA, TCSETA, TCSETAW, or TCSETAF are used. sys/termio.h must
525 be included before termios.h or it will generate an error. */
526#ifdef HAVE_SYS_TERMIO_H
527#include <sys/termio.h>
528#endif
529
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000530#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
531#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
532/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
533 functions, even though they are included in libutil. */
534#include <termios.h>
535extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
Christian Heimes951cc0f2008-01-31 23:08:23 +0000536extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000537#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
538#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
539
540
541/* These are pulled from various places. It isn't obvious on what platforms
542 they are necessary, nor what the exact prototype should look like (which
543 is likely to vary between platforms!) If you find you need one of these
544 declarations, please move them to a platform-specific block and include
545 proper prototypes. */
546#if 0
547
548/* From Modules/resource.c */
549extern int getrusage();
550extern int getpagesize();
551
552/* From Python/sysmodule.c and Modules/posixmodule.c */
553extern int fclose(FILE *);
554
555/* From Modules/posixmodule.c */
556extern int fdatasync(int);
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000557#endif /* 0 */
558
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000559
Hye-Shik Changf3032612006-03-22 08:52:43 +0000560/* On 4.4BSD-descendants, ctype functions serves the whole range of
561 * wchar_t character set rather than single byte code points only.
562 * This characteristic can break some operations of string object
563 * including str.upper() and str.split() on UTF-8 locales. This
564 * workaround was provided by Tim Robbins of FreeBSD project.
565 */
Hye-Shik Changb5047fd2004-08-04 06:33:51 +0000566
567#ifdef __FreeBSD__
568#include <osreldate.h>
569#if __FreeBSD_version > 500039
570#include <ctype.h>
571#include <wctype.h>
572#undef isalnum
573#define isalnum(c) iswalnum(btowc(c))
574#undef isalpha
575#define isalpha(c) iswalpha(btowc(c))
576#undef islower
577#define islower(c) iswlower(btowc(c))
578#undef isspace
579#define isspace(c) iswspace(btowc(c))
580#undef isupper
581#define isupper(c) iswupper(btowc(c))
582#undef tolower
583#define tolower(c) towlower(btowc(c))
584#undef toupper
585#define toupper(c) towupper(btowc(c))
586#endif
587#endif
588
589
Mark Hammond8235ea12002-07-19 06:55:41 +0000590/* Declarations for symbol visibility.
591
592 PyAPI_FUNC(type): Declares a public Python API function and return type
Tim Peters4643bd92002-12-28 21:56:08 +0000593 PyAPI_DATA(type): Declares public Python data and its type
Mark Hammond8235ea12002-07-19 06:55:41 +0000594 PyMODINIT_FUNC: A Python module init function. If these functions are
Tim Peters4643bd92002-12-28 21:56:08 +0000595 inside the Python core, they are private to the core.
596 If in an extension module, it may be declared with
Mark Hammond8235ea12002-07-19 06:55:41 +0000597 external linkage depending on the platform.
598
599 As a number of platforms support/require "__declspec(dllimport/dllexport)",
600 we support a HAVE_DECLSPEC_DLL macro to save duplication.
601*/
602
Tim Peters4643bd92002-12-28 21:56:08 +0000603/*
Michael W. Hudsonf163d102003-02-10 19:36:46 +0000604 All windows ports, except cygwin, are handled in PC/pyconfig.h.
605
606 BeOS and cygwin are the only other autoconf platform requiring special
607 linkage handling and both of these use __declspec().
Mark Hammond8235ea12002-07-19 06:55:41 +0000608*/
609#if defined(__CYGWIN__) || defined(__BEOS__)
610# define HAVE_DECLSPEC_DLL
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000611#endif
Mark Hammond8235ea12002-07-19 06:55:41 +0000612
Jason Tishler30765592003-09-04 11:04:06 +0000613/* only get special linkage if built as shared or platform is Cygwin */
614#if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__)
Mark Hammond8235ea12002-07-19 06:55:41 +0000615# if defined(HAVE_DECLSPEC_DLL)
616# ifdef Py_BUILD_CORE
617# define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
Mark Hammonda2905272002-07-29 13:42:14 +0000618# define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000619 /* module init functions inside the core need no external linkage */
Jason Tishler6bc06ec2003-09-04 11:59:50 +0000620 /* except for Cygwin to handle embedding (FIXME: BeOS too?) */
621# if defined(__CYGWIN__)
622# define PyMODINIT_FUNC __declspec(dllexport) void
623# else /* __CYGWIN__ */
624# define PyMODINIT_FUNC void
625# endif /* __CYGWIN__ */
Mark Hammond8235ea12002-07-19 06:55:41 +0000626# else /* Py_BUILD_CORE */
627 /* Building an extension module, or an embedded situation */
628 /* public Python functions and data are imported */
Jason Tishlerfb8595d2003-01-06 12:41:26 +0000629 /* Under Cygwin, auto-import functions to prevent compilation */
630 /* failures similar to http://python.org/doc/FAQ.html#3.24 */
631# if !defined(__CYGWIN__)
632# define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
633# endif /* !__CYGWIN__ */
Mark Hammonda2905272002-07-29 13:42:14 +0000634# define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000635 /* module init functions outside the core must be exported */
636# if defined(__cplusplus)
637# define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
638# else /* __cplusplus */
639# define PyMODINIT_FUNC __declspec(dllexport) void
640# endif /* __cplusplus */
641# endif /* Py_BUILD_CORE */
642# endif /* HAVE_DECLSPEC */
643#endif /* Py_ENABLE_SHARED */
644
645/* If no external linkage macros defined by now, create defaults */
646#ifndef PyAPI_FUNC
647# define PyAPI_FUNC(RTYPE) RTYPE
648#endif
649#ifndef PyAPI_DATA
Mark Hammonda2905272002-07-29 13:42:14 +0000650# define PyAPI_DATA(RTYPE) extern RTYPE
Mark Hammond8235ea12002-07-19 06:55:41 +0000651#endif
652#ifndef PyMODINIT_FUNC
653# if defined(__cplusplus)
654# define PyMODINIT_FUNC extern "C" void
655# else /* __cplusplus */
656# define PyMODINIT_FUNC void
657# endif /* __cplusplus */
658#endif
659
660/* Deprecated DL_IMPORT and DL_EXPORT macros */
661#if defined(Py_ENABLE_SHARED) && defined (HAVE_DECLSPEC_DLL)
662# if defined(Py_BUILD_CORE)
663# define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
664# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
665# else
666# define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
667# define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
668# endif
669#endif
670#ifndef DL_EXPORT
671# define DL_EXPORT(RTYPE) RTYPE
672#endif
673#ifndef DL_IMPORT
674# define DL_IMPORT(RTYPE) RTYPE
675#endif
676/* End of deprecated DL_* macros */
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000677
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000678/* If the fd manipulation macros aren't defined,
679 here is a set that should do the job */
680
Guido van Rossum367e46a2000-08-01 18:28:44 +0000681#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000682
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000683#ifndef FD_SETSIZE
684#define FD_SETSIZE 256
685#endif
686
687#ifndef FD_SET
688
689typedef long fd_mask;
690
691#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
692#ifndef howmany
693#define howmany(x, y) (((x)+((y)-1))/(y))
694#endif /* howmany */
695
696typedef struct fd_set {
697 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
698} fd_set;
699
700#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
701#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
702#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
703#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
704
705#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000706
707#endif /* fd manipulation macros */
708
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000709
Fred Draked5fadf72000-09-26 05:46:01 +0000710/* limits.h constants that may be missing */
711
712#ifndef INT_MAX
713#define INT_MAX 2147483647
714#endif
715
716#ifndef LONG_MAX
717#if SIZEOF_LONG == 4
718#define LONG_MAX 0X7FFFFFFFL
719#elif SIZEOF_LONG == 8
720#define LONG_MAX 0X7FFFFFFFFFFFFFFFL
721#else
722#error "could not set LONG_MAX in pyport.h"
723#endif
724#endif
725
726#ifndef LONG_MIN
727#define LONG_MIN (-LONG_MAX-1)
728#endif
729
Tim Petersd57731f2000-10-05 01:42:25 +0000730#ifndef LONG_BIT
731#define LONG_BIT (8 * SIZEOF_LONG)
732#endif
733
734#if LONG_BIT != 8 * SIZEOF_LONG
735/* 04-Oct-2000 LONG_BIT is apparently (mis)defined as 64 on some recent
736 * 32-bit platforms using gcc. We try to catch that here at compile-time
737 * rather than waiting for integer multiplication to trigger bogus
738 * overflows.
739 */
Andrew M. Kuchling234fb632001-01-12 15:06:28 +0000740#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
Tim Petersd57731f2000-10-05 01:42:25 +0000741#endif
742
Tim Peters7d3a5112000-07-08 04:17:21 +0000743#ifdef __cplusplus
744}
745#endif
746
Neil Schemenauer15691082001-10-23 02:20:37 +0000747/*
748 * Hide GCC attributes from compilers that don't support them.
749 */
Guido van Rossumbd67d6f2001-10-27 21:16:16 +0000750#if (!defined(__GNUC__) || __GNUC__ < 2 || \
Jack Jansen4892f242002-02-01 15:46:29 +0000751 (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ) && \
Guido van Rossumbd67d6f2001-10-27 21:16:16 +0000752 !defined(RISCOS)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +0000753#define Py_GCC_ATTRIBUTE(x)
754#else
755#define Py_GCC_ATTRIBUTE(x) __attribute__(x)
Neil Schemenauer15691082001-10-23 02:20:37 +0000756#endif
757
Martin v. Löwisaac13162006-10-19 10:58:46 +0000758/*
759 * Add PyArg_ParseTuple format where available.
760 */
761#ifdef HAVE_ATTRIBUTE_FORMAT_PARSETUPLE
762#define Py_FORMAT_PARSETUPLE(func,p1,p2) __attribute__((format(func,p1,p2)))
763#else
764#define Py_FORMAT_PARSETUPLE(func,p1,p2)
765#endif
766
Jeffrey Yasskind89f5b22009-01-09 16:47:07 +0000767/*
768 * Specify alignment on compilers that support it.
769 */
770#if defined(__GNUC__) && __GNUC__ >= 3
771#define Py_ALIGNED(x) __attribute__((aligned(x)))
772#else
773#define Py_ALIGNED(x)
774#endif
775
Nicholas Bastin9ba301e2004-07-15 15:54:05 +0000776/* Eliminate end-of-loop code not reached warnings from SunPro C
777 * when using do{...}while(0) macros
778 */
779#ifdef __SUNPRO_C
780#pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
781#endif
782
Martin v. Löwisa43190b2006-05-22 09:15:18 +0000783/*
784 * Older Microsoft compilers don't support the C99 long long literal suffixes,
785 * so these will be defined in PC/pyconfig.h for those compilers.
786 */
787#ifndef Py_LL
788#define Py_LL(x) x##LL
789#endif
790
791#ifndef Py_ULL
792#define Py_ULL(x) Py_LL(x##U)
793#endif
794
Tim Peters7d3a5112000-07-08 04:17:21 +0000795#endif /* Py_PYPORT_H */