PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/python/pyhead.swg

#
Unknown | 214 lines | 188 code | 26 blank | 0 comment | 0 complexity | e4fa19964040688934998689bad86c82 MD5 | raw file
  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_AsUnsignedLongMask(x) PyLong_AsUnsignedLongMask(x)
  8#define PyInt_FromLong(x) PyLong_FromLong(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 */
 37SWIGINTERN char*
 38SWIG_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
 62SWIGINTERN PyObject*
 63SWIG_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
 88static PyObject *
 89PyString_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
141static
142PyObject *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)
154typedef int Py_ssize_t;
155# define PY_SSIZE_T_MAX INT_MAX
156# define PY_SSIZE_T_MIN INT_MIN
157typedef inquiry lenfunc;
158typedef intargfunc ssizeargfunc;
159typedef intintargfunc ssizessizeargfunc;
160typedef intobjargproc ssizeobjargproc;
161typedef intintobjargproc ssizessizeobjargproc;
162typedef getreadbufferproc readbufferproc;
163typedef getwritebufferproc writebufferproc;
164typedef getsegcountproc segcountproc;
165typedef getcharbufferproc charbufferproc;
166static 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 < 0x02040000
179#define Py_VISIT(op)				\
180  do { 						\
181    if (op) {					\
182      int vret = visit((op), arg);		\
183      if (vret)					\
184        return vret;				\
185    }						\
186  } while (0)
187#endif
188
189#if PY_VERSION_HEX < 0x02030000
190typedef struct {
191  PyTypeObject type;
192  PyNumberMethods as_number;
193  PyMappingMethods as_mapping;
194  PySequenceMethods as_sequence;
195  PyBufferProcs as_buffer;
196  PyObject *name, *slots;
197} PyHeapTypeObject;
198#endif
199
200#if PY_VERSION_HEX < 0x02030000
201typedef destructor freefunc;
202#endif
203
204#if ((PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION > 6) || \
205     (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 0) || \
206     (PY_MAJOR_VERSION > 3))
207# define SWIGPY_USE_CAPSULE
208# define SWIGPY_CAPSULE_NAME ((char*)"swig_runtime_data" SWIG_RUNTIME_VERSION ".type_pointer_capsule" SWIG_TYPE_TABLE_NAME)
209#endif
210
211#if PY_VERSION_HEX < 0x03020000
212#define PyDescr_TYPE(x) (((PyDescrObject *)(x))->d_type)
213#define PyDescr_NAME(x) (((PyDescrObject *)(x))->d_name)
214#endif