From 05d379a9aa9a72a3d4aeee7ca725ba4c811c734b Mon Sep 17 00:00:00 2001 From: Jeff VanOss Date: Thu, 11 Jan 2018 18:43:37 -0500 Subject: [PATCH] fix return from std::map bindings to __delitem__ (#1229) Fix return from `std::map` bindings to `__delitem__`: we should be returning `void`, not an iterator. Also adds a test for map item deletion. --- include/pybind11/stl_bind.h | 2 +- tests/test_stl_binders.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 7ef687878..38dd68f69 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -587,7 +587,7 @@ class_ bind_map(handle scope, const std::string &name, Args&&. auto it = m.find(k); if (it == m.end()) throw key_error(); - return m.erase(it); + m.erase(it); } ); diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index bf1aa674c..0030924fb 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -181,3 +181,25 @@ def test_noncopyable_containers(): vsum += v.value assert vsum == 150 + + +def test_map_delitem(): + mm = m.MapStringDouble() + mm['a'] = 1 + mm['b'] = 2.5 + + assert list(mm) == ['a', 'b'] + assert list(mm.items()) == [('a', 1), ('b', 2.5)] + del mm['a'] + assert list(mm) == ['b'] + assert list(mm.items()) == [('b', 2.5)] + + um = m.UnorderedMapStringDouble() + um['ua'] = 1.1 + um['ub'] = 2.6 + + assert sorted(list(um)) == ['ua', 'ub'] + assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)] + del um['ua'] + assert sorted(list(um)) == ['ub'] + assert sorted(list(um.items())) == [('ub', 2.6)]