Imported Upstream version 3.3.1
[platform/upstream/pygobject2.git] / gi / _glib / pyglib-python-compat.h
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pyglib - Python bindings for GLib toolkit.
3  * Copyright (C) 2008  Johan Dahlin
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  * USA
19  */
20
21 #ifndef __PYGLIB_PYTHON_COMPAT_H__
22 #define __PYGLIB_PYTHON_COMPAT_H__
23
24 /* Python 2.3 does not define Py_CLEAR */
25 #ifndef Py_CLEAR
26 #define Py_CLEAR(op)                \
27         do {                                \
28                 if (op) {           \
29                         PyObject *tmp = (PyObject *)(op);   \
30                         (op) = NULL;        \
31                         Py_DECREF(tmp);     \
32                 }               \
33         } while (0)
34 #endif
35
36 /* Compilation on Python 2.4 */
37 #if PY_VERSION_HEX < 0x02050000
38 typedef int Py_ssize_t;
39 #define PY_SSIZE_T_MAX INT_MAX
40 #define PY_SSIZE_T_MIN INT_MIN
41 typedef inquiry lenfunc;
42 #endif
43
44 /* PyCObject superceded by PyCapsule on Python >= 2.7
45  * However since this effects header files used by
46  * static bindings we are only applying the change to
47  * Python 3.x where we don't support the static bindings.
48  * 3.2 removed PyCObject so we don't have any choice here.
49  *
50  * There is talk upstream of undeprecating PyCObject
51  * (at least where the 2.x branch is concerned)
52  * and there is no danger of it being remove from 2.7.
53  **/
54 #if PY_VERSION_HEX >= 0x03000000
55 # define PYGLIB_CPointer_Check PyCapsule_CheckExact
56 # define PYGLIB_CPointer_WrapPointer(ptr, typename) \
57     PyCapsule_New(ptr, typename, NULL)
58 # define PYGLIB_CPointer_GetPointer(obj, typename) \
59     PyCapsule_GetPointer(obj, typename)
60 # define PYGLIB_CPointer_Import(module, symbol) \
61     PyCapsule_Import(##module##.##symbol##, FALSE)
62 #else
63 # define PYGLIB_CPointer_Check PyCObject_Check
64 # define PYGLIB_CPointer_WrapPointer(ptr, typename) \
65     PyCObject_FromVoidPtr(ptr, NULL)
66 # define PYGLIB_CPointer_GetPointer(obj, typename) \
67   PyCObject_AsVoidPtr(obj)
68 # define PYGLIB_CPointer_Import(module, symbol) \
69     PyCObject_Import(module, symbol)
70 #endif
71
72 #if PY_VERSION_HEX < 0x03000000
73
74 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
75 static int _pyglib_init_##modname(PyObject *module); \
76 void init##modname(void) \
77 { \
78     PyObject *module = Py_InitModule(fullpkgname, functions); \
79     _pyglib_init_##modname(module); \
80 } \
81 static int _pyglib_init_##modname(PyObject *module)
82
83 #else
84
85 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
86 static struct PyModuleDef _##modname##module = {     \
87     PyModuleDef_HEAD_INIT,                              \
88     fullpkgname,                                        \
89     NULL,                                               \
90     -1,                                                 \
91     functions,                                          \
92     NULL,                                               \
93     NULL,                                               \
94     NULL,                                               \
95     NULL                                                \
96 };                                                      \
97 static int _pyglib_init_##modname(PyObject *module); \
98 PyObject *PyInit_##modname(void) \
99 { \
100     PyObject *module = PyModule_Create(&_##modname##module);  \
101     if (module == NULL) \
102         return NULL; \
103     if (_pyglib_init_##modname(module) != 0 ) {\
104         Py_DECREF(module); \
105         return NULL; \
106     } \
107     return module; \
108 } \
109 static int _pyglib_init_##modname(PyObject *module)
110
111 #endif
112
113 /* Compilation on Python 2.x */
114 #if PY_VERSION_HEX < 0x03000000
115 #define PYGLIB_MODULE_ERROR_RETURN
116
117 #define RO READONLY
118
119 #define PYGLIB_PyBaseString_Check(ob) (PyString_Check(ob) || PyUnicode_Check(ob))
120
121 #define PYGLIB_PyUnicode_Check PyString_Check
122 #define PYGLIB_PyUnicode_AsString PyString_AsString
123 #define PYGLIB_PyUnicode_AsStringAndSize PyString_AsStringAndSize
124 #define PYGLIB_PyUnicode_FromString PyString_FromString
125 #define PYGLIB_PyUnicode_FromStringAndSize PyString_FromStringAndSize
126 #define PYGLIB_PyUnicode_FromFormat PyString_FromFormat
127 #define PYGLIB_PyUnicode_AS_STRING PyString_AS_STRING
128 #define PYGLIB_PyUnicode_GET_SIZE PyString_GET_SIZE
129 #define PYGLIB_PyUnicode_Type PyString_Type
130
131 #define PYGLIB_PyBytes_FromString PyString_FromString
132 #define PYGLIB_PyBytes_FromStringAndSize PyString_FromStringAndSize
133 #define PYGLIB_PyBytes_Resize _PyString_Resize
134 #define PYGLIB_PyBytes_AsString PyString_AsString
135 #define PYGLIB_PyBytes_Size PyString_Size
136 #define PYGLIB_PyBytes_Check PyString_Check
137
138 #define PYGLIB_PyLong_Check PyInt_Check
139 #define PYGLIB_PyLong_FromLong PyInt_FromLong
140 #define PYGLIB_PyLong_FromSsize_t PyInt_FromSsize_t
141 #define PYGLIB_PyLong_FromSize_t PyInt_FromSize_t
142 #define PYGLIB_PyLong_AsLong  PyInt_AsLong
143 #define PYGLIB_PyLongObject PyIntObject
144 #define PYGLIB_PyLong_Type PyInt_Type
145 #define PYGLIB_PyLong_AS_LONG PyInt_AS_LONG
146 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
147
148 #define PYGLIB_PyNumber_Long PyNumber_Int
149
150 #ifndef PyVarObject_HEAD_INIT
151 #define PyVarObject_HEAD_INIT(base, size) \
152   PyObject_HEAD_INIT(base) \
153   size,
154 #endif
155
156 #define PYGLIB_MODULE_START(symbol, modname)            \
157 DL_EXPORT(void) init##symbol(void)                      \
158 {                                                       \
159     PyObject *module;                                   \
160     module = Py_InitModule(modname, symbol##_functions);
161 #define PYGLIB_MODULE_END }
162 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)   \
163 PyTypeObject symbol = {                                 \
164     PyObject_HEAD_INIT(NULL)                            \
165     0,                                                  \
166     typename,                                           \
167     sizeof(csymbol),                                    \
168     0,                                                  \
169 };
170 #define PYGLIB_REGISTER_TYPE(d, type, name)             \
171     if (!type.tp_alloc)                                 \
172         type.tp_alloc = PyType_GenericAlloc;            \
173     if (!type.tp_new)                                   \
174         type.tp_new = PyType_GenericNew;                \
175     if (PyType_Ready(&type))                            \
176         return;                                         \
177     PyDict_SetItemString(d, name, (PyObject *)&type);
178
179 #else
180
181 #define PYGLIB_MODULE_ERROR_RETURN 0
182
183 #define PYGLIB_MODULE_START(symbol, modname)            \
184     static struct PyModuleDef _##symbol##module = {     \
185     PyModuleDef_HEAD_INIT,                              \
186     modname,                                            \
187     NULL,                                               \
188     -1,                                                 \
189     symbol##_functions,                                 \
190     NULL,                                               \
191     NULL,                                               \
192     NULL,                                               \
193     NULL                                                \
194 };                                                      \
195 PyMODINIT_FUNC PyInit_##symbol(void)                    \
196 {                                                       \
197     PyObject *module;                                   \
198     module = PyModule_Create(&_##symbol##module);
199 #define PYGLIB_MODULE_END return module; }
200 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)   \
201 PyTypeObject symbol = {                                 \
202     PyVarObject_HEAD_INIT(NULL, 0)                      \
203     typename,                                           \
204     sizeof(csymbol)                                     \
205 };
206 #define PYGLIB_REGISTER_TYPE(d, type, name)                 \
207     if (!type.tp_alloc)                                 \
208             type.tp_alloc = PyType_GenericAlloc;            \
209     if (!type.tp_new)                                   \
210             type.tp_new = PyType_GenericNew;                \
211     if (PyType_Ready(&type))                            \
212             return;                                         \
213     PyDict_SetItemString(d, name, (PyObject *)&type);
214
215 #define PYGLIB_PyBaseString_Check PyUnicode_Check
216
217 #define PYGLIB_PyUnicode_Check PyUnicode_Check
218 #define PYGLIB_PyUnicode_AsString _PyUnicode_AsString
219 #define PYGLIB_PyUnicode_AsStringAndSize(obj, buf, size) \
220     (((*(buf) = _PyUnicode_AsStringAndSize(obj, size)) != NULL) ? 0 : -1) 
221 #define PYGLIB_PyUnicode_FromString PyUnicode_FromString
222 #define PYGLIB_PyUnicode_FromStringAndSize PyUnicode_FromStringAndSize
223 #define PYGLIB_PyUnicode_FromFormat PyUnicode_FromFormat
224 #define PYGLIB_PyUnicode_GET_SIZE PyUnicode_GET_SIZE
225 #define PYGLIB_PyUnicode_Resize PyUnicode_Resize
226 #define PYGLIB_PyUnicode_Type PyUnicode_Type
227 #define PYGLIB_PyLong_Check PyLong_Check
228 #define PYGLIB_PyLong_FromLong PyLong_FromLong
229 #define PYGLIB_PyLong_AsLong PyLong_AsLong
230 #define PYGLIB_PyLong_AS_LONG(o) PyLong_AS_LONG((PyObject*)(o))
231 #define PYGLIB_PyLongObject PyLongObject
232 #define PYGLIB_PyLong_Type PyLong_Type
233
234 #define PYGLIB_PyBytes_FromString PyBytes_FromString
235 #define PYGLIB_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
236 #define PYGLIB_PyBytes_Resize(o, len) _PyBytes_Resize(o, len)
237 #define PYGLIB_PyBytes_AsString PyBytes_AsString
238 #define PYGLIB_PyBytes_Size PyBytes_Size
239 #define PYGLIB_PyBytes_Check PyBytes_Check
240
241 #define PYGLIB_PyNumber_Long PyNumber_Long
242
243 #endif
244
245 #endif /* __PYGLIB_PYTHON_COMPAT_H__ */