3 * Grim hacks to support the Pyrex code
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to mLib.
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.
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.
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.
34 /*----- Header files ------------------------------------------------------*/
38 /*----- Utilities ---------------------------------------------------------*/
40 #define PSIZEOF(x) sizeof(*x)
41 typedef const void *cvp
;
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)
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 },
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 },
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))
64 #define FREEOBJ(obj) \
65 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
67 #define KWLIST ((char **)kwlist)
69 /*----- Compatibility hacks -----------------------------------------------*/
70 #if PY_VERSION_HEX >= 0x03000000
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
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)
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 { \
92 *(ptr) = PyUnicode_AsUTF8AndSize((obj), &len_); \
95 # define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
96 (obj) = PyUnicode_New((sz), 127); \
97 (ptr) = PyUnicode_DATA(obj); \
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_; \
106 # define TEXT_PTR(obj) _PyUnicode_AsString(obj)
107 # define TEXT_STR(obj) _PyUnicode_AsString(obj)
108 # define TEXT_PTRLEN(obj, ptr, len) do { \
110 *(ptr) = _PyUnicode_AsStringAndSize((obj), &len_); \
113 # define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
114 (obj) = PyBytes_FromStringAndSize(0, (sz)); \
115 (ptr) = PyBytes_AS_STRING(obj); \
117 # define TEXT_DONEWRITE(obj, len) do { \
118 PyObject *new_ = PyUnicode_FromEncodedObject(obj, 0, 0); \
119 assert(new_); Py_DECREF(obj); (obj) = new_; \
122 #define TEXT_FORMAT PyUnicode_FromFormat
123 #define TEXT_FROMSTR(str) PyUnicode_FromString(str)
124 #define TEXT_FROMSTRLEN(str, len) PyUnicode_FromStringAndSize(str, len)
126 #define Py_TPFLAGS_CHECKTYPES 0
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)
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); \
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); \
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)
157 #ifndef PyVarObject_HEAD_INIT
158 # define PyVarObject_HEAD_INIT(super, sz) PyObject_HEAD_INIT(super) sz,
161 #if PY_VERSION_HEX < 0x03020000
162 typedef long Py_hash_t
;
165 /*----- That's all, folks -------------------------------------------------*/