diff --git a/CHANGES.current b/CHANGES.current index b1817048c..e6e908dd2 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -4,6 +4,9 @@ See the RELEASENOTES file for a summary of changes in each release. Version 2.0.4 (in progress) =========================== +2011-04-29: szager + Bug 2635919: Convenience method to convert std::map to a python dict. + 2011-04-29: szager Fixed bug 2811549: return non-const iterators from STL methods begin(), end(), rbegin(), rend(). diff --git a/Lib/python/std_map.i b/Lib/python/std_map.i index 58ffffd8c..42f565908 100644 --- a/Lib/python/std_map.i +++ b/Lib/python/std_map.i @@ -43,29 +43,33 @@ typedef std::map map_type; typedef typename map_type::const_iterator const_iterator; typedef typename map_type::size_type size_type; - + + static PyObject *asdict(const map_type& map) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + size_type size = map.size(); + int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1; + if (pysize < 0) { + PyErr_SetString(PyExc_OverflowError, + "map size not valid in python"); + SWIG_PYTHON_THREAD_END_BLOCK; + return NULL; + } + PyObject *obj = PyDict_New(); + for (const_iterator i= map.begin(); i!= map.end(); ++i) { + swig::SwigVar_PyObject key = swig::from(i->first); + swig::SwigVar_PyObject val = swig::from(i->second); + PyDict_SetItem(obj, key, val); + } + SWIG_PYTHON_THREAD_END_BLOCK; + return obj; + } + static PyObject *from(const map_type& map) { swig_type_info *desc = swig::type_info(); if (desc && desc->clientdata) { return SWIG_InternalNewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN); } else { - SWIG_PYTHON_THREAD_BEGIN_BLOCK; - size_type size = map.size(); - int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1; - if (pysize < 0) { - PyErr_SetString(PyExc_OverflowError, - "map size not valid in python"); - SWIG_PYTHON_THREAD_END_BLOCK; - return NULL; - } - PyObject *obj = PyDict_New(); - for (const_iterator i= map.begin(); i!= map.end(); ++i) { - swig::SwigVar_PyObject key = swig::from(i->first); - swig::SwigVar_PyObject val = swig::from(i->second); - PyDict_SetItem(obj, key, val); - } - SWIG_PYTHON_THREAD_END_BLOCK; - return obj; + return asdict(map); } } }; @@ -164,6 +168,10 @@ swig::SwigPyIterator* iteritems(PyObject **PYTHON_SELF) { return swig::make_output_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF); } + + PyObject* asdict() { + return swig::traits_from< Map >::asdict(*self); + } } #else