Skip to content

Commit 05d379a

Browse files
vanossjjagerman
authored andcommitted
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.
1 parent 28cb676 commit 05d379a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

include/pybind11/stl_bind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&.
587587
auto it = m.find(k);
588588
if (it == m.end())
589589
throw key_error();
590-
return m.erase(it);
590+
m.erase(it);
591591
}
592592
);
593593

tests/test_stl_binders.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,25 @@ def test_noncopyable_containers():
181181
vsum += v.value
182182

183183
assert vsum == 150
184+
185+
186+
def test_map_delitem():
187+
mm = m.MapStringDouble()
188+
mm['a'] = 1
189+
mm['b'] = 2.5
190+
191+
assert list(mm) == ['a', 'b']
192+
assert list(mm.items()) == [('a', 1), ('b', 2.5)]
193+
del mm['a']
194+
assert list(mm) == ['b']
195+
assert list(mm.items()) == [('b', 2.5)]
196+
197+
um = m.UnorderedMapStringDouble()
198+
um['ua'] = 1.1
199+
um['ub'] = 2.6
200+
201+
assert sorted(list(um)) == ['ua', 'ub']
202+
assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
203+
del um['ua']
204+
assert sorted(list(um)) == ['ub']
205+
assert sorted(list(um.items())) == [('ub', 2.6)]

0 commit comments

Comments
 (0)