21 #ifdef _POSIX_C_SOURCE
22 # undef _POSIX_C_SOURCE
30 #define PY_SSIZE_T_CLEAN 1
34 #ifndef LINK_ALL_STATIC
35 # define EXPCL_PYPANDA
36 #elif defined(__GNUC__)
37 # define EXPCL_PYPANDA __attribute__((weak))
39 # define EXPCL_PYPANDA extern inline
45 #ifndef Py_RETURN_NONE
46 # define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
49 #ifndef Py_RETURN_TRUE
50 # define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
53 #ifndef Py_RETURN_FALSE
54 # define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
60 #if PY_VERSION_HEX < 0x02050000
61 typedef int Py_ssize_t;
62 # define PyInt_FromSsize_t PyInt_FromLong
63 # define PyInt_AsSsize_t PyInt_AsLong
69 # define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
74 #ifndef PyVarObject_HEAD_INIT
75 #define PyVarObject_HEAD_INIT(type, size) \
76 PyObject_HEAD_INIT(type) size,
81 #if PY_VERSION_HEX < 0x03020000
82 # define PyErr_NewExceptionWithDoc(name, doc, base, dict) \
83 PyErr_NewException(name, base, dict)
89 #ifndef Py_TPFLAGS_CHECKTYPES
90 # define Py_TPFLAGS_CHECKTYPES 0
94 #if PY_MAJOR_VERSION >= 3
95 # define nb_nonzero nb_bool
96 # define nb_divide nb_true_divide
97 # define nb_inplace_divide nb_inplace_true_divide
99 # define PyLongOrInt_Check(x) PyLong_Check(x)
100 # define PyLongOrInt_AS_LONG PyLong_AS_LONG
101 # define PyInt_Check PyLong_Check
102 # define PyInt_AsLong PyLong_AsLong
103 # define PyInt_AS_LONG PyLong_AS_LONG
104 # define PyLongOrInt_AsSize_t PyLong_AsSize_t
106 # define PyLongOrInt_Check(x) (PyInt_Check(x) || PyLong_Check(x))
108 # define PyLongOrInt_AS_LONG PyInt_AsLong
110 EXPCL_PYPANDA
size_t PyLongOrInt_AsSize_t(PyObject *);
114 #if PY_MAJOR_VERSION >= 3
115 # define FMTCHAR_BYTES "y"
117 # define FMTCHAR_BYTES "s"
122 #if PY_VERSION_HEX < 0x03020000
123 typedef long Py_hash_t;
128 #if PY_MAJOR_VERSION >= 3
130 # undef _PyErr_OCCURRED
131 # define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
134 # if PY_VERSION_HEX < 0x03030000
135 # define PyUnicode_AsUTF8 _PyUnicode_AsString
136 # define PyUnicode_AsUTF8AndSize _PyUnicode_AsStringAndSize
141 #if PY_VERSION_HEX < 0x03040000
142 #define PyGILState_Check() (PyGILState_GetThisThreadState() == _PyThreadState_Current)
147 #if PY_VERSION_HEX < 0x03080000 && !defined(_PyObject_CallNoArg)
148 INLINE PyObject *_PyObject_CallNoArg(PyObject *func) {
149 static PyObject *empty_tuple = PyTuple_New(0);
150 return PyObject_Call(func, empty_tuple,
nullptr);
152 # define _PyObject_CallNoArg _PyObject_CallNoArg
155 #if PY_VERSION_HEX < 0x03080000 && !defined(_PyObject_FastCall)
156 INLINE PyObject *_PyObject_FastCall(PyObject *func, PyObject **args, Py_ssize_t nargs) {
157 PyObject *tuple = PyTuple_New(nargs);
158 for (Py_ssize_t i = 0; i < nargs; ++i) {
159 PyTuple_SET_ITEM(tuple, i, args[i]);
162 PyObject *result = PyObject_Call(func, tuple,
nullptr);
166 # define _PyObject_FastCall _PyObject_FastCall
170 #ifndef HAVE_LONG_LONG
171 # define PyLong_FromLongLong(x) PyLong_FromLong((long) (x))
172 # define PyLong_FromUnsignedLongLong(x) PyLong_FromUnsignedLong((unsigned long) (x))
173 # define PyLong_AsLongLong(x) PyLong_AsLong(x)
174 # define PyLong_AsUnsignedLongLong(x) PyLong_AsUnsignedLong(x)
175 # define PyLong_AsUnsignedLongLongMask(x) PyLong_AsUnsignedLongMask(x)
176 # define PyLong_AsLongLongAndOverflow(x) PyLong_AsLongAndOverflow(x)
181 #ifndef PyDict_GET_SIZE
182 # define PyDict_GET_SIZE(mp) (((PyDictObject *)mp)->ma_used)
185 #ifndef Py_RETURN_RICHCOMPARE
186 # define Py_RETURN_RICHCOMPARE(val1, val2, op) \
190 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
191 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
192 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
193 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
194 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
195 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
201 #if PY_VERSION_HEX < 0x03080000
202 INLINE PyObject *_PyLong_Rshift(PyObject *a,
size_t shiftby) {
203 PyObject *b = PyLong_FromLong(shiftby);
204 PyObject *result = PyNumber_Rshift(a, b);
208 INLINE PyObject *_PyLong_Lshift(PyObject *a,
size_t shiftby) {
209 PyObject *b = PyLong_FromLong(shiftby);
210 PyObject *result = PyNumber_Lshift(a, b);
216 #if PY_VERSION_HEX < 0x03090000
217 INLINE EXPCL_PYPANDA PyObject *PyObject_CallNoArgs(PyObject *func) {
218 return _PyObject_CallNoArg(func);
221 INLINE PyObject *PyObject_CallOneArg(PyObject *callable, PyObject *arg) {
222 #if PY_VERSION_HEX >= 0x03060000
223 return _PyObject_FastCall(callable, &arg, 1);
225 return PyObject_CallFunctionObjArgs(callable, arg,
nullptr);
229 INLINE PyObject *PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name) {
230 return PyObject_CallMethodObjArgs(obj, name,
nullptr);
233 INLINE PyObject *PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg) {
234 return PyObject_CallMethodObjArgs(obj, name, arg,
nullptr);
243 #ifndef _PyErr_OCCURRED
244 # define _PyErr_OCCURRED() PyErr_Occurred()