2023-07-31 17:55:54 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-01-02 11:46:17 +00:00
|
|
|
import env
|
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 class_ as m
|
|
|
|
|
|
|
|
|
2023-01-02 11:46:17 +00:00
|
|
|
def test_obj_class_name():
|
2023-02-22 14:18:55 +00:00
|
|
|
expected_name = "UserType" if env.PYPY else "pybind11_tests.UserType"
|
2023-01-02 11:46:17 +00:00
|
|
|
assert m.obj_class_name(UserType(1)) == expected_name
|
|
|
|
assert m.obj_class_name(UserType) == expected_name
|
|
|
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_repr():
|
|
|
|
assert "pybind11_type" in repr(type(UserType))
|
|
|
|
assert "UserType" in repr(UserType)
|
|
|
|
|
|
|
|
|
|
|
|
def test_instance(msg):
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.NoConstructor()
|
|
|
|
assert msg(excinfo.value) == "m.class_.NoConstructor: No constructor defined!"
|
|
|
|
|
|
|
|
instance = m.NoConstructor.new_instance()
|
|
|
|
|
|
|
|
cstats = ConstructorStats.get(m.NoConstructor)
|
|
|
|
assert cstats.alive() == 1
|
|
|
|
del instance
|
|
|
|
assert cstats.alive() == 0
|
|
|
|
|
|
|
|
|
2023-02-22 14:18:55 +00:00
|
|
|
def test_instance_new():
|
2021-09-20 14:42:14 +00:00
|
|
|
instance = m.NoConstructorNew() # .__new__(m.NoConstructor.__class__)
|
|
|
|
cstats = ConstructorStats.get(m.NoConstructorNew)
|
|
|
|
assert cstats.alive() == 1
|
|
|
|
del instance
|
|
|
|
assert cstats.alive() == 0
|
|
|
|
|
|
|
|
|
2020-09-14 22:06:26 +00:00
|
|
|
def test_type():
|
|
|
|
assert m.check_type(1) == m.DerivedClass1
|
|
|
|
with pytest.raises(RuntimeError) as execinfo:
|
|
|
|
m.check_type(0)
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert "pybind11::detail::get_type_info: unable to find type info" in str(
|
|
|
|
execinfo.value
|
|
|
|
)
|
|
|
|
assert "Invalid" in str(execinfo.value)
|
2020-09-14 22:06:26 +00:00
|
|
|
|
|
|
|
# Currently not supported
|
|
|
|
# See https://github.com/pybind/pybind11/issues/2486
|
|
|
|
# assert m.check_type(2) == int
|
|
|
|
|
|
|
|
|
|
|
|
def test_type_of_py():
|
|
|
|
assert m.get_type_of(1) == int
|
|
|
|
assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
|
|
|
|
assert m.get_type_of(int) == type
|
|
|
|
|
|
|
|
|
2020-09-17 02:02:09 +00:00
|
|
|
def test_type_of_classic():
|
|
|
|
assert m.get_type_classic(1) == int
|
|
|
|
assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
|
|
|
|
assert m.get_type_classic(int) == type
|
|
|
|
|
|
|
|
|
2020-09-14 22:06:26 +00:00
|
|
|
def test_type_of_py_nodelete():
|
|
|
|
# If the above test deleted the class, this will segfault
|
|
|
|
assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
|
|
|
|
|
|
|
|
|
|
|
|
def test_as_type_py():
|
|
|
|
assert m.as_type(int) == int
|
|
|
|
|
2020-10-05 20:48:54 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-09-14 22:06:26 +00:00
|
|
|
assert m.as_type(1) == int
|
|
|
|
|
2020-10-05 20:48:54 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-09-14 22:06:26 +00:00
|
|
|
assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
|
|
|
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
def test_docstrings(doc):
|
|
|
|
assert doc(UserType) == "A `py::class_` type for testing"
|
|
|
|
assert UserType.__name__ == "UserType"
|
|
|
|
assert UserType.__module__ == "pybind11_tests"
|
|
|
|
assert UserType.get_value.__name__ == "get_value"
|
|
|
|
assert UserType.get_value.__module__ == "pybind11_tests"
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(UserType.get_value)
|
|
|
|
== """
|
2017-06-08 22:44:49 +00:00
|
|
|
get_value(self: m.UserType) -> int
|
|
|
|
|
|
|
|
Get value using a method
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert doc(UserType.value) == "Get/set value using a property"
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.NoConstructor.new_instance)
|
|
|
|
== """
|
2017-06-08 22:44:49 +00:00
|
|
|
new_instance() -> m.class_.NoConstructor
|
|
|
|
|
|
|
|
Return an instance
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-06-22 21:42:11 +00:00
|
|
|
|
|
|
|
|
2017-11-07 16:33:05 +00:00
|
|
|
def test_qualname(doc):
|
2022-02-11 02:28:08 +00:00
|
|
|
"""Tests that a properly qualified name is set in __qualname__ and that
|
|
|
|
generated docstrings properly use it and the module name"""
|
2017-11-07 16:33:05 +00:00
|
|
|
assert m.NestBase.__qualname__ == "NestBase"
|
|
|
|
assert m.NestBase.Nested.__qualname__ == "NestBase.Nested"
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.NestBase.__init__)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
__init__(self: m.class_.NestBase) -> None
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.NestBase.g)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.NestBase.Nested.__init__)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
__init__(self: m.class_.NestBase.Nested) -> None
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.NestBase.Nested.fn)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
fn(self: m.class_.NestBase.Nested, arg0: int, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.NestBase.Nested.fa)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
fa(self: m.class_.NestBase.Nested, a: int, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-11-07 16:33:05 +00:00
|
|
|
assert m.NestBase.__module__ == "pybind11_tests.class_"
|
|
|
|
assert m.NestBase.Nested.__module__ == "pybind11_tests.class_"
|
|
|
|
|
|
|
|
|
2017-06-22 21:42:11 +00:00
|
|
|
def test_inheritance(msg):
|
2020-10-16 20:38:13 +00:00
|
|
|
roger = m.Rabbit("Rabbit")
|
2017-06-22 21:42:11 +00:00
|
|
|
assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
|
|
|
|
assert m.pet_name_species(roger) == "Rabbit is a parrot"
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
polly = m.Pet("Polly", "parrot")
|
2017-06-22 21:42:11 +00:00
|
|
|
assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
|
|
|
|
assert m.pet_name_species(polly) == "Polly is a parrot"
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
molly = m.Dog("Molly")
|
2017-06-22 21:42:11 +00:00
|
|
|
assert molly.name() + " is a " + molly.species() == "Molly is a dog"
|
|
|
|
assert m.pet_name_species(molly) == "Molly is a dog"
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
fred = m.Hamster("Fred")
|
2017-06-22 21:42:11 +00:00
|
|
|
assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
|
|
|
|
|
|
|
|
assert m.dog_bark(molly) == "Woof!"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.dog_bark(polly)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-06-22 21:42:11 +00:00
|
|
|
dog_bark(): incompatible function arguments. The following argument types are supported:
|
|
|
|
1. (arg0: m.class_.Dog) -> str
|
|
|
|
|
|
|
|
Invoked with: <m.class_.Pet object at 0>
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-06-22 21:42:11 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.Chimera("lion", "goat")
|
|
|
|
assert "No constructor defined!" in str(excinfo.value)
|
|
|
|
|
|
|
|
|
2020-07-07 10:04:06 +00:00
|
|
|
def test_inheritance_init(msg):
|
|
|
|
# Single base
|
|
|
|
class Python(m.Pet):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-07-07 10:04:06 +00:00
|
|
|
with pytest.raises(TypeError) as exc_info:
|
|
|
|
Python()
|
2020-10-02 02:57:25 +00:00
|
|
|
expected = "m.class_.Pet.__init__() must be called when overriding __init__"
|
|
|
|
assert msg(exc_info.value) == expected
|
2020-07-07 10:04:06 +00:00
|
|
|
|
|
|
|
# Multiple bases
|
|
|
|
class RabbitHamster(m.Rabbit, m.Hamster):
|
|
|
|
def __init__(self):
|
|
|
|
m.Rabbit.__init__(self, "RabbitHamster")
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as exc_info:
|
|
|
|
RabbitHamster()
|
2020-10-02 02:57:25 +00:00
|
|
|
expected = "m.class_.Hamster.__init__() must be called when overriding __init__"
|
|
|
|
assert msg(exc_info.value) == expected
|
2020-07-07 10:04:06 +00:00
|
|
|
|
|
|
|
|
2023-07-31 17:55:54 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"mock_return_value", [None, (1, 2, 3), m.Pet("Polly", "parrot"), m.Dog("Molly")]
|
|
|
|
)
|
|
|
|
def test_mock_new(mock_return_value):
|
|
|
|
with mock.patch.object(
|
|
|
|
m.Pet, "__new__", return_value=mock_return_value
|
|
|
|
) as mock_new:
|
|
|
|
obj = m.Pet("Noname", "Nospecies")
|
|
|
|
assert obj is mock_return_value
|
|
|
|
mock_new.assert_called_once_with(m.Pet, "Noname", "Nospecies")
|
|
|
|
|
|
|
|
|
2017-06-22 21:42:11 +00:00
|
|
|
def test_automatic_upcasting():
|
|
|
|
assert type(m.return_class_1()).__name__ == "DerivedClass1"
|
|
|
|
assert type(m.return_class_2()).__name__ == "DerivedClass2"
|
|
|
|
assert type(m.return_none()).__name__ == "NoneType"
|
|
|
|
# Repeat these a few times in a random order to ensure no invalid caching is applied
|
|
|
|
assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
|
|
|
|
assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(m.return_class_n(0)).__name__ == "BaseClass"
|
|
|
|
assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(m.return_class_n(0)).__name__ == "BaseClass"
|
|
|
|
assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
|
|
|
|
|
|
|
|
|
|
|
|
def test_isinstance():
|
2023-02-22 14:18:55 +00:00
|
|
|
objects = [(), {}, m.Pet("Polly", "parrot")] + [m.Dog("Molly")] * 4
|
2017-06-22 21:42:11 +00:00
|
|
|
expected = (True, True, True, True, True, False, False)
|
|
|
|
assert m.check_instances(objects) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_mismatched_holder():
|
|
|
|
import re
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
m.mismatched_holder_1()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert re.match(
|
|
|
|
'generic_type: type ".*MismatchDerived1" does not have a non-default '
|
|
|
|
'holder type while its base ".*MismatchBase1" does',
|
|
|
|
str(excinfo.value),
|
|
|
|
)
|
2017-06-22 21:42:11 +00:00
|
|
|
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
m.mismatched_holder_2()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert re.match(
|
|
|
|
'generic_type: type ".*MismatchDerived2" has a non-default holder type '
|
|
|
|
'while its base ".*MismatchBase2" does not',
|
|
|
|
str(excinfo.value),
|
|
|
|
)
|
2017-06-22 21:42:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_override_static():
|
|
|
|
"""#511: problem with inheritance + overwritten def_static"""
|
|
|
|
b = m.MyBase.make()
|
|
|
|
d1 = m.MyDerived.make2()
|
|
|
|
d2 = m.MyDerived.make()
|
|
|
|
|
|
|
|
assert isinstance(b, m.MyBase)
|
|
|
|
assert isinstance(d1, m.MyDerived)
|
|
|
|
assert isinstance(d2, m.MyDerived)
|
2017-06-26 18:34:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_implicit_conversion_life_support():
|
|
|
|
"""Ensure the lifetime of temporary objects created for implicit conversions"""
|
|
|
|
assert m.implicitly_convert_argument(UserType(5)) == 5
|
|
|
|
assert m.implicitly_convert_variable(UserType(5)) == 5
|
|
|
|
|
|
|
|
assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
|
2017-07-23 04:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_operator_new_delete(capture):
|
|
|
|
"""Tests that class-specific operator new/delete functions are invoked"""
|
|
|
|
|
|
|
|
class SubAliased(m.AliasedHasOpNewDelSize):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with capture:
|
|
|
|
a = m.HasOpNewDel()
|
|
|
|
b = m.HasOpNewDelSize()
|
|
|
|
d = m.HasOpNewDelBoth()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2017-07-23 04:32:58 +00:00
|
|
|
A new 8
|
|
|
|
B new 4
|
|
|
|
D new 32
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-07-23 04:32:58 +00:00
|
|
|
sz_alias = str(m.AliasedHasOpNewDelSize.size_alias)
|
|
|
|
sz_noalias = str(m.AliasedHasOpNewDelSize.size_noalias)
|
|
|
|
with capture:
|
|
|
|
c = m.AliasedHasOpNewDelSize()
|
|
|
|
c2 = SubAliased()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert capture == ("C new " + sz_noalias + "\n" + "C new " + sz_alias + "\n")
|
2017-07-23 04:32:58 +00:00
|
|
|
|
|
|
|
with capture:
|
|
|
|
del a
|
2017-07-26 17:52:53 +00:00
|
|
|
pytest.gc_collect()
|
2017-07-23 04:32:58 +00:00
|
|
|
del b
|
2017-07-26 17:52:53 +00:00
|
|
|
pytest.gc_collect()
|
2017-07-23 04:32:58 +00:00
|
|
|
del d
|
2017-07-26 17:52:53 +00:00
|
|
|
pytest.gc_collect()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
capture
|
|
|
|
== """
|
2017-07-23 04:32:58 +00:00
|
|
|
A delete
|
|
|
|
B delete 4
|
|
|
|
D delete
|
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-07-23 04:32:58 +00:00
|
|
|
|
|
|
|
with capture:
|
|
|
|
del c
|
2017-07-26 17:52:53 +00:00
|
|
|
pytest.gc_collect()
|
2017-07-23 04:32:58 +00:00
|
|
|
del c2
|
2017-07-26 17:52:53 +00:00
|
|
|
pytest.gc_collect()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert capture == ("C delete " + sz_noalias + "\n" + "C delete " + sz_alias + "\n")
|
2017-08-17 15:03:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bind_protected_functions():
|
|
|
|
"""Expose protected member functions to Python using a helper class"""
|
|
|
|
a = m.ProtectedA()
|
|
|
|
assert a.foo() == 42
|
|
|
|
|
|
|
|
b = m.ProtectedB()
|
|
|
|
assert b.foo() == 42
|
2022-10-22 23:52:35 +00:00
|
|
|
assert m.read_foo(b.void_foo()) == 42
|
|
|
|
assert m.pointers_equal(b.get_self(), b)
|
2017-08-17 15:03:46 +00:00
|
|
|
|
|
|
|
class C(m.ProtectedB):
|
|
|
|
def __init__(self):
|
|
|
|
m.ProtectedB.__init__(self)
|
|
|
|
|
|
|
|
def foo(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
c = C()
|
|
|
|
assert c.foo() == 0
|
2017-08-21 20:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_brace_initialization():
|
2021-07-12 18:56:06 +00:00
|
|
|
"""Tests that simple POD classes can be constructed using C++11 brace initialization"""
|
2017-08-21 20:48:28 +00:00
|
|
|
a = m.BraceInitialization(123, "test")
|
|
|
|
assert a.field1 == 123
|
|
|
|
assert a.field2 == "test"
|
2017-08-25 14:02:18 +00:00
|
|
|
|
2018-01-11 17:22:13 +00:00
|
|
|
# Tests that a non-simple class doesn't get brace initialization (if the
|
|
|
|
# class defines an initializer_list constructor, in particular, it would
|
|
|
|
# win over the expected constructor).
|
|
|
|
b = m.NoBraceInitialization([123, 456])
|
|
|
|
assert b.vec == [123, 456]
|
|
|
|
|
2017-08-25 14:02:18 +00:00
|
|
|
|
2020-08-16 20:02:12 +00:00
|
|
|
@pytest.mark.xfail("env.PYPY")
|
2017-08-25 14:02:18 +00:00
|
|
|
def test_class_refcount():
|
|
|
|
"""Instances must correctly increase/decrease the reference count of their types (#1029)"""
|
|
|
|
from sys import getrefcount
|
|
|
|
|
|
|
|
class PyDog(m.Dog):
|
|
|
|
pass
|
|
|
|
|
|
|
|
for cls in m.Dog, PyDog:
|
|
|
|
refcount_1 = getrefcount(cls)
|
|
|
|
molly = [cls("Molly") for _ in range(10)]
|
|
|
|
refcount_2 = getrefcount(cls)
|
|
|
|
|
|
|
|
del molly
|
|
|
|
pytest.gc_collect()
|
|
|
|
refcount_3 = getrefcount(cls)
|
|
|
|
|
|
|
|
assert refcount_1 == refcount_3
|
|
|
|
assert refcount_2 > refcount_1
|
2017-08-28 14:34:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_reentrant_implicit_conversion_failure(msg):
|
|
|
|
# ensure that there is no runaway reentrant implicit conversion (#1035)
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.BogusImplicitConversion(0)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-11-07 16:33:05 +00:00
|
|
|
__init__(): incompatible constructor arguments. The following argument types are supported:
|
|
|
|
1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion)
|
2017-08-28 14:34:06 +00:00
|
|
|
|
2017-11-07 16:33:05 +00:00
|
|
|
Invoked with: 0
|
2020-10-16 20:38:13 +00:00
|
|
|
"""
|
|
|
|
)
|
2018-09-25 21:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_error_after_conversions():
|
|
|
|
with pytest.raises(TypeError) as exc_info:
|
|
|
|
m.test_error_after_conversions("hello")
|
|
|
|
assert str(exc_info.value).startswith(
|
2020-10-16 20:38:13 +00:00
|
|
|
"Unable to convert function return value to a Python type!"
|
|
|
|
)
|
2018-11-09 19:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_aligned():
|
|
|
|
if hasattr(m, "Aligned"):
|
|
|
|
p = m.Aligned().ptr()
|
|
|
|
assert p % 1024 == 0
|
2020-04-05 06:34:00 +00:00
|
|
|
|
|
|
|
|
2020-08-16 20:02:12 +00:00
|
|
|
# https://foss.heptapod.net/pypy/pypy/-/issues/2742
|
|
|
|
@pytest.mark.xfail("env.PYPY")
|
2020-04-05 06:34:00 +00:00
|
|
|
def test_final():
|
|
|
|
with pytest.raises(TypeError) as exc_info:
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-04-05 06:34:00 +00:00
|
|
|
class PyFinalChild(m.IsFinal):
|
|
|
|
pass
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-04-05 06:34:00 +00:00
|
|
|
assert str(exc_info.value).endswith("is not an acceptable base type")
|
|
|
|
|
|
|
|
|
2020-08-16 20:02:12 +00:00
|
|
|
# https://foss.heptapod.net/pypy/pypy/-/issues/2742
|
|
|
|
@pytest.mark.xfail("env.PYPY")
|
2020-04-05 06:34:00 +00:00
|
|
|
def test_non_final_final():
|
|
|
|
with pytest.raises(TypeError) as exc_info:
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-04-05 06:34:00 +00:00
|
|
|
class PyNonFinalFinalChild(m.IsNonFinalFinal):
|
|
|
|
pass
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-04-05 06:34:00 +00:00
|
|
|
assert str(exc_info.value).endswith("is not an acceptable base type")
|
2020-08-01 00:46:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# https://github.com/pybind/pybind11/issues/1878
|
|
|
|
def test_exception_rvalue_abort():
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
m.PyPrintDestructor().throw_something()
|
2020-10-02 17:06:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
# https://github.com/pybind/pybind11/issues/1568
|
2023-02-22 14:18:55 +00:00
|
|
|
def test_multiple_instances_with_same_pointer():
|
2020-10-02 17:06:04 +00:00
|
|
|
n = 100
|
|
|
|
instances = [m.SamePointer() for _ in range(n)]
|
|
|
|
for i in range(n):
|
|
|
|
# We need to reuse the same allocated memory for with a different type,
|
|
|
|
# to ensure the bug in `deregister_instance_impl` is detected. Otherwise
|
|
|
|
# `Py_TYPE(self) == Py_TYPE(it->second)` will still succeed, even though
|
|
|
|
# the `instance` is already deleted.
|
|
|
|
instances[i] = m.Empty()
|
|
|
|
# No assert: if this does not trigger the error
|
|
|
|
# pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
|
|
|
|
# and just completes without crashing, we're good.
|
2020-10-08 23:09:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# https://github.com/pybind/pybind11/issues/1624
|
|
|
|
def test_base_and_derived_nested_scope():
|
|
|
|
assert issubclass(m.DerivedWithNested, m.BaseWithNested)
|
|
|
|
assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
|
|
|
|
assert m.BaseWithNested.Nested.get_name() == "BaseWithNested::Nested"
|
|
|
|
assert m.DerivedWithNested.Nested.get_name() == "DerivedWithNested::Nested"
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_duplicate_class():
|
|
|
|
import types
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-10-08 23:09:56 +00:00
|
|
|
module_scope = types.ModuleType("module_scope")
|
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
m.register_duplicate_class_name(module_scope)
|
2020-10-16 20:38:13 +00:00
|
|
|
expected = (
|
|
|
|
'generic_type: cannot initialize type "Duplicate": '
|
|
|
|
"an object with that name is already defined"
|
|
|
|
)
|
2020-10-08 23:09:56 +00:00
|
|
|
assert str(exc_info.value) == expected
|
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
m.register_duplicate_class_type(module_scope)
|
|
|
|
expected = 'generic_type: type "YetAnotherDuplicate" is already registered!'
|
|
|
|
assert str(exc_info.value) == expected
|
|
|
|
|
|
|
|
class ClassScope:
|
|
|
|
pass
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2020-10-08 23:09:56 +00:00
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
m.register_duplicate_nested_class_name(ClassScope)
|
2020-10-16 20:38:13 +00:00
|
|
|
expected = (
|
|
|
|
'generic_type: cannot initialize type "DuplicateNested": '
|
|
|
|
"an object with that name is already defined"
|
|
|
|
)
|
2020-10-08 23:09:56 +00:00
|
|
|
assert str(exc_info.value) == expected
|
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
m.register_duplicate_nested_class_type(ClassScope)
|
|
|
|
expected = 'generic_type: type "YetAnotherDuplicateNested" is already registered!'
|
|
|
|
assert str(exc_info.value) == expected
|
Reproducer and fix for issue encountered in smart_holder update. (#4228)
* Reproducer for issue encountered in smart_holder update.
* clang-tidy compatibility (untested).
* Add `enable_if_t` to workaround.
* Bug fix: Move `PYBIND11_USING_WORKAROUND_FOR_CUDA_11_4_THROUGH_8` determination to detail/common.h
So that it actually is defined in pybind11.h
* Try using the workaround (which is nicer than the original code) universally.
* Reduce reproducer for CUDA 11.7 issue encountered in smart_holder update.
This commit tested in isolation on top of current master + first version of reproducer (62311eb431849d135a5db84f6c75ec390f2ede7c).
Succeeds with Debian Clang 14.0.6 C++17 (and probably all other compilers).
Fails for CUDA 11.7:
```
cd /build/tests && /usr/local/cuda/bin/nvcc -forward-unknown-to-host-compiler -Dpybind11_tests_EXPORTS -I/mounted_pybind11/include -isystem=/usr/include/python3.10 -g --generate-code=arch=compute_52,code=[compute_52,sm_52] -Xcompiler=-fPIC -Xcompiler=-fvisibility=hidden -Werror all-warnings -std=c++17 -MD -MT tests/CMakeFiles/pybind11_tests.dir/test_class.cpp.o -MF CMakeFiles/pybind11_tests.dir/test_class.cpp.o.d -x cu -c /mounted_pybind11/tests/test_class.cpp -o CMakeFiles/pybind11_tests.dir/test_class.cpp.o
/mounted_pybind11/tests/test_class.cpp(53): error: more than one instance of overloaded function "pybind11::class_<type_, options...>::def [with type_=test_class::pr4220_tripped_over_this::Empty0, options=<>]" matches the argument list:
function template "pybind11::class_<test_class::pr4220_tripped_over_this::Empty0> &pybind11::class_<type_, options...>::def(const char *, Func &&, const Extra &...) [with type_=test_class::pr4220_tripped_over_this::Empty0, options=<>]"
/mounted_pybind11/include/pybind11/pybind11.h(1557): here
function template "pybind11::class_<test_class::pr4220_tripped_over_this::Empty0> &pybind11::class_<type_, options...>::def(const T &, const Extra &...) [with type_=test_class::pr4220_tripped_over_this::Empty0, options=<>]"
/mounted_pybind11/include/pybind11/pybind11.h(1586): here
argument types are: (const char [8], <unknown-type>)
object type is: pybind11::class_<test_class::pr4220_tripped_over_this::Empty0>
1 error detected in the compilation of "/mounted_pybind11/tests/test_class.cpp".
```
2022-10-10 04:50:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pr4220_tripped_over_this():
|
|
|
|
assert (
|
|
|
|
m.Empty0().get_msg()
|
|
|
|
== "This is really only meant to exercise successful compilation."
|
|
|
|
)
|