Home | History | Annotate | Download | only in python
      1 /* Compatibility macros for Python 3 */
      2 #if PY_VERSION_HEX >= 0x03000000
      3 
      4 #define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type)
      5 #define PyInt_Check(x) PyLong_Check(x)
      6 #define PyInt_AsLong(x) PyLong_AsLong(x)
      7 #define PyInt_FromLong(x) PyLong_FromLong(x)
      8 #define PyInt_FromSize_t(x) PyLong_FromSize_t(x)
      9 #define PyString_Check(name) PyBytes_Check(name)
     10 #define PyString_FromString(x) PyUnicode_FromString(x)
     11 #define PyString_Format(fmt, args)  PyUnicode_Format(fmt, args)
     12 #define PyString_AsString(str) PyBytes_AsString(str)
     13 #define PyString_Size(str) PyBytes_Size(str)
     14 #define PyString_InternFromString(key) PyUnicode_InternFromString(key)
     15 #define Py_TPFLAGS_HAVE_CLASS Py_TPFLAGS_BASETYPE
     16 #define PyString_AS_STRING(x) PyUnicode_AS_STRING(x)
     17 #define _PyLong_FromSsize_t(x) PyLong_FromSsize_t(x)
     18 
     19 #endif
     20 
     21 #ifndef Py_TYPE
     22 #  define Py_TYPE(op) ((op)->ob_type)
     23 #endif
     24 
     25 /* SWIG APIs for compatibility of both Python 2 & 3 */
     26 
     27 #if PY_VERSION_HEX >= 0x03000000
     28 #  define SWIG_Python_str_FromFormat PyUnicode_FromFormat
     29 #else
     30 #  define SWIG_Python_str_FromFormat PyString_FromFormat
     31 #endif
     32 
     33 
     34 /* Warning: This function will allocate a new string in Python 3,
     35  * so please call SWIG_Python_str_DelForPy3(x) to free the space.
     36  */
     37 SWIGINTERN char*
     38 SWIG_Python_str_AsChar(PyObject *str)
     39 {
     40 #if PY_VERSION_HEX >= 0x03000000
     41   char *cstr;
     42   char *newstr;
     43   Py_ssize_t len;
     44   str = PyUnicode_AsUTF8String(str);
     45   PyBytes_AsStringAndSize(str, &cstr, &len);
     46   newstr = (char *) malloc(len+1);
     47   memcpy(newstr, cstr, len+1);
     48   Py_XDECREF(str);
     49   return newstr;
     50 #else
     51   return PyString_AsString(str);
     52 #endif
     53 }
     54 
     55 #if PY_VERSION_HEX >= 0x03000000
     56 #  define SWIG_Python_str_DelForPy3(x) free( (void*) (x) )
     57 #else
     58 #  define SWIG_Python_str_DelForPy3(x)
     59 #endif
     60 
     61 
     62 SWIGINTERN PyObject*
     63 SWIG_Python_str_FromChar(const char *c)
     64 {
     65 #if PY_VERSION_HEX >= 0x03000000
     66   return PyUnicode_FromString(c);
     67 #else
     68   return PyString_FromString(c);
     69 #endif
     70 }
     71 
     72 /* Add PyOS_snprintf for old Pythons */
     73 #if PY_VERSION_HEX < 0x02020000
     74 # if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WATCOM)
     75 #  define PyOS_snprintf _snprintf
     76 # else
     77 #  define PyOS_snprintf snprintf
     78 # endif
     79 #endif
     80 
     81 /* A crude PyString_FromFormat implementation for old Pythons */
     82 #if PY_VERSION_HEX < 0x02020000
     83 
     84 #ifndef SWIG_PYBUFFER_SIZE
     85 # define SWIG_PYBUFFER_SIZE 1024
     86 #endif
     87 
     88 static PyObject *
     89 PyString_FromFormat(const char *fmt, ...) {
     90   va_list ap;
     91   char buf[SWIG_PYBUFFER_SIZE * 2];
     92   int res;
     93   va_start(ap, fmt);
     94   res = vsnprintf(buf, sizeof(buf), fmt, ap);
     95   va_end(ap);
     96   return (res < 0 || res >= (int)sizeof(buf)) ? 0 : PyString_FromString(buf);
     97 }
     98 #endif
     99 
    100 /* Add PyObject_Del for old Pythons */
    101 #if PY_VERSION_HEX < 0x01060000
    102 # define PyObject_Del(op) PyMem_DEL((op))
    103 #endif
    104 #ifndef PyObject_DEL
    105 # define PyObject_DEL PyObject_Del
    106 #endif
    107 
    108 /* A crude PyExc_StopIteration exception for old Pythons */
    109 #if PY_VERSION_HEX < 0x02020000
    110 # ifndef PyExc_StopIteration
    111 #  define PyExc_StopIteration PyExc_RuntimeError
    112 # endif
    113 # ifndef PyObject_GenericGetAttr
    114 #  define PyObject_GenericGetAttr 0
    115 # endif
    116 #endif
    117 
    118 /* Py_NotImplemented is defined in 2.1 and up. */
    119 #if PY_VERSION_HEX < 0x02010000
    120 # ifndef Py_NotImplemented
    121 #  define Py_NotImplemented PyExc_RuntimeError
    122 # endif
    123 #endif
    124 
    125 /* A crude PyString_AsStringAndSize implementation for old Pythons */
    126 #if PY_VERSION_HEX < 0x02010000
    127 # ifndef PyString_AsStringAndSize
    128 #  define PyString_AsStringAndSize(obj, s, len) {*s = PyString_AsString(obj); *len = *s ? strlen(*s) : 0;}
    129 # endif
    130 #endif
    131 
    132 /* PySequence_Size for old Pythons */
    133 #if PY_VERSION_HEX < 0x02000000
    134 # ifndef PySequence_Size
    135 #  define PySequence_Size PySequence_Length
    136 # endif
    137 #endif
    138 
    139 /* PyBool_FromLong for old Pythons */
    140 #if PY_VERSION_HEX < 0x02030000
    141 static
    142 PyObject *PyBool_FromLong(long ok)
    143 {
    144   PyObject *result = ok ? Py_True : Py_False;
    145   Py_INCREF(result);
    146   return result;
    147 }
    148 #endif
    149 
    150 /* Py_ssize_t for old Pythons */
    151 /* This code is as recommended by: */
    152 /* http://www.python.org/dev/peps/pep-0353/#conversion-guidelines */
    153 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
    154 typedef int Py_ssize_t;
    155 # define PY_SSIZE_T_MAX INT_MAX
    156 # define PY_SSIZE_T_MIN INT_MIN
    157 typedef inquiry lenfunc;
    158 typedef intargfunc ssizeargfunc;
    159 typedef intintargfunc ssizessizeargfunc;
    160 typedef intobjargproc ssizeobjargproc;
    161 typedef intintobjargproc ssizessizeobjargproc;
    162 typedef getreadbufferproc readbufferproc;
    163 typedef getwritebufferproc writebufferproc;
    164 typedef getsegcountproc segcountproc;
    165 typedef getcharbufferproc charbufferproc;
    166 static long PyNumber_AsSsize_t (PyObject *x, void *SWIGUNUSEDPARM(exc))
    167 {
    168   long result = 0;
    169   PyObject *i = PyNumber_Int(x);
    170   if (i) {
    171     result = PyInt_AsLong(i);
    172     Py_DECREF(i);
    173   }
    174   return result;
    175 }
    176 #endif
    177 
    178 #if PY_VERSION_HEX < 0x02050000
    179 #define PyInt_FromSize_t(x) PyInt_FromLong((long)x)
    180 #endif
    181 
    182 #if PY_VERSION_HEX < 0x02040000
    183 #define Py_VISIT(op)				\
    184   do { 						\
    185     if (op) {					\
    186       int vret = visit((op), arg);		\
    187       if (vret)					\
    188         return vret;				\
    189     }						\
    190   } while (0)
    191 #endif
    192 
    193 #if PY_VERSION_HEX < 0x02030000
    194 typedef struct {
    195   PyTypeObject type;
    196   PyNumberMethods as_number;
    197   PyMappingMethods as_mapping;
    198   PySequenceMethods as_sequence;
    199   PyBufferProcs as_buffer;
    200   PyObject *name, *slots;
    201 } PyHeapTypeObject;
    202 #endif
    203 
    204 #if PY_VERSION_HEX < 0x02030000
    205 typedef destructor freefunc;
    206 #endif
    207 
    208 #if ((PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION > 6) || \
    209      (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 0) || \
    210      (PY_MAJOR_VERSION > 3))
    211 # define SWIGPY_USE_CAPSULE
    212 # define SWIGPY_CAPSULE_NAME ((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer_capsule" SWIG_TYPE_TABLE_NAME)
    213 #endif
    214 
    215 #if PY_VERSION_HEX < 0x03020000
    216 #define PyDescr_TYPE(x) (((PyDescrObject *)(x))->d_type)
    217 #define PyDescr_NAME(x) (((PyDescrObject *)(x))->d_name)
    218 #endif
    219