2016-08-12 11:50:00 +00:00
|
|
|
import pytest
|
|
|
|
import gc
|
2016-11-03 10:53:35 +00:00
|
|
|
from pybind11_tests import ConstructorStats
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_regressions():
|
2016-08-12 11:50:00 +00:00
|
|
|
from pybind11_tests.issues import print_cchar, print_char
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
# #137: const char* isn't handled properly
|
|
|
|
assert print_cchar("const char *") == "const char *"
|
|
|
|
# #150: char bindings broken
|
|
|
|
assert print_char("c") == "c"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_dispatch_issue(msg):
|
2016-08-12 11:50:00 +00:00
|
|
|
"""#159: virtual function dispatch has problems with similar-named functions"""
|
|
|
|
from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
|
|
|
|
|
|
|
|
class PyClass1(DispatchIssue):
|
|
|
|
def dispatch(self):
|
2016-08-12 20:28:31 +00:00
|
|
|
return "Yay.."
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
class PyClass2(DispatchIssue):
|
|
|
|
def dispatch(self):
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
super(PyClass2, self).dispatch()
|
|
|
|
assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
|
|
|
|
|
|
|
|
p = PyClass1()
|
2016-08-12 20:28:31 +00:00
|
|
|
return dispatch_issue_go(p)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
b = PyClass2()
|
2016-08-12 20:28:31 +00:00
|
|
|
assert dispatch_issue_go(b) == "Yay.."
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_reference_wrapper():
|
|
|
|
"""#171: Can't return reference wrappers (or STL data structures containing them)"""
|
|
|
|
from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
|
|
|
|
|
|
|
|
assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
|
|
|
|
"[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
|
|
|
|
|
|
|
|
|
|
|
|
def test_iterator_passthrough():
|
|
|
|
"""#181: iterator passthrough did not compile"""
|
|
|
|
from pybind11_tests.issues import iterator_passthrough
|
|
|
|
|
|
|
|
assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
|
|
|
|
|
|
|
|
|
|
|
|
def test_shared_ptr_gc():
|
|
|
|
"""// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
|
|
|
|
from pybind11_tests.issues import ElementList, ElementA
|
|
|
|
|
|
|
|
el = ElementList()
|
|
|
|
for i in range(10):
|
|
|
|
el.add(ElementA(i))
|
|
|
|
gc.collect()
|
|
|
|
for i, v in enumerate(el.get()):
|
|
|
|
assert i == v.value()
|
|
|
|
|
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
def test_no_id(msg):
|
2016-08-19 11:45:36 +00:00
|
|
|
from pybind11_tests.issues import get_element, expect_float, expect_int
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2016-08-19 11:45:36 +00:00
|
|
|
get_element(None)
|
2016-08-12 11:50:00 +00:00
|
|
|
assert msg(excinfo.value) == """
|
2016-09-12 02:44:37 +00:00
|
|
|
get_element(): incompatible function arguments. The following argument types are supported:
|
2016-08-19 11:45:36 +00:00
|
|
|
1. (arg0: m.issues.ElementA) -> int
|
2016-09-12 02:44:37 +00:00
|
|
|
|
|
|
|
Invoked with: None
|
2016-08-12 11:50:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
expect_int(5.2)
|
|
|
|
assert msg(excinfo.value) == """
|
2016-09-12 02:44:37 +00:00
|
|
|
expect_int(): incompatible function arguments. The following argument types are supported:
|
2016-08-12 11:50:00 +00:00
|
|
|
1. (arg0: int) -> int
|
2016-09-12 02:44:37 +00:00
|
|
|
|
|
|
|
Invoked with: 5.2
|
2016-08-12 11:50:00 +00:00
|
|
|
"""
|
|
|
|
assert expect_float(12) == 12
|
|
|
|
|
|
|
|
|
2016-08-19 11:45:36 +00:00
|
|
|
def test_str_issue(msg):
|
2016-08-12 11:50:00 +00:00
|
|
|
"""Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
|
|
|
|
from pybind11_tests.issues import StrIssue
|
|
|
|
|
2016-08-19 11:45:36 +00:00
|
|
|
assert str(StrIssue(3)) == "StrIssue[3]"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
str(StrIssue("no", "such", "constructor"))
|
|
|
|
assert msg(excinfo.value) == """
|
2016-09-12 02:44:37 +00:00
|
|
|
__init__(): incompatible constructor arguments. The following argument types are supported:
|
2016-08-12 11:50:00 +00:00
|
|
|
1. m.issues.StrIssue(arg0: int)
|
|
|
|
2. m.issues.StrIssue()
|
2016-09-12 02:44:37 +00:00
|
|
|
|
2016-11-22 10:28:40 +00:00
|
|
|
Invoked with: 'no', 'such', 'constructor'
|
2016-08-12 11:50:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_nested():
|
2016-08-12 11:50:00 +00:00
|
|
|
""" #328: first member in a class can't be used in operators"""
|
2016-08-12 20:28:31 +00:00
|
|
|
from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
a = NestA()
|
|
|
|
b = NestB()
|
|
|
|
c = NestC()
|
|
|
|
|
|
|
|
a += 10
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestA(a) == 13
|
2016-08-12 11:50:00 +00:00
|
|
|
b.a += 100
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestA(b.a) == 103
|
2016-08-12 11:50:00 +00:00
|
|
|
c.b.a += 1000
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestA(c.b.a) == 1003
|
2016-08-12 11:50:00 +00:00
|
|
|
b -= 1
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestB(b) == 3
|
2016-08-12 11:50:00 +00:00
|
|
|
c.b -= 3
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestB(c.b) == 1
|
2016-08-12 11:50:00 +00:00
|
|
|
c *= 7
|
2016-08-12 20:28:31 +00:00
|
|
|
assert get_NestC(c) == 35
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
abase = a.as_base()
|
|
|
|
assert abase.value == -2
|
|
|
|
a.as_base().value += 44
|
|
|
|
assert abase.value == 42
|
|
|
|
assert c.b.a.as_base().value == -2
|
|
|
|
c.b.a.as_base().value += 44
|
|
|
|
assert c.b.a.as_base().value == 42
|
|
|
|
|
|
|
|
del c
|
|
|
|
gc.collect()
|
|
|
|
del a # Should't delete while abase is still alive
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
assert abase.value == 42
|
|
|
|
del abase, b
|
|
|
|
gc.collect()
|
2016-09-06 15:47:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_move_fallback():
|
|
|
|
from pybind11_tests.issues import get_moveissue1, get_moveissue2
|
|
|
|
m2 = get_moveissue2(2)
|
|
|
|
assert m2.value == 2
|
|
|
|
m1 = get_moveissue1(1)
|
|
|
|
assert m1.value == 1
|
2016-09-07 17:32:49 +00:00
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
|
2016-09-07 17:32:49 +00:00
|
|
|
def test_override_ref():
|
|
|
|
from pybind11_tests.issues import OverrideTest
|
2016-09-08 15:03:08 +00:00
|
|
|
o = OverrideTest("asdf")
|
2016-09-07 17:32:49 +00:00
|
|
|
|
2016-09-07 17:38:32 +00:00
|
|
|
# Not allowed (see associated .cpp comment)
|
2016-11-20 20:21:54 +00:00
|
|
|
# i = o.str_ref()
|
|
|
|
# assert o.str_ref() == "asdf"
|
2016-09-08 15:03:08 +00:00
|
|
|
assert o.str_value() == "asdf"
|
2016-09-07 17:32:49 +00:00
|
|
|
|
2016-09-08 15:03:08 +00:00
|
|
|
assert o.A_value().value == "hi"
|
2016-09-07 17:32:49 +00:00
|
|
|
a = o.A_ref()
|
2016-09-08 15:03:08 +00:00
|
|
|
assert a.value == "hi"
|
|
|
|
a.value = "bye"
|
|
|
|
assert a.value == "bye"
|
2016-09-10 06:28:37 +00:00
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
|
2016-09-10 06:28:37 +00:00
|
|
|
def test_operators_notimplemented(capture):
|
|
|
|
from pybind11_tests.issues import OpTest1, OpTest2
|
|
|
|
with capture:
|
2016-11-20 20:21:54 +00:00
|
|
|
c1, c2 = OpTest1(), OpTest2()
|
|
|
|
c1 + c1
|
|
|
|
c2 + c2
|
|
|
|
c2 + c1
|
|
|
|
c1 + c2
|
|
|
|
assert capture == """
|
|
|
|
Add OpTest1 with OpTest1
|
|
|
|
Add OpTest2 with OpTest2
|
|
|
|
Add OpTest2 with OpTest1
|
|
|
|
Add OpTest2 with OpTest1
|
|
|
|
"""
|
2016-09-10 07:00:50 +00:00
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
|
2016-09-10 07:00:50 +00:00
|
|
|
def test_iterator_rvpolicy():
|
|
|
|
""" Issue 388: Can't make iterators via make_iterator() with different r/v policies """
|
|
|
|
from pybind11_tests.issues import make_iterator_1
|
|
|
|
from pybind11_tests.issues import make_iterator_2
|
|
|
|
|
|
|
|
assert list(make_iterator_1()) == [1, 2, 3]
|
|
|
|
assert list(make_iterator_2()) == [1, 2, 3]
|
2016-11-20 20:21:54 +00:00
|
|
|
assert not isinstance(make_iterator_1(), type(make_iterator_2()))
|
2016-10-25 01:58:22 +00:00
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
|
2016-10-25 01:58:22 +00:00
|
|
|
def test_dupe_assignment():
|
|
|
|
""" Issue 461: overwriting a class with a function """
|
|
|
|
from pybind11_tests.issues import dupe_exception_failures
|
|
|
|
assert dupe_exception_failures() == []
|
2016-11-03 10:53:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_enable_shared_from_this_with_reference_rvp():
|
|
|
|
""" Issue #471: shared pointer instance not dellocated """
|
|
|
|
from pybind11_tests import SharedParent, SharedChild
|
|
|
|
|
|
|
|
parent = SharedParent()
|
|
|
|
child = parent.get_child()
|
|
|
|
|
|
|
|
cstats = ConstructorStats.get(SharedChild)
|
|
|
|
assert cstats.alive() == 1
|
|
|
|
del child, parent
|
|
|
|
assert cstats.alive() == 0
|
Don't construct unique_ptr around unowned pointers (#478)
If we need to initialize a holder around an unowned instance, and the
holder type is non-copyable (i.e. a unique_ptr), we currently construct
the holder type around the value pointer, but then never actually
destruct the holder: the holder destructor is called only for the
instance that actually has `inst->owned = true` set.
This seems no pointer, however, in creating such a holder around an
unowned instance: we never actually intend to use anything that the
unique_ptr gives us: and, in fact, do not want the unique_ptr (because
if it ever actually got destroyed, it would cause destruction of the
wrapped pointer, despite the fact that that wrapped pointer isn't
owned).
This commit changes the logic to only create a unique_ptr holder if we
actually own the instance, and to destruct via the constructed holder
whenever we have a constructed holder--which will now only be the case
for owned-unique-holder or shared-holder types.
Other changes include:
* Added test for non-movable holder constructor/destructor counts
The three alive assertions now pass, before #478 they fail with counts
of 2/2/1 respectively, because of the unique_ptr that we don't want and
don't destroy (because we don't *want* its destructor to run).
* Return cstats reference; fix ConstructStats doc
Small cleanup to the #478 test code, and fix to the ConstructStats
documentation (the static method definition should use `reference` not
`reference_internal`).
* Rename inst->constructed to inst->holder_constructed
This makes it clearer exactly what it's referring to.
2016-11-06 18:12:48 +00:00
|
|
|
|
2016-11-07 14:59:01 +00:00
|
|
|
|
Don't construct unique_ptr around unowned pointers (#478)
If we need to initialize a holder around an unowned instance, and the
holder type is non-copyable (i.e. a unique_ptr), we currently construct
the holder type around the value pointer, but then never actually
destruct the holder: the holder destructor is called only for the
instance that actually has `inst->owned = true` set.
This seems no pointer, however, in creating such a holder around an
unowned instance: we never actually intend to use anything that the
unique_ptr gives us: and, in fact, do not want the unique_ptr (because
if it ever actually got destroyed, it would cause destruction of the
wrapped pointer, despite the fact that that wrapped pointer isn't
owned).
This commit changes the logic to only create a unique_ptr holder if we
actually own the instance, and to destruct via the constructed holder
whenever we have a constructed holder--which will now only be the case
for owned-unique-holder or shared-holder types.
Other changes include:
* Added test for non-movable holder constructor/destructor counts
The three alive assertions now pass, before #478 they fail with counts
of 2/2/1 respectively, because of the unique_ptr that we don't want and
don't destroy (because we don't *want* its destructor to run).
* Return cstats reference; fix ConstructStats doc
Small cleanup to the #478 test code, and fix to the ConstructStats
documentation (the static method definition should use `reference` not
`reference_internal`).
* Rename inst->constructed to inst->holder_constructed
This makes it clearer exactly what it's referring to.
2016-11-06 18:12:48 +00:00
|
|
|
def test_non_destructed_holders():
|
|
|
|
""" Issue #478: unique ptrs constructed and freed without destruction """
|
|
|
|
from pybind11_tests import SpecialHolderObj
|
|
|
|
|
|
|
|
a = SpecialHolderObj(123)
|
|
|
|
b = a.child()
|
|
|
|
|
|
|
|
assert a.val == 123
|
|
|
|
assert b.val == 124
|
|
|
|
|
|
|
|
cstats = SpecialHolderObj.holder_cstats()
|
|
|
|
assert cstats.alive() == 1
|
|
|
|
del b
|
|
|
|
assert cstats.alive() == 1
|
|
|
|
del a
|
|
|
|
assert cstats.alive() == 0
|
2016-11-07 14:59:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_complex_cast(capture):
|
|
|
|
""" Issue #484: number conversion generates unhandled exceptions """
|
|
|
|
from pybind11_tests.issues import test_complex
|
|
|
|
|
|
|
|
with capture:
|
|
|
|
test_complex(1)
|
|
|
|
test_complex(2j)
|
|
|
|
|
|
|
|
assert capture == """
|
2016-11-20 20:21:54 +00:00
|
|
|
1.0
|
|
|
|
(0.0, 2.0)
|
|
|
|
"""
|
2016-11-20 04:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_inheritance_override_def_static():
|
|
|
|
from pybind11_tests.issues import MyBase, MyDerived
|
|
|
|
|
|
|
|
b = MyBase.make()
|
|
|
|
d1 = MyDerived.make2()
|
|
|
|
d2 = MyDerived.make()
|
|
|
|
|
|
|
|
assert isinstance(b, MyBase)
|
|
|
|
assert isinstance(d1, MyDerived)
|
|
|
|
assert isinstance(d2, MyDerived)
|