@@@ cython and python3
[mLib-python] / grim.h
1 /* -*-c-*-
2 *
3 * Grim hacks to support the Pyrex code
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Python interface to mLib.
11 *
12 * mLib/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * mLib/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with mLib/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #ifndef GRIM_H
28 #define GRIM_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include <Python.h>
37
38 /*----- Utilities ---------------------------------------------------------*/
39
40 #define PSIZEOF(x) sizeof(*x)
41 typedef const void *cvp;
42
43 #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
44 #define RETURN_ME RETURN_OBJ(me)
45 #define RETURN_NONE RETURN_OBJ(Py_None)
46 #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
47
48 #define METH(func, doc) \
49 { #func, METHNAME(func), METH_VARARGS, doc },
50 #define KWMETH(func, doc) \
51 { #func, (PyCFunction)METHNAME(func), \
52 METH_VARARGS | METH_KEYWORDS, doc },
53
54 #define GET(func, doc) \
55 { #func, GETSETNAME(get, func), 0, doc },
56 #define GETSET(func, doc) \
57 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
58
59 #ifndef PySequence_Fast_ITEMS
60 # define PySequence_Fast_ITEMS(o) \
61 (PyList_Check(o) ? &PyList_GET_ITEM(o, 0) : &PyTuple_GET_ITEM(o, 0))
62 #endif
63
64 #define FREEOBJ(obj) \
65 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
66
67 #define KWLIST ((char **)kwlist)
68
69 /*----- Compatibility hacks -----------------------------------------------*/
70 #if PY_VERSION_HEX >= 0x03000000
71
72 /*#define PyInt_Check PyLong_Check*/
73 #define PyInt_FromLong PyLong_FromLong
74 #define PyInt_AS_LONG PyLong_AS_LONG
75 #define PyInt_AsLong PyLong_AsLong
76 #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
77 #define PyNumber_Int PyNumber_Long
78
79 #define BIN_CHECK(obj) PyBytes_Check(obj)
80 #define BIN_PTR(obj) PyBytes_AS_STRING(obj)
81 #define BIN_LEN(obj) PyBytes_GET_SIZE(obj)
82 #define BIN_FROMSTR(str) PyBytes_FromString(str)
83 #define BIN_FORMAT PyBytes_FromFormat
84 #define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0)
85
86 #define TEXT_CHECK(obj) PyUnicode_Check(obj)
87 #if PY_VERSION_HEX >= 0x03030000
88 # define TEXT_PTR(obj) PyUnicode_AsUTF8(obj)
89 # define TEXT_STR(obj) PyUnicode_AsUTF8(obj)
90 # define TEXT_PTRLEN(obj, ptr, len) do { \
91 Py_ssize_t len_; \
92 *(ptr) = PyUnicode_AsUTF8AndSize((obj), &len_); \
93 *(len) = len_; \
94 } while (0)
95 # define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
96 (obj) = PyUnicode_New((sz), 127); \
97 (ptr) = PyUnicode_DATA(obj); \
98 } while (0)
99 # define TEXT_DONEWRITE(obj, len) do { \
100 size_t len_ = (len); \
101 assert(PyUnicode_IS_COMPACT_ASCII(obj)); \
102 ((char *)PyUnicode_DATA(obj))[len_] = 0; \
103 ((PyASCIIObject *)(obj))->length = len_; \
104 } while (0)
105 #else
106 # define TEXT_PTR(obj) _PyUnicode_AsString(obj)
107 # define TEXT_STR(obj) _PyUnicode_AsString(obj)
108 # define TEXT_PTRLEN(obj, ptr, len) do { \
109 Py_ssize_t len_; \
110 *(ptr) = _PyUnicode_AsStringAndSize((obj), &len_); \
111 *(len) = len_; \
112 } while (0)
113 # define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
114 (obj) = PyBytes_FromStringAndSize(0, (sz)); \
115 (ptr) = PyBytes_AS_STRING(obj); \
116 } while (0)
117 # define TEXT_DONEWRITE(obj, len) do { \
118 PyObject *new_ = PyUnicode_FromEncodedObject(obj, 0, 0); \
119 assert(new_); Py_DECREF(obj); (obj) = new_; \
120 } while (0)
121 #endif
122 #define TEXT_FORMAT PyUnicode_FromFormat
123 #define TEXT_FROMSTR(str) PyUnicode_FromString(str)
124 #define TEXT_FROMSTRLEN(str, len) PyUnicode_FromStringAndSize(str, len)
125
126 #define Py_TPFLAGS_CHECKTYPES 0
127
128 #else
129
130 #define BIN_CHECK(obj) PyString_Check(obj)
131 #define BIN_PTR(obj) PyString_AS_STRING(obj)
132 #define BIN_LEN(obj) PyString_GET_SIZE(obj)
133 #define BIN_FROMSTR(str) PyString_FromString(str)
134 #define BIN_FORMAT PyString_FromFormat
135 #define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0)
136
137 #define TEXT_TYPE PyString_Type
138 #define TEXT_CHECK(obj) PyString_Check(obj)
139 #define TEXT_PTR(obj) PyString_AS_STRING(obj)
140 #define TEXT_STR(obj) PyString_AsString(obj)
141 #define TEXT_PTRLEN(obj, ptr, len) do { \
142 *(ptr) = PyString_AS_STRING(obj); \
143 *(len) = PyString_GET_SIZE(obj); \
144 } while (0)
145 #define TEXT_FORMAT PyString_FromFormat
146 #define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
147 (obj) = PyString_FromStringAndSize(0, (sz)); \
148 (ptr) = PyString_AS_STRING(obj); \
149 } while (0)
150 #define TEXT_DONEWRITE(obj, len) \
151 do _PyString_Resize(&(obj), (len)); while (0)
152 #define TEXT_FROMSTR(str) PyString_FromString(str)
153 #define TEXT_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len)
154
155 #endif
156
157 #ifndef PyVarObject_HEAD_INIT
158 # define PyVarObject_HEAD_INIT(super, sz) PyObject_HEAD_INIT(super) sz,
159 #endif
160
161 #if PY_VERSION_HEX < 0x03020000
162 typedef long Py_hash_t;
163 #endif
164
165 /*----- That's all, folks -------------------------------------------------*/
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171 #endif