diff --git a/Lib/python/argcargv.i b/Lib/python/argcargv.i new file mode 100644 index 000000000..4b3204005 --- /dev/null +++ b/Lib/python/argcargv.i @@ -0,0 +1,68 @@ +/* ------------------------------------------------------------ + * --- Argc & Argv --- + * ------------------------------------------------------------ */ + +%fragment("SWIG_AsArgcArgv","header") %{ +SWIGSTATIC(char**) +SWIG_AsArgcArgv(PyObject* input, + swig_type_info* ppchar_info, + swig_type_info* pchar_info, + size_t* argc, int* owner) +{ + char **argv = 0; + size_t i = 0; + if (SWIG_ConvertPtr(input, (void **)&argv, ppchar_info, 0) == -1) { + PyErr_Clear(); + int list = PyList_Check(input); + if (list || PyTuple_Check(input)) { + *argc = list ? PyList_Size(input) : PyTuple_Size(input); + argv = swig_new_array(char*, *argc + 1); + *owner = 1; + for (; i < *argc; ++i) { + PyObject *obj = list ? PyList_GetItem(input,i) : PyTuple_GetItem(input,i); + argv[i] = SWIG_AsCharPtr(obj, pchar_info); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError,"list or tuple must contain strings only"); + } + } + argv[i] = 0; + return argv; + } else { + *argc = 0; + PyErr_SetString(PyExc_TypeError,"a list or tuple is expected"); + return 0; + } + } else { + /* seems dangerous, but the user asked for it... */ + while (argv[i] != 0) ++i; + *argc = i; + owner = 0; + return argv; + } +} +%} + +/* + This typemap works with either a char**, a python list or a python + tuple + */ + +%typemap(in,fragment="SWIG_AsArgcArgv") (int ARGC, char **ARGV) + (int owner) { + size_t argc = 0; + char **argv = SWIG_AsArgcArgv($input, $descriptor(char**), + $descriptor(char*), &argc, &owner); + if (PyErr_Occurred()) { + $1 = 0; $2 = 0; + SWIG_fail; + } else { + $1 = ($1_ltype) argc; + $2 = ($2_ltype) argv; + } +} + +%typemap(freearg) (int ARGC, char **ARGV) (owner) { + if (owner) swig_delete_array($2); +} + diff --git a/Lib/python/ccomplex.i b/Lib/python/ccomplex.i index f4b17cd50..c6e0336a3 100644 --- a/Lib/python/ccomplex.i +++ b/Lib/python/ccomplex.i @@ -1,17 +1,19 @@ #ifndef __python_ccomplex_i__ #define __python_ccomplex_i__ +%include "complex_common.i" + /* * C complex wrap - * ISO C99: 7.3 Complex arithmetic + * ISO C99: 7.3 Complex arithmetic */ %{ #include %} - /* +*** swig workaround *** the %{}% around these typedefs must be removed once swig parser supports 'float complex'... */ @@ -20,19 +22,18 @@ typedef double complex double_complex; %} +/* C complex constructor */ +#define CCplxConst(r, i) ((r) + I*(i)) -%include "complex_common.i" +%swig_cplxflt_conv(float_complex, CCplxFlt, + CCplxConst, creal, cimag); -#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) +%swig_cplxdbl_conv(double_complex, CCplxDbl, + CCplxConst, 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); +%typemap_stype(float_complex, CPLXFLT, CCplxFlt); +%typemap_stype(double_complex, CPLXDBL, CCplxDbl); %apply double_complex { complex }; diff --git a/Lib/python/complex_common.i b/Lib/python/complex_common.i index ec055a3a7..76daa4a1d 100644 --- a/Lib/python/complex_common.i +++ b/Lib/python/complex_common.i @@ -1,27 +1,40 @@ #ifndef __python_complex_common_i__ #define __python_complex_common_i__ +/* + Defines the As/From conversors for double/float complex, you need to + provide complex Type, the Name you want to use in the conversors, + the complex Constructor method, and the Real and Imag complex + accesor methods. + + See the std_complex.i and ccomplex.i for concret examples. +*/ + +/* the common from conversor */ %define %swig_fromcplx_conv(Type, Name, Real, Imag) -%fragment("SWIG_PyObj_From"#Name,"header") +%fragment("SWIG_From"#Name,"header") %{ SWIGSTATIC(PyObject*) -SWIG_PyObj_From##Name(Type c) +SWIG_From##Name(Type c) { return PyComplex_FromDoubles(Real(c), Imag(c)); } %} %enddef + +/* the double case */ %define %swig_cplxdbl_conv(Type, Name, Constructor, Real, Imag) -%fragment("SWIG_PyObj_As"#Name,"header",fragment="SWIG_PyObj_AsDouble") +%fragment("SWIG_As"#Name,"header", + fragment="SWIG_AsDouble") %{ SWIGSTATIC(Type) -SWIG_PyObj_As##Name(PyObject *o) +SWIG_As##Name(PyObject *o) { Type c = PyComplex_Check(o) ? Constructor(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o)) : - Constructor(SWIG_PyObj_AsDouble(o), 0); + Constructor(SWIG_AsDouble(o), 0); if (PyErr_Occurred()){ PyErr_Clear(); PyErr_SetString(PyExc_TypeError, "a Type is expected"); @@ -32,24 +45,27 @@ SWIG_PyObj_As##Name(PyObject *o) %swig_fromcplx_conv(Type, Name, Real, Imag); %enddef +/* the float case */ %define %swig_cplxflt_conv(Type, Name, Constructor, Real, Imag) -%fragment("SWIG_PyObj_As"#Name,"header",fragment="SWIG_PyObj_AsDouble") +%fragment("SWIG_As"#Name,"header", + fragment="SWIG_CheckFloat", + fragment="SWIG_AsDouble") %{ SWIGSTATIC(Type) -SWIG_PyObj_As##Name(PyObject *o) +SWIG_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()){ + Constructor(SWIG_CheckFloat(PyComplex_RealAsDouble(o)), + SWIG_CheckFloat(PyComplex_RealAsDouble(o))) : + Constructor(SWIG_CheckFloat(SWIG_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 diff --git a/Lib/python/pymisctypes.swg b/Lib/python/pymisctypes.swg new file mode 100644 index 000000000..c57436211 --- /dev/null +++ b/Lib/python/pymisctypes.swg @@ -0,0 +1,17 @@ + +/* ------------------------------------------------------------ + * --- ANSI/Posix C/C++ types --- + * ------------------------------------------------------------ */ + +%apply unsigned long { size_t }; +%apply const unsigned long& { const size_t& }; +%apply long { ptrdiff_t }; +%apply const long& { const ptrdiff_t& }; + +#ifdef __cplusplus +%apply unsigned long { std::size_t }; +%apply const unsigned long& { const std::size_t& }; +%apply long { std::ptrdiff_t }; +%apply const long& { const std::ptrdiff_t& }; +#endif + diff --git a/Lib/python/pyobject.swg b/Lib/python/pyobject.swg new file mode 100644 index 000000000..06048431f --- /dev/null +++ b/Lib/python/pyobject.swg @@ -0,0 +1,17 @@ +/* ------------------------------------------------------------ + * PyObject * - Just pass straight through unmodified + * ------------------------------------------------------------ */ + +%typemap(in) PyObject * "$1 = $input;"; +%typemap(out) PyObject * "$result = $1;"; + +%typemap(constcode) PyObject * "PyDict_SetItemString(d,\"$symname\", $value);"; + +%typemap(directorin, parse="O") PyObject * ""; +%typemap(directorout) PyObject * "$result = $input;"; + +%typecheck(SWIG_TYPECHECK_POINTER) PyObject * "$1 = ($input != 0);"; + +%typemap(throws) PyObject * + "PyErr_SetObject(PyExc_RuntimeError, $1); + SWIG_fail;"; diff --git a/Lib/python/pyopers.swg b/Lib/python/pyopers.swg new file mode 100644 index 000000000..3911ff517 --- /dev/null +++ b/Lib/python/pyopers.swg @@ -0,0 +1,51 @@ +/* ------------------------------------------------------------ + * Overloaded operator support + * ------------------------------------------------------------ */ + +#ifdef __cplusplus +%rename(__add__) *::operator+; +%rename(__pos__) *::operator+(); +%rename(__pos__) *::operator+() const; +%rename(__sub__) *::operator-; +%rename(__neg__) *::operator-(); +%rename(__neg__) *::operator-() const; +%rename(__mul__) *::operator*; +%rename(__div__) *::operator/; +%rename(__mod__) *::operator%; +%rename(__lshift__) *::operator<<; +%rename(__rshift__) *::operator>>; +%rename(__and__) *::operator&; +%rename(__or__) *::operator|; +%rename(__xor__) *::operator^; +%rename(__invert__) *::operator~; +%rename(__iadd__) *::operator+=; +%rename(__isub__) *::operator-=; +%rename(__imul__) *::operator*=; +%rename(__idiv__) *::operator/=; +%rename(__imod__) *::operator%=; +%rename(__ilshift__) *::operator<<=; +%rename(__irshift__) *::operator>>=; +%rename(__iand__) *::operator&=; +%rename(__ior__) *::operator|=; +%rename(__ixor__) *::operator^=; +%rename(__lt__) *::operator<; +%rename(__le__) *::operator<=; +%rename(__gt__) *::operator>; +%rename(__ge__) *::operator>=; +%rename(__eq__) *::operator==; +%rename(__ne__) *::operator!=; + +/* Special cases */ +%rename(__call__) *::operator(); + +/* Ignored operators */ +%ignorewarn("362:operator= ignored") operator=; +%ignorewarn("383:operator++ ignored") operator++; +%ignorewarn("384:operator-- ignored") operator--; +%ignorewarn("361:operator! ignored") operator!; +%ignorewarn("381:operator&& ignored") operator&&; +%ignorewarn("382:operator|| ignored") operator||; +%ignorewarn("386:operator->* ignored") operator->*; +%ignorewarn("389:operator[] ignored (consider using %extend)") operator[]; + +#endif diff --git a/Lib/python/pyprim.swg b/Lib/python/pyprim.swg deleted file mode 100644 index 4f706ab9c..000000000 --- a/Lib/python/pyprim.swg +++ /dev/null @@ -1,244 +0,0 @@ -#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/Lib/python/pyprimtypes.swg b/Lib/python/pyprimtypes.swg new file mode 100644 index 000000000..f7912210a --- /dev/null +++ b/Lib/python/pyprimtypes.swg @@ -0,0 +1,364 @@ +/* ------------------------------------------------------------ + * Primitive Types + * ------------------------------------------------------------ */ + +/* + Define the SWIGAs/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. +*/ + +/* + no wrapped found needed here... yet, + and we define the names SWIG for consistency + */ + +%{ +#define SWIG_FromSignedChar PyInt_FromLong +#define SWIG_FromUnsignedChar PyInt_FromLong +#define SWIG_FromShort PyInt_FromLong +#define SWIG_FromUnsignedShort PyInt_FromLong +#define SWIG_FromInt PyInt_FromLong +#define SWIG_FromLong PyInt_FromLong +#define SWIG_FromFloat PyFloat_FromDouble +#define SWIG_FromDouble PyFloat_FromDouble +#define SWIG_FromFloat PyFloat_FromDouble +#define SWIG_FromDouble PyFloat_FromDouble +%} + +%fragment("","header") %{ +#include +%} + +%fragment("SWIG_AsUnsignedLong","header") %{ +SWIGSTATICINLINE(unsigned long) +SWIG_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; + } +} +%} + +%fragment("SWIG_CheckLongInRange","header", + fragment="") %{ +SWIGSTATICINLINE(long) +SWIG_CheckLongInRange(long value, const char* type, + long min_value, long max_value) +{ + 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_DECREF(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_DECREF(err); + } + } + return value; +} +%} + +%fragment("SWIG_CheckUnsignedLongInRange","header", + fragment="") %{ +SWIGSTATICINLINE(unsigned long) +SWIG_CheckUnsignedLongInRange(unsigned long value, const char* type, + unsigned long max_value) +{ + 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_DECREF(err); + } + } + return value; +} +%} + + +%fragment("SWIG_AsDouble","header") %{ +SWIGSTATICINLINE(double) +SWIG_AsDouble(PyObject *obj) +{ + double val = (PyFloat_Check(obj)) ? PyFloat_AsDouble(obj) : +#if HAVE_LONG_LONG + ((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj)); +#else + ((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLong(obj)); +#endif + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a double is expected"); + } + return val; +} +%} + +%fragment("SWIG_AsLong","header") %{ +SWIGSTATICINLINE(long) +SWIG_AsLong(PyObject * obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj); +} +%} + +%fragment("SWIG_FromLongLong","header", + fragment="") %{ +SWIGSTATICINLINE(PyObject* ) +SWIG_FromLongLong(long long value) +{ + return (value > LONG_MAX) ? + PyLong_FromLongLong(value) + : PyInt_FromLong(swig_numeric_cast(long,value)); +} +%} + +%fragment("SWIG_FromUnsignedLongLong","header", + fragment="") %{ +SWIGSTATICINLINE(PyObject* ) +SWIG_FromUnsignedLongLong(unsigned long long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLongLong(value) : + PyInt_FromLong(swig_numeric_cast(long, value)); +} +%} + +%fragment("SWIG_AsLongLong","header") %{ +SWIGSTATICINLINE(long long) +SWIG_AsLongLong(PyObject *obj) +{ + return PyInt_Check(obj) ? + PyInt_AsLong(obj) : PyLong_AsLongLong(obj); +} +%} + +%fragment("SWIG_AsUnsignedLongLong","header", + fragment="SWIG_AsUnsignedLong") %{ +SWIGSTATICINLINE(unsigned long long) +SWIG_AsUnsignedLongLong(PyObject *obj) +{ + return PyLong_Check(obj) ? + PyLong_AsUnsignedLongLong(obj) : SWIG_AsUnsignedLong(obj); +} +%} + +%fragment("SWIG_FromUnsignedLong","header") %{ +SWIGSTATICINLINE(PyObject* ) +SWIG_FromUnsignedLong(unsigned long value) +{ + return (value > LONG_MAX) ? + PyLong_FromUnsignedLong(value) + : PyInt_FromLong(swig_numeric_cast(long,value)); +} +%} + +%fragment("SWIG_AsSignedChar","header", + fragment="SWIG_CheckLongInRange", + fragment="SWIG_AsLong") %{ +SWIGSTATICINLINE(signed char) +SWIG_AsSignedChar(PyObject *obj) +{ + return swig_numeric_cast(signed char, + SWIG_CheckLongInRange(SWIG_AsLong(obj), + "signed char", SCHAR_MIN, SCHAR_MAX)); +} +%} + +%fragment("SWIG_AsShort","header", + fragment="SWIG_CheckLongInRange", + fragment="SWIG_AsLong") %{ +SWIGSTATICINLINE(short) +SWIG_AsShort(PyObject *obj) +{ + return swig_numeric_cast(short, + SWIG_CheckLongInRange(SWIG_AsLong(obj), + "short", SHRT_MIN, SHRT_MAX)); +} +%} + +/* need range checks */ + +%fragment("SWIG_AsInt","header", + fragment="SWIG_CheckLongInRange", + fragment="SWIG_AsLong") %{ +#if INT_MAX != LONG_MAX +SWIGSTATICINLINE(int) +SWIG_AsInt(PyObject *obj) +{ + return swig_numeric_cast(int, + SWIG_CheckLongInRange(SWIG_AsLong(obj), + "int", INT_MIN, INT_MAX)); +} +#else +#define SWIG_AsInt SWIG_AsLong +#endif +%} +%fragment("SWIG_AsUnsignedInt","header", + fragment="SWIG_CheckUnsignedLongInRange", + fragment="SWIG_AsUnsignedLong") %{ +#if UINT_MAX != ULONG_MAX +SWIGSTATICINLINE(unsigned int) +SWIG_AsUnsignedInt(PyObject *obj) +{ + return swig_numeric_cast(unsigned int, + SWIG_CheckUnsignedLongInRange(SWIG_AsUnsignedLong(obj), + "unsigned int", UINT_MAX)); +} +#else +#define SWIG_AsUnsignedInt SWIG_AsUnsignedLong +#endif +%} + +%fragment("SWIG_FromUnsignedInt","header", + fragment="SWIG_FromUnsignedLong") %{ +#if UINT_MAX < LONG_MAX +#define SWIG_FromUnsignedInt SWIG_FromLong +#else +#define SWIG_FromUnsignedInt SWIG_FromUnsignedLong +#endif +%} + + +%fragment("SWIG_AsUnsignedChar","header", + fragment="SWIG_CheckUnsignedLongInRange", + fragment="SWIG_AsUnsignedLong") %{ +SWIGSTATICINLINE(unsigned char) +SWIG_AsUnsignedChar(PyObject *obj) +{ + return swig_numeric_cast(unsigned char, + SWIG_CheckUnsignedLongInRange(SWIG_AsUnsignedLong(obj), + "unsigned char", UCHAR_MAX)); +} +%} + +%fragment("SWIG_AsUnsignedShort","header", + fragment="SWIG_CheckUnsignedLongInRange", + fragment="SWIG_AsUnsignedLong") %{ +SWIGSTATICINLINE(unsigned short ) +SWIG_AsUnsignedShort(PyObject *obj) +{ + return swig_numeric_cast(unsigned short, + SWIG_CheckUnsignedLongInRange(SWIG_AsUnsignedLong(obj), + "unsigned short", USHRT_MAX)); +} +%} + + +%fragment("SWIG_FloatCast","header") %{ +#include + +SWIGSTATIC(float) +SWIG_FloatCast(double value) +{ + float f = 0; + 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_DECREF(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_DECREF(err); + } else { + f = swig_numeric_cast(float, value); + } + } + return f; +} +%} + +%fragment("SWIG_AsFloat","header", + fragment="SWIG_FloatCast", + fragment="SWIG_AsDouble") %{ +SWIGSTATICINLINE(float) +SWIG_AsFloat(PyObject *obj) +{ + return SWIG_FloatCast(SWIG_AsDouble(obj)); +} +%} + +%fragment("SWIG_FromChar","header") %{ +SWIGSTATICINLINE(PyObject*) +SWIG_FromChar(char c) +{ + return PyString_FromStringAndSize(&c,1); +} +%} + +%fragment("SWIG_FromBool","header") %{ +SWIGSTATICINLINE(PyObject*) +SWIG_FromBool(bool value) +{ + PyObject *obj = value ? Py_True : Py_False; + Py_INCREF(obj); + return obj; +} +%} + +%fragment("SWIG_AsBool","header") %{ +SWIGSTATICINLINE(bool) +SWIG_AsBool(PyObject *obj) +{ + return PyObject_IsTrue(obj) ? true : false; +} +%} + +%fragment("SWIG_AsChar","header", + fragment="SWIG_AsCharArray", + fragment="SWIG_CheckLongInRange", + fragment="SWIG_AsLong") %{ +SWIGSTATICINLINE(char) +SWIG_AsChar(PyObject *obj) +{ + char c = 0; + if (PyInt_Check(obj) || PyLong_Check(obj)) { + c = swig_numeric_cast(char, + SWIG_CheckLongInRange(SWIG_AsLong(obj), + "char", CHAR_MIN, CHAR_MAX)); + } else { + SWIG_AsCharArray(obj, &c, 1); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a char is expected"); + } + } + return c; +} +%} + +%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); diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 17b57e4d7..b484a5254 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -71,27 +71,6 @@ 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 */ -/* - 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 @@ -314,10 +293,10 @@ cobject: type_error: if (flags & SWIG_POINTER_EXCEPTION) { if (ty && c) { - char *temp = (char *) malloc(64+strlen(ty->name)+strlen(c)); - sprintf(temp,"Type error. Got %s, expected %s", c, ty->name); - PyErr_SetString(PyExc_TypeError, temp); - free((char *) temp); + PyObject *err = + PyString_FromFormat("Type error. Got %s, expected %s",c,ty->name); + PyErr_SetObject(PyExc_TypeError, err); + Py_DECREF(err); } else { PyErr_SetString(PyExc_TypeError,"Expected a pointer"); } @@ -355,10 +334,10 @@ type_error: if (flags) { if (ty && c) { - char *temp = (char *) malloc(64+strlen(ty->name)+strlen(c)); - sprintf(temp,"Type error. Got %s, expected %s", c, ty->name); - PyErr_SetString(PyExc_TypeError, temp); - free((char *) temp); + PyObject *err = + PyString_FromFormat("Type error. Got %s, expected %s",c,ty->name); + PyErr_SetObject(PyExc_TypeError, err); + Py_DECREF(err); } else { PyErr_SetString(PyExc_TypeError,"Expected a pointer"); } @@ -416,196 +395,6 @@ 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(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; - } -} - -SWIGRUNTIME(long) -SWIG_PyObj_AsLongInRange(PyObject * obj, const char* type, - long min_value, long max_value) -{ - long value = PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLongLong(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(float) -SWIG_PyObj_AsFloatConv(PyObject *obj, py_objasdbl_conv pyconv) -{ - double value = pyconv(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, 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, &psize); - *size = (size_t) psize; - } else { - /* don't like strlen, but ... */ - *size = (*cptr) ? (strlen(*cptr) + 1) : 0; - } -} - - -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 res; -} - -SWIGRUNTIME(PyObject *) -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 { - Py_INCREF(Py_None); - return Py_None; - } -} - -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; -} - /* Install Constants */ SWIGRUNTIME(void) SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { @@ -620,7 +409,12 @@ SWIG_Python_InstallConstants(PyObject *d, swig_const_info constants[]) { obj = PyFloat_FromDouble(constants[i].dvalue); break; case SWIG_PY_STRING: - obj = SWIG_PyObj_FromCharPtr((char *) constants[i].pvalue); + if (constants[i].pvalue) { + obj = PyString_FromString((char *) constants[i].pvalue); + } else { + Py_INCREF(Py_None); + obj = Py_None; + } break; case SWIG_PY_POINTER: obj = SWIG_NewPointerObj(constants[i].pvalue, *(constants[i]).ptype,0); diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg new file mode 100644 index 000000000..b5bbb69ed --- /dev/null +++ b/Lib/python/pystrings.swg @@ -0,0 +1,384 @@ + +/* ------------------------------------------------------------ + * utility methods for strings handling + * ------------------------------------------------------------ */ + +%types(char *); + +%fragment("SWIG_AsCharPtrAndSize","header") %{ +/* returns '1' if the input is a raw char*, '0' if is a PyString */ +SWIGSTATIC(int) +SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* size) +{ + static swig_type_info* pchar_info = 0; + int psize = 0; + if (!pchar_info) pchar_info = SWIG_TypeQuery("char *"); + + if (SWIG_ConvertPtr(obj, swig_reinterpret_cast(void **,cptr), pchar_info, 0) == -1) { + PyErr_Clear(); + PyString_AsStringAndSize(obj, cptr, &psize); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError,"a string is expected"); + } + if (size) *size = psize; + return 0; + } else { + if (size) *size = (*cptr) ? (strlen(*cptr) + 1) : 0; + return 1; + } +} +%} + +%fragment("SWIG_AsCharPtr","header", + fragment="SWIG_AsCharPtrAndSize") %{ +SWIGSTATICINLINE(char* ) +SWIG_AsCharPtr(PyObject *obj) +{ + char* cptr; + SWIG_AsCharPtrAndSize(obj, &cptr, 0); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a char* is expected"); + } + return cptr; +} +%} + +%fragment("SWIG_FromCharPtr","header") %{ +SWIGSTATICINLINE(PyObject *) +SWIG_FromCharPtr(const char* cptr) +{ + size_t size = cptr ? strlen(cptr) : 0; + if (cptr) { + if (size > INT_MAX) { + return SWIG_NewPointerObj(swig_const_cast(char*,cptr), + SWIG_TypeQuery("char *"), 0); + } else { + return PyString_FromStringAndSize(cptr, swig_numeric_cast(int,size)); + } + } else { + Py_INCREF(Py_None); + return Py_None; + } +} +%} + +%fragment("SWIG_AsNewCharPtr","header", + fragment="SWIG_AsCharPtrAndSize") %{ +SWIGSTATIC(char*) +SWIG_AsNewCharPtr(PyObject *obj) +{ + char *res = 0; + char* cptr; size_t csize; + int is_raw_pchar = SWIG_AsCharPtrAndSize(obj, &cptr, &csize); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a char* is expected"); + } else if (cptr) { + /* we add the '0' terminator if needed */ + size_t size = (!is_raw_pchar && csize && !(cptr[csize - 1])) ? + csize : csize + 1; + if (size) { + res = swig_new_array(char, size); + if (csize) memcpy(res, cptr, csize); + if (csize < size) res[csize] = 0; + } + } + return res; +} +%} + +%fragment("SWIG_AsCharArray","header", + fragment="SWIG_AsCharPtrAndSize") %{ +SWIGSTATIC(void) +SWIG_AsCharArray(PyObject *obj, char* carray, size_t size) +{ + char* cptr; size_t csize; + SWIG_AsCharPtrAndSize(obj, &cptr, &csize); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyObject *err = + PyString_FromFormat("a char array of size %d is expected", size); + PyErr_SetObject(PyExc_TypeError, err); + Py_DECREF(err); + } else { + /* in C (but not in C++) you can do: + + char x[5] = "hello"; + + ie, assing the array using an extra '0' char. + */ +#ifndef __cplusplus + if ((csize == size + 1) && !(cptr[csize-1])) --csize; +#endif + if (csize > size) { + PyObject *err = + PyString_FromFormat("a char array of maximum size %d is expected", + size); + PyErr_SetObject(PyExc_TypeError, err); + Py_DECREF(err); + } else { + if (csize) memcpy(carray, cptr, csize); + if (csize < size) memset(carray + csize, 0, size - csize); + } + } +} +%} + +%fragment("SWIG_FromCharArray","header") %{ +SWIGSTATICINLINE(PyObject *) +SWIG_FromCharArray(const char* carray, size_t size) +{ + if (size > INT_MAX) { + SWIG_NewPointerObj(swig_const_cast(char*,carray), SWIG_TypeQuery("char *"), 0); + return Py_None; + } else { + return PyString_FromStringAndSize(carray, swig_numeric_cast(int,size)); + } +} +%} + +/* ------------------------------------------------------------ + * The plain char * handling + * ------------------------------------------------------------ */ + +/* in */ + +%typemap(in,fragment="SWIG_AsCharPtr") + char *, char const*, char *const, char const *const + "$1 = SWIG_AsCharPtr($input); + if (PyErr_Occurred()) SWIG_fail;"; + +%typemap(in,fragment="SWIG_AsCharPtr") + char const*&, char *const&, char const *const & +{ + $*ltype temp = SWIG_AsCharPtr($input); + if (PyErr_Occurred()) SWIG_fail; + $1 = &temp; +} + +/* out */ + +%typemap(out,fragment="SWIG_FromCharPtr") + char *, char const*, char *const, char const *const + "$result = SWIG_FromCharPtr($1);"; + +%typemap(out,fragment="SWIG_FromCharPtr") + char *const &, char const* &, char const *const & + "$result = SWIG_FromCharPtr(*$1);"; + +/* varin */ + +%typemap(varin,fragment="SWIG_AsNewCharPtr") char * +{ + char *cptr = SWIG_AsNewCharPtr($input); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + if ($1) swig_delete_array($1); + $1 = cptr; +} + +%typemap(varin,fragment="SWIG_AsNewCharPtr", + warning="451:Setting const char * variable may leak memory") + const char * +{ + char *cptr = SWIG_AsNewCharPtr($input); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = cptr; +} + +/* varout */ + +%typemap(varout,fragment="SWIG_FromCharPtr") + char*, char const*, char *const, char const *const + "$result = SWIG_FromCharPtr($1);"; + +/* constant */ + +%typemap(constcode,fragment="SWIG_FromCharPtr") + char *, char const*, char * const, char const* const + "PyDict_SetItemString(d,\"$symname\", SWIG_FromCharPtr($value));"; + +/* directorin */ + +%typemap(directorin,fragment="SWIG_FromCharPtr") + char *, char const*, char *const, char const *const, + char const *&, char *const &, char const *const & + "$input = SWIG_NewPointerObj(swig_const_cast(char*,$1_name), $descriptor(char *), 0);" + /* "$input = SWIG_FromCharPtr($1_name);"; */ + + +/* directorout */ + +%typemap(directorout,fragment="SWIG_AsCharPtr") + char *, char const*, char *const, char const* const + "$result = SWIG_AsCharPtr($input); + if (PyErr_Occurred()) { + Swig::DirectorTypeMismatchException(\"Error converting Python object into char*\"); + }"; + +%typemap(directorout,fragment="SWIG_AsCharPtr") + char const *&, char *const &, char const *const & +{ + char* temp = SWIG_AsCharPtr($input); + if (PyErr_Occurred()) { + Swig::DirectorTypeMismatchException("Error converting Python object into char*"); + } + $result = ($1_ltype) &temp; +} + +/* typecheck */ + +%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, + fragment="SWIG_AsCharPtr") + char *, char const*, char *const, char const *const, + char const*&, char *const&, char const *const & + "SWIG_AsCharPtr($input); + if (PyErr_Occurred()) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = 1; + }"; + +/* throws */ + +%typemap(throws,fragment="SWIG_FromCharPtr") + char *, char const*, char * const, char const* const +{ + PyErr_SetObject(PyExc_RuntimeError, SWIG_FromCharPtr($1)); + SWIG_fail; +} + + +/* ------------------------------------------------------------ + * Fix size character array char[ANY] handling + * ------------------------------------------------------------ */ + +/* memberin and globalin typemaps */ + +%typemap(memberin) char [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0); + else memset($1,0,$1_dim0); +} + +%typemap(globalin) char [ANY] +{ + if ($input) memcpy($1,$input,$1_dim0); + else memset($1,0,$1_dim0); +} + +/* in */ + +%typemap(in,fragment="SWIG_AsCharArray") + char [ANY], const char [ANY] +{ + char temp[$1_dim0]; + SWIG_AsCharArray($input, temp, $1_dim0); + if (PyErr_Occurred()) SWIG_fail; + $1 = temp; +} + +/* out */ + +%typemap(out,fragment="SWIG_FromCharArray") + char [ANY], const char [ANY] + "$result = SWIG_FromCharArray($1, $1_dim0);"; + +/* varin */ + +%typemap(varin,fragment="SWIG_AsCharArray") + char [ANY] +{ + SWIG_AsCharArray($input, $1, $1_dim0); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } +} + +/* varout */ + +%typemap(varout,fragment="SWIG_FromCharArray") + char [ANY], const char [ANY] + "$result = SWIG_FromCharArray($1, $1_dim0);"; + + +/* constants */ + +%typemap(constcode,fragment="SWIG_FromCharArray") + char [ANY], const char [ANY] + "PyDict_SetItemString(d,\"$symname\", SWIG_FromCharArray($value, $value_dim0));"; + +/* directorin */ + +%typemap(directorin,fragment="SWIG_FromCharArray") + char [ANY], const char [ANY] + "$input = SWIG_FromCharArray($1_name, $1_dim0);"; + +/* directorout */ + +%typemap(directorout,fragment="SWIG_AsCharArray") + char [ANY], const char [ANY] (char temp[$result_dim0]) +{ + SWIG_AsCharArray($input, temp, $result_dim0); + if (PyErr_Occurred()) { + Swig::DirectorTypeMismatchException("Error converting Python object into char[$result_dim0]"); + } + $result = temp; +} + +/* typecheck */ + +%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, + fragment="SWIG_AsCharArray") + char [ANY], const char[ANY] +{ + char* carray = 0; size_t size = 0; + SWIG_AsCharArray($input, &carray, &size); + if (PyErr_Occurred()) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = ((carray != 0) && (size <= $input_dim0)); + } +} + +/* throw */ + +%typemap(throws,fragment="SWIG_FromCharArray") + char[ANY], const char[ANY] { + PyErr_SetObject(PyExc_RuntimeError, SWIG_FromCharArray($1,$1_dim0)); + SWIG_fail; +} + +/* ------------------------------------------------------------ + * --- String & length --- + * ------------------------------------------------------------ */ + +/* Here len doesn't include the '0' terminator */ +%typemap(in, fragment="SWIG_AsCharPtrAndSize") + (char *STRING, int LENGTH) (char *buf, size_t size) +{ + int is_raw_pchar = SWIG_AsCharPtrAndSize($input, &buf, &size); + if (PyErr_Occurred()) SWIG_fail; + $1 = ($1_ltype) buf; + $2 = ($2_ltype) (is_raw_pchar && size) ? size - 1 : size; +} + +/* Here size includes the '0' terminator */ +%typemap(in,fragment="SWIG_AsCharPtrAndSize") + (char *STRING, int SIZE) (char *buf, size_t size) +{ + SWIG_AsCharPtrAndSize($input, &buf, &size); + if (PyErr_Occurred()) SWIG_fail; + $1 = ($1_ltype) buf; + $2 = ($2_ltype) size; +} diff --git a/Lib/python/pyswigtype.swg b/Lib/python/pyswigtype.swg new file mode 100644 index 000000000..54070dddd --- /dev/null +++ b/Lib/python/pyswigtype.swg @@ -0,0 +1,240 @@ +/* ----------------------------------------------------------------------------- + * --- Input arguments --- + * ----------------------------------------------------------------------------- */ + +/* Pointers, references, and arrays */ + +%typemap(in) SWIGTYPE *, SWIGTYPE [] + "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, + SWIG_POINTER_EXCEPTION | $disown)) == -1) SWIG_fail;"; + +%typemap(in) SWIGTYPE *DISOWN + "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, + SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) SWIG_fail;"; + +/* Additional check for null references */ +%typemap(in) SWIGTYPE & + "if ((SWIG_ConvertPtr($input,(void **)(&$1),$1_descriptor, + SWIG_POINTER_EXCEPTION | $disown)) == -1) + SWIG_fail; + if ($1 == NULL) { + PyErr_SetString(PyExc_TypeError,\"null reference\"); + SWIG_fail; + }"; + +/* Object passed by value. Convert to a pointer */ +%typemap(in) SWIGTYPE ($&1_ltype argp) + "if ((SWIG_ConvertPtr($input,(void **)(&argp),$&1_descriptor, + SWIG_POINTER_EXCEPTION) == -1)) SWIG_fail; + $1 = *argp;"; + +/* Pointer to a class member */ +%typemap(in) SWIGTYPE (CLASS::*) + "if ((SWIG_ConvertPacked($input,(void *)(&$1),sizeof($1_type), + $1_descriptor,SWIG_POINTER_EXCEPTION)) == -1) + SWIG_fail;"; + + +/* ----------------------------------------------------------------------------- + * --- Outnput arguments --- + * ----------------------------------------------------------------------------- */ + +/* Pointers, references, and arrays */ +%typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] + "$result = SWIG_NewPointerObj((void*)($1), $1_descriptor, $owner);"; + +/* Dynamic casts */ + +%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { + swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1); + $result = SWIG_NewPointerObj((void *) $1, ty, $owner); +} + +/* Member pointer */ +%typemap(out) SWIGTYPE (CLASS::*) + "$result = SWIG_NewPackedObj((void*)(&$1), sizeof($1_type), $1_descriptor);"; + +/* Primitive types--return by value */ +#ifdef __cplusplus +%typemap(out) SWIGTYPE +{ + $&1_ltype resultptr; + resultptr = new $1_ltype(($1_ltype &) $1); + $result = SWIG_NewPointerObj((void *)(resultptr), $&1_descriptor, 1); +} +#else +%typemap(out /* warning="452:Default return typemap could be unsafe" */) SWIGTYPE +{ + $&1_ltype resultptr; + resultptr = ($&1_ltype) malloc(sizeof($1_type)); + memmove(resultptr, &$1, sizeof($1_type)); + $result = SWIG_NewPointerObj((void *)(resultptr), $&1_descriptor, 1); +} +#endif + + +/* ----------------------------------------------------------------------------- + * --- Variable input --- + * ----------------------------------------------------------------------------- */ + + +/* Pointers, references, and arrays */ + +%typemap(varin) SWIGTYPE [ANY] { + void *temp; + int ii; + $1_basetype *b = 0; + if ((SWIG_ConvertPtr($input, &temp, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + b = ($1_basetype *) $1; + for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii); +} + +%typemap(varin) SWIGTYPE * { + void *temp; + if ((SWIG_ConvertPtr($input, &temp, $1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = ($1_ltype) temp; +} + +%typemap(varin) SWIGTYPE & { + void *temp; + if ((SWIG_ConvertPtr($input, &temp, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1 || temp == NULL) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = *($1_ltype) temp; +} + + +%typemap(varin) SWIGTYPE (CLASS::*) { + char temp[sizeof($1_type)]; + if ((SWIG_ConvertPacked($input,(void *) temp, sizeof($1_type), $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + memmove((void *) &$1,temp,sizeof($1_type)); +} + +%typemap(varin) SWIGTYPE { + $&1_ltype temp; + if ((SWIG_ConvertPtr($input, (void **)(&temp), $&1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = *(($&1_type) temp); +} + +/* ----------------------------------------------------------------------------- + * --- Variable output --- + * ----------------------------------------------------------------------------- */ + +/* Pointers and arrays */ +%typemap(varout) SWIGTYPE *, SWIGTYPE [] + "$result = SWIG_NewPointerObj((void *)($1), $1_descriptor, 0);"; + +/* References */ +%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 + "$result = SWIG_NewPointerObj((void *)(&$1), $&1_descriptor, 0);"; + +/* ----------------------------------------------------------------------------- + * --- Constants --- * + * ----------------------------------------------------------------------------- */ + + +/* Pointers, arrays, objects */ + +%typemap(consttab) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] +{ SWIG_PY_POINTER, (char*)"$symname", 0, 0, (void *)$value, &$1_descriptor} + +%typemap(consttab) SWIGTYPE (CLASS::*) +{ SWIG_PY_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor} + + +/* ----------------------------------------------------------------------------- + * --- Director typemaps --- * + * ----------------------------------------------------------------------------- */ + +/* director in not needed, see python.cxx */ + +/* directorout */ + +%typemap(directorout) SWIGTYPE ($<ype argp) + "if ((SWIG_ConvertPtr($input, (void **)(&argp), + $&descriptor, SWIG_POINTER_EXCEPTION | $disown)) == -1) + throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\"); + $result = *argp;"; + +%typemap(directorout) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] + "if ((SWIG_ConvertPtr($input,(void **)(&$result), + $descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) + throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; + + +/* ------------------------------------------------------------ + * --- Typechecking rules --- + * ------------------------------------------------------------ */ + +%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] +{ + void *ptr; + if (SWIG_ConvertPtr($input, &ptr, $1_descriptor, 0) == -1) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = 1; + } +} + +%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE +{ + void *ptr; + if (SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 0) == -1) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = 1; + } +} + + +/* ------------------------------------------------------------ + * --- Exception handling --- + * ------------------------------------------------------------ */ + +%typemap(throws) SWIGTYPE { + $&1_ltype temp = new $1_ltype($1); + if ($&1_descriptor->clientdata) { + PyErr_SetObject((PyObject *) ($&1_descriptor->clientdata), SWIG_NewPointerObj(temp,$&1_descriptor,1)); + } else { + PyErr_SetString(PyExc_RuntimeError,"$1_type"); + /* + PyErr_SetObject(PyExc_RuntimeError, SWIG_NewPointerObj(temp,$&1_descriptor,1)); + */ + } + SWIG_fail; +} + +/* This doesn't work, and not sure if it is needed... */ +#if 0 +%typecheck(throws) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { + if ($1_descriptor->clientdata) { + PyErr_SetObject((PyObject *) ($1_descriptor->clientdata), SWIG_NewPointerObj($1,$1_descriptor,1)); + } else { + PyErr_SetString(PyExc_RuntimeError,"$1_type"); + } + SWIG_fail; +} +#endif + diff --git a/Lib/python/python.swg b/Lib/python/python.swg index a2ce334ca..54df253bb 100644 --- a/Lib/python/python.swg +++ b/Lib/python/python.swg @@ -10,762 +10,84 @@ #include "Python.h" %} - - %insert(runtime) "precommon.swg"; -%insert(runtime) "common.swg"; // Common type-checking code -%insert(runtime) "pyrun.swg"; // Python run-time code +%insert(runtime) "common.swg"; /* Common type-checking code */ +%insert(runtime) "pyrun.swg"; /* Python run-time code */ /* Special directive for shadow code */ #define %shadow %insert("shadow") #define %pythoncode %insert("python") - -/* auxiliar memory allocators */ +%{ +/* Auxiliar swig macros */ #ifdef __cplusplus -#define %swig_new(type, size) new type[(size)] -#define %swig_delete(cptr) delete [] cptr; +#define SWIGSTATICINLINE(a) static inline a +#define SWIGSTATIC(a) static a +#define swig_new_array(type, size) (new type[(size)]) +#define swig_delete_array(cptr) delete[] cptr +#define swig_const_cast(type,a) const_cast(a) +#define swig_static_cast(type,a) static_cast(a) +#define swig_reinterpret_cast(type,a) reinterpret_cast(a) + +#ifdef HAVE_NUMERIC_CAST +#define swig_numeric_cast(type,a) numeric_cast(a) #else -#define %swig_new(type, size) (type*) malloc((size)*sizeof(type)) -#define %swig_delete(cptr) free((char*)cptr); +#define swig_numeric_cast(type,a) static_cast(a) #endif -/* ----------------------------------------------------------------------------- - * standard typemaps - * ----------------------------------------------------------------------------- */ +#else /* C case */ +#define SWIGSTATICINLINE(a) static a +#define SWIGSTATIC(a) static a +#define swig_new_array(type, size) ((type*) malloc((size)*sizeof(type))) +#define swig_delete_array(cptr) free((char*)cptr) +#define swig_const_cast(type,a) (type)(a) +#define swig_static_cast(type,a) (type)(a) +#define swig_reinterpret_cast(type,a) (type)(a) +#define swig_numeric_cast(type,a) (type)(a) +#endif /* __cplusplus */ +%} /* ----------------------------------------------------------------------------- - * --- memberin and globalin typemaps --- + * SWIGTYPE typemaps * ----------------------------------------------------------------------------- */ -/* Character array handling */ - -%typemap(memberin) char [ANY] { - if ($input) memcpy($1,$input,$1_dim0); - else memset($1,0,$1_dim0); -} - -%typemap(globalin) char [ANY] { - if ($input) memcpy($1,$input,$1_dim0); - else memset($1,0,$1_dim0); -} - +%include "pyswigtype.swg" /* ----------------------------------------------------------------------------- - * --- Input arguments --- + * Typemap specializations * ----------------------------------------------------------------------------- */ - -/* Primitive datatypes. */ - -%define PY_IN_TYPEMAP(type, pyobj_as) -%typemap(in,fragment=#pyobj_as) type { - $1 = ($1_type) pyobj_as($input); - if (PyErr_Occurred()) SWIG_fail; - } - %typemap(in) const type& ($basetype temp) { - temp = ($basetype) pyobj_as($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp; - } -%enddef - -/* Pointers, references, and arrays */ - -%typemap(in) SWIGTYPE *, - SWIGTYPE [] - "if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;" - -%typemap(in) SWIGTYPE *DISOWN - "if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN )) == -1) SWIG_fail;" - -/* Additional check for null references */ -%typemap(in) SWIGTYPE & - "if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail; - if ($1 == NULL) { PyErr_SetString(PyExc_TypeError,\"null reference\"); SWIG_fail; }" - -/* Void pointer. Accepts any kind of pointer */ -%typemap(in) void * "if ((SWIG_ConvertPtr($input,(void **) &$1, 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) SWIG_fail;" - -/* Object passed by value. Convert to a pointer */ -%typemap(in) SWIGTYPE ($&1_ltype argp) "if ((SWIG_ConvertPtr($input,(void **) &argp, $&1_descriptor,SWIG_POINTER_EXCEPTION) == -1)) SWIG_fail; - $1 = *argp; "; - -/* Pointer to a class member */ -%typemap(in) SWIGTYPE (CLASS::*) "if ((SWIG_ConvertPacked($input, (void *) &$1, sizeof($1_type), $1_descriptor,SWIG_POINTER_EXCEPTION)) == -1) SWIG_fail;"; - -/* The char* and char [ANY] case */ -%typemap(in) char *, char const*, char *const, char const *const -{ - $1 = SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); - if (PyErr_Occurred()) SWIG_fail; -} - -%typemap(in) char const*&, char *const&, char const *const & -{ - $*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]; - SWIG_PyObj_AsCharArray($input, $descriptor(char*), temp, $1_dim0); - if (PyErr_Occurred()) SWIG_fail; - $1 = temp; -} - - -/* ----------------------------------------------------------------------------- - * --- Outnput arguments --- - * ----------------------------------------------------------------------------- */ - -/* Primitive types */ -%define PY_OUT_TYPEMAP(type, pyobj_from) - %typemap(out,fragment=#pyobj_from) type "$result = pyobj_from((type)$1);"; - %typemap(out,fragment=#pyobj_from) const type& "$result = pyobj_from((type) *($1));"; -%enddef - -/* Pointers, references, and arrays */ -%typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, $owner);"; - -/* Dynamic casts */ - -%typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC { - swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor, (void **) &$1); - $result = SWIG_NewPointerObj((void *) $1, ty, $owner); -} - -/* Member pointer */ -%typemap(out) SWIGTYPE (CLASS::*) "$result = SWIG_NewPackedObj((void *) &$1, sizeof($1_type), $1_descriptor);"; - -/* Void */ -%typemap(out) void "Py_INCREF(Py_None); $result = Py_None;"; - -/* 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 = SWIG_PyObj_FromCharPtr((const char*)$1);"; - -%typemap(out) char [ANY], const char [ANY] - "$result = SWIG_PyObj_FromCharArray((const char*)$1, $1_dim0);"; - -/* Primitive types--return by value */ -%typemap(out) SWIGTYPE -#ifdef __cplusplus -{ - $&1_ltype resultptr; - resultptr = new $1_ltype(($1_ltype &) $1); - $result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1); -} -#else -{ - $&1_ltype resultptr; - resultptr = ($&1_ltype) malloc(sizeof($1_type)); - memmove(resultptr, &$1, sizeof($1_type)); - $result = SWIG_NewPointerObj((void *) resultptr, $&1_descriptor, 1); -} -#endif - - -/* ----------------------------------------------------------------------------- - * --- Variable input --- - * ----------------------------------------------------------------------------- */ - -/* primitive types */ -%define PY_VARIN_TYPEMAP(type, pyobj_as) - %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)'"); - return 1; - } - $1 = temp; - } -%enddef - -/* char* and char[ANY] */ -%typemap(varin) char * { - 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_delete($1); - $1 = cptr; -} - -%typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { - char *cptr = SWIG_PyObj_AsNewCharPtr($input, $descriptor(char*)); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = cptr; -} - -%typemap(varin) char [ANY] { - SWIG_PyObj_AsCharArray($input, $descriptor(char*), $1, $1_dim0); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } -} - -/* Pointers, references, and arrays */ - -%typemap(varin) SWIGTYPE [ANY] { - void *temp; - int ii; - $1_basetype *b = 0; - if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - b = ($1_basetype *) $1; - for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii); -} - -%typemap(varin) SWIGTYPE * { - void *temp; - if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = ($1_ltype) temp; -} - -%typemap(varin) SWIGTYPE & { - void *temp; - if ((SWIG_ConvertPtr($input,(void **) &temp, $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1 || temp == NULL) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = *($1_ltype) temp; -} - -%typemap(varin) void * { - void * temp; - if ((SWIG_ConvertPtr($input,(void **) &temp, 0, SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = ($1_ltype) temp; -} - -%typemap(varin) SWIGTYPE (CLASS::*) { - char temp[sizeof($1_type)]; - if ((SWIG_ConvertPacked($input,(void *) temp, sizeof($1_type), $1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - memmove((void *) &$1,temp,sizeof($1_type)); -} - -%typemap(varin) SWIGTYPE { - $&1_ltype temp; - if ((SWIG_ConvertPtr($input, (void **) &temp, $&1_descriptor, SWIG_POINTER_EXCEPTION)) == -1) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = *(($&1_type) temp); -} - -/* ----------------------------------------------------------------------------- - * --- Variable output --- - * ----------------------------------------------------------------------------- */ - -/* Primitive types */ -%define PY_VAROUT_TYPEMAP(type, pyobj_from) - %typemap(varout,fragment=#pyobj_from) type, const type& "$result = pyobj_from((type)$1);"; -%enddef - -/* Pointers and arrays */ -%typemap(varout) SWIGTYPE *, SWIGTYPE [] - "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);"; - -/* References */ -%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);"; - -/* 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 [ANY], const char [ANY] - "$result = SWIG_PyObj_FromCharArray($1, $1_dim0);"; - -%typemap(varout) SWIGTYPE - "$result = SWIG_NewPointerObj((void *) &$1, $&1_descriptor, 0);"; - -/* ----------------------------------------------------------------------------- - * --- Constants --- * - * ----------------------------------------------------------------------------- */ - -/* Primitive types */ -%define PY_CONSTCODE_TYPEMAP(type, pyobj_from) - %typemap(constcode,fragment=#pyobj_from) type - "PyDict_SetItemString(d,\"$symname\", pyobj_from((type)$value));"; -%enddef - -/* Pointers, arrays, objects */ -%typemap(consttab) char *, char const*, char * const, char const* const - { SWIG_PY_STRING, (char*)"$symname", 0, 0, (void *)$value, 0} - -%typemap(consttab) char [ANY], const char [ANY] - "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} - -%typemap(consttab) SWIGTYPE (CLASS::*) - { SWIG_PY_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor} - - -/* ----------------------------------------------------------------------------- - * --- Director typemaps --- * - * ----------------------------------------------------------------------------- */ - -/* - * - * Inverse argument typemaps are for marshaling C/C++ parameters to call Python - * methods from C++ proxy wrapper classes. - * - */ - -/* --- directorin typemaps --- */ - -/* Primitive datatypes */ - -%define PY_DIRECTORIN_TYPEMAP(type, pyobj_from) - %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 - -/* 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 = 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 = SWIG_PyObj_FromCharArray((const char*)$1_name, $1_dim0);"; - - -/* --- directorout typemaps --- */ - -%define PY_DIRECTOROUT_TYPEMAP(Type, pyobj_as) - %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,fragment=#pyobj_as) Type - "$result = ($basetype) pyobj_as($input); - if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; -%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,fragment=#pyobj_as) Type &DIRECTOROUT = Type -%enddef - -/* Char pointer and arrays */ -%typemap(directorout) char *, char const*, char *const, char const* const { - $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 & { - 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]) { - SWIG_PyObj_AsCharArray($input, $descriptor(char*), temp, $result_dim0); - if (PyErr_Occurred()) { - Swig::DirectorTypeMismatchException("Error converting Python object into char[$result_dim0]"); - } - $result = temp; -} - -/* Object returned by value. Convert from a pointer */ -%typemap(directorout) SWIGTYPE ($<ype argp) - "if ((SWIG_ConvertPtr($input,(void **) &argp, $&descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\"); $result = *argp;"; - -%typemap(directorout) SWIGTYPE *, - SWIGTYPE &, - SWIGTYPE [] - "if ((SWIG_ConvertPtr($input,(void **) &$result, $descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; - -%typemap(directorout) void * "if ((SWIG_ConvertPtr($input,(void **) &$result, 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; +%include "pyvoid.swg" +%include "pyobject.swg" +%include "pystrings.swg" +%include "pyvaltypes.swg" +%include "pyprimtypes.swg" +%include "pymisctypes.swg" /* ------------------------------------------------------------ - * --- Typechecking rules --- + * Enums * ------------------------------------------------------------ */ -%define PY_TYPECHECK_TYPEMAP(check, type, pyobj_asorcheck) -%typemap(typecheck,fragment=#pyobj_asorcheck,precedence=SWIG_TYPECHECK_##check) type, const type& -{ - pyobj_asorcheck($input); - if (PyErr_Occurred()) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} -%enddef - -%typecheck(SWIG_TYPECHECK_STRING) - char *, char const*, char *const, char const *const, - char const*&, char *const&, char const *const & -{ - SWIG_PyObj_AsCharPtr($input, $descriptor(char*)); - if (PyErr_Occurred()) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} - -%typecheck(SWIG_TYPECHECK_CHAR_ARRAY) char [ANY], const char[ANY] -{ - 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 [] -{ - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, $1_descriptor, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} - -%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE -{ - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, $&1_descriptor, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} - -%typecheck(SWIG_TYPECHECK_VOIDPTR) void * -{ - void *ptr; - if (SWIG_ConvertPtr($input, (void **) &ptr, 0, 0) == -1) { - $1 = 0; - PyErr_Clear(); - } else { - $1 = 1; - } -} - -/* ------------------------------------------------------------ - * --- Exception handling --- - * ------------------------------------------------------------ */ - -%define PY_THROWS_TYPEMAP(type, pyobj_from) - %typemap(throws,fragment=#pyobj_from) type { - PyErr_SetObject(PyExc_RuntimeError, pyobj_from((type)$1)); - SWIG_fail; - } -%enddef - -%typemap(throws) char *, char const*, char * const, char const* const { - PyErr_SetObject(PyExc_RuntimeError, SWIG_PyObj_FromCharPtr((const char*)$1)); - SWIG_fail; -} - -%typemap(throws) char[ANY], const char[ANY] { - PyErr_SetObject(PyExc_RuntimeError, SWIG_PyObj_FromCharArray((const char*)$1, $1_dim0)); - SWIG_fail; -} - -%typemap(throws) SWIGTYPE { - $&1_ltype temp = new $1_ltype($1); - if ($&1_descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($&1_descriptor->clientdata), SWIG_NewPointerObj(temp,$&1_descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$1_type"); - //PyErr_SetObject(PyExc_RuntimeError, SWIG_NewPointerObj(temp,$&1_descriptor,1)); - } - SWIG_fail; -} - -// This doesn't work, and not sure if it is needed... -#if 0 -%typecheck(throws) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] { - if ($1_descriptor->clientdata) { - PyErr_SetObject((PyObject *) ($1_descriptor->clientdata), SWIG_NewPointerObj($1,$1_descriptor,1)); - } else { - PyErr_SetString(PyExc_RuntimeError,"$1_type"); - } - SWIG_fail; -} -#endif - - -/* ------------------------------------------------------------ - * PyObject * - Just pass straight through unmodified - * ------------------------------------------------------------ */ - -%typemap(in) PyObject * "$1 = $input;"; -%typemap(out) PyObject * "$result = $1;"; - -%typemap(constcode) PyObject * "PyDict_SetItemString(d,\"$symname\", $value);"; - -%typemap(directorin, parse="O") PyObject * ""; -%typemap(directorout) PyObject * "$result = $input;"; - -%typecheck(SWIG_TYPECHECK_POINTER) PyObject * "$1 = ($input != 0);"; - -%typemap(throws) PyObject * { - PyErr_SetObject(PyExc_RuntimeError, $1); - 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 --- - * ------------------------------------------------------------ */ - %apply int { enum SWIGTYPE }; -// this doesn't work very well, you need to redefined it for each enum. +/* this doesn't work now, you need to redefined it for each enum. */ %apply const int& { const enum SWIGTYPE& }; /* ------------------------------------------------------------ - * --- ANSI/Posix C typemaps --- + * Overloaded operator support * ------------------------------------------------------------ */ - -%apply unsigned long { size_t }; -%apply const unsigned long& { const size_t& }; -%apply long { ptrdiff_t }; -%apply const long& { const ptrdiff_t& }; - -#ifdef __cplusplus -%apply unsigned long { std::size_t }; -%apply const unsigned long& { const std::size_t& }; -%apply long { std::ptrdiff_t }; -%apply const long& { const std::ptrdiff_t& }; -#endif +%include "pyopers.swg" /* ------------------------------------------------------------ - * --- String & length --- + * Warnings for Python keywords * ------------------------------------------------------------ */ - -/* 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) (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; -} - -/* ------------------------------------------------------------ - * --- Argc & Argv --- - * ------------------------------------------------------------ */ -/* This typemap works with either a list or a tuple */ -%typemap(in) (int ARGC, char **ARGV) { - /* 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 = %swig_new(char*, argc + 1); - int i = 0; - 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 or tuple must contain strings only"); - SWIG_fail; - } - } - argv[i] = 0; - $1 = ($1_ltype) argc; - $2 = ($2_ltype) argv; - } else { - $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_delete($2); -} - - -/***************************************************************************** - * - * End of C++ proxy wrapper typemaps. - * - *****************************************************************************/ - - -/* ------------------------------------------------------------ - * Overloaded operator support - * ------------------------------------------------------------ */ - -#ifdef __cplusplus -%rename(__add__) *::operator+; -%rename(__pos__) *::operator+(); -%rename(__pos__) *::operator+() const; -%rename(__sub__) *::operator-; -%rename(__neg__) *::operator-(); -%rename(__neg__) *::operator-() const; -%rename(__mul__) *::operator*; -%rename(__div__) *::operator/; -%rename(__mod__) *::operator%; -%rename(__lshift__) *::operator<<; -%rename(__rshift__) *::operator>>; -%rename(__and__) *::operator&; -%rename(__or__) *::operator|; -%rename(__xor__) *::operator^; -%rename(__invert__) *::operator~; -%rename(__iadd__) *::operator+=; -%rename(__isub__) *::operator-=; -%rename(__imul__) *::operator*=; -%rename(__idiv__) *::operator/=; -%rename(__imod__) *::operator%=; -%rename(__ilshift__) *::operator<<=; -%rename(__irshift__) *::operator>>=; -%rename(__iand__) *::operator&=; -%rename(__ior__) *::operator|=; -%rename(__ixor__) *::operator^=; -%rename(__lt__) *::operator<; -%rename(__le__) *::operator<=; -%rename(__gt__) *::operator>; -%rename(__ge__) *::operator>=; -%rename(__eq__) *::operator==; -%rename(__ne__) *::operator!=; - -/* Special cases */ -%rename(__call__) *::operator(); - -/* Ignored operators */ -%ignorewarn("362:operator= ignored") operator=; -%ignorewarn("383:operator++ ignored") operator++; -%ignorewarn("384:operator-- ignored") operator--; -%ignorewarn("361:operator! ignored") operator!; -%ignorewarn("381:operator&& ignored") operator&&; -%ignorewarn("382:operator|| ignored") operator||; -%ignorewarn("386:operator->* ignored") operator->*; -%ignorewarn("389:operator[] ignored (consider using %extend)") operator[]; - -#endif - -/* Warnings for Python keywords */ %include "pythonkw.swg" -/* The start of the Python initialization function */ +/* ------------------------------------------------------------ + * The start of the Python initialization function + * ------------------------------------------------------------ */ %init %{ #ifdef __cplusplus diff --git a/Lib/python/pythonkw.swg b/Lib/python/pythonkw.swg index 60a84570e..9f36ffdff 100644 --- a/Lib/python/pythonkw.swg +++ b/Lib/python/pythonkw.swg @@ -1,9 +1,11 @@ -#ifndef __python_pythonkw_swg__ -#define __python_pythonkw_swg__ +/* + Warnings for Python keywords, built-in names and bad names. +*/ #define PYTHONKW(x) %namewarn("314:" #x " is a python keyword") #x #define PYTHONBN(x) %namewarn("321:" #x " conflicts with a built-in name in python") "::"#x + /* Warnings for Python keywords http://www.fnorb.org/docs/1.2/Fnorb-Guide/node62.html @@ -113,5 +115,3 @@ PYTHONBN(self); #undef PYTHONBN #undef PYTHONKW - -#endif //__python_pythonkw_swg__ diff --git a/Lib/python/pyvaltypes.swg b/Lib/python/pyvaltypes.swg new file mode 100644 index 000000000..1880e1ed2 --- /dev/null +++ b/Lib/python/pyvaltypes.swg @@ -0,0 +1,128 @@ +/* in */ + +%define PY_IN_TYPEMAP(type, pyobj_as) + %typemap(in,fragment=#pyobj_as) type + "$1 = ($1_type) pyobj_as($input); + if (PyErr_Occurred()) SWIG_fail;" + %typemap(in) const type& ($basetype temp) + "temp = ($basetype) pyobj_as($input); + if (PyErr_Occurred()) SWIG_fail; + $1 = &temp;"; +%enddef + +/* out */ + +%define PY_OUT_TYPEMAP(type, pyobj_from) + %typemap(out,fragment=#pyobj_from) type + "$result = pyobj_from((type)$1);"; + %typemap(out,fragment=#pyobj_from) const type& + "$result = pyobj_from((type)*($1));"; +%enddef + +/* varin */ + +%define PY_VARIN_TYPEMAP(type, pyobj_as) + %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)'"); + return 1; + } + $1 = temp; + } +%enddef + +/* varout */ + +%define PY_VAROUT_TYPEMAP(type, pyobj_from) + %typemap(varout,fragment=#pyobj_from) + type, const type& "$result = pyobj_from((type)$1);"; +%enddef + + +/* Primitive types */ +%define PY_CONSTCODE_TYPEMAP(type, pyobj_from) + %typemap(constcode,fragment=#pyobj_from) type + "PyDict_SetItemString(d,\"$symname\", pyobj_from((type)$value));"; +%enddef + +/* directorin */ + +%define PY_DIRECTORIN_TYPEMAP(type, pyobj_from) + %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 + +/* directorout */ + +%define PY_DIRECTOROUT_TYPEMAP(Type, pyobj_as) + %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,fragment=#pyobj_as) Type + "$result = ($basetype) pyobj_as($input); + if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using pyobj_as\");"; +%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,fragment=#pyobj_as) Type &DIRECTOROUT = Type +%enddef + +/* throws */ + +%define PY_THROWS_TYPEMAP(type, pyobj_from) + %typemap(throws,fragment=#pyobj_from) type { + PyErr_SetObject(PyExc_RuntimeError, pyobj_from((type)$1)); + SWIG_fail; + } +%enddef + +/* typecheck */ + +%define PY_TYPECHECK_TYPEMAP(check, type, pyobj_check) +%typemap(typecheck,precedence=SWIG_TYPECHECK_##check, + fragment=#pyobj_check) type, const type& + "$1 = pyobj_check($input);"; +%enddef + +/* + 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 for simple swig types with only As/From conversor methods + named as SWIG_As##Name/SWIG_From##Name. +*/ +%define %typemap_stype(Type, CheckCode, Name) +%fragment("SWIG_Check"#Name,"header", + fragment="SWIG_As"#Name) %{ +SWIGSTATICINLINE(int) +SWIG_Check##Name(PyObject* obj) +{ + SWIG_As##Name(obj); + if (PyErr_Occurred()) { + PyErr_Clear(); + return 0; + } else { + return 1; + } +} +%} +%typemap_asfromcheck(Type, CheckCode, SWIG_As##Name, + SWIG_From##Name, SWIG_Check##Name) +%enddef diff --git a/Lib/python/pyvoid.swg b/Lib/python/pyvoid.swg new file mode 100644 index 000000000..a7eec4350 --- /dev/null +++ b/Lib/python/pyvoid.swg @@ -0,0 +1,50 @@ +/* ------------------------------------------------------------ + * Void * - Accepts any kind of pointer + * ------------------------------------------------------------ */ + +/* in */ + +%typemap(in) void * + "if ((SWIG_ConvertPtr($input,&$1,0,SWIG_POINTER_EXCEPTION | $disown)) == -1) SWIG_fail;"; + + +/* out */ + +%typemap(out) void "Py_INCREF(Py_None); $result = Py_None;"; + +/* varin */ + +%typemap(varin) void * { + void * temp; + if ((SWIG_ConvertPtr($input, &temp, 0, + SWIG_POINTER_EXCEPTION | SWIG_POINTER_DISOWN)) == -1) { + PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); + return 1; + } + $1 = ($1_ltype) temp; +} + +/* varout */ + +%typemap(varout) void "Py_INCREF(Py_None); $result = Py_None;"; + +/* directorout */ + +%typemap(directorout) void * + "if ((SWIG_ConvertPtr($input,(void **)(&$result), + 0, SWIG_POINTER_EXCEPTION | $disown )) == -1) + throw Swig::DirectorTypeMismatchException(\"Pointer conversion failed.\");"; + + +/* typecheck */ + +%typecheck(SWIG_TYPECHECK_VOIDPTR) void * +{ + void *ptr; + if (SWIG_ConvertPtr($input, &ptr, 0, 0) == -1) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = 1; + } +} diff --git a/Lib/python/std_complex.i b/Lib/python/std_complex.i index 4ed5de93a..4ca975fd8 100644 --- a/Lib/python/std_complex.i +++ b/Lib/python/std_complex.i @@ -1,28 +1,25 @@ #ifndef SWIG_STD_COMPLEX_I_ #define SWIG_STD_COMPLEX_I_ +%include "complex_common.i" + + %{ #include %} -%include "complex_common.i" - - /* defining the complex as/from converters */ %swig_cplxdbl_conv(std::complex, StdCplxDbl, std::complex, std::real, std::imag) -%swig_cplxflt_conv(std::complex, StdCplxFlt, - 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); +%typemap_stype(std::complex, CPLXDBL, StdCplxDbl); +%typemap_stype(std::complex, CPLXFLT, StdCplxFlt); #endif //SWIG_STD_COMPLEX_I_ diff --git a/Lib/python/std_string.i b/Lib/python/std_string.i index 1c7bb6d21..2e2dd0152 100644 --- a/Lib/python/std_string.i +++ b/Lib/python/std_string.i @@ -16,46 +16,56 @@ #include %} -/* defining the std::string as/from converters */ +/* defining the std::string as/from/check methods */ -%fragment("SWIG_PyObj_AsStdString","header") %{ -static inline std::string -SWIG_PyObj_AsStdString(PyObject* obj) { - static swig_type_info* pchar_info = SWIG_TypeQuery("char *"); +%fragment("SWIG_TryStdString","header", + fragment="SWIG_AsCharPtrAndSize") %{ +SWIGSTATICINLINE(int) +SWIG_TryStdString(PyObject* obj, char*& buf, size_t& size) { + SWIG_AsCharPtrAndSize(obj, &buf, &size); + if (PyErr_Occurred() || !buf) { + if (PyErr_Occurred()) PyErr_Clear(); + return 0; + } + return 1; +} +%} + +%fragment("SWIG_CheckStdString","header", + fragment="SWIG_TryStdString") %{ +SWIGSTATICINLINE(int) +SWIG_CheckStdString(PyObject* obj) { 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); + return SWIG_TryStdString(obj, 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(); + +%fragment("SWIG_AsStdString","header", + fragment="SWIG_TryStdString") %{ +SWIGSTATICINLINE(std::string) +SWIG_AsStdString(PyObject* obj) { + char* buf = 0 ; size_t size = 0; + if (SWIG_TryStdString(obj, buf, size)) { + return std::string(buf, size); + } else { PyErr_SetString(PyExc_TypeError,"a string is expected"); + return std::string(); } } %} -%fragment("SWIG_PyObj_FromStdString","header") %{ -static inline PyObject* -SWIG_PyObj_FromStdString(const std::string& s) { - return SWIG_PyObj_FromCharArray(s.data(), s.size()); +%fragment("SWIG_FromStdString","header", + fragment="SWIG_FromCharArray") %{ +SWIGSTATICINLINE(PyObject*) +SWIG_FromStdString(const std::string& s) { + return SWIG_FromCharArray(s.data(), s.size()); } %} - /* declaring the typemaps */ -%typemap_asfromcheck(std::string, STRING, - SWIG_PyObj_AsStdString, SWIG_PyObj_FromStdString, - SWIG_PyObj_CheckStdString); +%typemap_asfromcheck(std::string, STRING, + SWIG_AsStdString, + SWIG_FromStdString, + SWIG_CheckStdString);