wrap_python.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // pyconfig.h defines a macro with hypot name, what breaks libstdc++ math headers
  45. // that Python.h tries to include afterwards.
  46. #if defined(__MINGW32__)
  47. # include <cmath>
  48. # include <math.h>
  49. #endif
  50. # include <pyconfig.h>
  51. # if defined(_SGI_COMPILER_VERSION) && _SGI_COMPILER_VERSION >= 740
  52. # undef _POSIX_C_SOURCE
  53. # undef _XOPEN_SOURCE
  54. # undef HAVE_STDINT_H // undo Python 2.5.1 define
  55. # endif
  56. //
  57. // Python's LongObject.h helpfully #defines ULONGLONG_MAX for us,
  58. // which confuses Boost's config
  59. //
  60. #include <limits.h>
  61. #ifndef ULONG_MAX
  62. # define BOOST_PYTHON_ULONG_MAX_UNDEFINED
  63. #endif
  64. #ifndef LONGLONG_MAX
  65. # define BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  66. #endif
  67. #ifndef ULONGLONG_MAX
  68. # define BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  69. #endif
  70. //
  71. // Get ahold of Python's version number
  72. //
  73. #include <patchlevel.h>
  74. #if PY_MAJOR_VERSION<2 || PY_MAJOR_VERSION==2 && PY_MINOR_VERSION<2
  75. #error Python 2.2 or higher is required for this version of Boost.Python.
  76. #endif
  77. //
  78. // Some things we need in order to get Python.h to work with compilers other
  79. // than MSVC on Win32
  80. //
  81. #if defined(_WIN32) || defined(__CYGWIN__)
  82. # if defined(__GNUC__) && defined(__CYGWIN__)
  83. # if defined(__LP64__)
  84. # define SIZEOF_LONG 8
  85. # else
  86. # define SIZEOF_LONG 4
  87. # endif
  88. # if PY_MAJOR_VERSION < 2 || PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 2
  89. typedef int pid_t;
  90. # if defined(__LP64__)
  91. # define WORD_BIT 64
  92. # else
  93. # define WORD_BIT 32
  94. # endif
  95. # define hypot _hypot
  96. # include <stdio.h>
  97. # if PY_MAJOR_VERSION < 2
  98. # define HAVE_CLOCK
  99. # define HAVE_STRFTIME
  100. # define HAVE_STRERROR
  101. # endif
  102. # define NT_THREADS
  103. # ifndef NETSCAPE_PI
  104. # define USE_SOCKET
  105. # endif
  106. # ifdef USE_DL_IMPORT
  107. # define DL_IMPORT(RTYPE) __declspec(dllimport) RTYPE
  108. # endif
  109. # ifdef USE_DL_EXPORT
  110. # define DL_IMPORT(RTYPE) __declspec(dllexport) RTYPE
  111. # define DL_EXPORT(RTYPE) __declspec(dllexport) RTYPE
  112. # endif
  113. # define HAVE_LONG_LONG 1
  114. # define LONG_LONG long long
  115. # endif
  116. # elif defined(__MWERKS__)
  117. # ifndef _MSC_VER
  118. # define PY_MSC_VER_DEFINED_FROM_WRAP_PYTHON_H 1
  119. # define _MSC_VER 900
  120. # endif
  121. # undef hypot // undo the evil #define left by Python.
  122. # elif defined(__BORLANDC__)
  123. # undef HAVE_HYPOT
  124. # define HAVE_HYPOT 1
  125. # endif
  126. #endif // _WIN32
  127. #if defined(__GNUC__)
  128. # if defined(__has_warning)
  129. # define BOOST_PYTHON_GCC_HAS_WREGISTER __has_warning("-Wregister")
  130. # else
  131. # define BOOST_PYTHON_GCC_HAS_WREGISTER __GNUC__ >= 7
  132. # endif
  133. #else
  134. # define BOOST_PYTHON_GCC_HAS_WREGISTER 0
  135. #endif
  136. // Python.h header uses `register` keyword until Python 3.4
  137. #if BOOST_PYTHON_GCC_HAS_WREGISTER
  138. # pragma GCC diagnostic push
  139. # pragma GCC diagnostic ignored "-Wregister"
  140. #elif defined(_MSC_VER)
  141. # pragma warning(push)
  142. # pragma warning(disable : 5033) // 'register' is no longer a supported storage class
  143. #endif
  144. #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 2 && PY_MICRO_VERSION < 2
  145. # include <boost/python/detail/python22_fixed.h>
  146. #else
  147. # include <Python.h>
  148. #endif
  149. #if BOOST_PYTHON_GCC_HAS_WREGISTER
  150. # pragma GCC diagnostic pop
  151. #elif defined(_MSC_VER)
  152. # pragma warning(pop)
  153. #endif
  154. #undef BOOST_PYTHON_GCC_HAS_WREGISTER
  155. #ifdef BOOST_PYTHON_ULONG_MAX_UNDEFINED
  156. # undef ULONG_MAX
  157. # undef BOOST_PYTHON_ULONG_MAX_UNDEFINED
  158. #endif
  159. #ifdef BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  160. # undef LONGLONG_MAX
  161. # undef BOOST_PYTHON_LONGLONG_MAX_UNDEFINED
  162. #endif
  163. #ifdef BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  164. # undef ULONGLONG_MAX
  165. # undef BOOST_PYTHON_ULONGLONG_MAX_UNDEFINED
  166. #endif
  167. #ifdef PY_MSC_VER_DEFINED_FROM_WRAP_PYTHON_H
  168. # undef _MSC_VER
  169. #endif
  170. #ifdef DEBUG_UNDEFINED_FROM_WRAP_PYTHON_H
  171. # undef DEBUG_UNDEFINED_FROM_WRAP_PYTHON_H
  172. # define _DEBUG
  173. # ifdef _CRT_NOFORCE_MANIFEST_DEFINED_FROM_WRAP_PYTHON_H
  174. # undef _CRT_NOFORCE_MANIFEST_DEFINED_FROM_WRAP_PYTHON_H
  175. # undef _CRT_NOFORCE_MANIFEST
  176. # endif
  177. #endif
  178. #if !defined(PY_MAJOR_VERSION) || PY_MAJOR_VERSION < 2
  179. # define PyObject_INIT(op, typeobj) \
  180. ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  181. #endif
  182. // Define Python 3 macros for Python 2.x
  183. #if PY_VERSION_HEX < 0x02060000
  184. # define Py_TYPE(o) (((PyObject*)(o))->ob_type)
  185. # define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt)
  186. # define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)
  187. # define PyVarObject_HEAD_INIT(type, size) \
  188. PyObject_HEAD_INIT(type) size,
  189. #endif
  190. #ifdef __MWERKS__
  191. # pragma warn_possunwant off
  192. #elif _MSC_VER
  193. # pragma warning(disable:4786)
  194. #endif
  195. #if defined(HAVE_LONG_LONG)
  196. # if defined(PY_LONG_LONG)
  197. # define BOOST_PYTHON_LONG_LONG PY_LONG_LONG
  198. # elif defined(LONG_LONG)
  199. # define BOOST_PYTHON_LONG_LONG LONG_LONG
  200. # else
  201. # error "HAVE_LONG_LONG defined but not PY_LONG_LONG or LONG_LONG"
  202. # endif
  203. #endif