2024-06-22 04:55:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-08-13 16:37:05 +00:00
|
|
|
from pybind11_tests import ConstructorStats, UserType
|
2017-06-08 22:44:49 +00:00
|
|
|
from pybind11_tests import stl as m
|
|
|
|
|
|
|
|
|
|
|
|
def test_vector(doc):
|
|
|
|
"""std::vector <-> list"""
|
2017-10-24 23:39:46 +00:00
|
|
|
lst = m.cast_vector()
|
|
|
|
assert lst == [1]
|
|
|
|
lst.append(2)
|
|
|
|
assert m.load_vector(lst)
|
|
|
|
assert m.load_vector(tuple(lst))
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2017-09-01 19:42:20 +00:00
|
|
|
assert m.cast_bool_vector() == [True, False]
|
|
|
|
assert m.load_bool_vector([True, False])
|
2023-02-22 14:18:55 +00:00
|
|
|
assert m.load_bool_vector((True, False))
|
2017-09-01 19:42:20 +00:00
|
|
|
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.cast_vector) == "cast_vector() -> list[int]"
|
|
|
|
assert doc(m.load_vector) == "load_vector(arg0: list[int]) -> bool"
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2017-07-07 00:41:52 +00:00
|
|
|
# Test regression caused by 936: pointers to stl containers weren't castable
|
|
|
|
assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2023-02-22 14:18:55 +00:00
|
|
|
def test_deque():
|
2018-11-16 05:45:19 +00:00
|
|
|
"""std::deque <-> list"""
|
|
|
|
lst = m.cast_deque()
|
|
|
|
assert lst == [1]
|
|
|
|
lst.append(2)
|
|
|
|
assert m.load_deque(lst)
|
|
|
|
assert m.load_deque(tuple(lst))
|
|
|
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_array(doc):
|
|
|
|
"""std::array <-> list"""
|
2017-10-24 23:39:46 +00:00
|
|
|
lst = m.cast_array()
|
|
|
|
assert lst == [1, 2]
|
|
|
|
assert m.load_array(lst)
|
2022-04-24 21:39:47 +00:00
|
|
|
assert m.load_array(tuple(lst))
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.cast_array) == "cast_array() -> Annotated[list[int], FixedSize(2)]"
|
2023-05-25 04:39:36 +00:00
|
|
|
assert (
|
|
|
|
doc(m.load_array)
|
2023-09-12 19:46:58 +00:00
|
|
|
== "load_array(arg0: Annotated[list[int], FixedSize(2)]) -> bool"
|
2023-05-25 04:39:36 +00:00
|
|
|
)
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_valarray(doc):
|
|
|
|
"""std::valarray <-> list"""
|
2017-10-24 23:39:46 +00:00
|
|
|
lst = m.cast_valarray()
|
|
|
|
assert lst == [1, 4, 9]
|
|
|
|
assert m.load_valarray(lst)
|
2022-04-24 21:39:47 +00:00
|
|
|
assert m.load_valarray(tuple(lst))
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.cast_valarray) == "cast_valarray() -> list[int]"
|
|
|
|
assert doc(m.load_valarray) == "load_valarray(arg0: list[int]) -> bool"
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_map(doc):
|
|
|
|
"""std::map <-> dict"""
|
|
|
|
d = m.cast_map()
|
|
|
|
assert d == {"key": "value"}
|
2019-06-10 19:01:11 +00:00
|
|
|
assert "key" in d
|
2017-06-08 22:44:49 +00:00
|
|
|
d["key2"] = "value2"
|
2019-06-10 19:01:11 +00:00
|
|
|
assert "key2" in d
|
2017-06-08 22:44:49 +00:00
|
|
|
assert m.load_map(d)
|
|
|
|
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.cast_map) == "cast_map() -> dict[str, str]"
|
|
|
|
assert doc(m.load_map) == "load_map(arg0: dict[str, str]) -> bool"
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_set(doc):
|
|
|
|
"""std::set <-> set"""
|
|
|
|
s = m.cast_set()
|
|
|
|
assert s == {"key1", "key2"}
|
|
|
|
s.add("key3")
|
|
|
|
assert m.load_set(s)
|
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>
2022-05-05 19:09:56 +00:00
|
|
|
assert m.load_set(frozenset(s))
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.cast_set) == "cast_set() -> set[str]"
|
|
|
|
assert doc(m.load_set) == "load_set(arg0: set[str]) -> bool"
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
def test_recursive_casting():
|
|
|
|
"""Tests that stl casters preserve lvalue/rvalue context for container values"""
|
|
|
|
assert m.cast_rv_vector() == ["rvalue", "rvalue"]
|
|
|
|
assert m.cast_lv_vector() == ["lvalue", "lvalue"]
|
|
|
|
assert m.cast_rv_array() == ["rvalue", "rvalue", "rvalue"]
|
|
|
|
assert m.cast_lv_array() == ["lvalue", "lvalue"]
|
|
|
|
assert m.cast_rv_map() == {"a": "rvalue"}
|
|
|
|
assert m.cast_lv_map() == {"a": "lvalue", "b": "lvalue"}
|
|
|
|
assert m.cast_rv_nested() == [[[{"b": "rvalue", "c": "rvalue"}], [{"a": "rvalue"}]]]
|
|
|
|
assert m.cast_lv_nested() == {
|
|
|
|
"a": [[["lvalue", "lvalue"]], [["lvalue", "lvalue"]]],
|
2020-10-16 20:38:13 +00:00
|
|
|
"b": [[["lvalue", "lvalue"], ["lvalue", "lvalue"]]],
|
2017-07-03 23:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Issue #853 test case:
|
|
|
|
z = m.cast_unique_ptr_vector()
|
2023-02-22 14:18:55 +00:00
|
|
|
assert z[0].value == 7
|
|
|
|
assert z[1].value == 42
|
2017-07-03 23:12:09 +00:00
|
|
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_move_out_container():
|
|
|
|
"""Properties use the `reference_internal` policy by default. If the underlying function
|
|
|
|
returns an rvalue, the policy is automatically changed to `move` to avoid referencing
|
|
|
|
a temporary. In case the return value is a container of user-defined types, the policy
|
|
|
|
also needs to be applied to the elements, not just the container."""
|
|
|
|
c = m.MoveOutContainer()
|
|
|
|
moved_out_list = c.move_list
|
|
|
|
assert [x.value for x in moved_out_list] == [0, 1, 2]
|
|
|
|
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
@pytest.mark.skipif(not hasattr(m, "has_optional"), reason="no <optional>")
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_optional():
|
|
|
|
assert m.double_or_zero(None) == 0
|
|
|
|
assert m.double_or_zero(42) == 84
|
2020-10-16 20:38:13 +00:00
|
|
|
pytest.raises(TypeError, m.double_or_zero, "foo")
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
assert m.half_or_none(0) is None
|
|
|
|
assert m.half_or_none(42) == 21
|
2020-10-16 20:38:13 +00:00
|
|
|
pytest.raises(TypeError, m.half_or_none, "foo")
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
assert m.test_nullopt() == 42
|
|
|
|
assert m.test_nullopt(None) == 42
|
|
|
|
assert m.test_nullopt(42) == 42
|
|
|
|
assert m.test_nullopt(43) == 43
|
|
|
|
|
|
|
|
assert m.test_no_assign() == 42
|
|
|
|
assert m.test_no_assign(None) == 42
|
|
|
|
assert m.test_no_assign(m.NoAssign(43)) == 43
|
|
|
|
pytest.raises(TypeError, m.test_no_assign, 43)
|
|
|
|
|
|
|
|
assert m.nodefer_none_optional(None)
|
|
|
|
|
2020-03-14 13:15:12 +00:00
|
|
|
holder = m.OptionalHolder()
|
|
|
|
mvalue = holder.member
|
|
|
|
assert mvalue.initialized
|
|
|
|
assert holder.member_initialized()
|
|
|
|
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
props = m.OptionalProperties()
|
|
|
|
assert int(props.access_by_ref) == 42
|
|
|
|
assert int(props.access_by_copy) == 42
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not hasattr(m, "has_exp_optional"), reason="no <experimental/optional>"
|
|
|
|
)
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_exp_optional():
|
|
|
|
assert m.double_or_zero_exp(None) == 0
|
|
|
|
assert m.double_or_zero_exp(42) == 84
|
2020-10-16 20:38:13 +00:00
|
|
|
pytest.raises(TypeError, m.double_or_zero_exp, "foo")
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
assert m.half_or_none_exp(0) is None
|
|
|
|
assert m.half_or_none_exp(42) == 21
|
2020-10-16 20:38:13 +00:00
|
|
|
pytest.raises(TypeError, m.half_or_none_exp, "foo")
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
assert m.test_nullopt_exp() == 42
|
|
|
|
assert m.test_nullopt_exp(None) == 42
|
|
|
|
assert m.test_nullopt_exp(42) == 42
|
|
|
|
assert m.test_nullopt_exp(43) == 43
|
|
|
|
|
|
|
|
assert m.test_no_assign_exp() == 42
|
|
|
|
assert m.test_no_assign_exp(None) == 42
|
|
|
|
assert m.test_no_assign_exp(m.NoAssign(43)) == 43
|
|
|
|
pytest.raises(TypeError, m.test_no_assign_exp, 43)
|
|
|
|
|
2020-03-14 13:15:12 +00:00
|
|
|
holder = m.OptionalExpHolder()
|
|
|
|
mvalue = holder.member
|
|
|
|
assert mvalue.initialized
|
|
|
|
assert holder.member_initialized()
|
|
|
|
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
props = m.OptionalExpProperties()
|
|
|
|
assert int(props.access_by_ref) == 42
|
|
|
|
assert int(props.access_by_copy) == 42
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not hasattr(m, "has_boost_optional"), reason="no <boost/optional>")
|
|
|
|
def test_boost_optional():
|
|
|
|
assert m.double_or_zero_boost(None) == 0
|
|
|
|
assert m.double_or_zero_boost(42) == 84
|
|
|
|
pytest.raises(TypeError, m.double_or_zero_boost, "foo")
|
|
|
|
|
|
|
|
assert m.half_or_none_boost(0) is None
|
|
|
|
assert m.half_or_none_boost(42) == 21
|
|
|
|
pytest.raises(TypeError, m.half_or_none_boost, "foo")
|
|
|
|
|
|
|
|
assert m.test_nullopt_boost() == 42
|
|
|
|
assert m.test_nullopt_boost(None) == 42
|
|
|
|
assert m.test_nullopt_boost(42) == 42
|
|
|
|
assert m.test_nullopt_boost(43) == 43
|
|
|
|
|
|
|
|
assert m.test_no_assign_boost() == 42
|
|
|
|
assert m.test_no_assign_boost(None) == 42
|
|
|
|
assert m.test_no_assign_boost(m.NoAssign(43)) == 43
|
|
|
|
pytest.raises(TypeError, m.test_no_assign_boost, 43)
|
|
|
|
|
|
|
|
holder = m.OptionalBoostHolder()
|
|
|
|
mvalue = holder.member
|
|
|
|
assert mvalue.initialized
|
|
|
|
assert holder.member_initialized()
|
|
|
|
|
|
|
|
props = m.OptionalBoostProperties()
|
|
|
|
assert int(props.access_by_ref) == 42
|
|
|
|
assert int(props.access_by_copy) == 42
|
|
|
|
|
|
|
|
|
|
|
|
def test_reference_sensitive_optional():
|
|
|
|
assert m.double_or_zero_refsensitive(None) == 0
|
|
|
|
assert m.double_or_zero_refsensitive(42) == 84
|
|
|
|
pytest.raises(TypeError, m.double_or_zero_refsensitive, "foo")
|
|
|
|
|
|
|
|
assert m.half_or_none_refsensitive(0) is None
|
|
|
|
assert m.half_or_none_refsensitive(42) == 21
|
|
|
|
pytest.raises(TypeError, m.half_or_none_refsensitive, "foo")
|
|
|
|
|
|
|
|
assert m.test_nullopt_refsensitive() == 42
|
|
|
|
assert m.test_nullopt_refsensitive(None) == 42
|
|
|
|
assert m.test_nullopt_refsensitive(42) == 42
|
|
|
|
assert m.test_nullopt_refsensitive(43) == 43
|
|
|
|
|
|
|
|
assert m.test_no_assign_refsensitive() == 42
|
|
|
|
assert m.test_no_assign_refsensitive(None) == 42
|
|
|
|
assert m.test_no_assign_refsensitive(m.NoAssign(43)) == 43
|
|
|
|
pytest.raises(TypeError, m.test_no_assign_refsensitive, 43)
|
|
|
|
|
|
|
|
holder = m.OptionalRefSensitiveHolder()
|
|
|
|
mvalue = holder.member
|
|
|
|
assert mvalue.initialized
|
|
|
|
assert holder.member_initialized()
|
|
|
|
|
|
|
|
props = m.OptionalRefSensitiveProperties()
|
|
|
|
assert int(props.access_by_ref) == 42
|
|
|
|
assert int(props.access_by_copy) == 42
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2021-07-02 14:00:50 +00:00
|
|
|
@pytest.mark.skipif(not hasattr(m, "has_filesystem"), reason="no <filesystem>")
|
|
|
|
def test_fs_path():
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
class PseudoStrPath:
|
|
|
|
def __fspath__(self):
|
|
|
|
return "foo/bar"
|
|
|
|
|
|
|
|
class PseudoBytesPath:
|
|
|
|
def __fspath__(self):
|
|
|
|
return b"foo/bar"
|
|
|
|
|
|
|
|
assert m.parent_path(Path("foo/bar")) == Path("foo")
|
|
|
|
assert m.parent_path("foo/bar") == Path("foo")
|
|
|
|
assert m.parent_path(b"foo/bar") == Path("foo")
|
|
|
|
assert m.parent_path(PseudoStrPath()) == Path("foo")
|
|
|
|
assert m.parent_path(PseudoBytesPath()) == Path("foo")
|
|
|
|
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
@pytest.mark.skipif(not hasattr(m, "load_variant"), reason="no <variant>")
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_variant(doc):
|
|
|
|
assert m.load_variant(1) == "int"
|
|
|
|
assert m.load_variant("1") == "std::string"
|
|
|
|
assert m.load_variant(1.0) == "double"
|
|
|
|
assert m.load_variant(None) == "std::nullptr_t"
|
|
|
|
|
|
|
|
assert m.load_variant_2pass(1) == "int"
|
|
|
|
assert m.load_variant_2pass(1.0) == "double"
|
|
|
|
|
|
|
|
assert m.cast_variant() == (5, "Hello")
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"
|
|
|
|
)
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
2022-03-22 05:58:04 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not hasattr(m, "load_monostate_variant"), reason="no std::monostate"
|
|
|
|
)
|
|
|
|
def test_variant_monostate(doc):
|
|
|
|
assert m.load_monostate_variant(None) == "std::monostate"
|
|
|
|
assert m.load_monostate_variant(1) == "int"
|
|
|
|
assert m.load_monostate_variant("1") == "std::string"
|
|
|
|
|
|
|
|
assert m.cast_monostate_variant() == (None, 5, "Hello")
|
|
|
|
|
|
|
|
assert (
|
|
|
|
doc(m.load_monostate_variant)
|
|
|
|
== "load_monostate_variant(arg0: Union[None, int, str]) -> str"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_vec_of_reference_wrapper():
|
|
|
|
"""#171: Can't return reference wrappers (or STL structures containing them)"""
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
str(m.return_vec_of_reference_wrapper(UserType(4)))
|
|
|
|
== "[UserType(1), UserType(2), UserType(3), UserType(4)]"
|
|
|
|
)
|
2017-05-09 13:01:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stl_pass_by_pointer(msg):
|
|
|
|
"""Passing nullptr or None to an STL container pointer is not expected to work"""
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.stl_pass_by_pointer() # default value is `nullptr`
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-05-09 13:01:22 +00:00
|
|
|
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
|
2023-09-12 19:46:58 +00:00
|
|
|
1. (v: list[int] = None) -> list[int]
|
2017-05-09 13:01:22 +00:00
|
|
|
|
|
|
|
Invoked with:
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-05-09 13:01:22 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.stl_pass_by_pointer(None)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-05-09 13:01:22 +00:00
|
|
|
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
|
2023-09-12 19:46:58 +00:00
|
|
|
1. (v: list[int] = None) -> list[int]
|
2017-05-09 13:01:22 +00:00
|
|
|
|
|
|
|
Invoked with: None
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-05-09 13:01:22 +00:00
|
|
|
|
|
|
|
assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
|
2017-09-09 18:21:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_missing_header_message():
|
|
|
|
"""Trying convert `list` to a `std::vector`, or vice versa, without including
|
|
|
|
<pybind11/stl.h> should result in a helpful suggestion in the error message"""
|
|
|
|
import pybind11_cross_module_tests as cm
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
expected_message = (
|
|
|
|
"Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
|
|
|
|
"<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
|
|
|
|
"conversions are optional and require extra headers to be included\n"
|
|
|
|
"when compiling your pybind11 module."
|
|
|
|
)
|
2017-09-09 18:21:34 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
cm.missing_header_arg([1.0, 2.0, 3.0])
|
|
|
|
assert expected_message in str(excinfo.value)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
cm.missing_header_return()
|
|
|
|
assert expected_message in str(excinfo.value)
|
2018-07-17 14:56:26 +00:00
|
|
|
|
|
|
|
|
2018-10-11 08:28:12 +00:00
|
|
|
def test_function_with_string_and_vector_string_arg():
|
|
|
|
"""Check if a string is NOT implicitly converted to a list, which was the
|
|
|
|
behavior before fix of issue #1258"""
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.func_with_string_or_vector_string_arg_overload(("A", "B")) == 2
|
|
|
|
assert m.func_with_string_or_vector_string_arg_overload(["A", "B"]) == 2
|
|
|
|
assert m.func_with_string_or_vector_string_arg_overload("A") == 3
|
2018-10-11 08:28:12 +00:00
|
|
|
|
|
|
|
|
2018-07-17 14:56:26 +00:00
|
|
|
def test_stl_ownership():
|
|
|
|
cstats = ConstructorStats.get(m.Placeholder)
|
|
|
|
assert cstats.alive() == 0
|
|
|
|
r = m.test_stl_ownership()
|
|
|
|
assert len(r) == 1
|
|
|
|
del r
|
|
|
|
assert cstats.alive() == 0
|
2018-11-09 11:32:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_array_cast_sequence():
|
|
|
|
assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]
|
2018-11-09 19:12:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_issue_1561():
|
2021-07-12 18:56:06 +00:00
|
|
|
"""check fix for issue #1561"""
|
2018-11-09 19:12:46 +00:00
|
|
|
bar = m.Issue1561Outer()
|
2020-10-16 20:38:13 +00:00
|
|
|
bar.list = [m.Issue1561Inner("bar")]
|
2023-04-28 18:32:32 +00:00
|
|
|
assert bar.list
|
2020-10-16 20:38:13 +00:00
|
|
|
assert bar.list[0].data == "bar"
|
2021-07-12 23:56:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_return_vector_bool_raw_ptr():
|
|
|
|
# Add `while True:` for manual leak checking.
|
|
|
|
v = m.return_vector_bool_raw_ptr()
|
|
|
|
assert isinstance(v, list)
|
|
|
|
assert len(v) == 4513
|