mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +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:
@@ -66,11 +66,12 @@ def test_none(capture, doc):
|
||||
|
||||
def test_set(capture, doc):
|
||||
s = m.get_set()
|
||||
assert isinstance(s, set)
|
||||
assert s == {"key1", "key2", "key3"}
|
||||
|
||||
s.add("key4")
|
||||
with capture:
|
||||
s.add("key4")
|
||||
m.print_set(s)
|
||||
m.print_anyset(s)
|
||||
assert (
|
||||
capture.unordered
|
||||
== """
|
||||
@@ -81,12 +82,43 @@ def test_set(capture, doc):
|
||||
"""
|
||||
)
|
||||
|
||||
assert not m.set_contains(set(), 42)
|
||||
assert m.set_contains({42}, 42)
|
||||
assert m.set_contains({"foo"}, "foo")
|
||||
m.set_add(s, "key5")
|
||||
assert m.anyset_size(s) == 5
|
||||
|
||||
assert doc(m.get_list) == "get_list() -> list"
|
||||
assert doc(m.print_list) == "print_list(arg0: list) -> None"
|
||||
m.set_clear(s)
|
||||
assert m.anyset_empty(s)
|
||||
|
||||
assert not m.anyset_contains(set(), 42)
|
||||
assert m.anyset_contains({42}, 42)
|
||||
assert m.anyset_contains({"foo"}, "foo")
|
||||
|
||||
assert doc(m.get_set) == "get_set() -> set"
|
||||
assert doc(m.print_anyset) == "print_anyset(arg0: anyset) -> None"
|
||||
|
||||
|
||||
def test_frozenset(capture, doc):
|
||||
s = m.get_frozenset()
|
||||
assert isinstance(s, frozenset)
|
||||
assert s == frozenset({"key1", "key2", "key3"})
|
||||
|
||||
with capture:
|
||||
m.print_anyset(s)
|
||||
assert (
|
||||
capture.unordered
|
||||
== """
|
||||
key: key1
|
||||
key: key2
|
||||
key: key3
|
||||
"""
|
||||
)
|
||||
assert m.anyset_size(s) == 3
|
||||
assert not m.anyset_empty(s)
|
||||
|
||||
assert not m.anyset_contains(frozenset(), 42)
|
||||
assert m.anyset_contains(frozenset({42}), 42)
|
||||
assert m.anyset_contains(frozenset({"foo"}), "foo")
|
||||
|
||||
assert doc(m.get_frozenset) == "get_frozenset() -> frozenset"
|
||||
|
||||
|
||||
def test_dict(capture, doc):
|
||||
@@ -302,6 +334,7 @@ def test_constructors():
|
||||
list: range(3),
|
||||
dict: [("two", 2), ("one", 1), ("three", 3)],
|
||||
set: [4, 4, 5, 6, 6, 6],
|
||||
frozenset: [4, 4, 5, 6, 6, 6],
|
||||
memoryview: b"abc",
|
||||
}
|
||||
inputs = {k.__name__: v for k, v in data.items()}
|
||||
|
||||
Reference in New Issue
Block a user