@@@ cython and python 3 wip
[mLib-python] / grim.h
CommitLineData
20bce5e9 1/* -*-c-*-
2 *
20bce5e9 3 * Grim hacks to support the Pyrex code
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
d8d81d1b 8/*----- Licensing notice --------------------------------------------------*
20bce5e9 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.
d8d81d1b 16 *
20bce5e9 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.
d8d81d1b 21 *
20bce5e9 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
579d0169 38/*----- Utilities ---------------------------------------------------------*/
20bce5e9 39
40#define PSIZEOF(x) sizeof(*x)
704500e1 41typedef const void *cvp;
20bce5e9 42
20bce5e9 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
efb0bf0e
MW
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)
965caf5f 83#define BIN_FROMSTRLEN(str, len) PyBytes_FromStringAndSize(str, len)
efb0bf0e
MW
84#define BIN_FORMAT PyBytes_FromFormat
85#define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0)
86
87#define TEXT_CHECK(obj) PyUnicode_Check(obj)
88#if PY_VERSION_HEX >= 0x03030000
89# define TEXT_PTR(obj) PyUnicode_AsUTF8(obj)
90# define TEXT_STR(obj) PyUnicode_AsUTF8(obj)
91# define TEXT_PTRLEN(obj, ptr, len) do { \
92 Py_ssize_t len_; \
93 *(ptr) = PyUnicode_AsUTF8AndSize((obj), &len_); \
94 *(len) = len_; \
95} while (0)
96# define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
97 (obj) = PyUnicode_New((sz), 127); \
98 (ptr) = PyUnicode_DATA(obj); \
99} while (0)
100# define TEXT_DONEWRITE(obj, len) do { \
101 size_t len_ = (len); \
102 assert(PyUnicode_IS_COMPACT_ASCII(obj)); \
103 ((char *)PyUnicode_DATA(obj))[len_] = 0; \
104 ((PyASCIIObject *)(obj))->length = len_; \
105} while (0)
106#else
107# define TEXT_PTR(obj) _PyUnicode_AsString(obj)
108# define TEXT_STR(obj) _PyUnicode_AsString(obj)
109# define TEXT_PTRLEN(obj, ptr, len) do { \
110 Py_ssize_t len_; \
111 *(ptr) = _PyUnicode_AsStringAndSize((obj), &len_); \
112 *(len) = len_; \
113} while (0)
114# define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
115 (obj) = PyBytes_FromStringAndSize(0, (sz)); \
116 (ptr) = PyBytes_AS_STRING(obj); \
117} while (0)
118# define TEXT_DONEWRITE(obj, len) do { \
119 PyObject *new_ = PyUnicode_FromEncodedObject(obj, 0, 0); \
120 assert(new_); Py_DECREF(obj); (obj) = new_; \
121} while (0)
122#endif
123#define TEXT_FORMAT PyUnicode_FromFormat
124#define TEXT_FROMSTR(str) PyUnicode_FromString(str)
125#define TEXT_FROMSTRLEN(str, len) PyUnicode_FromStringAndSize(str, len)
126
127#define Py_TPFLAGS_CHECKTYPES 0
128
129#else
130
131#define BIN_CHECK(obj) PyString_Check(obj)
132#define BIN_PTR(obj) PyString_AS_STRING(obj)
133#define BIN_LEN(obj) PyString_GET_SIZE(obj)
134#define BIN_FROMSTR(str) PyString_FromString(str)
965caf5f 135#define BIN_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len)
efb0bf0e
MW
136#define BIN_FORMAT PyString_FromFormat
137#define BIN_SETLEN(obj, len) do Py_SIZE(obj) = (len); while (0)
138
139#define TEXT_TYPE PyString_Type
140#define TEXT_CHECK(obj) PyString_Check(obj)
141#define TEXT_PTR(obj) PyString_AS_STRING(obj)
142#define TEXT_STR(obj) PyString_AsString(obj)
143#define TEXT_PTRLEN(obj, ptr, len) do { \
144 *(ptr) = PyString_AS_STRING(obj); \
145 *(len) = PyString_GET_SIZE(obj); \
146} while (0)
147#define TEXT_FORMAT PyString_FromFormat
148#define TEXT_PREPAREWRITE(obj, ptr, sz) do { \
149 (obj) = PyString_FromStringAndSize(0, (sz)); \
150 (ptr) = PyString_AS_STRING(obj); \
151} while (0)
152#define TEXT_DONEWRITE(obj, len) \
153 do _PyString_Resize(&(obj), (len)); while (0)
154#define TEXT_FROMSTR(str) PyString_FromString(str)
155#define TEXT_FROMSTRLEN(str, len) PyString_FromStringAndSize(str, len)
156
157#endif
158
159#ifndef PyVarObject_HEAD_INIT
160# define PyVarObject_HEAD_INIT(super, sz) PyObject_HEAD_INIT(super) sz,
161#endif
162
163#if PY_VERSION_HEX < 0x03020000
164 typedef long Py_hash_t;
165#endif
166
20bce5e9 167/*----- That's all, folks -------------------------------------------------*/
168
169#ifdef __cplusplus
170 }
171#endif
172
173#endif