From 2807027b83032043f16de9bb276ae5025c20535a Mon Sep 17 00:00:00 2001 From: Blake Thompson Date: Fri, 12 Apr 2019 11:31:25 -0500 Subject: [PATCH 1/2] Added __contains__ to stl bindings for maps --- include/pybind11/stl_bind.h | 9 +++++++++ tests/test_stl.py | 2 ++ 2 files changed, 11 insertions(+) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 38dd68f695..7ca3ad90b6 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -578,6 +578,15 @@ class_ bind_map(handle scope, const std::string &name, Args&&. }, return_value_policy::reference_internal // ref + keepalive ); + + cl.def("__contains__", + [](Map &m, const KeyType &k) -> bool { + auto it = m.find(k); + if (it == m.end()) + return false; + return true; + } + ); // Assignment provided only if the type is copyable detail::map_assignment(cl); diff --git a/tests/test_stl.py b/tests/test_stl.py index bf185d57b5..2335cb9fdf 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -56,7 +56,9 @@ def test_map(doc): """std::map <-> dict""" d = m.cast_map() assert d == {"key": "value"} + assert "key" in d d["key2"] = "value2" + assert "key2" in d assert m.load_map(d) assert doc(m.cast_map) == "cast_map() -> Dict[str, str]" From cd08846756197ce45f375cefe6b2050277dc99b8 Mon Sep 17 00:00:00 2001 From: Blake Thompson Date: Fri, 12 Apr 2019 11:49:30 -0500 Subject: [PATCH 2/2] Remove trailing whitespace --- include/pybind11/stl_bind.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 7ca3ad90b6..d6f4c6332e 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -578,7 +578,7 @@ class_ bind_map(handle scope, const std::string &name, Args&&. }, return_value_policy::reference_internal // ref + keepalive ); - + cl.def("__contains__", [](Map &m, const KeyType &k) -> bool { auto it = m.find(k);