Skip to content

Commit 2d77e4e

Browse files
committed
py: Remove custom definition of keys() as it exists in pybind11 now
pybind/pybind11#3310 added keys() to maps
1 parent 06cf765 commit 2d77e4e

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

py/pybind11_common.h

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,34 +130,30 @@ template <typename Map, typename holder_type = std::unique_ptr<Map>, typename...
130130
py::class_<Map, holder_type> BindMap(py::handle scope, const std::string& name, Args&&... args) {
131131
using Key = typename Map::key_type;
132132
using Value = typename Map::mapped_type;
133-
auto cl =
134-
py::bind_map<Map, holder_type>(scope, name, std::forward<Args>(args)...)
135-
.def(py::init([&](py::iterator it) {
136-
return MapFromIter<Map, Key>(it, MapCastValue<Map, Key, Value>);
137-
}),
138-
"iterator"_a)
139-
.def(py::init([&](py::dict dict) {
140-
return MapFromDict<Map, Key>(dict, MapCastValue<Map, Key, Value>);
141-
}),
142-
"dictionary"_a)
143-
.def(py::self == py::self)
144-
.def("clear", &Map::clear)
145-
.def(
146-
"get",
147-
[](const Map& map, const Key& key,
148-
std::optional<py::object> default_value) -> std::variant<py::object, Value> {
149-
const auto it = map.find(key);
150-
if (it == map.cend()) {
151-
if (default_value)
152-
return *default_value;
153-
throw py::key_error();
154-
}
155-
return it->second;
156-
},
157-
"key"_a, "default"_a = std::nullopt, py::keep_alive<0, 1>())
158-
.def(
159-
"keys", [](const Map& map) { return py::make_key_iterator(map.begin(), map.end()); },
160-
py::keep_alive<0, 1>());
133+
auto cl = py::bind_map<Map, holder_type>(scope, name, std::forward<Args>(args)...)
134+
.def(py::init([&](py::iterator it) {
135+
return MapFromIter<Map, Key>(it, MapCastValue<Map, Key, Value>);
136+
}),
137+
"iterator"_a)
138+
.def(py::init([&](py::dict dict) {
139+
return MapFromDict<Map, Key>(dict, MapCastValue<Map, Key, Value>);
140+
}),
141+
"dictionary"_a)
142+
.def(py::self == py::self)
143+
.def("clear", &Map::clear)
144+
.def(
145+
"get",
146+
[](const Map& map, const Key& key,
147+
std::optional<py::object> default_value) -> std::variant<py::object, Value> {
148+
const auto it = map.find(key);
149+
if (it == map.cend()) {
150+
if (default_value)
151+
return *default_value;
152+
throw py::key_error();
153+
}
154+
return it->second;
155+
},
156+
"key"_a, "default"_a = std::nullopt, py::keep_alive<0, 1>());
161157
py::implicitly_convertible<py::dict, Map>();
162158
return cl;
163159
}

0 commit comments

Comments
 (0)