diff options
author | robot-piglet <[email protected]> | 2025-08-28 14:27:58 +0300 |
---|---|---|
committer | robot-piglet <[email protected]> | 2025-08-28 14:57:06 +0300 |
commit | 81d828c32c8d5477cb2f0ce5da06a1a8d9392ca3 (patch) | |
tree | 3081d566f0d5158d76e9093261344f6406fd09f7 /contrib/tools/swig/Lib/python/std_multimap.i | |
parent | 77ea11423f959e51795cc3ef36a48d808b4ffb98 (diff) |
Intermediate changes
commit_hash:d5b1af16dbe9030537a04c27eb410c88c2f496cd
Diffstat (limited to 'contrib/tools/swig/Lib/python/std_multimap.i')
-rw-r--r-- | contrib/tools/swig/Lib/python/std_multimap.i | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/contrib/tools/swig/Lib/python/std_multimap.i b/contrib/tools/swig/Lib/python/std_multimap.i new file mode 100644 index 00000000000..75b4d7fcfac --- /dev/null +++ b/contrib/tools/swig/Lib/python/std_multimap.i @@ -0,0 +1,83 @@ +/* + Multimaps +*/ +%include <std_map.i> + +%fragment("StdMultimapTraits","header",fragment="StdMapCommonTraits") +{ + namespace swig { + template <class K, class T> + struct traits_asptr<std::multimap<K,T> > { + typedef std::multimap<K,T> multimap_type; + static int asptr(PyObject *obj, std::multimap<K,T> **val) { + int res = SWIG_ERROR; + if (PyDict_Check(obj)) { + SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL); +%#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::multimap<K,T>, std::pair<K, T> >::asptr(items, val); + } else { + multimap_type *p = 0; + swig_type_info *descriptor = swig::type_info<multimap_type>(); + res = descriptor ? SWIG_ConvertPtr(obj, (void **)&p, descriptor, 0) : SWIG_ERROR; + if (SWIG_IsOK(res) && val) *val = p; + } + return res; + } + }; + + template <class K, class T > + struct traits_from<std::multimap<K,T> > { + typedef std::multimap<K,T> multimap_type; + typedef typename multimap_type::const_iterator const_iterator; + typedef typename multimap_type::size_type size_type; + + static PyObject *from(const multimap_type& multimap) { + swig_type_info *desc = swig::type_info<multimap_type>(); + if (desc && desc->clientdata) { + return SWIG_InternalNewPointerObj(new multimap_type(multimap), desc, SWIG_POINTER_OWN); + } else { + size_type size = multimap.size(); + Py_ssize_t pysize = (size <= (size_type) INT_MAX) ? (Py_ssize_t) size : -1; + if (pysize < 0) { + SWIG_PYTHON_THREAD_BEGIN_BLOCK; + PyErr_SetString(PyExc_OverflowError, "multimap size not valid in python"); + SWIG_PYTHON_THREAD_END_BLOCK; + return NULL; + } + PyObject *obj = PyDict_New(); + for (const_iterator i= multimap.begin(); i!= multimap.end(); ++i) { + swig::SwigVar_PyObject key = swig::from(i->first); + swig::SwigVar_PyObject val = swig::from(i->second); + PyDict_SetItem(obj, key, val); + } + return obj; + } + } + }; + } +} + +%define %swig_multimap_methods(Type...) + %swig_map_common(Type); + +#if defined(SWIGPYTHON_BUILTIN) + %feature("python:slot", "mp_ass_subscript", functype="objobjargproc") __setitem__; +#endif + + %extend { + // This will be called through the mp_ass_subscript slot to delete an entry. + void __setitem__(const key_type& key) { + self->erase(key); + } + + void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) { + self->insert(Type::value_type(key,x)); + } + } +%enddef + +%include <std/std_multimap.i> + |