diff --git a/SWIG/Examples/test-suite/lib_std_string.i b/SWIG/Examples/test-suite/lib_std_string.i index 8c2c39be5..8d3bb2e71 100644 --- a/SWIG/Examples/test-suite/lib_std_string.i +++ b/SWIG/Examples/test-suite/lib_std_string.i @@ -35,6 +35,12 @@ std::string& test_reference_out() { return x; } +void test_throw() throw(std::string){ + static std::string x = "x"; + + throw x; +} + %} diff --git a/SWIG/Examples/test-suite/primitive_types.i b/SWIG/Examples/test-suite/primitive_types.i index cf0a3933b..fc90c1d42 100644 --- a/SWIG/Examples/test-suite/primitive_types.i +++ b/SWIG/Examples/test-suite/primitive_types.i @@ -471,6 +471,7 @@ macro(size_t, pfx, sizet) } %test_prim_types_ovr(ovr_decl, ovr); + virtual Test* vtest(Test* t) const throw (Test) { diff --git a/SWIG/Examples/test-suite/python/primitive_types_runme.py b/SWIG/Examples/test-suite/python/primitive_types_runme.py index 34043df2c..97434e690 100644 --- a/SWIG/Examples/test-suite/python/primitive_types_runme.py +++ b/SWIG/Examples/test-suite/python/primitive_types_runme.py @@ -315,12 +315,11 @@ 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' + t.var_namet = '123456' error = 1 except TypeError: if a != t.var_namet: diff --git a/SWIG/Lib/python/ccomplex.i b/SWIG/Lib/python/ccomplex.i new file mode 100644 index 000000000..f4b17cd50 --- /dev/null +++ b/SWIG/Lib/python/ccomplex.i @@ -0,0 +1,41 @@ +#ifndef __python_ccomplex_i__ +#define __python_ccomplex_i__ + +/* + * C complex wrap + * ISO C99: 7.3 Complex arithmetic + */ + +%{ +#include +%} + + +/* + the %{}% around these typedefs must be removed once + swig parser supports 'float complex'... +*/ +%{ + typedef float complex float_complex; + typedef double complex double_complex; +%} + + +%include "complex_common.i" + +#define CCOMPLEX(r, i) ((r) + I*(i)) +%swig_cplxflt_conv(float_complex, CCplxFlt, CCOMPLEX, creal, cimag) +%swig_cplxdbl_conv(double_complex, CCplxDbl, CCOMPLEX, creal, cimag) + +/* declaring the typemaps */ +%typemap_asfrom(float_complex, CPLXFLT, + SWIG_PyObj_AsCCplxFlt, SWIG_PyObj_FromCCplxFlt); + +%typemap_asfrom(double_complex, CPLXDBL, + SWIG_PyObj_AsCCplxDbl, SWIG_PyObj_FromCCplxDbl); + +%apply double_complex { complex }; + + + +#endif //__python_ccomplex_i__ diff --git a/SWIG/Lib/python/complex.i b/SWIG/Lib/python/complex.i new file mode 100644 index 000000000..37682ca08 --- /dev/null +++ b/SWIG/Lib/python/complex.i @@ -0,0 +1,10 @@ +#ifndef __python_complex_i__ +#define __python_complex_i__ + +#ifdef __cplusplus +%include +#else +%include +#endif + +#endif //__python_complex_i__ diff --git a/SWIG/Lib/python/complex_common.i b/SWIG/Lib/python/complex_common.i new file mode 100644 index 000000000..ec055a3a7 --- /dev/null +++ b/SWIG/Lib/python/complex_common.i @@ -0,0 +1,57 @@ +#ifndef __python_complex_common_i__ +#define __python_complex_common_i__ + +%define %swig_fromcplx_conv(Type, Name, Real, Imag) +%fragment("SWIG_PyObj_From"#Name,"header") +%{ +SWIGSTATIC(PyObject*) +SWIG_PyObj_From##Name(Type c) +{ + return PyComplex_FromDoubles(Real(c), Imag(c)); +} +%} +%enddef + +%define %swig_cplxdbl_conv(Type, Name, Constructor, Real, Imag) +%fragment("SWIG_PyObj_As"#Name,"header",fragment="SWIG_PyObj_AsDouble") +%{ +SWIGSTATIC(Type) +SWIG_PyObj_As##Name(PyObject *o) +{ + Type c = PyComplex_Check(o) ? + Constructor(PyComplex_RealAsDouble(o), + PyComplex_ImagAsDouble(o)) : + Constructor(SWIG_PyObj_AsDouble(o), 0); + if (PyErr_Occurred()){ + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a Type is expected"); + } + return c; +} +%} +%swig_fromcplx_conv(Type, Name, Real, Imag); +%enddef + +%define %swig_cplxflt_conv(Type, Name, Constructor, Real, Imag) +%fragment("SWIG_PyObj_As"#Name,"header",fragment="SWIG_PyObj_AsDouble") +%{ +SWIGSTATIC(Type) +SWIG_PyObj_As##Name(PyObject *o) +{ + Type c = PyComplex_Check(o) ? + Constructor(SWIG_PyObj_AsFloatConv(o, PyComplex_RealAsDouble), + SWIG_PyObj_AsFloatConv(o, PyComplex_ImagAsDouble)) : + Constructor(SWIG_PyObj_AsFloatConv(o, SWIG_PyObj_AsDouble), + 0); + if (PyErr_Occurred()){ + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a Type is expected"); + } + return c; + } +%} +%swig_fromcplx_conv(Type, Name, Real, Imag); +%enddef + + +#endif //__python_complex_common_i__ diff --git a/SWIG/Lib/python/pyprim.swg b/SWIG/Lib/python/pyprim.swg new file mode 100644 index 000000000..4f706ab9c --- /dev/null +++ b/SWIG/Lib/python/pyprim.swg @@ -0,0 +1,244 @@ +#ifndef __python_pyprim_swg__ +#define __python_pyprim_swg__ + +/* + Define the SWIG_PyObjAs/From methods for the basic types. In many + cases, these method are just aliases of the original python As/From + methods. In the other cases, some extra work is needed. +*/ + +%{ +#include +#include +#include + +#ifndef SWIGSTATIC +#ifdef __cplusplus +#define SWIGSTATIC(a) static inline a +#else +#define SWIGSTATIC(a) static a +#endif +#endif + +#ifndef numeric_cast +#ifdef __cplusplus +#ifdef HAVE_NUMERIC_CAST +#define numeric_cast(type,a) numeric_cast(a) +#else +#define numeric_cast(type,a) static_cast(a) +#endif +#else +#define numeric_cast(type,a) (type)(a) +#endif +#endif + +%} + +/* + no wrapped found needed here... yet, + and we define the names SWIG_PyObj for consistency + */ + +%{ +#define SWIG_PyObj_FromSignedChar PyInt_FromLong +#define SWIG_PyObj_FromUnsignedChar PyInt_FromLong +#define SWIG_PyObj_FromShort PyInt_FromLong +#define SWIG_PyObj_FromUnsignedShort PyInt_FromLong +#define SWIG_PyObj_FromInt PyInt_FromLong +#define SWIG_PyObj_FromLong PyInt_FromLong +#define SWIG_PyObj_FromFloat PyFloat_FromDouble +#define SWIG_PyObj_FromDouble PyFloat_FromDouble +#define SWIG_PyObj_FromFloat PyFloat_FromDouble +#define SWIG_PyObj_FromDouble PyFloat_FromDouble +%} + +%fragment("SWIG_PyObj_AsDouble","header") %{ +SWIGSTATIC(double) +SWIG_PyObj_AsDouble(PyObject *obj) +{ + return (PyFloat_Check(obj)) ? PyFloat_AsDouble(obj) : +#if HAVE_LONG_LONG + (double)((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj)); +#else + (double)((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLong(obj)); +#endif + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a double is expected"); + } +} +%} + + +%fragment("SWIG_PyObj_AsLong","header") %{ +SWIGSTATIC(long) +SWIG_PyObj_AsLong(PyObject * obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj); +} +%} + + +%fragment("SWIG_PyObj_FromLongLong","header") %{ +SWIGSTATIC(PyObject* ) +SWIG_PyObj_FromLongLong(long long value) +{ + return (value > (long long)(LONG_MAX)) ? + PyLong_FromLongLong(value) : PyInt_FromLong((long)value); +} +%} + +%fragment("SWIG_PyObj_FromUnsignedLongLong","header") %{ +SWIGSTATIC(PyObject* ) +SWIG_PyObj_FromUnsignedLongLong(unsigned long long value) +{ + return (value > (unsigned long long)(LONG_MAX)) ? + PyLong_FromUnsignedLongLong(value) : + PyInt_FromLong(numeric_cast(long, value)); +} +%} + +%fragment("SWIG_PyObj_AsLongLong","header") %{ +SWIGSTATIC(long long) +SWIG_PyObj_AsLongLong(PyObject *obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj); +} +%} + +%fragment("SWIG_PyObj_AsUnsignedLongLong","header", + fragment="SWIG_PyObj_AsUnsignedLong") %{ +SWIGSTATIC(unsigned long long) +SWIG_PyObj_AsUnsignedLongLong(PyObject *obj) +{ + return PyLong_Check(obj) ? + PyLong_AsUnsignedLongLong(obj) : SWIG_PyObj_AsUnsignedLong(obj); +} +%} + + +%fragment("SWIG_PyObj_FromUnsignedLong","header") %{ +SWIGSTATIC(PyObject* ) +SWIG_PyObj_FromUnsignedLong(unsigned long value) +{ + return (value > (unsigned long)(LONG_MAX)) ? + PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)value); +} +%} + +%fragment("SWIG_PyObj_AsSignedChar","header") %{ +SWIGSTATIC(signed char) +SWIG_PyObj_AsSignedChar(PyObject *obj) +{ + return numeric_cast(signed char, + SWIG_PyObj_AsLongInRange(obj, "signed char", SCHAR_MIN, SCHAR_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsShort","header") %{ +SWIGSTATIC(short) +SWIG_PyObj_AsShort(PyObject *obj) +{ + return numeric_cast(short, + SWIG_PyObj_AsLongInRange(obj, "short", SHRT_MIN, SHRT_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsInt","header") %{ +SWIGSTATIC(int) +SWIG_PyObj_AsInt(PyObject *obj) +{ + return numeric_cast(int, + SWIG_PyObj_AsLongInRange(obj, "int", INT_MIN, INT_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsUnsignedChar","header") %{ +SWIGSTATIC(unsigned char) +SWIG_PyObj_AsUnsignedChar(PyObject *obj) +{ + return numeric_cast(unsigned char, + SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned char", UCHAR_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsUnsignedShort","header") %{ +SWIGSTATIC(unsigned short ) +SWIG_PyObj_AsUnsignedShort(PyObject *obj) +{ + return numeric_cast(unsigned short, + SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned short", USHRT_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsUnsignedInt","header") %{ +SWIGSTATIC(unsigned int) +SWIG_PyObj_AsUnsignedInt(PyObject *obj) +{ + return numeric_cast(unsigned int, + SWIG_PyObj_AsUnsignedLongInRange(obj, "unsigned int", UINT_MAX)); +} +%} + +%fragment("SWIG_PyObj_AsFloat","header", + fragment="SWIG_PyObj_AsDouble") %{ +SWIGSTATIC(float) +SWIG_PyObj_AsFloat(PyObject *obj) +{ + return SWIG_PyObj_AsFloatConv(obj, SWIG_PyObj_AsDouble); +} +%} + +%fragment("SWIG_PyObj_FromChar","header") %{ +SWIGSTATIC(PyObject*) +SWIG_PyObj_FromChar(char c) +{ + return PyString_FromStringAndSize(&c,1); +} +%} + +%fragment("SWIG_PyObj_FromBool","header") %{ +SWIGSTATIC(PyObject*) +SWIG_PyObj_FromBool(bool value) +{ + PyObject *obj = value ? Py_True : Py_False; + Py_INCREF(obj); + return obj; +} +%} + +%fragment("SWIG_PyObj_AsBool","header") %{ +SWIGSTATIC(bool) +SWIG_PyObj_AsBool(PyObject *obj) +{ + return PyObject_IsTrue(obj) ? true : false; +} +%} + +%fragment("SWIG_PyObj_AsChar","header" ) %{ +SWIGSTATIC(char) +SWIG_PyObj_AsChar(PyObject *obj) +{ + char c = 0; + if (PyInt_Check(obj) || PyLong_Check(obj)) { + c = numeric_cast(char, + SWIG_PyObj_AsLongInRange(obj, "char",CHAR_MIN, CHAR_MAX)); + } else { + char* cptr; size_t csize; + SWIG_PyObj_AsCharPtrAndSize(obj, 0, &cptr, &csize); + if (csize == 1) { + c = cptr[0]; + } else { + PyErr_SetString(PyExc_TypeError, "a char is expected"); + } + } + return c; +} +%} + +%fragment("SWIG_PyObj_FromUnsignedInt","header", + fragment="SWIG_PyObj_FromUnsignedLong") %{ +#define SWIG_PyObj_FromUnsignedInt SWIG_PyObj_FromUnsignedLong +%} + +#endif //__python_pyprim_swg__ diff --git a/SWIG/Lib/python/pyrun.swg b/SWIG/Lib/python/pyrun.swg index 7aa5e9b75..d9c58cf0f 100644 --- a/SWIG/Lib/python/pyrun.swg +++ b/SWIG/Lib/python/pyrun.swg @@ -58,6 +58,8 @@ typedef struct swig_const_info { #define SWIG_InstallConstants(d, constants) \ SWIG_Python_InstallConstants(d, constants) +typedef double (*py_objasdbl_conv)(PyObject *obj); + #ifdef SWIG_NOINCLUDE SWIGIMPORT(int) SWIG_Python_ConvertPtr(PyObject *, void **, swig_type_info *, int); @@ -73,46 +75,23 @@ SWIGIMPORT(void) SWIG_Python_InstallConstants(PyObject *d, swig_con * 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); - +/* + utilities +*/ +SWIGIMPORT(char* ) SWIG_PyObj_AsCharPtr(PyObject *obj, swig_type_info* pchar_info); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromCharPtr(const char* cptr); +SWIGIMPORT(unsigned long) SWIG_PyObj_AsUnsignedLong(PyObject * obj); +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(void) SWIG_PyObj_AsCharPtrAndSize(PyObject *obj, swig_type_info* pchar_info, + char** cptr, size_t* size); +SWIGIMPORT(void) SWIG_PyObj_AsCharArray(PyObject *obj, swig_type_info* pchar_info, + char* carray, size_t size); +SWIGIMPORT(PyObject *) SWIG_PyObj_FromCharArray(const char* carray, size_t size); +SWIGIMPORT(float) SWIG_PyObj_AsFloatConv(PyObject *obj, py_objasdbl_conv pyconv); #else @@ -413,12 +392,6 @@ SWIG_Python_NewPackedObj(void *ptr, int sz, swig_type_info *type) { #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) { @@ -433,50 +406,11 @@ SWIG_PyObj_AsUnsignedLong(PyObject * obj) } } -#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); + long value = PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj); if (!PyErr_Occurred()) { if (value < min_value) { PyObject *err = @@ -513,53 +447,10 @@ SWIG_PyObj_AsUnsignedLongInRange(PyObject *obj, const char* type, 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) +SWIG_PyObj_AsFloatConv(PyObject *obj, py_objasdbl_conv pyconv) { - double value = SWIG_PyObj_AsDouble(obj); + double value = pyconv(obj); if (!PyErr_Occurred()) { if (value < FLT_MIN) { PyObject *err = @@ -580,29 +471,90 @@ SWIG_PyObj_AsFloat(PyObject *obj) SWIGRUNTIME(void) SWIG_PyObj_AsCharPtrAndSize(PyObject *obj, swig_type_info* pchar_info, - char** cptr, int* len) + char** cptr, size_t* size) { + int psize; if ((!pchar_info) || SWIG_ConvertPtr(obj,(void **)cptr, pchar_info, 0) == -1) { if (pchar_info && PyErr_Occurred()) PyErr_Clear(); - PyString_AsStringAndSize(obj, cptr, len); + PyString_AsStringAndSize(obj, cptr, &psize); + *size = (size_t) psize; } else { /* don't like strlen, but ... */ - *len = (*cptr) ? (int) (strlen(*cptr) + 1) : 0; + *size = (*cptr) ? (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); + +SWIGRUNTIME(char*) +SWIG_PyObj_AsNewCharPtr(PyObject *obj, swig_type_info* pchar_info) +{ + char *res = 0; + char* cptr; size_t csize; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &cptr, &csize); + if (!PyErr_Occurred() && cptr) { + /* we add the '0' terminator if needed */ + size_t size = (csize && !(cptr[csize - 1])) ? csize : csize + 1; + if (size) { +#ifdef __cplusplus + res = new char[size]; +#else + res = malloc(size); +#endif + if (csize) memcpy(res, cptr, csize); + if (csize < size) res[csize] = 0; + } } - return ptr; + return res; } SWIGRUNTIME(PyObject *) -SWIG_PyObj_FromCharPtr(const char* cptr) { +SWIG_PyObj_FromCharArray(const char* carray, size_t size) +{ + if (size > INT_MAX) { + PyObject *err = + PyString_FromFormat("a char array of size %d is not allowed in python", + size); + PyErr_SetObject(PyExc_TypeError, err); + Py_XDECREF(err); + Py_INCREF(Py_None); + return Py_None; + } else { + int psize = (int) size; + return PyString_FromStringAndSize(carray, psize); + } +} + +SWIGRUNTIME(void) +SWIG_PyObj_AsCharArray(PyObject *obj, swig_type_info* pchar_info, + char* carray, size_t size) +{ + char* cptr; size_t csize; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &cptr, &csize); + if (!PyErr_Occurred()) { + /* in C (but not in C++) you can do: + + char x[5] = "hello"; + + ie, assing the array using an extra '0' char. Here, + we assume the C behavior... + */ + if ((csize == size + 1) && !(cptr[csize-1])) --csize; + if (csize > 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 (csize) memcpy(carray, cptr, csize); + if (csize < size) memset(carray + csize, 0, size - csize); + } + } +} + +SWIGRUNTIME(PyObject *) +SWIG_PyObj_FromCharPtr(const char* cptr) +{ if (cptr) { return PyString_FromString(cptr); } else { @@ -611,102 +563,15 @@ SWIG_PyObj_FromCharPtr(const char* cptr) { } } -SWIGRUNTIME(char*) -SWIG_PyObj_AsNewCharPtr(PyObject *obj, swig_type_info* pchar_info) +SWIGRUNTIME(char* ) +SWIG_PyObj_AsCharPtr(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; + char* ptr; + if (SWIG_ConvertPtr(obj,(void **)&ptr, pchar_info, 0) == -1) { + if (PyErr_Occurred()) PyErr_Clear(); + ptr = PyString_AsString(obj); } - 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); + return ptr; } /* Install Constants */ @@ -723,7 +588,7 @@ SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { obj = PyFloat_FromDouble(constants[i].dvalue); break; case SWIG_PY_STRING: - obj = SWIG_PyObj_FromCharPtr((const char *) constants[i].pvalue); + obj = SWIG_PyObj_FromCharPtr((char *) constants[i].pvalue); break; case SWIG_PY_POINTER: obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); diff --git a/SWIG/Lib/python/python.swg b/SWIG/Lib/python/python.swg index 8ebf343ca..a2ce334ca 100644 --- a/SWIG/Lib/python/python.swg +++ b/SWIG/Lib/python/python.swg @@ -22,59 +22,20 @@ #define %pythoncode %insert("python") -/* auxiliar macros for char array alloc/dealloc */ +/* auxiliar memory allocators */ #ifdef __cplusplus -%define %swig_new_carray(size) new char[(size)] -%enddef -%define %swig_del_carray(cptr) delete [] cptr; -%enddef +#define %swig_new(type, size) new type[(size)] +#define %swig_delete(cptr) delete [] cptr; #else -%define %swig_new_carray(size) malloc((size)) -%enddef -%define %swig_del_carray(cptr) free((char*)cptr); -%enddef +#define %swig_new(type, size) (type*) malloc((size)*sizeof(type)) +#define %swig_delete(cptr) free((char*)cptr); #endif /* ----------------------------------------------------------------------------- * standard typemaps * ----------------------------------------------------------------------------- */ -/* --- macro to expand a given typemap macro using the As methods --- */ -%define %expand_primitives_as(typemap) - 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, 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, SWIG_PyObj_FromUnsignedLong); - typemap(unsigned long, SWIG_PyObj_FromUnsignedLong); - typemap(char, SWIG_PyObj_FromChar); - typemap(float, PyFloat_FromDouble); - typemap(double, PyFloat_FromDouble); - typemap(long long, SWIG_PyObj_FromLongLong); - typemap(unsigned long long, SWIG_PyObj_FromUnsignedLongLong); -%enddef /* ----------------------------------------------------------------------------- @@ -101,7 +62,7 @@ /* Primitive datatypes. */ %define PY_IN_TYPEMAP(type, pyobj_as) - %typemap(in) type { +%typemap(in,fragment=#pyobj_as) type { $1 = ($1_type) pyobj_as($input); if (PyErr_Occurred()) SWIG_fail; } @@ -112,8 +73,6 @@ } %enddef -%expand_primitives_as(PY_IN_TYPEMAP); - /* Pointers, references, and arrays */ %typemap(in) SWIGTYPE *, @@ -167,12 +126,10 @@ /* Primitive types */ %define PY_OUT_TYPEMAP(type, pyobj_from) - %typemap(out) type "$result = pyobj_from((type)$1);"; - %typemap(out) const type& "$result = pyobj_from((type) *($1));"; + %typemap(out,fragment=#pyobj_from) type "$result = pyobj_from((type)$1);"; + %typemap(out,fragment=#pyobj_from) const type& "$result = pyobj_from((type) *($1));"; %enddef -%expand_primitives_from(PY_OUT_TYPEMAP); - /* Pointers, references, and arrays */ %typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, $owner);"; @@ -221,7 +178,7 @@ /* primitive types */ %define PY_VARIN_TYPEMAP(type, pyobj_as) - %typemap(varin) type { + %typemap(varin,fragment=#pyobj_as) type { $1_type temp = ($1_type) pyobj_as($input); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); @@ -231,8 +188,6 @@ } %enddef -%expand_primitives_as(PY_VARIN_TYPEMAP); - /* char* and char[ANY] */ %typemap(varin) char * { char *cptr = SWIG_PyObj_AsNewCharPtr($input, $descriptor(char*)); @@ -240,7 +195,7 @@ PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); return 1; } - if ($1) %swig_del_carray($1); + if ($1) %swig_delete($1); $1 = cptr; } @@ -326,29 +281,34 @@ /* Primitive types */ %define PY_VAROUT_TYPEMAP(type, pyobj_from) - %typemap(varout) type, const type& "$result = pyobj_from((type)$1);"; + %typemap(varout,fragment=#pyobj_from) type, const type& "$result = pyobj_from((type)$1);"; %enddef -%expand_primitives_from(PY_VAROUT_TYPEMAP); - /* Pointers and arrays */ -%typemap(varout) SWIGTYPE *, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);"; +%typemap(varout) SWIGTYPE *, SWIGTYPE [] + "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);"; /* References */ -%typemap(varout) SWIGTYPE & "$result = SWIG_NewPointerObj((void *) &$1, $1_descriptor, 0);"; +%typemap(varout) SWIGTYPE & + "$result = SWIG_NewPointerObj((void *) &$1, $1_descriptor, 0);"; /* Member pointer */ -%typemap(varout) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);"; +%typemap(varout) SWIGTYPE (CLASS::*) + "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);"; /* Void */ %typemap(varout) void "Py_INCREF(Py_None); $result = Py_None;"; +/* Special typemap for char* return values */ +%typemap(varout) char*, char const*, char *const, char const *const + "$result = SWIG_PyObj_FromCharPtr($1);"; + /* Special typemap for character array return values */ -%typemap(varout) char *, const char * "$result = SWIG_PyObj_FromCharPtr((const char*)$1);"; +%typemap(varout) char [ANY], const char [ANY] + "$result = SWIG_PyObj_FromCharArray($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);"; +%typemap(varout) SWIGTYPE + "$result = SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0);"; /* ----------------------------------------------------------------------------- * --- Constants --- * @@ -356,13 +316,10 @@ /* Primitive types */ %define PY_CONSTCODE_TYPEMAP(type, pyobj_from) - %typemap(constcode) type + %typemap(constcode,fragment=#pyobj_from) type "PyDict_SetItemString(d,\"$symname\", pyobj_from((type)$value));"; %enddef -%expand_primitives_from(PY_CONSTCODE_TYPEMAP); - - /* Pointers, arrays, objects */ %typemap(consttab) char *, char const*, char * const, char const* const { SWIG_PY_STRING, (char*)"$symname", 0, 0, (void *)$value, 0} @@ -393,12 +350,10 @@ /* Primitive datatypes */ %define PY_DIRECTORIN_TYPEMAP(type, pyobj_from) - %typemap(directorin) type *DIRECTORIN "$input = pyobj_from((type) *$1_name);"; - %typemap(directorin) type, const type& "$input = pyobj_from((type) $1_name);"; + %typemap(directorin,fragment=#pyobj_from) type *DIRECTORIN "$input = pyobj_from((type) *$1_name);"; + %typemap(directorin,fragment=#pyobj_from) type, const type& "$input = pyobj_from((type) $1_name);"; %enddef -%expand_primitives_from(PY_DIRECTORIN_TYPEMAP); - /* 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 & @@ -413,21 +368,19 @@ /* --- directorout typemaps --- */ %define PY_DIRECTOROUT_TYPEMAP(Type, pyobj_as) - %typemap(directorargout) Type *DIRECTOROUT + %typemap(directorargout,fragment=#pyobj_as) Type *DIRECTOROUT "*$result = ($basetype) pyobj_as($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; -%typemap(directorout) Type +%typemap(directorout,fragment=#pyobj_as) Type "$result = ($basetype) pyobj_as($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; -%typemap(directorout) const Type& +%typemap(directorout,fragment=#pyobj_as) const Type& "$basetype temp = ($basetype) pyobj_as($input); $result = &temp; if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; - %typemap(directorout) Type &DIRECTOROUT = Type + %typemap(directorout,fragment=#pyobj_as) Type &DIRECTOROUT = Type %enddef -%expand_primitives_as(PY_DIRECTOROUT_TYPEMAP); - /* Char pointer and arrays */ %typemap(directorout) char *, char const*, char *const, char const* const { $result = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); @@ -466,10 +419,10 @@ /* ------------------------------------------------------------ * --- Typechecking rules --- * ------------------------------------------------------------ */ -%define PY_TYPECHECK_TYPEMAP(check, type, pyobj_as) -%typecheck(SWIG_TYPECHECK_##check) type, const type& +%define PY_TYPECHECK_TYPEMAP(check, type, pyobj_asorcheck) +%typemap(typecheck,fragment=#pyobj_asorcheck,precedence=SWIG_TYPECHECK_##check) type, const type& { - pyobj_as($input); + pyobj_asorcheck($input); if (PyErr_Occurred()) { $1 = 0; PyErr_Clear(); @@ -479,39 +432,29 @@ } %enddef -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 & { - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, $descriptor(char*), 0) == -1) { + SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); + if (PyErr_Occurred()) { + $1 = 0; PyErr_Clear(); - $1 = (PyString_Check($input)); } else { $1 = 1; - } + } } -%typecheck(SWIG_TYPECHECK_STRING) char [ANY], const char[ANY] +%typecheck(SWIG_TYPECHECK_CHAR_ARRAY) char [ANY], const char[ANY] { - $1 = (PyString_Check($input) - && (PyString_Size($input) <= $input_dim0)) ? 1 : 0; + char* carray = 0; size_t size = 0; + SWIG_PyObj_AsCharArray($input, $descriptor(char*), &carray, &size); + if (PyErr_Occurred()) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = ((carray != 0) && (size <= $input_dim0)); + } } %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] @@ -552,14 +495,12 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); * ------------------------------------------------------------ */ %define PY_THROWS_TYPEMAP(type, pyobj_from) - %typemap(throws) type { + %typemap(throws,fragment=#pyobj_from) type { PyErr_SetObject(PyExc_RuntimeError, pyobj_from((type)$1)); SWIG_fail; } %enddef -%expand_primitives_from(PY_THROWS_TYPEMAP); - %typemap(throws) char *, char const*, char * const, char const* const { PyErr_SetObject(PyExc_RuntimeError, SWIG_PyObj_FromCharPtr((const char*)$1)); SWIG_fail; @@ -613,6 +554,69 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); SWIG_fail; } +/* ------------------------------------------------------------ + * Basic as/from type specialization + * ------------------------------------------------------------ */ +/* + typemap definition for types with As/From/Check methods + */ +%define %typemap_asfromcheck(Type, CheckCode, AsType, FromType, CheckType) + PY_IN_TYPEMAP(Type, AsType); + PY_OUT_TYPEMAP(Type, FromType); + PY_VARIN_TYPEMAP(Type, AsType); + PY_VAROUT_TYPEMAP(Type, FromType); + PY_CONSTCODE_TYPEMAP(Type, FromType); + PY_DIRECTORIN_TYPEMAP(Type, FromType); + PY_DIRECTOROUT_TYPEMAP(Type, AsType); + PY_THROWS_TYPEMAP(Type, FromType); + PY_TYPECHECK_TYPEMAP(CheckCode, Type, CheckType); +%enddef + +/* + typemap definition for types with As/From methods. The cheking is + performed by calling the As method. + */ +%define %typemap_asfrom(Type, CheckCode, AsType, FromType) + PY_IN_TYPEMAP(Type, AsType); + PY_OUT_TYPEMAP(Type, FromType); + PY_VARIN_TYPEMAP(Type, AsType); + PY_VAROUT_TYPEMAP(Type, FromType); + PY_CONSTCODE_TYPEMAP(Type, FromType); + PY_DIRECTORIN_TYPEMAP(Type, FromType); + PY_DIRECTOROUT_TYPEMAP(Type, AsType); + PY_THROWS_TYPEMAP(Type, FromType); + PY_TYPECHECK_TYPEMAP(CheckCode, Type, AsType); +%enddef + +/* + typemap for simple types with conversor methods + named as SWIG_PyObj_As##Name/SWIG_PyObj_From##Name +*/ +%define %typemap_stype(Type, CheckCode, Name) +%typemap_asfrom(Type, CheckCode, SWIG_PyObj_As##Name,SWIG_PyObj_From##Name) +%enddef + +/* ------------------------------------------------------------ + * Primitive Types + * ------------------------------------------------------------ */ + +%include "pyprim.swg" + +%typemap_stype(bool, BOOL, Bool); +%typemap_stype(signed char, INT8, SignedChar); +%typemap_stype(unsigned char, UINT8, UnsignedChar); +%typemap_stype(short, INT16, Short); +%typemap_stype(unsigned short, UINT16, UnsignedShort); +%typemap_stype(int, INT32, Int); +%typemap_stype(unsigned int, UINT32, UnsignedInt); +%typemap_stype(long, INT64, Long); +%typemap_stype(unsigned long, UINT64, UnsignedLong); +%typemap_stype(long long, INT128, LongLong); +%typemap_stype(unsigned long long, UINT128, UnsignedLongLong); +%typemap_stype(float, FLOAT, Float); +%typemap_stype(double, DOUBLE, Double); +%typemap_stype(char, CHAR, Char); + /* ------------------------------------------------------------ * --- Enums --- * ------------------------------------------------------------ */ @@ -642,14 +646,26 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); * --- String & length --- * ------------------------------------------------------------ */ -%typemap(in) (char *STRING, int LENGTH) (char *buf, int len) { - SWIG_PyObj_AsCharPtrAndSize($input, $descriptor(char*), &buf, &len); +/* Here len doesn't include the '0' terminator */ +%typemap(in) (char *STRING, int LENGTH) (char *buf, size_t size) { + SWIG_PyObj_AsCharPtrAndSize($input, $descriptor(char*), &buf, &size); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError,"a string is expected"); SWIG_fail; } $1 = ($1_ltype) buf; - $2 = ($2_ltype) len; + $2 = ($2_ltype) (size && (buf[size -1] == 0)) ? size - 1 : size; +} + +/* Here size includes the '0' terminator */ +%typemap(in) (char *STRING, int SIZE) (char *buf, size_t size) { + SWIG_PyObj_AsCharPtrAndSize($input, $descriptor(char*), &buf, &size); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError,"a string is expected"); + SWIG_fail; + } + $1 = ($1_ltype) buf; + $2 = ($2_ltype) size; } /* ------------------------------------------------------------ @@ -661,7 +677,7 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); 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*)); + char **argv = %swig_new(char*, argc + 1); int i = 0; for (; i < argc; ++i) { PyObject *obj = list ? PyList_GetItem($input,i) : PyTuple_GetItem($input,i); @@ -683,7 +699,7 @@ PY_TYPECHECK_TYPEMAP(CHAR, char, SWIG_PyObj_AsChar); } %typemap(freearg) (int ARGC, char **ARGV) { - if ($2) %swig_del_carray($2); + if ($2) %swig_delete($2); } diff --git a/SWIG/Lib/python/std_complex.i b/SWIG/Lib/python/std_complex.i index 3c0122bdc..4ed5de93a 100644 --- a/SWIG/Lib/python/std_complex.i +++ b/SWIG/Lib/python/std_complex.i @@ -1,108 +1,28 @@ #ifndef SWIG_STD_COMPLEX_I_ #define SWIG_STD_COMPLEX_I_ -#ifdef SWIG - %{ -#include - -static inline int -SwigComplex_Check(PyObject *o) -{ - return PyComplex_Check(o) || PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o); -} - -template -__Complex -SwigComplex_As(PyObject *o) -{ - if (PyComplex_Check(o)) { - return __Complex(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o)); - } else if (PyFloat_Check(o)) { - return __Complex(PyFloat_AsDouble(o), 0); - } else if (PyInt_Check(o)) { - return __Complex(PyInt_AsLong(o), 0); - } else if (PyLong_Check(o)) { - return __Complex(PyLong_AsLongLong(o), 0); - } else { - PyErr_SetString(PyExc_TypeError, "Expecting a complex or compatible type"); - return __Complex(0,0); - } -} - -static inline std::complex -SwigComplex_AsComplexDouble(PyObject *o) -{ - return SwigComplex_As >(o); -} - - +#include %} +%include "complex_common.i" -%define swig_specialize_complex(Complex) - - %typecheck(SWIG_TYPECHECK_COMPLEX) - Complex, const Complex& - { - $1 = SwigComplex_Check($input); - } - - %typemap(in) Complex { - $1 = SwigComplex_As< Complex >($input); - if (PyErr_Occurred()) SWIG_fail; - } - - %typemap(in) const Complex& (Complex temp) { - temp = SwigComplex_As< Complex >($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp; - } - - %typemap(out) Complex { - $result = PyComplex_FromDoubles($1.real(), $1.imag()); - } - - %typemap(out) const Complex & { - $result = PyComplex_FromDoubles($1->real(), $1->imag()); - } - - // C++ proxy class typemaps - %typemap(directorin) Complex { - $input = PyComplex_FromDoubles($1_name.real(), $1_name.imag()); - } - - %typemap(directorin) const Complex & { - $input = PyComplex_FromDoubles($1_name.real(), $1_name.imag()); - } - - %typemap(directorout) Complex { - $result = SwigComplex_As< Complex >($input); - if (PyErr_Occurred()) { - throw Swig::DirectorTypeMismatchException("Expecting a complex or compatible type"); - } - } - - %typemap(directorout) const Complex & (Complex temp) { - temp = SwigComplex_As< Complex >($input); - if (PyErr_Occurred()) { - throw Swig::DirectorTypeMismatchException("Expecting a complex or compatible type"); - } - $result = &temp; - } - +/* defining the complex as/from converters */ -%enddef - -namespace std -{ - template class complex; -} +%swig_cplxdbl_conv(std::complex, StdCplxDbl, + std::complex, std::real, std::imag) + +%swig_cplxflt_conv(std::complex, StdCplxFlt, + std::complex, std::real, std::imag) + +/* declaring the typemaps */ + +%typemap_asfrom(std::complex, CPLXDBL, + SWIG_PyObj_AsStdCplxDbl, SWIG_PyObj_FromStdCplxDbl); + +%typemap_asfrom(std::complex, CPLXFLT, + SWIG_PyObj_AsStdCplxFlt, SWIG_PyObj_FromStdCplxFlt); -swig_specialize_complex(std::complex); -swig_specialize_complex(std::complex); - -#endif // SWIG #endif //SWIG_STD_COMPLEX_I_ diff --git a/SWIG/Lib/python/std_string.i b/SWIG/Lib/python/std_string.i index 25a2639e1..1c7bb6d21 100644 --- a/SWIG/Lib/python/std_string.i +++ b/SWIG/Lib/python/std_string.i @@ -12,68 +12,50 @@ // However, I think I'll wait until someone asks for it... // ------------------------------------------------------------------------ -%include exception.i - %{ #include %} -namespace std { - - class string; - - /* Overloading check */ - - %typemap(typecheck) string = char *; - %typemap(typecheck) const string & = char *; - - %typemap(in) string { - if (PyString_Check($input)) - $1 = std::string(PyString_AsString($input), - PyString_Size($input)); - else - SWIG_exception(SWIG_TypeError, "string expected"); - } - - %typemap(in) const string & (std::string temp) { - if (PyString_Check($input)) { - temp = std::string(PyString_AsString($input), - PyString_Size($input)); - $1 = &temp; - } else { - SWIG_exception(SWIG_TypeError, "string expected"); - } - } - - %typemap(out) string { - $result = PyString_FromStringAndSize($1.data(),$1.size()); - } - - %typemap(out) const string & { - $result = PyString_FromStringAndSize($1->data(),$1->size()); - } - - %typemap(directorin, parse="s") string, const string &, string & "$1_name.c_str()"; - - %typemap(directorin, parse="s") string *, const string * "$1_name->c_str()"; - - %typemap(directorout) string { - if (PyString_Check($input)) - $result = std::string(PyString_AsString($input), - PyString_Size($input)); - else - throw Swig::DirectorTypeMismatchException("string expected"); - } - - %typemap(directorout) const string & (std::string temp) { - if (PyString_Check($input)) { - temp = std::string(PyString_AsString($input), - PyString_Size($input)); - $result = &temp; - } else { - throw Swig::DirectorTypeMismatchException("string expected"); - } - } +/* defining the std::string as/from converters */ +%fragment("SWIG_PyObj_AsStdString","header") %{ +static inline std::string +SWIG_PyObj_AsStdString(PyObject* obj) { + static swig_type_info* pchar_info = SWIG_TypeQuery("char *"); + char* buf = 0 ; size_t size = 0; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &buf, &size); + if (PyErr_Occurred() || !buf) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError,"a string is expected"); + return std::string(); + } + return std::string(buf, size); } +%} +%fragment("SWIG_PyObj_CheckStdString","header") %{ +static inline void +SWIG_PyObj_CheckStdString(PyObject* obj) { + static swig_type_info* pchar_info = SWIG_TypeQuery("char *"); + char* buf = 0; size_t size = 0; + SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &buf, &size); + if (PyErr_Occurred() || !buf) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError,"a string is expected"); + } +} +%} + +%fragment("SWIG_PyObj_FromStdString","header") %{ +static inline PyObject* +SWIG_PyObj_FromStdString(const std::string& s) { + return SWIG_PyObj_FromCharArray(s.data(), s.size()); +} +%} + + +/* declaring the typemaps */ + +%typemap_asfromcheck(std::string, STRING, + SWIG_PyObj_AsStdString, SWIG_PyObj_FromStdString, + SWIG_PyObj_CheckStdString);