Debian Codegraph @

cext.h

raw symbols SCIP
1 /*
2 * Copyright 2006 - 2025
3 * Andr\xe9 Malo or his licensors, as applicable
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * central naming stuff
20 */
21
22 #ifndef CEXT_H
23 #define CEXT_H
24
25 #define LCOV_EXCL_LINE(x) (x)
26 #define LCOV_EXCL_LINE_GOTO(x) goto x
27 #define LCOV_EXCL_LINE_RETURN(x) return (x)
28 #define LCOV_EXCL_START
29 #define LCOV_EXCL_STOP
30
31 #ifndef EXT_MODULE
32 #error EXT_MODULE must be defined outside of this file (-DEXT_MODULE=...)
33 #endif
34
35 /*
36 * include core header files
37 */
38 #define PY_SSIZE_T_CLEAN
39
40 #include "Python.h"
41 #include "structmember.h"
42
43 /*
44 * define our helper macros depending on the stuff above
45 */
46 #define STRINGIFY(n) STRINGIFY_HELPER(n)
47 #define STRINGIFY_HELPER(n) #n
48 #define CONCATENATE(first, second) CONCATENATE_HELPER(first, second)
49 #define CONCATENATE_HELPER(first, second) first##second
50
51 #define EXT_MODULE_NAME STRINGIFY(EXT_MODULE)
52 #ifdef EXT_PACKAGE
53 #define EXT_PACKAGE_NAME STRINGIFY(EXT_PACKAGE)
54 #define EXT_MODULE_PATH EXT_PACKAGE_NAME "." EXT_MODULE_NAME
55 #else
56 #define EXT_PACKAGE_NAME ""
57 #define EXT_MODULE_PATH EXT_MODULE_NAME
58 #endif
59
60 #define EXT_DOCS_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, __doc__))
61 #define EXT_METHODS_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, _methods))
62 #define EXT_METHODS static PyMethodDef EXT_METHODS_VAR[]
63
64 #define EXT_DEFINE_VAR CONCATENATE(var, CONCATENATE(EXT_MODULE, _module))
65
66 /* GCC 8 doesn't like the generic (PyCFunction) cast */
67 #define EXT_CFUNC(func) ((PyCFunction)(void (*) (void))(func))
68
69 /*
70 * Link helpers
71 */
72 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
73 #define EXT_LOCAL __attribute__((visibility("hidden")))
74 #else
75 #define EXT_LOCAL
76 #endif
77
78
79 /*
80 * Py-version specific support
81 */
82
83 /* Py3K */
84 #if PY_MAJOR_VERSION >= 3
85
86 #if PY_VERSION_HEX >= 0x03040000
87 #define TEXT_SIGNATURE
88 #endif
89
90 #define EXT3
91
92 #ifndef Py_TPFLAGS_HAVE_CLASS
93 #define Py_TPFLAGS_HAVE_CLASS (0)
94 #endif
95
96 #ifndef Py_TPFLAGS_HAVE_WEAKREFS
97 #define Py_TPFLAGS_HAVE_WEAKREFS (0)
98 #endif
99
100 #ifndef Py_TPFLAGS_HAVE_ITER
101 #define Py_TPFLAGS_HAVE_ITER (0)
102 #endif
103
104 #ifndef Py_TPFLAGS_HAVE_RICHCOMPARE
105 #define Py_TPFLAGS_HAVE_RICHCOMPARE (0)
106 #endif
107
108 #ifndef Py_TPFLAGS_HAVE_SEQUENCE_IN
109 #define Py_TPFLAGS_HAVE_SEQUENCE_IN (0)
110 #endif
111
112 #ifndef PyMODINIT_FUNC
113 #define EXT_INIT_FUNC PyObject *CONCATENATE(PyInit_, EXT_MODULE)(void)
114 #else
115 #define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(PyInit_, EXT_MODULE)(void)
116 #endif
117
118 #define EXT_DEFINE(name, methods, doc) \
119 static struct PyModuleDef EXT_DEFINE_VAR = { \
120 PyModuleDef_HEAD_INIT, \
121 name, \
122 doc, \
123 -1, \
124 methods, \
125 NULL, \
126 NULL, \
127 NULL, \
128 NULL \
129 }
130
131 #define EXT_CREATE(def) (PyModule_Create(def))
132 #define EXT_INIT_ERROR(module) do {Py_XDECREF(module); return NULL;} while(0)
133 #define EXT_INIT_RETURN(module) return module
134
135 #define EXT_DOC_UNICODE(m)
136
137 #else /* end py3k */
138
139 #define EXT2
140
141 /*
142 * Py3 Object defintions
143 */
144 #ifndef PyVarObject_HEAD_INIT
145 #define PyVarObject_HEAD_INIT(type, size) \
146 PyObject_HEAD_INIT(type) size,
147 #endif
148
149 #ifndef PyMODINIT_FUNC
150 #define EXT_INIT_FUNC void CONCATENATE(init, EXT_MODULE)(void)
151 #else
152 #define EXT_INIT_FUNC PyMODINIT_FUNC CONCATENATE(init, EXT_MODULE)(void)
153 #endif
154
155 #define EXT_DEFINE__STRUCT \
156 CONCATENATE(struct, CONCATENATE(EXT_MODULE, _module))
157
158 struct EXT_DEFINE__STRUCT {
159 char *m_name;
160 char *m_doc;
161 PyMethodDef *m_methods;
162 };
163 #define EXT_DEFINE(name, methods, doc) \
164 static struct EXT_DEFINE__STRUCT EXT_DEFINE_VAR = { \
165 name, \
166 doc, \
167 methods \
168 }
169
170 #define EXT_CREATE(def) ((def)->m_doc \
171 ? Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc) \
172 : Py_InitModule((def)->m_name, (def)->m_methods) \
173 )
174 #define EXT_INIT_ERROR(module) return
175 #define EXT_INIT_RETURN(module) return
176
177 #define EXT_DOC_UNICODE(m) do { \
178 PyObject *doc__, *uni__; \
179 int res__; \
180 \
181 if ((doc__ = PyObject_GetAttrString(m, "__doc__"))) { \
182 uni__ = PyUnicode_FromEncodedObject(doc__, "utf-8", "strict"); \
183 Py_DECREF(doc__); \
184 if (!uni__) \
185 EXT_INIT_ERROR(m); \
186 res__ = PyObject_SetAttrString(m, "__doc__", uni__); \
187 Py_DECREF(uni__); \
188 if (res__ == -1) \
189 EXT_INIT_ERROR(m); \
190 } \
191 else if (!(PyErr_Occurred() \
192 && PyErr_ExceptionMatches(PyExc_AttributeError))) \
193 EXT_INIT_ERROR(m); \
194 } while(0)
195
196 #endif /* end py2K */
197
198
199 /*
200 * Fake py 3.3 Unicode APIs
201 */
202 #ifndef PyUnicode_GET_LENGTH
203
204 #ifdef EXT_PEP393
205 #undef EXT_PEP393
206 #endif
207
208 #define EXT_UNI_CP Py_UNICODE
209 enum PyUnicode_Kind {
210 /* String contains only wstr byte characters. This is only possible
211 when the string was created with a legacy API and _PyUnicode_Ready()
212 has not been called yet. */
213 PyUnicode_WCHAR_KIND = 0,
214 /* Return values of the PyUnicode_KIND() macro: */
215 PyUnicode_1BYTE_KIND = 1,
216 PyUnicode_2BYTE_KIND = 2,
217 PyUnicode_4BYTE_KIND = 4
218 };
219
220 #define EXT_UNI_KIND_DECL(var)
221 #define EXT_UNI_KIND_SET(var, value)
222
223 #if Py_UNICODE_SIZE == 2
224 #define PyUnicode_KIND(x) (2)
225 #elif Py_UNICODE_SIZE == 4
226 #define PyUnicode_KIND(x) (4)
227 #else
228 #error "Cannot recognize sizeof(Py_UNICODE)"
229 #endif
230
231 #define PyUnicode_GET_LENGTH(x) (PyUnicode_GET_SIZE(x))
232 #define PyUnicode_DATA(x) ((void *)PyUnicode_AS_UNICODE(x))
233 #define PyUnicode_READ(kind, data, index) \
234 ((Py_UNICODE) (((const Py_UNICODE *)(data))[(index)]))
235 #define PyUnicode_WRITE(kind, data, index, value) \
236 do { \
237 ((Py_UNICODE *)(data))[(index)] = (Py_UNICODE)(value); \
238 } while(0)
239
240 #define PyUnicode_FromKindAndData(kind, data, length) \
241 (PyUnicode_FromUnicode((Py_UNICODE *)data, length))
242 #define EXT_UNI_MAX_DECL(var)
243 #define EXT_UNI_MAX_SET(var, value)
244 #define EXT_UNI_MAX_LEVEL(var, add)
245 #define PyUnicode_New(size, maxchar) PyUnicode_FromUnicode(NULL, size)
246
247 #else
248
249 #ifndef EXT_PEP393
250 #define EXT_PEP393
251 #endif
252
253 #define EXT_UNI_CP Py_UCS4
254
255 #define EXT_UNI_KIND_DECL(var) enum PyUnicode_Kind var;
256 #define EXT_UNI_KIND_SET(var, value) var = (value);
257
258 #define EXT_UNI_MAX_DECL(var) Py_UCS4 var;
259 #define EXT_UNI_MAX_SET(var, value) (var) = (value);
260 #define EXT_UNI_MAX_LEVEL(var, add) do { \
261 if ((add) > (var)) (var) = (add); \
262 } while(0)
263
264 #endif
265
266 #define U(c) ((EXT_UNI_CP)(c))
267
268
269 /*
270 * Module init tools
271 */
272 #define EXT_INIT_TYPE(module, type) do { \
273 if (PyType_Ready(type) < 0) \
274 EXT_INIT_ERROR(module); \
275 } while (0)
276
277 #define EXT_ADD_TYPE(module, name, type) do { \
278 Py_INCREF(type); \
279 if (PyModule_AddObject(module, name, (PyObject *)(type)) < 0) \
280 EXT_INIT_ERROR(module); \
281 } while (0)
282
283 #define EXT_ADD_UNICODE(module, name, string, encoding) do { \
284 if (PyModule_AddObject( \
285 module, \
286 name, \
287 PyUnicode_Decode( \
288 string, \
289 sizeof(string) - 1, \
290 encoding, \
291 "strict" \
292 )) < 0) \
293 EXT_INIT_ERROR(module); \
294 } while (0)
295
296 #define EXT_ADD_STRING(module, name, string) do { \
297 if (PyModule_AddStringConstant(module, name, string) < 0) \
298 EXT_INIT_ERROR(module); \
299 } while (0)
300
301 #define EXT_ADD_INT(module, name, number) do { \
302 if (PyModule_AddIntConstant(module, name, number) < 0) \
303 EXT_INIT_ERROR(module); \
304 } while (0)
305
306
307 /* PEP 353 support, implemented as of python 2.5 */
308 #if PY_VERSION_HEX < 0x02050000
309 typedef int Py_ssize_t;
310 #define PyInt_FromSsize_t(arg) PyInt_FromLong((long)arg)
311 #define PyInt_AsSsize_t(arg) (int)PyInt_AsLong(arg)
312 #define PY_SSIZE_T_MAX ((Py_ssize_t)INT_MAX)
313 #endif
314
315 /*
316 * some helper macros (Python 2.4)
317 */
318 #ifndef Py_VISIT
319 #define Py_VISIT(op) do { \
320 if (op) { \
321 int vret = visit((op), arg); \
322 if (vret) return vret; \
323 } \
324 } while (0)
325 #endif
326
327 #ifdef Py_CLEAR
328 #undef Py_CLEAR
329 #endif
330 #define Py_CLEAR(op) do { \
331 if (op) { \
332 PyObject *tmp__ = (PyObject *)(op); \
333 (op) = NULL; \
334 Py_DECREF(tmp__); \
335 } \
336 } while (0)
337
338 #ifndef Py_RETURN_NONE
339 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
340 #endif
341
342 #ifndef Py_RETURN_FALSE
343 #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
344 #endif
345
346 #ifndef Py_RETURN_TRUE
347 #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
348 #endif
349
350 /* Macros for inline documentation. (Python 2.3) */
351 #ifndef PyDoc_VAR
352 #define PyDoc_VAR(name) static char name[]
353 #endif
354
355 #ifndef PyDoc_STRVAR
356 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
357 #endif
358
359 #ifndef PyDoc_STR
360 #ifdef WITH_DOC_STRINGS
361 #define PyDoc_STR(str) str
362 #else
363 #define PyDoc_STR(str) ""
364 #endif
365 #endif
366
367 /* Basestring check (basestring introduced in Python 2.3) */
368 #if PY_VERSION_HEX < 0x02030000
369 #define BaseString_Check(type) ( \
370 PyObject_TypeCheck((type), &PyString_Type) \
371 || PyObject_TypeCheck((type), &PyUnicode_Type) \
372 )
373 #else
374 #define BaseString_Check(type) PyObject_TypeCheck((type), &PyBaseString_Type)
375 #endif
376
377 #define GENERIC_ALLOC(type) \
378 ((void *)((PyTypeObject *)type)->tp_alloc(type, (Py_ssize_t)0))
379
380 /* PyPy doesn't define it */
381 #ifndef PyType_IS_GC
382 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
383 #endif
384
385 #ifndef Py_TYPE
386 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
387 #endif
388
389 #define DEFINE_GENERIC_DEALLOC(prefix) \
390 static void prefix##_dealloc(void *self) \
391 { \
392 if (PyType_IS_GC(Py_TYPE(self))) \
393 PyObject_GC_UnTrack(self); \
394 (void)prefix##_clear(self); \
395 (Py_TYPE(self))->tp_free((PyObject *)self); \
396 }
397
398 #endif /* CEXT_H */