release 3.27.0
[pygobject.git] / gi / pyglib-python-compat.h
blobd6f7553f7cd1244e6333d46a2c3ffd572b9df39b
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, see <http://www.gnu.org/licenses/>.
19 #ifndef __PYGLIB_PYTHON_COMPAT_H__
20 #define __PYGLIB_PYTHON_COMPAT_H__
22 # define PYGLIB_CPointer_Check PyCapsule_CheckExact
23 # define PYGLIB_CPointer_WrapPointer(ptr, typename) \
24 PyCapsule_New(ptr, typename, NULL)
25 # define PYGLIB_CPointer_GetPointer(obj, typename) \
26 PyCapsule_GetPointer(obj, typename)
27 # define PYGLIB_CPointer_Import(module, symbol) \
28 PyCapsule_Import(##module##.##symbol##, FALSE)
31 #define PYGLIB_MODULE_ERROR_RETURN NULL
33 /* Compilation on Python 2.x */
34 #if PY_VERSION_HEX < 0x03000000
36 #define RO READONLY
38 #define PYGLIB_PyBaseString_Check(ob) (PyString_Check(ob) || PyUnicode_Check(ob))
40 #define PYGLIB_PyUnicode_Check PyString_Check
41 #define PYGLIB_PyUnicode_AsString PyString_AsString
42 #define PYGLIB_PyUnicode_AsStringAndSize PyString_AsStringAndSize
43 #define PYGLIB_PyUnicode_FromString PyString_FromString
44 #define PYGLIB_PyUnicode_FromStringAndSize PyString_FromStringAndSize
45 #define PYGLIB_PyUnicode_FromFormat PyString_FromFormat
46 #define PYGLIB_PyUnicode_AS_STRING PyString_AS_STRING
47 #define PYGLIB_PyUnicode_GET_SIZE PyString_GET_SIZE
48 #define PYGLIB_PyUnicode_Type PyString_Type
49 #define PYGLIB_PyUnicode_InternFromString PyString_InternFromString
50 #define PYGLIB_PyUnicode_InternInPlace PyString_InternInPlace
51 #define PYGLIB_PyUnicode_Format PyString_Format
53 #define PYGLIB_PyBytes_FromString PyString_FromString
54 #define PYGLIB_PyBytes_FromStringAndSize PyString_FromStringAndSize
55 #define PYGLIB_PyBytes_Resize _PyString_Resize
56 #define PYGLIB_PyBytes_AsString PyString_AsString
57 #define PYGLIB_PyBytes_AsStringAndSize PyString_AsStringAndSize
58 #define PYGLIB_PyBytes_Size PyString_Size
59 #define PYGLIB_PyBytes_Check PyString_Check
61 #define PYGLIB_PyLong_Check PyInt_Check
62 #define PYGLIB_PyLong_FromLong PyInt_FromLong
63 #define PYGLIB_PyLong_FromSsize_t PyInt_FromSsize_t
64 #define PYGLIB_PyLong_FromSize_t PyInt_FromSize_t
65 #define PYGLIB_PyLong_AsLong PyInt_AsLong
66 #define PYGLIB_PyLong_AsSsize_t PyInt_AsSsize_t
67 #define PYGLIB_PyLongObject PyIntObject
68 #define PYGLIB_PyLong_Type PyInt_Type
69 #define PYGLIB_PyLong_AS_LONG PyInt_AS_LONG
70 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
72 #define PYGLIB_Py_hash_t long
74 /* Python 2.7 lacks a PyInt_FromUnsignedLong function; use signed longs, and
75 * rely on PyInt_AsUnsignedLong() to interpret them correctly */
76 #define PYGLIB_PyLong_FromUnsignedLong PyInt_FromLong
77 #define PYGLIB_PyLong_AsUnsignedLong(o) PyInt_AsUnsignedLongMask((PyObject*)(o))
79 #define PYGLIB_PyNumber_Long PyNumber_Int
81 #ifndef PyVarObject_HEAD_INIT
82 #define PyVarObject_HEAD_INIT(base, size) \
83 PyObject_HEAD_INIT(base) \
84 size,
85 #endif
87 #define PYGLIB_MODULE_START(symbol, modname) \
88 PyObject * pyglib_##symbol##_module_create(void); \
89 DL_EXPORT(void) init##symbol(void); \
90 DL_EXPORT(void) init##symbol(void) { \
91 pyglib_##symbol##_module_create(); \
92 }; \
93 PyObject * pyglib_##symbol##_module_create(void) \
94 { \
95 PyObject *module; \
96 module = Py_InitModule(modname, symbol##_functions);
98 #define PYGLIB_MODULE_END return module; }
100 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol) \
101 PyTypeObject symbol = { \
102 PyObject_HEAD_INIT(NULL) \
103 0, \
104 typename, \
105 sizeof(csymbol), \
106 0, \
109 #define PYGLIB_REGISTER_TYPE(d, type, name) \
110 if (!type.tp_alloc) \
111 type.tp_alloc = PyType_GenericAlloc; \
112 if (!type.tp_new) \
113 type.tp_new = PyType_GenericNew; \
114 if (PyType_Ready(&type)) \
115 return; \
116 PyDict_SetItemString(d, name, (PyObject *)&type);
118 #else
120 #define PYGLIB_MODULE_START(symbol, modname) \
121 static struct PyModuleDef _##symbol##module = { \
122 PyModuleDef_HEAD_INIT, \
123 modname, \
124 NULL, \
125 -1, \
126 symbol##_functions, \
127 NULL, \
128 NULL, \
129 NULL, \
130 NULL \
131 }; \
132 PyObject * pyglib_##symbol##_module_create(void); \
133 PyMODINIT_FUNC PyInit_##symbol(void); \
134 PyMODINIT_FUNC PyInit_##symbol(void) { \
135 return pyglib_##symbol##_module_create(); \
136 }; \
137 PyObject * pyglib_##symbol##_module_create(void) \
139 PyObject *module; \
140 module = PyModule_Create(&_##symbol##module);
142 #define PYGLIB_MODULE_END return module; }
144 #define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol) \
145 PyTypeObject symbol = { \
146 PyVarObject_HEAD_INIT(NULL, 0) \
147 typename, \
148 sizeof(csymbol) \
151 #define PYGLIB_REGISTER_TYPE(d, type, name) \
152 if (!type.tp_alloc) \
153 type.tp_alloc = PyType_GenericAlloc; \
154 if (!type.tp_new) \
155 type.tp_new = PyType_GenericNew; \
156 if (PyType_Ready(&type)) \
157 return; \
158 PyDict_SetItemString(d, name, (PyObject *)&type);
160 #define PYGLIB_PyBaseString_Check PyUnicode_Check
162 #define PYGLIB_PyUnicode_Check PyUnicode_Check
163 #define PYGLIB_PyUnicode_AsString _PyUnicode_AsString
164 #define PYGLIB_PyUnicode_AsStringAndSize(obj, buf, size) \
165 (((*(buf) = _PyUnicode_AsStringAndSize(obj, size)) != NULL) ? 0 : -1)
166 #define PYGLIB_PyUnicode_FromString PyUnicode_FromString
167 #define PYGLIB_PyUnicode_FromStringAndSize PyUnicode_FromStringAndSize
168 #define PYGLIB_PyUnicode_FromFormat PyUnicode_FromFormat
169 #define PYGLIB_PyUnicode_GET_SIZE PyUnicode_GET_SIZE
170 #define PYGLIB_PyUnicode_Resize PyUnicode_Resize
171 #define PYGLIB_PyUnicode_Type PyUnicode_Type
172 #define PYGLIB_PyUnicode_InternFromString PyUnicode_InternFromString
173 #define PYGLIB_PyUnicode_InternInPlace PyUnicode_InternInPlace
174 #define PYGLIB_PyUnicode_Format PyUnicode_Format
176 #define PYGLIB_PyLong_Check PyLong_Check
177 #define PYGLIB_PyLong_FromLong PyLong_FromLong
178 #define PYGLIB_PyLong_FromSsize_t PyLong_FromSsize_t
179 #define PYGLIB_PyLong_FromSize_t PyLong_FromSize_t
180 #define PYGLIB_PyLong_AsLong PyLong_AsLong
181 #define PYGLIB_PyLong_AsSsize_t PyLong_AsSsize_t
182 #define PYGLIB_PyLong_AS_LONG(o) PyLong_AS_LONG((PyObject*)(o))
183 #define PYGLIB_PyLongObject PyLongObject
184 #define PYGLIB_PyLong_Type PyLong_Type
186 #define PYGLIB_PyLong_FromUnsignedLong PyLong_FromUnsignedLong
187 #define PYGLIB_PyLong_AsUnsignedLong(o) PyLong_AsUnsignedLongMask((PyObject*)(o))
189 #define PYGLIB_PyBytes_FromString PyBytes_FromString
190 #define PYGLIB_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
191 #define PYGLIB_PyBytes_Resize(o, len) _PyBytes_Resize(o, len)
192 #define PYGLIB_PyBytes_AsString PyBytes_AsString
193 #define PYGLIB_PyBytes_AsStringAndSize PyBytes_AsStringAndSize
194 #define PYGLIB_PyBytes_Size PyBytes_Size
195 #define PYGLIB_PyBytes_Check PyBytes_Check
197 #define PYGLIB_PyNumber_Long PyNumber_Long
199 #define PYGLIB_Py_hash_t Py_hash_t
201 #endif
203 #define PYGLIB_Py_hash_t_FromVoidPtr(ptr) ((PYGLIB_Py_hash_t)(gintptr)(ptr))
205 #endif /* __PYGLIB_PYTHON_COMPAT_H__ */