diff --git a/Examples/test-suite/python/li_std_map.i b/Examples/test-suite/python/li_std_map.i index bb4ececdf..0418613f2 100644 --- a/Examples/test-suite/python/li_std_map.i +++ b/Examples/test-suite/python/li_std_map.i @@ -27,6 +27,11 @@ namespace std %template(paircA2) pair; %template(pairiiA) pair >; %template(pairiiAc) pair >; + + + %template() pair; + %template(pymap) map; + } diff --git a/Examples/test-suite/python/li_std_map_runme.py b/Examples/test-suite/python/li_std_map_runme.py index be703c181..f45d288ab 100644 --- a/Examples/test-suite/python/li_std_map_runme.py +++ b/Examples/test-suite/python/li_std_map_runme.py @@ -31,3 +31,11 @@ for k in m: + +pm = li_std_map.pymap() + +pm[1] = (1,2) +pm["foo"] = "hello" + +for k in pm: + print pm[k] diff --git a/Examples/test-suite/python/li_std_vector.i b/Examples/test-suite/python/li_std_vector.i index 1aa9bbe48..e6dc180f8 100644 --- a/Examples/test-suite/python/li_std_vector.i +++ b/Examples/test-suite/python/li_std_vector.i @@ -121,3 +121,6 @@ std::vector vecStr(std::vector v) { %pointer_class(int,PtrInt) %array_functions(int,ArrInt) + + +%template(pyvector) std::vector; diff --git a/Examples/test-suite/python/li_std_vector_runme.py b/Examples/test-suite/python/li_std_vector_runme.py index cf3325bc1..a2d974cae 100644 --- a/Examples/test-suite/python/li_std_vector_runme.py +++ b/Examples/test-suite/python/li_std_vector_runme.py @@ -94,3 +94,10 @@ v = vecStr(["hello ", "world"]) if v[0] != 'hello world': raise RuntimeError,"bad std::string+std::vector" + + +pv = pyvector([1, "hello", (1,2)]) + +if pv[1] != "hello": + raise RuntimeError + diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index 77d04572b..3f773c3cd 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -26,6 +26,27 @@ %fragment("PySequence_Base","header") { +namespace swig { + template <> struct traits { + typedef value_category category; + static const char* type_name() { return "PyObject *"; } + }; + template <> struct traits_asval { + typedef PyObject * value_type; + static int asval(PyObject *obj, value_type *val) { + if (val) *val = obj; + return SWIG_OK; + } + }; + template <> struct traits_from { + typedef PyObject * value_type; + static PyObject *from(const value_type& val) { + Py_XINCREF(val); + return val; + } + }; +} + namespace swig { inline size_t diff --git a/Lib/python/pytypemaps.swg b/Lib/python/pytypemaps.swg index 692f5b320..feb3f5bab 100644 --- a/Lib/python/pytypemaps.swg +++ b/Lib/python/pytypemaps.swg @@ -73,6 +73,17 @@ %typemap(in,numinputs=0) PyObject **PYTHON_SELF "$1 = &obj0;"; +/* For output, we increase the reference object */ +%typemap(out,noblock=1) PyObject * { + Py_XINCREF($1); + $result = $1; +} + +%typemap(out,noblock=1) PyObject * const & { + Py_XINCREF(*$1); + $result = *$1; +} + /* Consttab, needed for callbacks, it should be removed later */ %typemap(consttab) SWIGTYPE ((*)(ANY)) @@ -82,3 +93,5 @@ + + diff --git a/Lib/typemaps/swigobject.swg b/Lib/typemaps/swigobject.swg index 5eacfe707..b75233d31 100644 --- a/Lib/typemaps/swigobject.swg +++ b/Lib/typemaps/swigobject.swg @@ -3,11 +3,20 @@ * ------------------------------------------------------------ */ %typemap(in) SWIG_Object "$1 = $input;"; +%typemap(freearg) SWIG_Object ""; -%typemap(out,noblock=1) SWIG_Object { +%typemap(in) SWIG_Object const & "$1 = &$input;"; +%typemap(freearg) SWIG_Object const & ""; + + +%typemap(out,noblock=1) SWIG_Object { $result = $1; } +%typemap(out,noblock=1) SWIG_Object const & { + $result = *$1; +} + %typecheck(SWIG_TYPECHECK_POINTER) SWIG_Object "$1 = ($input != 0);"; %typemap(throws,noblock=1) SWIG_Object {