1 /*========================================================================= 2 3 Program: Visualization Toolkit 4 Module: vtkPythonCompatibility.h 5 6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 7 All rights reserved. 8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 9 10 This software is distributed WITHOUT ANY WARRANTY; without even 11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 PURPOSE. See the above copyright notice for more information. 13 14 =========================================================================*/ 15 /*----------------------------------------------------------------------- 16 This header contains macros to make Python 2 and Python 3 play nice. 17 It must be included after vtkPython.h. 18 -----------------------------------------------------------------------*/ 19 #ifndef vtkPythonCompatibility_h 20 #define vtkPythonCompatibility_h 21 22 // define our main check macro VTK_PY3K 23 #if PY_MAJOR_VERSION >= 3 24 #define VTK_PY3K 25 #endif 26 27 // ===== Macros needed for Python 3 ==== 28 #ifdef VTK_PY3K 29 30 // Int/Long compatibility 31 #define PyIntObject PyLongObject 32 #define PyInt_Type PyLong_Type 33 #define PyInt_Check PyLong_Check 34 #define PyInt_FromLong PyLong_FromLong 35 #define PyInt_AsLong PyLong_AsLong 36 37 // Unicode/String compatibility 38 #define PyString_InternFromString PyUnicode_InternFromString 39 #define PyString_FromFormat PyUnicode_FromFormat 40 #define PyString_Check PyUnicode_Check 41 #define PyString_FromString PyUnicode_FromString 42 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize 43 44 // Use this for PyUnicode_EncodeLocale, see PEP 383 45 #define VTK_PYUNICODE_ENC "surrogateescape" 46 47 // Required for Python 3.2 compatibility 48 #if PY_VERSION_HEX < 0x03030000 49 #define PyUnicode_DecodeLocaleAndSize PyUnicode_DecodeFSDefaultAndSize 50 #define PyUnicode_DecodeLocale PyUnicode_DecodeFSDefault 51 #define PyUnicode_EncodeLocale(o,e) PyUnicode_EncodeFSDefault(o) 52 #define PyString_AsString _PyUnicode_AsString 53 #else 54 #define PyString_AsString PyUnicode_AsUTF8 55 #endif 56 57 // Buffer compatibility 58 #if PY_VERSION_HEX < 0x03030000 59 #define VTK_PYBUFFER_INITIALIZER \ 60 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0, 0 }, 0 } 61 #else 62 #define VTK_PYBUFFER_INITIALIZER \ 63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 64 #endif 65 66 // PyTypeObject compatibility 67 #if PY_VERSION_HEX >= 0x03040000 68 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \ 69 0, 0, 0, 70 #else 71 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \ 72 0, 0, 73 #endif 74 75 #endif 76 77 // ===== Macros needed for Python 2 ==== 78 #ifndef VTK_PY3K 79 80 // Py3k introduced a new type "Py_hash_t" 81 typedef long Py_hash_t; 82 typedef unsigned long Py_uhash_t; 83 84 // Required for Python 2.5 compatibility 85 #ifndef PyVarObject_HEAD_INIT 86 #define PyVarObject_HEAD_INIT(type, size) \ 87 PyObject_HEAD_INIT(type) size, 88 #endif 89 90 // Required for Python 2.5 compatibility 91 #ifndef Py_TYPE 92 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 93 #endif 94 95 // Required for Python 2.5 compatibility 96 #ifndef PyBytes_Check 97 #define PyBytesObject PyStringObject 98 #define PyBytes_Type PyString_Type 99 #define PyBytes_Check PyString_Check 100 #define PyBytes_CheckExact PyString_CheckExact 101 #define PyBytes_AS_STRING PyString_AS_STRING 102 #define PyBytes_GET_SIZE PyString_GET_SIZE 103 #define PyBytes_FromStringAndSize PyString_FromStringAndSize 104 #define PyBytes_FromString PyString_FromString 105 #define PyBytes_FromFormat PyString_FromFormat 106 #define PyBytes_Size PyString_Size 107 #define PyBytes_AsString PyString_AsString 108 #define PyBytes_Concat PyString_Concat 109 #define PyBytes_ConcatAndDel PyString_ConcatAndDel 110 #define _PyBytes_Resize _PyString_Resize 111 #define PyBytes_Format PyString_Format 112 #define PyBytes_AsStringAndSize PyString_AsStringAndSize 113 #endif 114 115 // Buffer struct initialization is different for every version 116 #if PY_VERSION_HEX < 0x02060000 117 typedef struct bufferinfo { PyObject *obj; } Py_buffer; 118 #define VTK_PYBUFFER_INITIALIZER \ 119 { 0 } 120 #elif PY_VERSION_HEX < 0x02070000 121 #define VTK_PYBUFFER_INITIALIZER \ 122 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } 123 #else 124 #define VTK_PYBUFFER_INITIALIZER \ 125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0, 0 }, 0 } 126 #endif 127 128 // PyTypeObject compatibility 129 #if PY_VERSION_HEX >= 0x02060000 130 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED 0, 0, 131 #else 132 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED 0, 133 #endif 134 135 #endif 136 #endif 137