mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
Add anyset & frozenset, enable copying (cast) to std::set (#3901)
* Add frozenset, and allow it cast to std::set For the reverse direction, std::set still casts to set. This is in concordance with the behavior for sequence containers, where e.g. tuple casts to std::vector but std::vector casts to list. Extracted from #3886. * Rename set_base to any_set to match Python C API since this will be part of pybind11 public API * PR: static_cast, anyset * Add tests for frozenset and rename anyset methods * Remove frozenset default ctor, add tests Making frozenset non-default constructible means that we need to adjust pyobject_caster to not require that its value is default constructible, by initializing value to a nil handle. This also allows writing C++ functions taking anyset, and is arguably a performance improvement, since there is no need to allocate an object that will just be replaced by load. Add some more tests, including anyset::empty, anyset::size, set::add and set::clear. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add rationale to `pyobject_caster` default ctor * Remove ineffectual protected: access control Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -75,7 +75,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("get_none", [] { return py::none(); });
|
||||
m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); });
|
||||
|
||||
// test_set
|
||||
// test_set, test_frozenset
|
||||
m.def("get_set", []() {
|
||||
py::set set;
|
||||
set.add(py::str("key1"));
|
||||
@@ -83,14 +83,26 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
set.add(std::string("key3"));
|
||||
return set;
|
||||
});
|
||||
m.def("print_set", [](const py::set &set) {
|
||||
m.def("get_frozenset", []() {
|
||||
py::set set;
|
||||
set.add(py::str("key1"));
|
||||
set.add("key2");
|
||||
set.add(std::string("key3"));
|
||||
return py::frozenset(set);
|
||||
});
|
||||
m.def("print_anyset", [](const py::anyset &set) {
|
||||
for (auto item : set) {
|
||||
py::print("key:", item);
|
||||
}
|
||||
});
|
||||
m.def("set_contains",
|
||||
[](const py::set &set, const py::object &key) { return set.contains(key); });
|
||||
m.def("set_contains", [](const py::set &set, const char *key) { return set.contains(key); });
|
||||
m.def("anyset_size", [](const py::anyset &set) { return set.size(); });
|
||||
m.def("anyset_empty", [](const py::anyset &set) { return set.empty(); });
|
||||
m.def("anyset_contains",
|
||||
[](const py::anyset &set, const py::object &key) { return set.contains(key); });
|
||||
m.def("anyset_contains",
|
||||
[](const py::anyset &set, const char *key) { return set.contains(key); });
|
||||
m.def("set_add", [](py::set &set, const py::object &key) { set.add(key); });
|
||||
m.def("set_clear", [](py::set &set) { set.clear(); });
|
||||
|
||||
// test_dict
|
||||
m.def("get_dict", []() { return py::dict("key"_a = "value"); });
|
||||
@@ -310,6 +322,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
"list"_a = py::list(d["list"]),
|
||||
"dict"_a = py::dict(d["dict"]),
|
||||
"set"_a = py::set(d["set"]),
|
||||
"frozenset"_a = py::frozenset(d["frozenset"]),
|
||||
"memoryview"_a = py::memoryview(d["memoryview"]));
|
||||
});
|
||||
|
||||
@@ -325,6 +338,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
"list"_a = d["list"].cast<py::list>(),
|
||||
"dict"_a = d["dict"].cast<py::dict>(),
|
||||
"set"_a = d["set"].cast<py::set>(),
|
||||
"frozenset"_a = d["frozenset"].cast<py::frozenset>(),
|
||||
"memoryview"_a = d["memoryview"].cast<py::memoryview>());
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user