diff --git a/Examples/python/exception/example.h b/Examples/python/exception/example.h index 47526434c..9f137b9a9 100644 --- a/Examples/python/exception/example.h +++ b/Examples/python/exception/example.h @@ -1,6 +1,10 @@ /* File : example.h */ #include +#ifndef SWIG +struct A { +}; +#endif class Exc { public: @@ -22,6 +26,9 @@ public: } int hosed() throw(Exc) { throw(Exc(42,"Hosed")); + } + int unknown() throw(A) { + throw A(); } int multi(int x) throw(int, const char *, Exc) { if (x == 1) throw(37); diff --git a/Examples/python/exception/runme.py b/Examples/python/exception/runme.py index 436958984..718707861 100644 --- a/Examples/python/exception/runme.py +++ b/Examples/python/exception/runme.py @@ -5,6 +5,11 @@ import example t = example.Test() +try: + t.unknown() +except RuntimeError,e: + print "incomplete type", e.args[0] + try: t.simple() except RuntimeError,e: @@ -27,3 +32,5 @@ for i in range(1,4): print e.args[0] except example.Exc,e: print e.code, e.msg + + diff --git a/Examples/python/operator/example.i b/Examples/python/operator/example.i index f46619501..7968087fe 100644 --- a/Examples/python/operator/example.i +++ b/Examples/python/operator/example.i @@ -1,6 +1,6 @@ /* File : example.i */ %module example - +#pragma SWIG nowarn=362 %{ #include "example.h" %} diff --git a/Examples/python/varargs/example.i b/Examples/python/varargs/example.i index 323bb82ee..6cb88f5f4 100644 --- a/Examples/python/varargs/example.i +++ b/Examples/python/varargs/example.i @@ -21,7 +21,7 @@ int printf(const char *fmt, ...); %varargs(char *) fprintf; /* Ignore the format string, but set it to %s */ -%typemap(ignore) const char *fmt { +%typemap(in,numinputs=0) const char *fmt { $1 = "%s"; } #else diff --git a/Examples/python/variables/example.i b/Examples/python/variables/example.i index 91bd0679d..cc80ee284 100644 --- a/Examples/python/variables/example.i +++ b/Examples/python/variables/example.i @@ -1,5 +1,6 @@ /* File : example.i */ %module example +#pragma SWIG nowarn=451 %{ #include "example.h" %} @@ -17,7 +18,7 @@ extern char cvar; extern float fvar; extern double dvar; extern char *strvar; -extern const char *cstrvar; +extern const char * cstrvar; extern int *iptrvar; extern char name[256]; diff --git a/Examples/test-suite/python/exception_order_runme.py b/Examples/test-suite/python/exception_order_runme.py index 7967c2619..451518d05 100644 --- a/Examples/test-suite/python/exception_order_runme.py +++ b/Examples/test-suite/python/exception_order_runme.py @@ -1,21 +1,21 @@ -import exception_order +from exception_order import * -a = exception_order.A() +a = A() try: a.foo() -except RuntimeError,e: - if e.args[0] != "E1": - print "bad exception order", - raise RuntimeError, e.args +except E1,e: + pass +except: + raise RuntimeError, "bad exception order" try: a.bar() -except RuntimeError,e: - if e.args[0] != "E2": - print "bad exception order", - raise RuntimeError, e.args +except E2,e: + pass +except: + raise RuntimeError, "bad exception order" try: a.foobar() diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index c9548ed45..f7d54df38 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -10,6 +10,172 @@ #include "Python.h" +#include +#include + +#ifdef __cplusplus +#define SWIG_STATIC_INLINE static inline +#else +#define SWIG_STATIC_INLINE static +#endif + +SWIG_STATIC_INLINE long +SPyObj_AsLong(PyObject * obj) +{ + return PyInt_Check(obj) ? PyInt_AsLong(obj) : PyLong_AsLong(obj); +} + +SWIG_STATIC_INLINE unsigned long +SPyObj_AsUnsignedLong(PyObject * obj) +{ + if (PyLong_Check(obj)) { + return PyLong_AsUnsignedLong(obj); + } else { + long i = PyInt_AsLong(obj); + if ( !PyErr_Occurred() && (i < 0)) { + PyErr_SetString(PyExc_TypeError, "negative value for unsigned type"); + } + return i; + } +} + +SWIG_STATIC_INLINE PyObject* +SPyObj_FromLongLong(long long value) +{ + return (value > (long)(LONG_MAX)) ? + PyLong_FromLongLong(value) : PyInt_FromLong((long)value); +} + +SWIG_STATIC_INLINE PyObject* +SPyObj_FromUnsignedLong(unsigned long value) +{ + return (value > (unsigned long)(LONG_MAX)) ? + PyLong_FromUnsignedLong(value) : PyInt_FromLong((long)value); +} + +SWIG_STATIC_INLINE PyObject* +SPyObj_FromUnsignedLongLong(unsigned long long value) +{ + return (value > (unsigned long long)(LONG_MAX)) ? + PyLong_FromUnsignedLongLong(value) : PyInt_FromLong((long)value); +} + +SWIG_STATIC_INLINE long +SPyObj_AsLongInRange(PyObject * obj, long min_value, long max_value) +{ + long value = SPyObj_AsLong(obj); + if (!PyErr_Occurred()) { + if (value < min_value) { + PyErr_SetString(PyExc_OverflowError,"value is smaller than type minimum"); + } else if (value > max_value) { + PyErr_SetString(PyExc_OverflowError,"value is greater than type maximum"); + } + } + return value; +} + +SWIG_STATIC_INLINE unsigned long +SPyObj_AsUnsignedLongInRange(PyObject *obj, unsigned long max_value) +{ + unsigned long value = SPyObj_AsUnsignedLong(obj); + if (!PyErr_Occurred()) { + if (value > max_value) { + PyErr_SetString(PyExc_OverflowError,"value is greater than type maximum"); + } + } + return value; +} + +SWIG_STATIC_INLINE signed char +SPyObj_AsSignedChar(PyObject *obj) { + return SPyObj_AsLongInRange(obj, SCHAR_MIN, SCHAR_MAX); +} + +SWIG_STATIC_INLINE short +SPyObj_AsShort(PyObject *obj) { + return SPyObj_AsLongInRange(obj, SHRT_MIN, SHRT_MAX); +} + +SWIG_STATIC_INLINE int +SPyObj_AsInt(PyObject *obj) { + return SPyObj_AsLongInRange(obj, INT_MIN, INT_MAX); +} + +SWIG_STATIC_INLINE unsigned char +SPyObj_AsUnsignedChar(PyObject *obj) { + return SPyObj_AsUnsignedLongInRange(obj, UCHAR_MAX); +} + +SWIG_STATIC_INLINE unsigned short +SPyObj_AsUnsignedShort(PyObject *obj) { + return SPyObj_AsUnsignedLongInRange(obj, USHRT_MAX); +} + +SWIG_STATIC_INLINE unsigned int +SPyObj_AsUnsignedInt(PyObject *obj) { + return SPyObj_AsUnsignedLongInRange(obj, UINT_MAX); +} + +SWIG_STATIC_INLINE long long +SPyObj_AsLongLong(PyObject *obj) { + return PyInt_Check(obj) ? + PyInt_AsLong(obj) : PyLong_AsLongLong(obj); +} + +SWIG_STATIC_INLINE unsigned long long +SPyObj_AsUnsignedLongLong(PyObject *obj) { + return PyLong_Check(obj) ? + PyLong_AsUnsignedLongLong(obj) : SPyObj_AsUnsignedLong(obj); +} + +SWIG_STATIC_INLINE double +SPyObj_AsDouble(PyObject *obj) { + return (PyFloat_Check(obj)) ? PyFloat_AsDouble(obj) : + (double)((PyInt_Check(obj)) ? PyInt_AsLong(obj) : PyLong_AsLongLong(obj)); +} + +SWIG_STATIC_INLINE float +SPyObj_AsFloat(PyObject *obj) { + double value = SPyObj_AsDouble(obj); + if (!PyErr_Occurred()) { + if (value < FLT_MIN) { + PyErr_SetString(PyExc_OverflowError,"float is smaller than flt_min"); + } else if (value > FLT_MAX) { + PyErr_SetString(PyExc_OverflowError,"float is greater than flt_max"); + } + } + return (float) value; +} + +SWIG_STATIC_INLINE char +SPyObj_AsChar(PyObject *obj) { + char c = (PyString_Check(obj) && PyString_Size(obj) == 1) ? + PyString_AsString(obj)[0] + : (char) SPyObj_AsLongInRange(obj, CHAR_MIN, CHAR_MAX); + if (PyErr_Occurred()) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, "a char is required"); + } + return c; +} + +SWIG_STATIC_INLINE PyObject * +SPyObj_FromChar(char c) { + return PyString_FromStringAndSize(&c,1); +} + +SWIG_STATIC_INLINE PyObject * +SPyObj_FromCharPtr(const char* cptr) { + return cptr ? PyString_FromString(cptr) : Py_BuildValue((char*)""); +} + +SWIG_STATIC_INLINE int +SPyObj_AsBool(PyObject *obj) { + return SPyObj_AsLongLong(obj) ? 1 : 0; +} + + + #ifdef __cplusplus extern "C" { #endif @@ -45,7 +211,7 @@ typedef struct swig_const_info { SWIG_Python_NewPointerObj(p, type, flags) #define SWIG_MustGetPtr(p, type, argnum, flags) \ SWIG_Python_MustGetPtr(p, type, argnum, flags) - + /* Python-specific SWIG API */ #define SWIG_newvarlink() \ SWIG_Python_newvarlink() diff --git a/Lib/python/python.swg b/Lib/python/python.swg index 63ab2cfcf..13a794d22 100644 --- a/Lib/python/python.swg +++ b/Lib/python/python.swg @@ -27,37 +27,34 @@ /* --- Input arguments --- */ + /* Primitive datatypes. These only supply a parse code to PyTuple_ParseArgs */ -%typemap(in,parse="i") int ""; %typemap(in,parse="h") short ""; +%typemap(in,parse="i") int ""; %typemap(in,parse="l") long ""; -%typemap(in,parse="b") signed char ""; - -%typemap(in) unsigned int, unsigned short, unsigned long, unsigned char - "$1 = ($1_ltype) PyInt_AsLong($input); - if (PyErr_Occurred()) SWIG_fail;"; - -%typemap(in) long long - "$1 = ($1_ltype) PyLong_AsLongLong($input); - if (PyErr_Occurred()) SWIG_fail;"; - - -%typemap(in) unsigned long long - "$1 = ($1_ltype) PyLong_AsUnsignedLongLong($input); - if (PyErr_Occurred()) SWIG_fail;"; - %typemap(in,parse="f") float ""; %typemap(in,parse="d") double ""; %typemap(in,parse="c") char ""; %typemap(in,parse="s") char *, char [ANY] ""; -/* Boolean values. Have to convert from a long since */ -%typemap(in) bool "$1 = PyInt_AsLong($input) ? true : false; - if (PyErr_Occurred()) SWIG_fail;"; +%define %typemapin_pyfunc(type, pyfunc) +%typemap(in) type { + $1 = ($1_type) pyfunc($input); + if (PyErr_Occurred()) SWIG_fail; + } +%enddef + +%typemapin_pyfunc(bool, SPyObj_AsBool); +%typemapin_pyfunc(signed char, SPyObj_AsSignedChar); +%typemapin_pyfunc(unsigned char, SPyObj_AsUnsignedChar); +%typemapin_pyfunc(unsigned short, SPyObj_AsUnsignedShort); +%typemapin_pyfunc(unsigned int, SPyObj_AsUnsignedInt); +%typemapin_pyfunc(unsigned long, SPyObj_AsUnsignedLong); + +%typemapin_pyfunc(long long, SPyObj_AsLongLong); +%typemapin_pyfunc(unsigned long long, SPyObj_AsUnsignedLongLong); -/* Enum values. */ -%typemap(in,parse="i") enum SWIGTYPE ""; /* Pointers, references, and arrays */ %typemap(in) SWIGTYPE *, @@ -84,60 +81,53 @@ /* Const primitive references. Passed by value */ -%typemap(in) const int & (int temp), - const short & (short temp), - const long & (long temp), - const unsigned int & (unsigned int temp), - const unsigned short & (unsigned short temp), - const unsigned long & (unsigned long temp), - const signed char & (signed char temp), - const unsigned char & (unsigned char temp) - "temp = ($*1_ltype) PyInt_AsLong($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp;"; +%define %typemapin_pyfunc_cr(type, pyfunc) + %typemap(in) const type& ($*1_ltype temp) { + temp = ($*1_ltype) pyfunc($input); + if (PyErr_Occurred()) SWIG_fail; + $1 = &temp; + } +%enddef -%typemap(in) const bool & (bool temp) - "temp = PyInt_AsLong($input) ? true : false; - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp;"; +%typemapin_pyfunc_cr(bool, SPyObj_AsBool); +%typemapin_pyfunc_cr(signed char, SPyObj_AsSignedChar); +%typemapin_pyfunc_cr(int, SPyObj_AsInt); +%typemapin_pyfunc_cr(short, SPyObj_AsShort); +%typemapin_pyfunc_cr(long, SPyObj_AsLong); +%typemapin_pyfunc_cr(unsigned char, SPyObj_AsUnsignedChar); +%typemapin_pyfunc_cr(unsigned short, SPyObj_AsUnsignedShort); +%typemapin_pyfunc_cr(unsigned int, SPyObj_AsUnsignedInt); +%typemapin_pyfunc_cr(unsigned long, SPyObj_AsUnsignedLong); -%typemap(in) const float & (float temp), - const double & (double temp) - "temp = ($*1_ltype) PyFloat_AsDouble($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp;"; +%typemapin_pyfunc_cr(long long, SPyObj_AsLongLong); +%typemapin_pyfunc_cr(unsigned long long, SPyObj_AsUnsignedLongLong); -%typemap(in) const long long & ($*1_ltype temp) - "temp = ($*1_ltype) PyLong_AsLongLong($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp;"; +%typemapin_pyfunc_cr(float, SPyObj_AsFloat); +%typemapin_pyfunc_cr(double, SPyObj_AsDouble); -%typemap(in) const unsigned long long & ($*1_ltype temp) - "temp = ($*1_ltype) PyLong_AsUnsignedLongLong($input); - if (PyErr_Occurred()) SWIG_fail; - $1 = &temp;"; +%typemapin_pyfunc_cr(char, SPyObj_AsChar); +%typemapin_pyfunc_cr(char*, PyString_AsString); -%typemap(in) const char &(char temp) { - char *stemp = PyString_AsString($input); - if (PyErr_Occurred()) SWIG_fail; - temp = *stemp; - $1 = &temp; -} /* --- Output values --- */ -%typemap(out) int, unsigned int, - short, unsigned short, - long, unsigned long, - signed char, unsigned char, - bool, enum SWIGTYPE +%typemap(out) bool, + signed char, + short, + unsigned char, + unsigned short, + int, + long "$result = PyInt_FromLong((long)$1);"; -%typemap(out) long long "$result = PyLong_FromLongLong($1);"; -%typemap(out) unsigned long long "$result = PyLong_FromUnsignedLongLong($1);"; +%typemap(out) unsigned int, unsigned long + "$result = SPyObj_FromUnsignedLong((unsigned long)$1);"; + +%typemap(out) long long "$result = SPyObj_FromLongLong($1);"; +%typemap(out) unsigned long long "$result = SPyObj_FromUnsignedLongLong($1);"; %typemap(out) float, double "$result = PyFloat_FromDouble($1);"; -%typemap(out) char * "$result = $1 ? PyString_FromString($1) : Py_BuildValue((char*)\"\");"; -%typemap(out) char "$result = Py_BuildValue((char*)\"c\",$1);"; +%typemap(out) char * "$result = SPyObj_FromCharPtr($1);"; +%typemap(out) char "$result = SPyObj_FromChar($1);"; /* Pointers, references, and arrays */ %typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, $owner);"; @@ -177,86 +167,68 @@ /* References to primitive types. Return by value */ -%typemap(out) const int &, const unsigned int &, - const short &, const unsigned short &, - const long &, const unsigned long &, - const signed char &, const unsigned char &, - const bool & +%typemap(out) const bool &, + const signed char &, + const short &, + const unsigned char &, + const unsigned short &, + const int &, + const long & "$result = PyInt_FromLong((long) *($1));"; +%typemap(out) const unsigned int &, const unsigned long & + "$result = SPyObj_FromUnsignedLong((unsigned long) *($1));"; + %typemap(out) const float &, const double & "$result = PyFloat_FromDouble((double) *($1));"; %typemap(out) const long long & - "$result = PyLong_FromLongLong(*($1));"; + "$result = SPyObj_FromLongLong(*($1));"; %typemap(out) const unsigned long long & - "$result = PyLong_FromUnsignedLongLong(*($1));"; + "$result = SPyObj_FromUnsignedLongLong(*($1));"; %typemap(out) const char & - "$result = PyString_FromStringAndSize($1,1);"; + "$result = SPyObj_FromChar(*($1));"; + +%typemap(out) const char* & + "$result = SPyObj_FromCharPtr(*($1));"; /* --- Variable input --- */ -%typemap(varin) int, unsigned int, short, unsigned short, long, unsigned long, signed char, unsigned char, enum SWIGTYPE -{ - long temp = PyInt_AsLong($input); +%define %typemapvarin_pyfunc(type, pyfunc) +%typemap(varin) type { + $1_type temp = ($1_type) pyfunc($input); if (PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); return 1; } - $1 = ($1_type) temp; -} + $1 = temp; + } +%enddef -%typemap(varin) bool -{ - long temp = PyInt_AsLong($input); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = temp ? true : false; -} +%typemapvarin_pyfunc(bool, SPyObj_AsBool); +%typemapvarin_pyfunc(signed char, SPyObj_AsSignedChar); +%typemapvarin_pyfunc(int, SPyObj_AsInt); +%typemapvarin_pyfunc(short, SPyObj_AsShort); +%typemapvarin_pyfunc(long, SPyObj_AsLong); +%typemapvarin_pyfunc(unsigned char, SPyObj_AsUnsignedChar); +%typemapvarin_pyfunc(unsigned short, SPyObj_AsUnsignedShort); +%typemapvarin_pyfunc(unsigned int, SPyObj_AsUnsignedInt); +%typemapvarin_pyfunc(unsigned long, SPyObj_AsUnsignedLong); -%typemap(varin) long long { - $1_type temp = PyLong_AsLongLong($input); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = temp; -} +%typemapvarin_pyfunc(long long, SPyObj_AsLongLong); +%typemapvarin_pyfunc(unsigned long long, SPyObj_AsUnsignedLongLong); -%typemap(varin) unsigned long long { - $1_type temp = PyLong_AsUnsignedLongLong($input); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = temp; -} - -%typemap(varin) float, double { - double temp = PyFloat_AsDouble($input); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = ($1_ltype) temp; -} +%typemapvarin_pyfunc(float, SPyObj_AsFloat); +%typemapvarin_pyfunc(double, SPyObj_AsDouble); /* A single character */ -%typemap(varin) char { - char *temp = PyString_AsString($input); - if (PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'"); - return 1; - } - $1 = *temp; -} +%typemapvarin_pyfunc(char, SPyObj_AsChar); /* A string */ #ifdef __cplusplus + %typemap(varin) char * { char *temp = (char *) PyString_AsString($input); if (PyErr_Occurred()) { @@ -267,6 +239,7 @@ $1 = ($type) new char[strlen(temp)+1]; strcpy((char*)$1,temp); } + %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { char *temp = (char *) PyString_AsString($input); if (PyErr_Occurred()) { @@ -276,7 +249,9 @@ $1 = ($type) new char[strlen(temp)+1]; strcpy((char*)$1,temp); } + #else + %typemap(varin) char * { char *temp = (char *) PyString_AsString($input); if (PyErr_Occurred()) { @@ -287,6 +262,7 @@ $1 = ($type) malloc(strlen(temp)+1); strcpy((char*)$1,temp); } + %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { char *temp = (char *) PyString_AsString($input); if (PyErr_Occurred()) { @@ -296,6 +272,7 @@ $1 = ($type) malloc(strlen(temp)+1); strcpy((char*)$1,temp); } + #endif %typemap(varin) SWIGTYPE [ANY] { @@ -367,18 +344,23 @@ /* --- Variable output --- */ -%typemap(varout) int, unsigned int, - short, unsigned short, - long, unsigned long, - signed char, unsigned char, - bool, enum SWIGTYPE +%typemap(varout) bool, + signed char, + short, + unsigned char, + unsigned short, + int, + long "$result = PyInt_FromLong((long)$1);"; -%typemap(varout) long long "$result = PyLong_FromLongLong($1);"; -%typemap(varout) unsigned long long "$result = PyLong_FromUnsignedLongLong($1);"; +%typemap(varout) unsigned int, unsigned long + "$result = SPyObj_FromUnsignedLong((unsigned long)$1);"; + +%typemap(varout) long long "$result = SPyObj_FromLongLong($1);"; +%typemap(varout) unsigned long long "$result = SPyObj_FromUnsignedLongLong($1);"; %typemap(varout) float, double "$result = PyFloat_FromDouble($1);"; -%typemap(varout) char * "$result = $1 ? PyString_FromString($1) : Py_BuildValue((char*)\"\");"; -%typemap(varout) char "$result = Py_BuildValue((char*)\"c\",$1);"; +%typemap(varout) char * "$result = SPyObj_FromCharPtr($1);"; +%typemap(varout) char "$result = SPyObj_FromChar($1);"; /* Pointers and arrays */ %typemap(varout) SWIGTYPE *, SWIGTYPE [] "$result = SWIG_NewPointerObj((void *) $1, $1_descriptor, 0);"; @@ -399,7 +381,7 @@ /* --- Constants --- */ -%typemap(consttab) int, unsigned int, short, unsigned short, long, unsigned long, unsigned char, signed char, bool, enum SWIGTYPE +%typemap(consttab) bool, signed char, short, int, long, unsigned char, unsigned short { SWIG_PY_INT, (char *)"$symname", (long) $value, 0, 0, 0} %typemap(consttab) float, double @@ -414,8 +396,14 @@ %typemap(consttab) SWIGTYPE (CLASS::*) { SWIG_PY_BINARY, (char *)"$symname", sizeof($type), 0, (void *)&$value, &$1_descriptor} -%typemap(constcode) long long "PyDict_SetItemString(d,\"$symname\", PyLong_FromLongLong($value));"; -%typemap(constcode) unsigned long long "PyDict_SetItemString(d,\"$symname\", PyLong_FromUnsignedLongLong($value));"; +%typemap(constcode) unsigned int, unsigned long + "PyDict_SetItemString(d,\"$symname\", SPyObj_FromUnsignedLong($value));"; + +%typemap(constcode) long long + "PyDict_SetItemString(d,\"$symname\", SPyObj_FromLongLong($value));"; + +%typemap(constcode) unsigned long long + "PyDict_SetItemString(d,\"$symname\", SPyObj_FromUnsignedLongLong($value));"; @@ -429,56 +417,53 @@ /* directorin typemaps */ /* Primitive datatypes. These only supply a parse code to PyObject_CallMethod */ +%define %typemapdirectorin_pyfunc(type, _pyfunc) +%typemap(directorin) _type "$input = _pyfunc(($1_ltype) $1_name);"; +%typemap(directorin) _type "$input = _pyfunc(($*1_ltype) $1_name);"; +%enddef -%typemap(directorin,parse="i") int ""; -%typemap(directorin,parse="h") short ""; -%typemap(directorin,parse="l") long ""; -%typemap(directorin,parse="b") signed char ""; -%typemap(directorin,parse="f") float ""; -%typemap(directorin,parse="d") double ""; -%typemap(directorin,parse="s") char* ""; -%typemap(directorin,parse="i") bool ""; -%typemap(directorin,parse="i") enum SWIGTYPE ""; +%define %typemapdirectorin_parse(_type, _parse) + %typemap(directorin,parse=_parse) _type "($1_ltype) $1_name"; + %typemap(directorin,parse=_parse) const _type& "($*1_ltype) $1_name"; +%enddef -%typemap(directorin,parse="l") unsigned int, unsigned short, - unsigned long, unsigned char "(long) $1_name"; +%typemapdirectorin_parse(bool, "i"); +%typemapdirectorin_parse(signed char, "i"); +%typemapdirectorin_parse(unsigned char, "i"); +%typemapdirectorin_parse(short, "i"); +%typemapdirectorin_parse(unsigned short, "i"); +%typemapdirectorin_parse(int, "i"); +%typemapdirectorin_parse(long, "l"); - -%typemap(directorin) long long - "$input = PyLong_FromLongLong($1_name);"; -%typemap(directorin) unsigned long long - "$input = PyLong_FromUnsignedLongLong($1_name);"; +%typemapdirectorin_parse(float, "f"); +%typemapdirectorin_parse(double, "d"); +%typemapdirectorin_parse(char, "c"); +%typemapdirectorin_parse(char*, "s"); -%typemap(directorin,parse="i") const int& ""; -%typemap(directorin,parse="h") const short& ""; -%typemap(directorin,parse="l") const long& ""; -%typemap(directorin,parse="b") const signed char& ""; -%typemap(directorin,parse="f") const float& ""; -%typemap(directorin,parse="d") const double& ""; -%typemap(directorin,parse="i") const bool& ""; - -%typemap(directorin,parse="l") const unsigned int&, - const unsigned short&, const unsigned long&, - const unsigned char& "(long) $1_name"; - - -%typemap(directorin) const long long& - "$input = PyLong_FromLongLong($1_name);"; -%typemap(directorin) const unsigned long long& - "$input = PyLong_FromUnsignedLongLong($1_name);"; +%typemapdirectorin_pyfunc(unsigned int, SPyObj_FromUnsignedLong); +%typemapdirectorin_pyfunc(unsigned long, SPyObj_FromUnsignedLong); +%typemapdirectorin_pyfunc(long long, SPyObj_FromLongLong); +%typemapdirectorin_pyfunc(unsigned long long, SPyObj_FromUnsignedLongLong); -%typemap(directorin, parse="l") int *DIRECTORIN, long* DIRECTORIN, - unsigned int *DIRECTORIN, unsigned long *DIRECTORIN, - short *DIRECTORIN, unsigned short *DIRECTORIN, - char *DIRECTORIN, unsigned char *DIRECTORIN - "(long) *$1_name"; -%typemap(directorin, parse="f") float *DIRECTORIN "*$1_name"; -%typemap(directorin, parse="d") double *DIRECTORIN "*$1_name"; +%typemap(directorin, parse="l") bool *DIRECTORIN, + signed char *DIRECTORIN, + unsigned char *DIRECTORIN, + short *DIRECTORIN, + unsigned short *DIRECTORIN, + int *DIRECTORIN, + long *DIRECTORIN "(long) *$1_name"; -%typemap(directorin, parse="O") PyObject* ""; +%typemap(directorin) unsigned int *DIRECTORIN, + unsigned long *DIRECTORIN + "$input = SPyObj_FromUnsignedLong((unsigned long) *$1_name);"; -%typemap(directorin, parse="l") std::size_t, const std::size_t& "(long) $input"; +%typemap(directorin,parse="f") float *DIRECTORIN "*$1_name"; +%typemap(directorin,parse="d") double *DIRECTORIN "*$1_name"; +%typemap(directorin,parse="c") char *DIRECTORIN "*$1_name"; +%typemap(directorin,parse="s") char **DIRECTORIN "*$1_name"; + +%typemap(directorin,parse="O") PyObject* ""; /* // this is rather dangerous %typemap(directorin) SWIGTYPE { @@ -516,30 +501,32 @@ %define DIRECTOROUT_TYPEMAP(type, converter) %typemap(directorargout) type *DIRECTOROUT - "*$result = (type) converter($input); + "*$result = ($ltype) converter($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using converter\");"; %typemap(directorout) type - "$result = (type) converter($input); + "$result = ($ltype) converter($input); if (PyErr_Occurred()) throw Swig::DirectorTypeMismatchException(\"Error converting Python object using converter\");"; %typemap(directorout) type &DIRECTOROUT = type %enddef -DIRECTOROUT_TYPEMAP(char, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(unsigned char, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(short, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(unsigned short, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(int, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(unsigned int, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(long, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(unsigned long, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(long long, PyLong_AsLongLong); -DIRECTOROUT_TYPEMAP(unsigned long long, PyLong_AsUnsignedLongLong); -DIRECTOROUT_TYPEMAP(float, PyFloat_AsDouble); -DIRECTOROUT_TYPEMAP(double, PyFloat_AsDouble); -DIRECTOROUT_TYPEMAP(bool, PyInt_AsLong); -DIRECTOROUT_TYPEMAP(PyObject *, ); +DIRECTOROUT_TYPEMAP(signed char, SPyObj_AsSignedChar); +DIRECTOROUT_TYPEMAP(short, SPyObj_AsShort); +DIRECTOROUT_TYPEMAP(int, SPyObj_AsInt); +DIRECTOROUT_TYPEMAP(long, SPyObj_AsLong); +DIRECTOROUT_TYPEMAP(long long, SPyObj_AsLongLong); + +DIRECTOROUT_TYPEMAP(unsigned char, SPyObj_AsUnsignedChar); +DIRECTOROUT_TYPEMAP(unsigned short, SPyObj_AsUnsignedShort); +DIRECTOROUT_TYPEMAP(unsigned int, SPyObj_AsUnsignedInt); +DIRECTOROUT_TYPEMAP(unsigned long, SPyObj_AsUnsignedLong); +DIRECTOROUT_TYPEMAP(unsigned long long, SPyObj_AsUnsignedLongLong); + +DIRECTOROUT_TYPEMAP(float, SPyObj_AsFloat); +DIRECTOROUT_TYPEMAP(double, SPyObj_AsDouble); +DIRECTOROUT_TYPEMAP(bool, SPyObj_AsBool); +DIRECTOROUT_TYPEMAP(char, SPyObj_AsChar); DIRECTOROUT_TYPEMAP(char *, PyString_AsString); -DIRECTOROUT_TYPEMAP(std::size_t, PyInt_AsLong); +DIRECTOROUT_TYPEMAP(PyObject *, ); /* Object returned by value. Convert from a pointer */ %typemap(directorout) SWIGTYPE ($<ype argp) @@ -569,17 +556,25 @@ DIRECTOROUT_TYPEMAP(std::size_t, PyInt_AsLong); $2 = ($2_ltype) PyString_Size($input); } +/* ------------------------------------------------------------ + * Enums + * ------------------------------------------------------------ */ + +%apply int { enum SWIGTYPE }; +%apply const int& { const enum SWIGTYPE& }; + /* ------------------------------------------------------------ * ANSI C typemaps * ------------------------------------------------------------ */ -%typemap(in) size_t "$1 = (size_t) PyInt_AsLong($input); - if (PyErr_Occurred()) SWIG_fail;"; +%apply unsigned long { size_t }; +%apply const unsigned long& { const size_t& }; -%typemap(out) size_t = long; -%typemap(varin) size_t = long; -%typemap(varout) size_t = long; -%typemap(consttab) size_t = long; + +#ifdef __cplusplus +%apply unsigned long { std::size_t }; +%apply const unsigned long& { const std::size_t& }; +#endif /* ------------------------------------------------------------ * PyObject * - Just pass straight through unmodified @@ -591,32 +586,43 @@ DIRECTOROUT_TYPEMAP(std::size_t, PyInt_AsLong); /* ------------------------------------------------------------ * Typechecking rules * ------------------------------------------------------------ */ +%define %typecheck_pyfunc(check, type, pyfunc) +%typecheck(SWIG_TYPECHECK_##check) type, const type& +{ + pyfunc($input); + if (PyErr_Occurred()) { + $1 = 0; + PyErr_Clear(); + } else { + $1 = 1; + } +} +%enddef -%typecheck(SWIG_TYPECHECK_INTEGER) - int, short, long, - unsigned int, unsigned short, unsigned long, - signed char, unsigned char, - long long, unsigned long long, - const int &, const short &, const long &, - const unsigned int &, const unsigned short &, const unsigned long &, - const long long &, const unsigned long long &, - enum SWIGTYPE, - bool, const bool & +%typecheck_pyfunc(BOOL, bool, SPyObj_AsBool); +%typecheck_pyfunc(INT8, signed char, SPyObj_AsSignedChar); +%typecheck_pyfunc(UINT8, unsigned char, SPyObj_AsUnsignedChar); +%typecheck_pyfunc(INT16, short, SPyObj_AsShort); +%typecheck_pyfunc(UINT16, unsigned short, SPyObj_AsUnsignedShort); +%typecheck_pyfunc(INT32, int, SPyObj_AsInt); +%typecheck_pyfunc(UINT32, unsigned int, SPyObj_AsUnsignedInt); +%typecheck_pyfunc(INTEGER, unsigned long, SPyObj_AsUnsginedLong); +%typecheck_pyfunc(INTEGER, unsigned long long, SPyObj_AsUnsignedLongLong); +%typecheck_pyfunc(CHAR, char, SPyObj_AsChar); +%typecheck_pyfunc(FLOAT, float, SPyObj_AsFloat); + +%typecheck(SWIG_TYPECHECK_INTEGER) + long, const long &, + long long, const long long & { $1 = (PyInt_Check($input) || PyLong_Check($input)) ? 1 : 0; } -%typecheck(SWIG_TYPECHECK_DOUBLE) - float, double, - const float &, const double & +%typecheck(SWIG_TYPECHECK_DOUBLE) double, const double & { $1 = (PyFloat_Check($input) || PyInt_Check($input) || PyLong_Check($input)) ? 1 : 0; } -%typecheck(SWIG_TYPECHECK_CHAR) char { - $1 = (PyString_Check($input) && (PyString_Size($input) == 1)) ? 1 : 0; -} - %typecheck(SWIG_TYPECHECK_STRING) char * { $1 = PyString_Check($input) ? 1 : 0; } @@ -660,28 +666,34 @@ DIRECTOROUT_TYPEMAP(std::size_t, PyInt_AsLong); * Exception handling * ------------------------------------------------------------ */ -%typemap(throws) int, - long, - short, - unsigned int, - unsigned long, - unsigned short { - PyErr_SetObject(PyExc_RuntimeError, PyInt_FromLong((long) $1)); +%typemap(throws) bool, + signed char, + unsigned short, + short, + unsigned char, + int, + long { + PyErr_SetObject(PyExc_RuntimeError, PyInt_FromLong($1)); SWIG_fail; } -%typemap(throws) SWIGTYPE CLASS { - $&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_SetObject(PyExc_RuntimeError, SWIG_NewPointerObj(temp,$&1_descriptor,1)); - } +%typemap(throws) unsigned int, unsigned long { + PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromUnsignedLong($1)); SWIG_fail; } -%typemap(throws) SWIGTYPE { - PyErr_SetString(PyExc_RuntimeError,"$1_type"); +%typemap(throws) long long { + PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromLongLong($1)); + SWIG_fail; +} + +%typemap(throws) unsigned long long { + PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromUnsignedLongLong($1)); + SWIG_fail; +} + +%typemap(throws) char { + PyErr_SetObject(PyExc_RuntimeError, SPyObj_FromChar($1)); SWIG_fail; } @@ -690,6 +702,29 @@ DIRECTOROUT_TYPEMAP(std::size_t, PyInt_AsLong); SWIG_fail; } +%typemap(throws) float, double { + PyErr_SetObject(PyExc_RuntimeError, PyFloat_FromDouble($1)); + 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; +} + +/* +%typemap(throws) SWIGTYPE { + PyErr_SetString(PyExc_RuntimeError,"$1_type"); + SWIG_fail; +} +*/ + /* ------------------------------------------------------------ * Overloaded operator support * ------------------------------------------------------------ */