diff --git a/Examples/test-suite/primitive_types.i b/Examples/test-suite/primitive_types.i index efe3df297..cf0a3933b 100644 --- a/Examples/test-suite/primitive_types.i +++ b/Examples/test-suite/primitive_types.i @@ -472,6 +472,12 @@ macro(size_t, pfx, sizet) %test_prim_types_ovr(ovr_decl, ovr); + virtual Test* vtest(Test* t) const throw (Test) + { + return t; + } + + }; int v_check() @@ -483,3 +489,10 @@ macro(size_t, pfx, sizet) } } + + +%apply SWIGTYPE* { char *}; + +%include "carrays.i" +%array_functions(char,pchar); + diff --git a/Examples/test-suite/python/primitive_types_runme.py b/Examples/test-suite/python/primitive_types_runme.py index 1b32b1d5e..34043df2c 100644 --- a/Examples/test-suite/python/primitive_types_runme.py +++ b/Examples/test-suite/python/primitive_types_runme.py @@ -202,12 +202,10 @@ targs=('hi','hola') if t.mainv(targs,1) != 'hola': raise RuntimeError, "bad main typemap" - if t.strlen('hile') != 4: raise RuntimeError, "bad string typemap" - cvar.var_char = '\0' if cvar.var_char != '\0': raise RuntimeError, "bad char '0' case" @@ -241,4 +239,100 @@ if cvar.var_pcharc != '': raise RuntimeError, "bad char empty case" +# +# creating a raw char* +# +pc = new_pchar(5) +pchar_setitem(pc, 0, 'h') +pchar_setitem(pc, 1, 'o') +pchar_setitem(pc, 2, 'l') +pchar_setitem(pc, 3, 'a') +pchar_setitem(pc, 4, 0) + +cvar.var_pchar = pc +if cvar.var_pchar != "hola": + raise RuntimeError, "bad char empty case" + +cvar.var_namet = pc +if cvar.var_namet != "hola\0": + raise RuntimeError, "bad char empty case" + +targs=('hi', pc) +if t.mainv(targs,1) != 'hola': + raise RuntimeError, "bad main typemap" + +# +# Now when things should fail +# + +try: + error = 0 + t.mainv('hello',1) + error = 1 +except TypeError: + pass +if error: + raise RuntimeError, "bad main typemap" + + +try: + error = 0 + a = t.var_uchar + t.var_uchar = 10000 + error = 1 +except OverflowError: + if a != t.var_uchar: + error = 1 + pass +if error: + raise RuntimeError, "bad uchar typemap" + + + +try: + error = 0 + a = t.var_char + t.var_char = '23' + error = 1 +except TypeError: + if a != t.var_char: + error = 1 + pass +if error: + raise RuntimeError, "bad char typemap" + +try: + error = 0 + a = t.var_uint + t.var_uint = -1 + error = 1 +except TypeError: + if a != t.var_uint: + error = 1 + pass +if error: + raise RuntimeError, "bad uint typemap" + +# +# this should be an error in C++, but not in C. +# +try: + error = 0 + a = t.var_namet + t.var_namet = '12345\0' + error = 1 +except TypeError: + if a != t.var_namet: + error = 1 + pass +if error: + raise RuntimeError, "bad namet typemap" + +# +# +# +t2 = p.vtest(t) +if "%s" % (t2,) != "%s" % (t,): + raise RuntimeError, "bad SWIGTYPE* typemap" + diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 8e106b648..7aa5e9b75 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -10,239 +10,6 @@ #include "Python.h" -#include -#include -#include - -#ifdef __cplusplus -#define SWIG_STATIC_INLINE static inline -#else -#define SWIG_STATIC_INLINE static -#endif - -SWIG_STATIC_INLINE long -SPyObj_AsLong(PyObject * obj) -{ - return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj); -} - -SWIG_STATIC_INLINE unsigned long -SPyObj_AsUnsignedLong(PyObject * obj) -{ - if (PyLong_Check(obj)) { - return PyLong_AsUnsignedLong(obj); - } else { - long i = PyInt_AsLong(obj); - if ( !PyErr_Occurred() && (i < 0)) { - PyErr_SetString(PyExc_TypeError, "negative value for unsigned type"); - } - return i; - } -} - -SWIG_STATIC_INLINE PyObject* -SPyObj_FromLongLong(long long value) -{ - return (value > (long)(LONG_MAX)) ? - PyLong_FromLongLong(value) : PyInt_FromLong((long)value); -} - -SWIG_STATIC_INLINE PyObject* -SPyObj_FromUnsignedLong(unsigned long value) -{ - return (value > (unsigned long)(LONG_MAX)) ? - PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)value); -} - -SWIG_STATIC_INLINE PyObject* -SPyObj_FromUnsignedLongLong(unsigned long long value) -{ - return (value > (unsigned long long)(LONG_MAX)) ? - PyLong_FromUnsignedLongLong(value) : PyInt_FromLong((long)value); -} - -SWIG_STATIC_INLINE long -SPyObj_AsLongInRange(PyObject * obj, long min_value, long max_value) -{ - long value = SPyObj_AsLong(obj); - if (!PyErr_Occurred()) { - if (value < min_value) { - PyErr_SetString(PyExc_OverflowError,"value is smaller than type minimum"); - } else if (value > max_value) { - PyErr_SetString(PyExc_OverflowError,"value is greater than type maximum"); - } - } - return value; -} - -SWIG_STATIC_INLINE unsigned long -SPyObj_AsUnsignedLongInRange(PyObject *obj, unsigned long max_value) -{ - unsigned long value = SPyObj_AsUnsignedLong(obj); - if (!PyErr_Occurred()) { - if (value > max_value) { - PyErr_SetString(PyExc_OverflowError,"value is greater than type maximum"); - } - } - return value; -} - -SWIG_STATIC_INLINE signed char -SPyObj_AsSignedChar(PyObject *obj) { - return SPyObj_AsLongInRange(obj, SCHAR_MIN, SCHAR_MAX); -} - -SWIG_STATIC_INLINE short -SPyObj_AsShort(PyObject *obj) { - return SPyObj_AsLongInRange(obj, SHRT_MIN, SHRT_MAX); -} - -SWIG_STATIC_INLINE int -SPyObj_AsInt(PyObject *obj) { - return SPyObj_AsLongInRange(obj, INT_MIN, INT_MAX); -} - -SWIG_STATIC_INLINE unsigned char -SPyObj_AsUnsignedChar(PyObject *obj) { - return SPyObj_AsUnsignedLongInRange(obj, UCHAR_MAX); -} - -SWIG_STATIC_INLINE unsigned short -SPyObj_AsUnsignedShort(PyObject *obj) { - return SPyObj_AsUnsignedLongInRange(obj, USHRT_MAX); -} - -SWIG_STATIC_INLINE unsigned int -SPyObj_AsUnsignedInt(PyObject *obj) { - return SPyObj_AsUnsignedLongInRange(obj, UINT_MAX); -} - -SWIG_STATIC_INLINE long long -SPyObj_AsLongLong(PyObject *obj) { - return PyInt_Check(obj) ? - PyInt_AsLong(obj) : PyLong_AsLongLong(obj); -} - -SWIG_STATIC_INLINE unsigned long long -SPyObj_AsUnsignedLongLong(PyObject *obj) { - return PyLong_Check(obj) ? - PyLong_AsUnsignedLongLong(obj) : SPyObj_AsUnsignedLong(obj); -} - -SWIG_STATIC_INLINE double -SPyObj_AsDouble(PyObject *obj) { - return (PyFloat_Check(obj)) ? PyFloat_AsDouble(obj) : - (double)((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj)); -} - -SWIG_STATIC_INLINE float -SPyObj_AsFloat(PyObject *obj) { - double value = SPyObj_AsDouble(obj); - if (!PyErr_Occurred()) { - if (value < FLT_MIN) { - PyErr_SetString(PyExc_OverflowError,"float is smaller than flt_min"); - } else if (value > FLT_MAX) { - PyErr_SetString(PyExc_OverflowError,"float is greater than flt_max"); - } - } - return (float) value; -} - -SWIG_STATIC_INLINE void -SPyObj_AsCharPtrAndSize(PyObject *obj, char** cptr, int* len) { - if (obj != Py_None) { - PyString_AsStringAndSize(obj, cptr, len); - } else { - *cptr = 0; *len = 0; - } -} - -SWIG_STATIC_INLINE char -SPyObj_AsChar(PyObject *obj) { - char c; - if (PyString_Check(obj)) { - char* cptr; int len; - SPyObj_AsCharPtrAndSize(obj, &cptr, &len); - if (len == 1) { - c = cptr[0]; - } else { - PyErr_SetString(PyExc_OverflowError, "a char is required"); - } - } else { - c = (char) SPyObj_AsLongInRange(obj, CHAR_MIN, CHAR_MAX); - if (PyErr_Occurred()) { - PyErr_Clear(); - PyErr_SetString(PyExc_TypeError, "a char is required"); - } - } - return c; -} - -SWIG_STATIC_INLINE PyObject * -SPyObj_FromChar(char c) { - return PyString_FromStringAndSize(&c,1); -} - -SWIG_STATIC_INLINE char* -SPyObj_AsCharPtr(PyObject *obj) { - return (obj != Py_None) ? PyString_AsString(obj) : (char*) 0; -} - -SWIG_STATIC_INLINE PyObject * -SPyObj_FromCharPtr(const char* cptr) { - if (cptr) { - return PyString_FromString(cptr); - } else { - Py_INCREF(Py_None); - return Py_None; - } -} - -SWIG_STATIC_INLINE char* -SPyObj_AsNewCharPtr(PyObject *obj) { - char *res = 0; - char* cptr; int len; - SPyObj_AsCharPtrAndSize(obj, &cptr, &len); - if (!PyErr_Occurred() && cptr) { - size_t size = (len && !(cptr[len - 1])) ? len : len + 1; -#ifdef __cplusplus - res = new char[size]; -#else - res = malloc(size); -#endif - if (len) memcpy(res, cptr, len); - res[size-1] = 0; - } - return res; -} - -SWIG_STATIC_INLINE PyObject * -SPyObj_FromCharArray(const char* carray, int size) { - /* checking the effective size backward */ - //for (; size && (carray[size - 1] == 0); --size); - return PyString_FromStringAndSize(carray, size); -} - -SWIG_STATIC_INLINE void -SPyObj_AsCharArray(PyObject *obj, char* carray, int size) { - char* cptr; int len; - SPyObj_AsCharPtrAndSize(obj, &cptr, &len); - if (!PyErr_Occurred()) { - if (len > size) { - PyErr_SetObject(PyExc_TypeError, - PyString_FromFormat("a string of maximum size %d is required", size)); - } else { - if (len) memcpy(carray, cptr, len); - if (len < size) memset(carray + len, 0, size - len); - } - } -} - -SWIG_STATIC_INLINE int -SPyObj_AsBool(PyObject *obj) { - return SPyObj_AsLongLong(obj) ? 1 : 0; -} - #ifdef __cplusplus extern "C" { #endif @@ -302,8 +69,54 @@ SWIGIMPORT(int) SWIG_Python_ConvertPacked(PyObject *, void *, int SWIGIMPORT(PyObject *) SWIG_Python_NewPackedObj(void *, int sz, swig_type_info *); SWIGIMPORT(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]); +/* ----------------------------------------------------------------------------- + * the needed conversions between C++ and python + * ----------------------------------------------------------------------------- */ +/* basic types */ +SWIGIMPORT(int) SWIG_PyObj_AsBool(PyObject *obj); +SWIGIMPORT(char) SWIG_PyObj_AsChar(PyObject *obj); +SWIGIMPORT(signed char) SWIG_PyObj_AsSignedChar(PyObject *obj); +SWIGIMPORT(short) SWIG_PyObj_AsShort(PyObject *obj); +SWIGIMPORT(int) SWIG_PyObj_AsInt(PyObject *obj); +SWIGIMPORT(long) SWIG_PyObj_AsLong(PyObject * obj); + +SWIGIMPORT(unsigned char) SWIG_PyObj_AsUnsignedChar(PyObject *obj); +SWIGIMPORT(unsigned short) SWIG_PyObj_AsUnsignedShort(PyObject *obj); +SWIGIMPORT(unsigned int) SWIG_PyObj_AsUnsignedInt(PyObject *obj); +SWIGIMPORT(unsigned long) SWIG_PyObj_AsUnsignedLong(PyObject * obj) ; +SWIGIMPORT(float) SWIG_PyObj_AsFloat(PyObject *obj); +SWIGIMPORT(double) SWIG_PyObj_AsDouble(PyObject *obj); + +SWIGIMPORT(PyObject *) SWIG_PyObj_FromBool(int b); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromChar(char c); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromUnsignedLong(unsigned long value); + +#ifdef HAVE_LONG_LONG +SWIGIMPORT(long long) SWIG_PyObj_AsLongLong(PyObject *obj); +SWIGIMPORT(unsigned long long) SWIG_PyObj_AsUnsignedLongLong(PyObject *obj); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromLongLong(long long value); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromUnsignedLongLong(unsigned long long value); +#endif /* HAVE_LONG_LONG */ + +/* utilities */ +SWIGIMPORT(long) SWIG_PyObj_AsLongInRange(PyObject * obj, const char* type, + long min_value, long max_value); +SWIGIMPORT(unsigned long) SWIG_PyObj_AsUnsignedLongInRange(PyObject *obj, const char* type, + unsigned long max_value); +SWIGIMPORT(char *) SWIG_PyObj_AsNewCharPtr(PyObject *obj, swig_type_info* pchar_info); +SWIGIMPORT(char *) SWIG_PyObj_AsCharPtr(PyObject *obj, swig_type_info* pchar_info); +SWIGIMPORT(void) SWIG_PyObj_AsCharPtrAndSize(PyObject *obj, swig_type_info* pchar_info, + char** cptr, int* len); +SWIGIMPORT(void) SWIG_PyObj_AsCharArray(PyObject *obj, swig_type_info* pchar_info, + char* carray, int size); + +SWIGIMPORT(PyObject *) SWIG_PyObj_FromCharPtr(const char* cptr); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromCharArray(const char* carray, int size); + + #else + /* ----------------------------------------------------------------------------- * global variable support code. * ----------------------------------------------------------------------------- */ @@ -592,6 +405,310 @@ SWIG_Python_NewPackedObj(void *ptr, int sz, swig_type_info *type) { return PyString_FromString(result); } +/* ----------------------------------------------------------------------------- + * the needed conversions between C++ and python + * ----------------------------------------------------------------------------- */ + +#include +#include +#include + +SWIGRUNTIME(long) +SWIG_PyObj_AsLong(PyObject * obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj); +} + +SWIGRUNTIME(unsigned long) +SWIG_PyObj_AsUnsignedLong(PyObject * obj) +{ + if (PyLong_Check(obj)) { + return PyLong_AsUnsignedLong(obj); + } else { + long i = PyInt_AsLong(obj); + if ( !PyErr_Occurred() && (i < 0)) { + PyErr_SetString(PyExc_TypeError, "negative value for unsigned type"); + } + return i; + } +} + +#ifdef HAVE_LONG_LONG + +SWIGRUNTIME(PyObject* ) +SWIG_PyObj_FromLongLong(long long value) +{ + return (value > (long)(LONG_MAX)) ? + PyLong_FromLongLong(value) : PyInt_FromLong((long)value); +} + +SWIGRUNTIME(PyObject* ) +SWIG_PyObj_FromUnsignedLongLong(unsigned long long value) +{ + return (value > (unsigned long long)(LONG_MAX)) ? + PyLong_FromUnsignedLongLong(value) : PyInt_FromLong((long)value); +} + +SWIGRUNTIME(long long) +SWIG_PyObj_AsLongLong(PyObject *obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj); +} + +SWIGRUNTIME(unsigned long long) +SWIG_PyObj_AsUnsignedLongLong(PyObject *obj) +{ + return PyLong_Check(obj) ? + PyLong_AsUnsignedLongLong(obj) : SWIG_PyObj_AsUnsignedLong(obj); +} + +#endif /* HAVE_LONG_LONG */ + +SWIGRUNTIME(PyObject* ) +SWIG_PyObj_FromUnsignedLong(unsigned long value) +{ + return (value > (unsigned long)(LONG_MAX)) ? + PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)value); +} + + +SWIGRUNTIME(long) +SWIG_PyObj_AsLongInRange(PyObject * obj, const char* type, + long min_value, long max_value) +{ + long value = SWIG_PyObj_AsLong(obj); + if (!PyErr_Occurred()) { + if (value < min_value) { + PyObject *err = + PyString_FromFormat("value %ld is less than '%s' minimum %ld", + value, type, min_value); + + PyErr_SetObject(PyExc_OverflowError, err); + Py_XDECREF(err); + } else if (value > max_value) { + PyObject *err = + PyString_FromFormat("value %ld is greater than '%s' maximum %ld", + value, type, max_value); + PyErr_SetObject(PyExc_OverflowError, err); + Py_XDECREF(err); + } + } + return value; +} + +SWIGRUNTIME(unsigned long) +SWIG_PyObj_AsUnsignedLongInRange(PyObject *obj, const char* type, + unsigned long max_value) +{ + unsigned long value = SWIG_PyObj_AsUnsignedLong(obj); + if (!PyErr_Occurred()) { + if (value > max_value) { + PyObject *err = + PyString_FromFormat("value %ld is greater than '%s' minimum %ld", + value, type, max_value); + PyErr_SetObject(PyExc_OverflowError, err); + Py_XDECREF(err); + } + } + return value; +} + +SWIGRUNTIME(signed char) +SWIG_PyObj_AsSignedChar(PyObject *obj) +{ + return SWIG_PyObj_AsLongInRange(obj, "signed char", SCHAR_MIN, SCHAR_MAX); +} + +SWIGRUNTIME(short) +SWIG_PyObj_AsShort(PyObject *obj) +{ + return SWIG_PyObj_AsLongInRange(obj, "short", SHRT_MIN, SHRT_MAX); +} + +SWIGRUNTIME(int) +SWIG_PyObj_AsInt(PyObject *obj) +{ + return SWIG_PyObj_AsLongInRange(obj, "int", INT_MIN, INT_MAX); +} + +SWIGRUNTIME(unsigned char) +SWIG_PyObj_AsUnsignedChar(PyObject *obj) +{ + return SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned char", UCHAR_MAX); +} + +SWIGRUNTIME(unsigned short ) +SWIG_PyObj_AsUnsignedShort(PyObject *obj) +{ + return SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned short", USHRT_MAX); +} + +SWIGRUNTIME(unsigned int) +SWIG_PyObj_AsUnsignedInt(PyObject *obj) +{ + return SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned int", UINT_MAX); +} + +SWIGRUNTIME(double) +SWIG_PyObj_AsDouble(PyObject *obj) +{ + return (PyFloat_Check(obj)) ? PyFloat_AsDouble(obj) : + (double)((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj)); +} + +SWIGRUNTIME(float) +SWIG_PyObj_AsFloat(PyObject *obj) +{ + double value = SWIG_PyObj_AsDouble(obj); + if (!PyErr_Occurred()) { + if (value < FLT_MIN) { + PyObject *err = + PyString_FromFormat("value %g is less than float minimum %g", + value, FLT_MIN); + PyErr_SetObject(PyExc_OverflowError, err); + Py_XDECREF(err); + } else if (value > FLT_MAX) { + PyObject *err = + PyString_FromFormat("value %g is greater than float maximum %g", + value, FLT_MAX); + PyErr_SetObject(PyExc_OverflowError, err); + Py_XDECREF(err); + } + } + return (float) value; +} + +SWIGRUNTIME(void) +SWIG_PyObj_AsCharPtrAndSize(PyObject *obj, swig_type_info* pchar_info, + char** cptr, int* len) +{ + if ((!pchar_info) || SWIG_ConvertPtr(obj,(void **)cptr, pchar_info, 0) == -1) { + if (pchar_info && PyErr_Occurred()) PyErr_Clear(); + PyString_AsStringAndSize(obj, cptr, len); + } else { + /* don't like strlen, but ... */ + *len = (*cptr) ? (int) (strlen(*cptr) + 1) : 0; + } +} + +SWIGRUNTIME(char* ) +SWIG_PyObj_AsCharPtr(PyObject *obj, swig_type_info* pchar_info) { + char* ptr; + if (SWIG_ConvertPtr(obj,(void **)&ptr, pchar_info, 0) == -1) { + if (PyErr_Occurred()) PyErr_Clear(); + ptr = PyString_AsString(obj); + } + return ptr; +} + +SWIGRUNTIME(PyObject *) +SWIG_PyObj_FromCharPtr(const char* cptr) { + if (cptr) { + return PyString_FromString(cptr); + } else { + Py_INCREF(Py_None); + return Py_None; + } +} + +SWIGRUNTIME(char*) +SWIG_PyObj_AsNewCharPtr(PyObject *obj, swig_type_info* pchar_info) +{ + char *res = 0; + char* cptr; int len; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &cptr, &len); + if (!PyErr_Occurred() && cptr) { + /* we add the '0' terminator if needed */ + int size = (len && !(cptr[len - 1])) ? len : len + 1; +#ifdef __cplusplus + res = new char[size]; +#else + res = malloc(size); +#endif + if (len) memcpy(res, cptr, len); + if (len < size) res[len] = 0; + } + return res; +} + +SWIGRUNTIME(PyObject *) +SWIG_PyObj_FromCharArray(const char* carray, int size) +{ + /* checking the effective size backward, not needed now */ + /* for (; size && (carray[size - 1] == 0); --size); */ + return PyString_FromStringAndSize(carray, size); +} + +SWIGRUNTIME(void) +SWIG_PyObj_AsCharArray(PyObject *obj, swig_type_info* pchar_info, char* carray, int size) +{ + char* cptr; int len; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &cptr, &len); + if (!PyErr_Occurred()) { +#ifndef __cplusplus + /* in C (but not in C++) you can do: + + char x[5] = "hello"; + + ie, assing the array using an extra '0' char. + */ + if ((len == size + 1) && !(cptr[len-1])) --len; +#endif + if (len > size) { + PyObject *err = + PyString_FromFormat("a char array of maximum size %d is expected", + size); + PyErr_SetObject(PyExc_TypeError, err); + Py_XDECREF(err); + } else { + if (len) memcpy(carray, cptr, len); + if (len < size) memset(carray + len, 0, size - len); + } + } +} + +SWIGRUNTIME(char) +SWIG_PyObj_AsChar(PyObject *obj) +{ + char c = 0; + if (PyInt_Check(obj) || PyLong_Check(obj)) { + c = SWIG_PyObj_AsLongInRange(obj, "char", + CHAR_MIN, CHAR_MAX); + } else { + char* cptr; int len; + SWIG_PyObj_AsCharPtrAndSize(obj, 0, &cptr, &len); + if (len == 1) { + c = cptr[0]; + } else { + PyErr_SetString(PyExc_TypeError, "a char is expected"); + } + } + return c; +} + +SWIGRUNTIME(PyObject *) +SWIG_PyObj_FromChar(char c) { + return PyString_FromStringAndSize(&c,1); +} + +SWIGRUNTIME(PyObject *) +SWIG_PyObj_FromBool(int value) +{ +#ifdef Py_True + PyObject *obj = value ? Py_True : Py_False; + Py_INCREF(obj); + return obj; +#else + return PyInt_FromLong((value ? 1 : 0)); +#endif +} + +SWIGRUNTIME(int) +SWIG_PyObj_AsBool(PyObject *obj) +{ + return PyObject_IsTrue(obj); +} + /* Install Constants */ SWIGRUNTIME(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { @@ -606,7 +723,7 @@ SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { obj = PyFloat_FromDouble(constants[i].dvalue); break; case SWIG_PY_STRING: - obj = SPyObj_FromCharPtr((const char *) constants[i].pvalue); + obj = SWIG_PyObj_FromCharPtr((const char *) constants[i].pvalue); break; case SWIG_PY_POINTER: obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); diff --git a/Lib/python/python.swg b/Lib/python/python.swg index 9cc50fd0e..8ebf343ca 100644 --- a/Lib/python/python.swg +++ b/Lib/python/python.swg @@ -42,38 +42,38 @@ /* --- macro to expand a given typemap macro using the As methods --- */ %define %expand_primitives_as(typemap) - typemap(bool, SPyObj_AsBool); - typemap(signed char, SPyObj_AsSignedChar); - typemap(int, SPyObj_AsInt); - typemap(short, SPyObj_AsShort); - typemap(long, SPyObj_AsLong); - typemap(unsigned char, SPyObj_AsUnsignedChar); - typemap(unsigned short, SPyObj_AsUnsignedShort); - typemap(unsigned int, SPyObj_AsUnsignedInt); - typemap(unsigned long, SPyObj_AsUnsignedLong); - typemap(long long, SPyObj_AsLongLong); - typemap(unsigned long long, SPyObj_AsUnsignedLongLong); - typemap(float, SPyObj_AsFloat); - typemap(double, SPyObj_AsDouble); - typemap(char, SPyObj_AsChar); + typemap(bool, SWIG_PyObj_AsBool); + typemap(signed char, SWIG_PyObj_AsSignedChar); + typemap(int, SWIG_PyObj_AsInt); + typemap(short, SWIG_PyObj_AsShort); + typemap(long, SWIG_PyObj_AsLong); + typemap(unsigned char, SWIG_PyObj_AsUnsignedChar); + typemap(unsigned short, SWIG_PyObj_AsUnsignedShort); + typemap(unsigned int, SWIG_PyObj_AsUnsignedInt); + typemap(unsigned long, SWIG_PyObj_AsUnsignedLong); + typemap(char, SWIG_PyObj_AsChar); + typemap(float, SWIG_PyObj_AsFloat); + typemap(double, SWIG_PyObj_AsDouble); + typemap(long long, SWIG_PyObj_AsLongLong); + typemap(unsigned long long, SWIG_PyObj_AsUnsignedLongLong); %enddef /* --- macro to expand a given typemap macro using the From methods --- */ %define %expand_primitives_from(typemap) - typemap(bool, PyInt_FromLong); + typemap(bool, SWIG_PyObj_FromBool); typemap(signed char, PyInt_FromLong); typemap(int, PyInt_FromLong); typemap(short, PyInt_FromLong); typemap(long, PyInt_FromLong); typemap(unsigned char, PyInt_FromLong); typemap(unsigned short, PyInt_FromLong); - typemap(unsigned int, SPyObj_FromUnsignedLong); - typemap(unsigned long, SPyObj_FromUnsignedLong); - typemap(long long, SPyObj_FromLongLong); - typemap(unsigned long long, SPyObj_FromUnsignedLongLong); + typemap(unsigned int, SWIG_PyObj_FromUnsignedLong); + typemap(unsigned long, SWIG_PyObj_FromUnsignedLong); + typemap(char, SWIG_PyObj_FromChar); typemap(float, PyFloat_FromDouble); typemap(double, PyFloat_FromDouble); - typemap(char, SPyObj_FromChar); + typemap(long long, SWIG_PyObj_FromLongLong); + typemap(unsigned long long, SWIG_PyObj_FromUnsignedLongLong); %enddef @@ -141,21 +141,21 @@ /* The char* and char [ANY] case */ %typemap(in) char *, char const*, char *const, char const *const { - $1 = SPyObj_AsCharPtr($input); + $1 = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) SWIG_fail; } %typemap(in) char const*&, char *const&, char const *const & { - $*ltype temp = SPyObj_AsCharPtr($input); - $1 = &temp; + $*ltype temp = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) SWIG_fail; + $1 = &temp; } %typemap(in) char [ANY], const char [ANY] { char temp[$1_dim0]; - SPyObj_AsCharArray($input, temp, $1_dim0); + SWIG_PyObj_AsCharArray($input, $descriptor(char*), temp, $1_dim0); if (PyErr_Occurred()) SWIG_fail; $1 = temp; } @@ -192,10 +192,10 @@ /* Special typemap for character array return values */ %typemap(out) char*, char const*, char *const, char const *const, char *const &, char const* &, char const *const & - "$result = SPyObj_FromCharPtr((const char*)$1);"; + "$result = SWIG_PyObj_FromCharPtr((const char*)$1);"; %typemap(out) char [ANY], const char [ANY] - "$result = SPyObj_FromCharArray((const char*)$1, $1_dim0);"; + "$result = SWIG_PyObj_FromCharArray((const char*)$1, $1_dim0);"; /* Primitive types--return by value */ %typemap(out) SWIGTYPE @@ -235,18 +235,17 @@ /* char* and char[ANY] */ %typemap(varin) char * { - char *cptr = SPyObj_AsNewCharPtr($input); + char *cptr = SWIG_PyObj_AsNewCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); return 1; } - if ($1) - %swig_del_carray($1); + if ($1) %swig_del_carray($1); $1 = cptr; } %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { - char *cptr = SPyObj_AsNewCharPtr($input); + char *cptr = SWIG_PyObj_AsNewCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); return 1; @@ -255,7 +254,7 @@ } %typemap(varin) char [ANY] { - SPyObj_AsCharArray($input, $1, $1_dim0); + SWIG_PyObj_AsCharArray($input, $descriptor(char*), $1, $1_dim0); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); return 1; @@ -345,9 +344,9 @@ %typemap(varout) void "Py_INCREF(Py_None); $result = Py_None;"; /* Special typemap for character array return values */ -%typemap(varout) char *, const char * "$result = SPyObj_FromCharPtr((const char*)$1);"; +%typemap(varout) char *, const char * "$result = SWIG_PyObj_FromCharPtr((const char*)$1);"; -%typemap(varout) char [ANY], const char [ANY] "$result = SPyObj_FromCharArray((const char*)$1, $1_dim0);"; +%typemap(varout) char [ANY], const char [ANY] "$result = SWIG_PyObj_FromCharArray((const char*)$1, $1_dim0);"; %typemap(varout) SWIGTYPE "$result = SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0);"; @@ -369,7 +368,7 @@ { SWIG_PY_STRING, (char*)"$symname", 0, 0, (void *)$value, 0} %typemap(consttab) char [ANY], const char [ANY] - "PyDict_SetItemString(d,\"$symname\", SPyObj_FromCharArray($value, $value_dim0));"; + "PyDict_SetItemString(d,\"$symname\", SWIG_PyObj_FromCharArray($value, $value_dim0));"; %typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)$value, &$1_descriptor} @@ -400,16 +399,15 @@ %expand_primitives_from(PY_DIRECTORIN_TYPEMAP); - -/* Special typemap for character array return values */ - +/* we just pass the char* as a pointer, much cheaper */ %typemap(directorin) char *, char const*, char *const, char const *const, char const *&, char *const &, char const *const & - "$input = SPyObj_FromCharPtr((const char*)$1_name);"; - + "$input = SWIG_NewPointerObj((void *) $1_name, $descriptor(char*), $owner);" + /* "$input = SWIG_PyObj_FromCharPtr((const char*)$1_name);"; */ +/* here we need to create a python object, or we lost the 'ANY' dimension */ %typemap(directorin) char [ANY], const char [ANY] - "$input = SPyObj_FromCharArray((const char*)$1_name, $1_dim0);"; + "$input = SWIG_PyObj_FromCharArray((const char*)$1_name, $1_dim0);"; /* --- directorout typemaps --- */ @@ -419,7 +417,7 @@ "*$result = ($basetype) pyobj_as($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; %typemap(directorout) Type - "$result = ($ltype) pyobj_as($input); + "$result = ($basetype) pyobj_as($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; %typemap(directorout) const Type& "$basetype temp = ($basetype) pyobj_as($input); @@ -432,22 +430,22 @@ /* Char pointer and arrays */ %typemap(directorout) char *, char const*, char *const, char const* const { - $result = SPyObj_AsCharPtr($input); + $result = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) { Swig::DirectorTypeMismatchException("Error converting Python object into char*"); } -} + } -%typemap(directorout) char const *&, char *const &,char const *const & { - $*ltype temp = SPyObj_AsCharPtr($input); - $result = &temp; +%typemap(directorout) char const *&, char *const &, char const *const & { + char* temp = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); if (PyErr_Occurred()) { Swig::DirectorTypeMismatchException("Error converting Python object into char*"); } + $result = ($1_ltype) &temp; } %typemap(directorout) char [ANY], const char[ANY] (char temp[$result_dim0]) { - SPyObj_AsCharArray($input, temp, $result_dim0); + SWIG_PyObj_AsCharArray($input, $descriptor(char*), temp, $result_dim0); if (PyErr_Occurred()) { Swig::DirectorTypeMismatchException("Error converting Python object into char[$result_dim0]"); } @@ -481,27 +479,33 @@ } %enddef -PY_TYPECHECK_TYPEMAP(BOOL, bool, SPyObj_AsBool); -PY_TYPECHECK_TYPEMAP(INT8, signed char, SPyObj_AsSignedChar); -PY_TYPECHECK_TYPEMAP(UINT8, unsigned char, SPyObj_AsUnsignedChar); -PY_TYPECHECK_TYPEMAP(INT16, short, SPyObj_AsShort); -PY_TYPECHECK_TYPEMAP(UINT16, unsigned short, SPyObj_AsUnsignedShort); -PY_TYPECHECK_TYPEMAP(INT32, int, SPyObj_AsInt); -PY_TYPECHECK_TYPEMAP(UINT32, unsigned int, SPyObj_AsUnsignedInt); -PY_TYPECHECK_TYPEMAP(INT64, long, SPyObj_AsLong); -PY_TYPECHECK_TYPEMAP(UINT64, unsigned long, SPyObj_AsUnsignedLong); -PY_TYPECHECK_TYPEMAP(INT128, long long, SPyObj_AsLongLong); -PY_TYPECHECK_TYPEMAP(UINT128, unsigned long long, SPyObj_AsUnsignedLongLong); -PY_TYPECHECK_TYPEMAP(FLOAT, float, SPyObj_AsFloat); -PY_TYPECHECK_TYPEMAP(DOUBLE, double, SPyObj_AsDouble); -PY_TYPECHECK_TYPEMAP(CHAR, char, SPyObj_AsChar); +PY_TYPECHECK_TYPEMAP(BOOL, bool, SWIG_PyObj_AsBool); +PY_TYPECHECK_TYPEMAP(INT8, signed char, SWIG_PyObj_AsSignedChar); +PY_TYPECHECK_TYPEMAP(UINT8, unsigned char, SWIG_PyObj_AsUnsignedChar); +PY_TYPECHECK_TYPEMAP(INT16, short, SWIG_PyObj_AsShort); +PY_TYPECHECK_TYPEMAP(UINT16, unsigned short, SWIG_PyObj_AsUnsignedShort); +PY_TYPECHECK_TYPEMAP(INT32, int, SWIG_PyObj_AsInt); +PY_TYPECHECK_TYPEMAP(UINT32, unsigned int, SWIG_PyObj_AsUnsignedInt); +PY_TYPECHECK_TYPEMAP(INT64, long, SWIG_PyObj_AsLong); +PY_TYPECHECK_TYPEMAP(UINT64, unsigned long, SWIG_PyObj_AsUnsignedLong); +PY_TYPECHECK_TYPEMAP(INT128, long long, SWIG_PyObj_AsLongLong); +PY_TYPECHECK_TYPEMAP(UINT128, unsigned long long, SWIG_PyObj_AsUnsignedLongLong); +PY_TYPECHECK_TYPEMAP(FLOAT, float, SWIG_PyObj_AsFloat); +PY_TYPECHECK_TYPEMAP(DOUBLE, double, SWIG_PyObj_AsDouble); +PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); %typecheck(SWIG_TYPECHECK_STRING) char *, char const*, char *const, char const *const, char const*&, char *const&, char const *const & { - $1 = (($input == Py_None) || (PyString_Check($input))); + void *ptr; + if (SWIG_ConvertPtr($input, (void **) &ptr, $descriptor(char*), 0) == -1) { + PyErr_Clear(); + $1 = (PyString_Check($input)); + } else { + $1 = 1; + } } %typecheck(SWIG_TYPECHECK_STRING) char [ANY], const char[ANY] @@ -557,12 +561,12 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SPyObj_AsChar); %expand_primitives_from(PY_THROWS_TYPEMAP); %typemap(throws) char *, char const*, char * const, char const* const { - PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromCharPtr((const char*)$1)); + PyErr_SetObject(PyExc_RuntimeError, SWIG_PyObj_FromCharPtr((const char*)$1)); SWIG_fail; } %typemap(throws) char[ANY], const char[ANY] { - PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromCharArray((const char*)$1, $1_dim0)); + PyErr_SetObject(PyExc_RuntimeError, SWIG_PyObj_FromCharArray((const char*)$1, $1_dim0)); SWIG_fail; } @@ -639,7 +643,7 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SPyObj_AsChar); * ------------------------------------------------------------ */ %typemap(in) (char *STRING, int LENGTH) (char *buf, int len) { - SPyObj_AsCharPtrAndSize($input, &buf, &len); + SWIG_PyObj_AsCharPtrAndSize($input, $descriptor(char*), &buf, &len); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError,"a string is expected"); SWIG_fail; @@ -656,29 +660,30 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SPyObj_AsChar); /* Check if is a list */ int list = PyList_Check($input); if (list || PyTuple_Check($input)) { + int argc = list ? PyList_Size($input) : PyTuple_Size($input); + char **argv = (char **)%swig_new_carray((argc + 1)*sizeof(char*)); int i = 0; - int size = list ? PyList_Size($input) : PyTuple_Size($input); - $1 = ($1_ltype) size; - $2 = ($2_ltype) %swig_new_carray((size + 1)*sizeof(char*)); - for (; i < size; ++i) { - PyObject *obj = list ? - PyList_GetItem($input,i) : PyTuple_GetItem($input,i); - $2[i] = SPyObj_AsCharPtr(obj); + for (; i < argc; ++i) { + PyObject *obj = list ? PyList_GetItem($input,i) : PyTuple_GetItem($input,i); + argv[i] = SWIG_PyObj_AsCharPtr(obj, $descriptor(char*)); if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError,"list must contain strings only"); + PyErr_SetString(PyExc_TypeError,"list or tuple must contain strings only"); SWIG_fail; } } - $2[i] = 0; + argv[i] = 0; + $1 = ($1_ltype) argc; + $2 = ($2_ltype) argv; } else { - PyErr_SetString(PyExc_TypeError,"argument is not a python list or tuple"); + $1 = 0; + $2 = 0; + PyErr_SetString(PyExc_TypeError,"a list or tuple is expected"); SWIG_fail; } } %typemap(freearg) (int ARGC, char **ARGV) { - if ($2) - %swig_del_carray($2); + if ($2) %swig_del_carray($2); }