Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | import gc |
| 3 | |
| 4 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 5 | def test_regressions(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 6 | from pybind11_tests.issues import print_cchar, print_char |
| 7 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 8 | # #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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 12 | |
| 13 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 14 | def test_dispatch_issue(msg): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 15 | """#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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 20 | return "Yay.." |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 21 | |
| 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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 29 | return dispatch_issue_go(p) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 30 | |
| 31 | b = PyClass2() |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 32 | assert dispatch_issue_go(b) == "Yay.." |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def 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 | |
| 43 | def 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 | |
| 50 | def 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 | |
| 62 | def test_no_id(capture, msg): |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame^] | 63 | from pybind11_tests.issues import get_element, expect_float, expect_int |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 64 | |
| 65 | with pytest.raises(TypeError) as excinfo: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame^] | 66 | get_element(None) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 67 | assert msg(excinfo.value) == """ |
| 68 | Incompatible function arguments. The following argument types are supported: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame^] | 69 | 1. (arg0: m.issues.ElementA) -> int |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 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 | |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame^] | 108 | def test_str_issue(msg): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 109 | """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid""" |
| 110 | from pybind11_tests.issues import StrIssue |
| 111 | |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame^] | 112 | assert str(StrIssue(3)) == "StrIssue[3]" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 113 | |
| 114 | with pytest.raises(TypeError) as excinfo: |
| 115 | str(StrIssue("no", "such", "constructor")) |
| 116 | assert msg(excinfo.value) == """ |
| 117 | Incompatible constructor arguments. The following argument types are supported: |
| 118 | 1. m.issues.StrIssue(arg0: int) |
| 119 | 2. m.issues.StrIssue() |
| 120 | Invoked with: no, such, constructor |
| 121 | """ |
| 122 | |
| 123 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 124 | def test_nested(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 125 | """ #328: first member in a class can't be used in operators""" |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 126 | from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 127 | |
| 128 | a = NestA() |
| 129 | b = NestB() |
| 130 | c = NestC() |
| 131 | |
| 132 | a += 10 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 133 | assert get_NestA(a) == 13 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 134 | b.a += 100 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 135 | assert get_NestA(b.a) == 103 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 136 | c.b.a += 1000 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 137 | assert get_NestA(c.b.a) == 1003 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 138 | b -= 1 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 139 | assert get_NestB(b) == 3 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 140 | c.b -= 3 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 141 | assert get_NestB(c.b) == 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 142 | c *= 7 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 143 | assert get_NestC(c) == 35 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 144 | |
| 145 | abase = a.as_base() |
| 146 | assert abase.value == -2 |
| 147 | a.as_base().value += 44 |
| 148 | assert abase.value == 42 |
| 149 | assert c.b.a.as_base().value == -2 |
| 150 | c.b.a.as_base().value += 44 |
| 151 | assert c.b.a.as_base().value == 42 |
| 152 | |
| 153 | del c |
| 154 | gc.collect() |
| 155 | del a # Should't delete while abase is still alive |
| 156 | gc.collect() |
| 157 | |
| 158 | assert abase.value == 42 |
| 159 | del abase, b |
| 160 | gc.collect() |