1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pyglib - Python bindings for GLib toolkit.
3 * Copyright (C) 2008 Johan Dahlin
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.
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.
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
21 #ifndef __PYGLIB_PYTHON_COMPAT_H__
22 #define __PYGLIB_PYTHON_COMPAT_H__
24 /* Python 2.3 does not define Py_CLEAR */
26 #define Py_CLEAR(op) \
29 PyObject *tmp = (PyObject *)(op); \
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.
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.
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)
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)
64 #if PY_VERSION_HEX < 0x03000000
66 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
67 static int _pyglib_init_##modname(PyObject *module); \
68 void init##modname(void) \
70 PyObject *module = Py_InitModule(fullpkgname, functions); \
71 _pyglib_init_##modname(module); \
73 static int _pyglib_init_##modname(PyObject *module)
77 #define PYGLIB_INIT_FUNCTION(modname, fullpkgname, functions) \
78 static struct PyModuleDef _##modname##module = { \
79 PyModuleDef_HEAD_INIT, \
89 static int _pyglib_init_##modname(PyObject *module); \
90 PyObject *PyInit_##modname(void) \
92 PyObject *module = PyModule_Create(&_##modname##module); \
95 if (_pyglib_init_##modname(module) != 0 ) {\
101 static int _pyglib_init_##modname(PyObject *module)
105 /* Compilation on Python 2.x */
106 #if PY_VERSION_HEX < 0x03000000
107 #define PYGLIB_MODULE_ERROR_RETURN
111 #define PYGLIB_PyBaseString_Check(ob) (PyString_Check(ob) || PyUnicode_Check(ob))
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
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
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)
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))
145 #define PYGLIB_PyNumber_Long PyNumber_Int
147 #ifndef PyVarObject_HEAD_INIT
148 #define PyVarObject_HEAD_INIT(base, size) \
149 PyObject_HEAD_INIT(base) \
153 #define PYGLIB_MODULE_START(symbol, modname) \
154 DL_EXPORT(void) init##symbol(void); \
155 DL_EXPORT(void) init##symbol(void) \
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) \
168 #define PYGLIB_REGISTER_TYPE(d, type, name) \
169 if (!type.tp_alloc) \
170 type.tp_alloc = PyType_GenericAlloc; \
172 type.tp_new = PyType_GenericNew; \
173 if (PyType_Ready(&type)) \
175 PyDict_SetItemString(d, name, (PyObject *)&type);
179 #define PYGLIB_MODULE_ERROR_RETURN 0
181 #define PYGLIB_MODULE_START(symbol, modname) \
182 static struct PyModuleDef _##symbol##module = { \
183 PyModuleDef_HEAD_INIT, \
187 symbol##_functions, \
193 PyMODINIT_FUNC PyInit_##symbol(void); \
194 PyMODINIT_FUNC PyInit_##symbol(void) \
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) \
205 #define PYGLIB_REGISTER_TYPE(d, type, name) \
206 if (!type.tp_alloc) \
207 type.tp_alloc = PyType_GenericAlloc; \
209 type.tp_new = PyType_GenericNew; \
210 if (PyType_Ready(&type)) \
212 PyDict_SetItemString(d, name, (PyObject *)&type);
214 #define PYGLIB_PyBaseString_Check PyUnicode_Check
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
233 #define PYGLIB_PyLong_FromUnsignedLong PyLong_FromUnsignedLong
234 #define PYGLIB_PyLong_AsUnsignedLong(o) PyLong_AsUnsignedLongMask((PyObject*)(o))
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
243 #define PYGLIB_PyNumber_Long PyNumber_Long
247 #endif /* __PYGLIB_PYTHON_COMPAT_H__ */