Imported Upstream version 3.7.91.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 /* PyCObject superceded by PyCapsule on Python >= 2.7
37  * However since this effects header files used by
38  * static bindings we are only applying the change to
39  * Python 3.x where we don't support the static bindings.
40  * 3.2 removed PyCObject so we don't have any choice here.
41  *
42  * There is talk upstream of undeprecating PyCObject
43  * (at least where the 2.x branch is concerned)
44  * and there is no danger of it being remove from 2.7.
45  **/
46 #if PY_VERSION_HEX >= 0x03000000
47 # define PYGLIB_CPointer_Check PyCapsule_CheckExact
48 # define PYGLIB_CPointer_WrapPointer(ptr, typename) \
49     PyCapsule_New(ptr, typename, NULL)
50 # define PYGLIB_CPointer_GetPointer(obj, typename) \
51     PyCapsule_GetPointer(obj, typename)
52 # define PYGLIB_CPointer_Import(module, symbol) \
53     PyCapsule_Import(##module##.##symbol##, FALSE)
54 #else
55 # define PYGLIB_CPointer_Check PyCObject_Check
56 # define PYGLIB_CPointer_WrapPointer(ptr, typename) \
57     PyCObject_FromVoidPtr(ptr, NULL)
58 # define PYGLIB_CPointer_GetPointer(obj, typename) \
59   PyCObject_AsVoidPtr(obj)
60 # define PYGLIB_CPointer_Import(module, symbol) \
61     PyCObject_Import(module, symbol)
62 #endif
63
64 #if PY_VERSION_HEX < 0x03000000
65
66 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
67 static int _pyglib_init_##modname(PyObject *module); \
68 void init##modname(void) \
69 { \
70     PyObject *module = Py_InitModule(fullpkgname, functions); \
71     _pyglib_init_##modname(module); \
72 } \
73 static int _pyglib_init_##modname(PyObject *module)
74
75 #else
76
77 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
78 static struct PyModuleDef _##modname##module = {     \
79     PyModuleDef_HEAD_INIT,                              \
80     fullpkgname,                                        \
81     NULL,                                               \
82     -1,                                                 \
83     functions,                                          \
84     NULL,                                               \
85     NULL,                                               \
86     NULL,                                               \
87     NULL                                                \
88 };                                                      \
89 static int _pyglib_init_##modname(PyObject *module); \
90 PyObject *PyInit_##modname(void) \
91 { \
92     PyObject *module = PyModule_Create(&_##modname##module);  \
93     if (module == NULL) \
94         return NULL; \
95     if (_pyglib_init_##modname(module) != 0 ) {\
96         Py_DECREF(module); \
97         return NULL; \
98     } \
99     return module; \
100 } \
101 static int _pyglib_init_##modname(PyObject *module)
102
103 #endif
104
105 /* Compilation on Python 2.x */
106 #if PY_VERSION_HEX < 0x03000000
107 #define PYGLIB_MODULE_ERROR_RETURN
108
109 #define RO READONLY
110
111 #define PYGLIB_PyBaseString_Check(ob) (PyString_Check(ob) || PyUnicode_Check(ob))
112
113 #define PYGLIB_PyUnicode_Check PyString_Check
114 #define PYGLIB_PyUnicode_AsString PyString_AsString
115 #define PYGLIB_PyUnicode_AsStringAndSize PyString_AsStringAndSize
116 #define PYGLIB_PyUnicode_FromString PyString_FromString
117 #define PYGLIB_PyUnicode_FromStringAndSize PyString_FromStringAndSize
118 #define PYGLIB_PyUnicode_FromFormat PyString_FromFormat
119 #define PYGLIB_PyUnicode_AS_STRING PyString_AS_STRING
120 #define PYGLIB_PyUnicode_GET_SIZE PyString_GET_SIZE
121 #define PYGLIB_PyUnicode_Type PyString_Type
122
123 #define PYGLIB_PyBytes_FromString PyString_FromString
124 #define PYGLIB_PyBytes_FromStringAndSize PyString_FromStringAndSize
125 #define PYGLIB_PyBytes_Resize _PyString_Resize
126 #define PYGLIB_PyBytes_AsString PyString_AsString
127 #define PYGLIB_PyBytes_Size PyString_Size
128 #define PYGLIB_PyBytes_Check PyString_Check
129
130 #define PYGLIB_PyLong_Check PyInt_Check
131 #define PYGLIB_PyLong_FromLong PyInt_FromLong
132 #define PYGLIB_PyLong_FromSsize_t PyInt_FromSsize_t
133 #define PYGLIB_PyLong_FromSize_t PyInt_FromSize_t
134 #define PYGLIB_PyLong_AsLong  PyInt_AsLong
135 #define PYGLIB_PyLongObject PyIntObject
136 #define PYGLIB_PyLong_Type PyInt_Type
137 #define PYGLIB_PyLong_AS_LONG PyInt_AS_LONG
138 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
139
140 /* Python 2.7 lacks a PyInt_FromUnsignedLong function; use signed longs, and
141  * rely on PyInt_AsUnsignedLong() to interpret them correctly */
142 #define PYGLIB_PyLong_FromUnsignedLong PyInt_FromLong
143 #define PYGLIB_PyLong_AsUnsignedLong(o) PyInt_AsUnsignedLongMask((PyObject*)(o))
144
145 #define PYGLIB_PyNumber_Long PyNumber_Int
146
147 #ifndef PyVarObject_HEAD_INIT
148 #define PyVarObject_HEAD_INIT(base, size) \
149   PyObject_HEAD_INIT(base) \
150   size,
151 #endif
152
153 #define PYGLIB_MODULE_START(symbol, modname)            \
154 DL_EXPORT(void) init##symbol(void); \
155 DL_EXPORT(void) init##symbol(void)                      \
156 {                                                       \
157     PyObject *module;                                   \
158     module = Py_InitModule(modname, symbol##_functions);
159 #define PYGLIB_MODULE_END }
160 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)   \
161 PyTypeObject symbol = {                                 \
162     PyObject_HEAD_INIT(NULL)                            \
163     0,                                                  \
164     typename,                                           \
165     sizeof(csymbol),                                    \
166     0,                                                  \
167 };
168 #define PYGLIB_REGISTER_TYPE(d, type, name)             \
169     if (!type.tp_alloc)                                 \
170         type.tp_alloc = PyType_GenericAlloc;            \
171     if (!type.tp_new)                                   \
172         type.tp_new = PyType_GenericNew;                \
173     if (PyType_Ready(&type))                            \
174         return;                                         \
175     PyDict_SetItemString(d, name, (PyObject *)&type);
176
177 #else
178
179 #define PYGLIB_MODULE_ERROR_RETURN 0
180
181 #define PYGLIB_MODULE_START(symbol, modname)            \
182     static struct PyModuleDef _##symbol##module = {     \
183     PyModuleDef_HEAD_INIT,                              \
184     modname,                                            \
185     NULL,                                               \
186     -1,                                                 \
187     symbol##_functions,                                 \
188     NULL,                                               \
189     NULL,                                               \
190     NULL,                                               \
191     NULL                                                \
192 };                                                      \
193 PyMODINIT_FUNC PyInit_##symbol(void);                   \
194 PyMODINIT_FUNC PyInit_##symbol(void)                    \
195 {                                                       \
196     PyObject *module;                                   \
197     module = PyModule_Create(&_##symbol##module);
198 #define PYGLIB_MODULE_END return module; }
199 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)   \
200 PyTypeObject symbol = {                                 \
201     PyVarObject_HEAD_INIT(NULL, 0)                      \
202     typename,                                           \
203     sizeof(csymbol)                                     \
204 };
205 #define PYGLIB_REGISTER_TYPE(d, type, name)                 \
206     if (!type.tp_alloc)                                 \
207             type.tp_alloc = PyType_GenericAlloc;            \
208     if (!type.tp_new)                                   \
209             type.tp_new = PyType_GenericNew;                \
210     if (PyType_Ready(&type))                            \
211             return;                                         \
212     PyDict_SetItemString(d, name, (PyObject *)&type);
213
214 #define PYGLIB_PyBaseString_Check PyUnicode_Check
215
216 #define PYGLIB_PyUnicode_Check PyUnicode_Check
217 #define PYGLIB_PyUnicode_AsString _PyUnicode_AsString
218 #define PYGLIB_PyUnicode_AsStringAndSize(obj, buf, size) \
219     (((*(buf) = _PyUnicode_AsStringAndSize(obj, size)) != NULL) ? 0 : -1) 
220 #define PYGLIB_PyUnicode_FromString PyUnicode_FromString
221 #define PYGLIB_PyUnicode_FromStringAndSize PyUnicode_FromStringAndSize
222 #define PYGLIB_PyUnicode_FromFormat PyUnicode_FromFormat
223 #define PYGLIB_PyUnicode_GET_SIZE PyUnicode_GET_SIZE
224 #define PYGLIB_PyUnicode_Resize PyUnicode_Resize
225 #define PYGLIB_PyUnicode_Type PyUnicode_Type
226 #define PYGLIB_PyLong_Check PyLong_Check
227 #define PYGLIB_PyLong_FromLong PyLong_FromLong
228 #define PYGLIB_PyLong_AsLong PyLong_AsLong
229 #define PYGLIB_PyLong_AS_LONG(o) PyLong_AS_LONG((PyObject*)(o))
230 #define PYGLIB_PyLongObject PyLongObject
231 #define PYGLIB_PyLong_Type PyLong_Type
232
233 #define PYGLIB_PyLong_FromUnsignedLong PyLong_FromUnsignedLong
234 #define PYGLIB_PyLong_AsUnsignedLong(o) PyLong_AsUnsignedLongMask((PyObject*)(o))
235
236 #define PYGLIB_PyBytes_FromString PyBytes_FromString
237 #define PYGLIB_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
238 #define PYGLIB_PyBytes_Resize(o, len) _PyBytes_Resize(o, len)
239 #define PYGLIB_PyBytes_AsString PyBytes_AsString
240 #define PYGLIB_PyBytes_Size PyBytes_Size
241 #define PYGLIB_PyBytes_Check PyBytes_Check
242
243 #define PYGLIB_PyNumber_Long PyNumber_Long
244
245 #endif
246
247 #endif /* __PYGLIB_PYTHON_COMPAT_H__ */