diff --git a/Lib/python/pystdcommon.swg b/Lib/python/pystdcommon.swg index b56249791..1b0d7191b 100644 --- a/Lib/python/pystdcommon.swg +++ b/Lib/python/pystdcommon.swg @@ -57,7 +57,7 @@ namespace swig { static int asval(PyObject *obj, Type *val) { if (val) { Type *p = 0; - int res = traits_asptr::asptr(obj, &p); + int res = traits_asptr::asptr(obj, (val ? &p : 0)); if ((res != 0) && p) { typedef typename noconst_traits::noconst_type noconst_type; *(const_cast(val)) = *p; @@ -77,7 +77,7 @@ namespace swig { if (val) { typedef typename noconst_traits::noconst_type noconst_type; noconst_type *p = 0; - int res = traits_asptr::asptr(obj, &p); + int res = traits_asptr::asptr(obj, (val ? &p : 0)); if (res) { *(const_cast(val)) = p; return SWIG_OK; diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index af159ff31..1f3c8dd1c 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -7,38 +7,58 @@ namespace swig { template struct traits_asptr > { - static int asptr(PyObject *obj, std::pair **val) { - typedef std::pair value_type; - if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) { - swig::PyObject_var first = PySequence_GetItem(obj,0); - swig::PyObject_var second = PySequence_GetItem(obj,1); - T *pfirst = 0; - U *psecond = 0; - value_type *vp = 0; - if (val) { - vp = %new_instance(std::pair); - pfirst = &(vp->first); - psecond = &(vp->second); - } - if ((swig::asval(first,pfirst) == SWIG_OK) && (swig::asval(second,psecond) == SWIG_OK)) { - if (val) *val = vp; + typedef std::pair value_type; + + static int get_pair(PyObject* first, PyObject* second, + std::pair **val) + { + if (val) { + value_type *vp = %new_instance(std::pair); + T *pfirst = &(vp->first); + U *psecond = &(vp->second); + if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) + && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { + *val = vp; return SWIG_NEWOBJ; } else { %delete(vp); + return SWIG_BADOBJ; + } + } else { + T *pfirst = 0; + U *psecond = 0; + if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) + && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { + return SWIG_NEWOBJ; + } else { + return SWIG_BADOBJ; + } + } + } + + static int asptr(PyObject *obj, std::pair **val) { + int res = SWIG_BADOBJ; + if (PyTuple_Check(obj)) { + if (PyTuple_GET_SIZE(obj) == 2) { + res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val); + } + } else if (PySequence_Check(obj)) { + if (PySequence_Size(obj) == 2) { + swig::PyObject_var first = PySequence_GetItem(obj,0); + swig::PyObject_var second = PySequence_GetItem(obj,1); + res = get_pair(first, second, val); } } else { value_type *p; - if (SWIG_ConvertPtr(obj,(void**)&p, - swig::type_info(),0) != -1) { + if (SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0) == SWIG_OK) { if (val) *val = p; - return SWIG_OLDOBJ; - } + res = SWIG_OLDOBJ; + } } - if (val) { - PyErr_Format(PyExc_TypeError, "a %s is expected", - swig::type_name()); + if ((res == SWIG_BADOBJ) && val) { + PyErr_Format(PyExc_TypeError, "a %s is expected", swig::type_name()); } - return 0; + return res; } }; diff --git a/Lib/swigrun.swg b/Lib/swigrun.swg index 1dc9f02d2..bd79d552b 100644 --- a/Lib/swigrun.swg +++ b/Lib/swigrun.swg @@ -48,6 +48,7 @@ #define SWIG_POINTER_OWN 0x1 /* Alloc. memory flags */ +#define SWIG_BADOBJ 0 #define SWIG_OLDOBJ 1 #define SWIG_NEWOBJ 2