blob: 2edbf631ffa53d7d916f25fac1f23c2b0419006f [file] [log] [blame]
/* Compatibility macros for Python 3 */
#if PY_VERSION_HEX >= 0x03000000
#define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type)
#define PyInt_Check(x) PyLong_Check(x)
#define PyInt_AsLong(x) PyLong_AsLong(x)
#define PyInt_FromLong(x) PyLong_FromLong(x)
#define PyString_Format(fmt, args) PyUnicode_Format(fmt, args)
#endif
#ifndef Py_TYPE
# define Py_TYPE(op) ((op)->ob_type)
#endif
/* SWIG APIs for compatibility of both Python 2 & 3 */
#if PY_VERSION_HEX >= 0x03000000
# define SWIG_Python_str_FromFormat PyUnicode_FromFormat
#else
# define SWIG_Python_str_FromFormat PyString_FromFormat
#endif
/* Warning: This function will allocate a new string in Python 3,
* so please call SWIG_Python_str_DelForPy3(x) to free the space.
*/
SWIGINTERN char*
SWIG_Python_str_AsChar(PyObject *str)
{
#if PY_VERSION_HEX >= 0x03000000
char *cstr;
char *newstr;
Py_ssize_t len;
str = PyUnicode_AsUTF8String(str);
PyBytes_AsStringAndSize(str, &cstr, &len);
newstr = (char *) malloc(len+1);
memcpy(newstr, cstr, len+1);
Py_XDECREF(str);
return newstr;
#else
return PyString_AsString(str);
#endif
}
#if PY_VERSION_HEX >= 0x03000000
# define SWIG_Python_str_DelForPy3(x) free( (void*) (x) )
#else
# define SWIG_Python_str_DelForPy3(x)
#endif
SWIGINTERN PyObject*
SWIG_Python_str_FromChar(const char *c)
{
#if PY_VERSION_HEX >= 0x03000000
return PyUnicode_FromString(c);
#else
return PyString_FromString(c);
#endif
}
/* Add PyOS_snprintf for old Pythons */
#if PY_VERSION_HEX < 0x02020000
# if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WATCOM)
# define PyOS_snprintf _snprintf
# else
# define PyOS_snprintf snprintf
# endif
#endif
/* A crude PyString_FromFormat implementation for old Pythons */
#if PY_VERSION_HEX < 0x02020000
#ifndef SWIG_PYBUFFER_SIZE
# define SWIG_PYBUFFER_SIZE 1024
#endif
static PyObject *
PyString_FromFormat(const char *fmt, ...) {
va_list ap;
char buf[SWIG_PYBUFFER_SIZE * 2];
int res;
va_start(ap, fmt);
res = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
return (res < 0 || res >= (int)sizeof(buf)) ? 0 : PyString_FromString(buf);
}
#endif
/* Add PyObject_Del for old Pythons */
#if PY_VERSION_HEX < 0x01060000
# define PyObject_Del(op) PyMem_DEL((op))
#endif
#ifndef PyObject_DEL
# define PyObject_DEL PyObject_Del
#endif
/* A crude PyExc_StopIteration exception for old Pythons */
#if PY_VERSION_HEX < 0x02020000
# ifndef PyExc_StopIteration
# define PyExc_StopIteration PyExc_RuntimeError
# endif
# ifndef PyObject_GenericGetAttr
# define PyObject_GenericGetAttr 0
# endif
#endif
/* Py_NotImplemented is defined in 2.1 and up. */
#if PY_VERSION_HEX < 0x02010000
# ifndef Py_NotImplemented
# define Py_NotImplemented PyExc_RuntimeError
# endif
#endif
/* A crude PyString_AsStringAndSize implementation for old Pythons */
#if PY_VERSION_HEX < 0x02010000
# ifndef PyString_AsStringAndSize
# define PyString_AsStringAndSize(obj, s, len) {*s = PyString_AsString(obj); *len = *s ? strlen(*s) : 0;}
# endif
#endif
/* PySequence_Size for old Pythons */
#if PY_VERSION_HEX < 0x02000000
# ifndef PySequence_Size
# define PySequence_Size PySequence_Length
# endif
#endif
/* PyBool_FromLong for old Pythons */
#if PY_VERSION_HEX < 0x02030000
static
PyObject *PyBool_FromLong(long ok)
{
PyObject *result = ok ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
#endif
/* Py_ssize_t for old Pythons */
/* This code is as recommended by: */
/* http://www.python.org/dev/peps/pep-0353/#conversion-guidelines */
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
#endif