From 005ff93dbdabb9ce0708d74588c36b04aff1e658 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 23 Jun 2017 14:38:26 +0100 Subject: [PATCH] Fix construction from dict for std::multimap std::unordered_multimap (Python 3) --- Lib/python/std_multimap.i | 6 +++++- Lib/python/std_unordered_multimap.i | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/python/std_multimap.i b/Lib/python/std_multimap.i index 21c84b009..f78a5277c 100644 --- a/Lib/python/std_multimap.i +++ b/Lib/python/std_multimap.i @@ -23,7 +23,11 @@ int res = SWIG_ERROR; if (PyDict_Check(obj)) { SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL); - return traits_asptr_stdseq, std::pair >::asptr(items, val); +%#if PY_VERSION_HEX >= 0x03000000 + /* In Python 3.x the ".items()" method returns a dict_items object */ + items = PySequence_Fast(items, ".items() didn't return a sequence!"); +%#endif + res = traits_asptr_stdseq, std::pair >::asptr(items, val); } else { multimap_type *p; swig_type_info *descriptor = swig::type_info(); diff --git a/Lib/python/std_unordered_multimap.i b/Lib/python/std_unordered_multimap.i index 4dbc2a4dd..6d7def900 100644 --- a/Lib/python/std_unordered_multimap.i +++ b/Lib/python/std_unordered_multimap.i @@ -30,7 +30,11 @@ int res = SWIG_ERROR; if (PyDict_Check(obj)) { SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL); - return traits_asptr_stdseq, std::pair >::asptr(items, val); +%#if PY_VERSION_HEX >= 0x03000000 + /* In Python 3.x the ".items()" method returns a dict_items object */ + items = PySequence_Fast(items, ".items() didn't return a sequence!"); +%#endif + res = traits_asptr_stdseq, std::pair >::asptr(items, val); } else { unordered_multimap_type *p; swig_type_info *descriptor = swig::type_info();