blob: f76813964aa549aed30944a3904090d556cd2979 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2import gc
3
4
Dean Moldovan665e8802016-08-12 22:28:31 +02005def test_regressions():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006 from pybind11_tests.issues import print_cchar, print_char
7
Dean Moldovan665e8802016-08-12 22:28:31 +02008 # #137: const char* isn't handled properly
9 assert print_cchar("const char *") == "const char *"
10 # #150: char bindings broken
11 assert print_char("c") == "c"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020012
13
Dean Moldovan665e8802016-08-12 22:28:31 +020014def test_dispatch_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020015 """#159: virtual function dispatch has problems with similar-named functions"""
16 from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
17
18 class PyClass1(DispatchIssue):
19 def dispatch(self):
Dean Moldovan665e8802016-08-12 22:28:31 +020020 return "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020021
22 class PyClass2(DispatchIssue):
23 def dispatch(self):
24 with pytest.raises(RuntimeError) as excinfo:
25 super(PyClass2, self).dispatch()
26 assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
27
28 p = PyClass1()
Dean Moldovan665e8802016-08-12 22:28:31 +020029 return dispatch_issue_go(p)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020030
31 b = PyClass2()
Dean Moldovan665e8802016-08-12 22:28:31 +020032 assert dispatch_issue_go(b) == "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020033
34
35def test_reference_wrapper():
36 """#171: Can't return reference wrappers (or STL data structures containing them)"""
37 from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
38
39 assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
40 "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
41
42
43def test_iterator_passthrough():
44 """#181: iterator passthrough did not compile"""
45 from pybind11_tests.issues import iterator_passthrough
46
47 assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
48
49
50def test_shared_ptr_gc():
51 """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
52 from pybind11_tests.issues import ElementList, ElementA
53
54 el = ElementList()
55 for i in range(10):
56 el.add(ElementA(i))
57 gc.collect()
58 for i, v in enumerate(el.get()):
59 assert i == v.value()
60
61
62def test_no_id(capture, msg):
63 from pybind11_tests.issues import print_element, expect_float, expect_int
64
65 with pytest.raises(TypeError) as excinfo:
66 print_element(None)
67 assert msg(excinfo.value) == """
68 Incompatible function arguments. The following argument types are supported:
69 1. (arg0: m.issues.ElementA) -> None
70 Invoked with: None
71 """
72
73 with pytest.raises(TypeError) as excinfo:
74 expect_int(5.2)
75 assert msg(excinfo.value) == """
76 Incompatible function arguments. The following argument types are supported:
77 1. (arg0: int) -> int
78 Invoked with: 5.2
79 """
80 assert expect_float(12) == 12
81
82 from pybind11_tests.issues import A, call_f
83
84 class B(A):
85 def __init__(self):
86 super(B, self).__init__()
87
88 def f(self):
89 print("In python f()")
90
91 # C++ version
92 with capture:
93 a = A()
94 call_f(a)
95 assert capture == "A.f()"
96
97 # Python version
98 with capture:
99 b = B()
100 call_f(b)
101 assert capture == """
102 PyA.PyA()
103 PyA.f()
104 In python f()
105 """
106
107
108def test_str_issue(capture, msg):
109 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
110 from pybind11_tests.issues import StrIssue
111
112 with capture:
113 assert str(StrIssue(3)) == "StrIssue[3]"
114 assert capture == "StrIssue.__str__ called"
115
116 with pytest.raises(TypeError) as excinfo:
117 str(StrIssue("no", "such", "constructor"))
118 assert msg(excinfo.value) == """
119 Incompatible constructor arguments. The following argument types are supported:
120 1. m.issues.StrIssue(arg0: int)
121 2. m.issues.StrIssue()
122 Invoked with: no, such, constructor
123 """
124
125
Dean Moldovan665e8802016-08-12 22:28:31 +0200126def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200127 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200128 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200129
130 a = NestA()
131 b = NestB()
132 c = NestC()
133
134 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200135 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200136 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200137 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200138 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200139 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200140 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200141 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200142 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200143 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200144 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200145 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200146
147 abase = a.as_base()
148 assert abase.value == -2
149 a.as_base().value += 44
150 assert abase.value == 42
151 assert c.b.a.as_base().value == -2
152 c.b.a.as_base().value += 44
153 assert c.b.a.as_base().value == 42
154
155 del c
156 gc.collect()
157 del a # Should't delete while abase is still alive
158 gc.collect()
159
160 assert abase.value == 42
161 del abase, b
162 gc.collect()