VTK
vtkPythonCompatibility.h
Go to the documentation of this file.
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 compability
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 #endif
53 
54 // Buffer compatibility
55 #if PY_VERSION_HEX < 0x03030000
56 #define VTK_PYBUFFER_INITIALIZER \
57  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0, 0 }, 0 }
58 #else
59 #define VTK_PYBUFFER_INITIALIZER \
60  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
61 #endif
62 
63 // PyTypeObject compatibility
64 #if PY_VERSION_HEX >= 0x03040000
65 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \
66  0, 0, 0,
67 #else
68 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \
69  0, 0,
70 #endif
71 
72 #endif
73 
74 // ===== Macros needed for Python 2 ====
75 #ifndef VTK_PY3K
76 
77 // Py3k introduced a new type "Py_hash_t"
78 typedef long Py_hash_t;
79 typedef unsigned long Py_uhash_t;
80 
81 // Required for Python 2.5 compatibility
82 #ifndef PyVarObject_HEAD_INIT
83 #define PyVarObject_HEAD_INIT(type, size) \
84  PyObject_HEAD_INIT(type) size,
85 #endif
86 
87 // Required for Python 2.5 compatibility
88 #ifndef Py_TYPE
89 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
90 #endif
91 
92 // Required for Python 2.5 compatibility
93 #ifndef PyBytes_Check
94 #define PyBytesObject PyStringObject
95 #define PyBytes_Type PyString_Type
96 #define PyBytes_Check PyString_Check
97 #define PyBytes_CheckExact PyString_CheckExact
98 #define PyBytes_AS_STRING PyString_AS_STRING
99 #define PyBytes_GET_SIZE PyString_GET_SIZE
100 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
101 #define PyBytes_FromString PyString_FromString
102 #define PyBytes_FromFormat PyString_FromFormat
103 #define PyBytes_Size PyString_Size
104 #define PyBytes_AsString PyString_AsString
105 #define PyBytes_Concat PyString_Concat
106 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
107 #define _PyBytes_Resize _PyString_Resize
108 #define PyBytes_Format PyString_Format
109 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
110 #endif
111 
112 // Buffer struct initialization is different for every version
113 #if PY_VERSION_HEX < 0x02060000
114 typedef struct bufferinfo { PyObject *obj; } Py_buffer;
115 #define VTK_PYBUFFER_INITIALIZER \
116  { 0 }
117 #elif PY_VERSION_HEX < 0x02070000
118 #define VTK_PYBUFFER_INITIALIZER \
119  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
120 #else
121 #define VTK_PYBUFFER_INITIALIZER \
122  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0, 0 }, 0 }
123 #endif
124 
125 // PyTypeObject compatibility
126 #if PY_VERSION_HEX >= 0x02060000
127 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED 0, 0,
128 #else
129 #define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED 0,
130 #endif
131 
132 #endif
133 #endif
struct bufferinfo Py_buffer
unsigned long Py_uhash_t
struct _object PyObject
long Py_hash_t