wrap_python.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // (C) Copyright David Abrahams 2000.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // The author gratefully acknowleges the support of Dragon Systems, Inc., in
  7. // producing this work.
  8. // This file serves as a wrapper around <Python.h> which allows it to be
  9. // compiled with GCC 2.95.2 under Win32 and which disables the default MSVC
  10. // behavior so that a program may be compiled in debug mode without requiring a
  11. // special debugging build of the Python library.
  12. // To use the Python debugging library, #define BOOST_DEBUG_PYTHON on the
  13. // compiler command-line.
  14. // Revision History:
  15. // 05 Mar 01 Suppress warnings under Cygwin with Python 2.0 (Dave Abrahams)
  16. // 04 Mar 01 Rolled in some changes from the Dragon fork (Dave Abrahams)
  17. // 01 Mar 01 define PyObject_INIT() for Python 1.x (Dave Abrahams)
  18. #ifdef _DEBUG
  19. # ifndef BOOST_DEBUG_PYTHON
  20. # ifdef _MSC_VER
  21. // VC8.0 will complain if system headers are #included both with
  22. // and without _DEBUG defined, so we have to #include all the
  23. // system headers used by pyconfig.h right here.
  24. # include <stddef.h>
  25. # include <stdarg.h>
  26. # include <stdio.h>
  27. # include <stdlib.h>
  28. # include <assert.h>
  29. # include <errno.h>
  30. # include <ctype.h>
  31. # include <wchar.h>
  32. # include <basetsd.h>
  33. # include <io.h>
  34. # include <limits.h>
  35. # include <float.h>
  36. # include <string.h>
  37. # include <math.h>
  38. # include <time.h>
  39. # endif
  40. # undef _DEBUG // Don't let Python force the debug library just because we're debugging.
  41. # define DEBUG_UNDEFINED_FROM_WRAP_PYTHON_H
  42. # endif
  43. #endif
  44. # include <pyconfig.h>
  45. # if defined(_SGI_COMPILER_VERSION) && _SGI_COMPILER_VERSION >= 740
  46. # undef _POSIX_C_SOURCE
  47. # undef _XOPEN_SOURCE
  48. # undef HAVE_STDINT_H // undo Python 2.5.1 define
  49. # endif
  50. //
  51. // Python's LongObject.h helpfully #defines ULONGLONG_MAX for us,
  52. // which confuses Boost's config
  53. //
  54. #include <limits.h>
  55. #ifndef ULONG_MAX
  56. # define BOOST_PYTHON_ULONG_MAX_UNDEFINED
  57. #endif
  58. #ifndef LONGLONG_MAX
  59. # define BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  60. #endif
  61. #ifndef ULONGLONG_MAX
  62. # define BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  63. #endif
  64. //
  65. // Get ahold of Python's version number
  66. //
  67. #include <patchlevel.h>
  68. #if PY_MAJOR_VERSION<2 || PY_MAJOR_VERSION==2 && PY_MINOR_VERSION<2
  69. #error Python 2.2 or higher is required for this version of Boost.Python.
  70. #endif
  71. //
  72. // Some things we need in order to get Python.h to work with compilers other
  73. // than MSVC on Win32
  74. //
  75. #if defined(_WIN32) || defined(__CYGWIN__)
  76. # if defined(__GNUC__) && defined(__CYGWIN__)
  77. # if defined(__LP64__)
  78. # define SIZEOF_LONG 8
  79. # else
  80. # define SIZEOF_LONG 4
  81. # endif
  82. # if PY_MAJOR_VERSION < 2 || PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 2
  83. typedef int pid_t;
  84. # if defined(__LP64__)
  85. # define WORD_BIT 64
  86. # else
  87. # define WORD_BIT 32
  88. # endif
  89. # define hypot _hypot
  90. # include <stdio.h>
  91. # if PY_MAJOR_VERSION < 2
  92. # define HAVE_CLOCK
  93. # define HAVE_STRFTIME
  94. # define HAVE_STRERROR
  95. # endif
  96. # define NT_THREADS
  97. # ifndef NETSCAPE_PI
  98. # define USE_SOCKET
  99. # endif
  100. # ifdef USE_DL_IMPORT
  101. # define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
  102. # endif
  103. # ifdef USE_DL_EXPORT
  104. # define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
  105. # define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
  106. # endif
  107. # define HAVE_LONG_LONG 1
  108. # define LONG_LONG long long
  109. # endif
  110. # elif defined(__MWERKS__)
  111. # ifndef _MSC_VER
  112. # define PY_MSC_VER_DEFINED_FROM_WRAP_PYTHON_H 1
  113. # define _MSC_VER 900
  114. # endif
  115. # undef hypot // undo the evil #define left by Python.
  116. # elif defined(__BORLANDC__)
  117. # undef HAVE_HYPOT
  118. # define HAVE_HYPOT 1
  119. # endif
  120. #endif // _WIN32
  121. #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 2 && PY_MICRO_VERSION < 2
  122. # include <boost/python/detail/python22_fixed.h>
  123. #else
  124. # include <Python.h>
  125. #endif
  126. #ifdef BOOST_PYTHON_ULONG_MAX_UNDEFINED
  127. # undef ULONG_MAX
  128. # undef BOOST_PYTHON_ULONG_MAX_UNDEFINED
  129. #endif
  130. #ifdef BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  131. # undef LONGLONG_MAX
  132. # undef BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  133. #endif
  134. #ifdef BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  135. # undef ULONGLONG_MAX
  136. # undef BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  137. #endif
  138. #ifdef PY_MSC_VER_DEFINED_FROM_WRAP_PYTHON_H
  139. # undef _MSC_VER
  140. #endif
  141. #ifdef DEBUG_UNDEFINED_FROM_WRAP_PYTHON_H
  142. # undef DEBUG_UNDEFINED_FROM_WRAP_PYTHON_H
  143. # define _DEBUG
  144. # ifdef _CRT_NOFORCE_MANIFEST_DEFINED_FROM_WRAP_PYTHON_H
  145. # undef _CRT_NOFORCE_MANIFEST_DEFINED_FROM_WRAP_PYTHON_H
  146. # undef _CRT_NOFORCE_MANIFEST
  147. # endif
  148. #endif
  149. #if !defined(PY_MAJOR_VERSION) || PY_MAJOR_VERSION < 2
  150. # define PyObject_INIT(op, typeobj) \
  151. ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  152. #endif
  153. // Define Python 3 macros for Python 2.x
  154. #if PY_VERSION_HEX < 0x02060000
  155. # define Py_TYPE(o) (((PyObject*)(o))->ob_type)
  156. # define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt)
  157. # define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)
  158. # define PyVarObject_HEAD_INIT(type, size) \
  159. PyObject_HEAD_INIT(type) size,
  160. #endif
  161. #ifdef __MWERKS__
  162. # pragma warn_possunwant off
  163. #elif _MSC_VER
  164. # pragma warning(disable:4786)
  165. #endif
  166. #if defined(HAVE_LONG_LONG)
  167. # if defined(PY_LONG_LONG)
  168. # define BOOST_PYTHON_LONG_LONG PY_LONG_LONG
  169. # elif defined(LONG_LONG)
  170. # define BOOST_PYTHON_LONG_LONG LONG_LONG
  171. # else
  172. # error "HAVE_LONG_LONG defined but not PY_LONG_LONG or LONG_LONG"
  173. # endif
  174. #endif