|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +import re |
2 | 3 | import pytest |
| 4 | +import env # noqa: F401 |
3 | 5 |
|
4 | 6 | from pybind11_tests import class_sh_with_alias as m |
5 | 7 |
|
6 | 8 |
|
| 9 | +def check_regex(expected, actual): |
| 10 | + result = re.match(expected + "$", actual) |
| 11 | + if result is None: |
| 12 | + pytest.fail("expected: '{}' != actual: '{}'".format(expected, actual)) |
| 13 | + |
| 14 | + |
7 | 15 | class PyDrvd0(m.Abase0): |
8 | 16 | def __init__(self, val): |
9 | 17 | super(PyDrvd0, self).__init__(val) |
@@ -56,3 +64,89 @@ def test_drvd1_add_in_cpp_unique_ptr(): |
56 | 64 | drvd = PyDrvd1(25) |
57 | 65 | assert m.AddInCppUniquePtr(drvd, 83) == ((25 * 10 + 3) * 200 + 83) * 100 + 13 |
58 | 66 | return # Comment out for manual leak checking (use `top` command). |
| 67 | + |
| 68 | + |
| 69 | +class PyConsumer1(m.ConsumerBase): |
| 70 | + def __init__(self): |
| 71 | + m.ConsumerBase.__init__(self) |
| 72 | + |
| 73 | + def pass_uq_cref(self, obj): |
| 74 | + obj.mtxt = obj.mtxt + "pass_uq_cref" |
| 75 | + |
| 76 | + def pass_valu(self, obj): |
| 77 | + obj.mtxt = obj.mtxt + "pass_valu" |
| 78 | + |
| 79 | + def pass_lref(self, obj): |
| 80 | + obj.mtxt = obj.mtxt + "pass_lref" |
| 81 | + |
| 82 | + def pass_cref(self, obj): |
| 83 | + obj.mtxt = obj.mtxt + "pass_cref" |
| 84 | + |
| 85 | + |
| 86 | +class PyConsumer2(m.ConsumerBase): |
| 87 | + """This one, additionally to PyConsumer1 calls the base methods. |
| 88 | + This results in a second call to the trampoline override dispatcher. |
| 89 | + Hence arguments have travelled a long way back and forth between C++ |
| 90 | + and Python: C++ -> Python (call #1) -> C++ (call #2).""" |
| 91 | + |
| 92 | + def __init__(self): |
| 93 | + m.ConsumerBase.__init__(self) |
| 94 | + |
| 95 | + def pass_uq_cref(self, obj): |
| 96 | + obj.mtxt = obj.mtxt + "pass_uq_cref" |
| 97 | + m.ConsumerBase.pass_uq_cref(self, obj) |
| 98 | + |
| 99 | + def pass_valu(self, obj): |
| 100 | + obj.mtxt = obj.mtxt + "pass_valu" |
| 101 | + m.ConsumerBase.pass_valu(self, obj) |
| 102 | + |
| 103 | + def pass_lref(self, obj): |
| 104 | + obj.mtxt = obj.mtxt + "pass_lref" |
| 105 | + m.ConsumerBase.pass_lref(self, obj) |
| 106 | + |
| 107 | + def pass_cref(self, obj): |
| 108 | + obj.mtxt = obj.mtxt + "pass_cref" |
| 109 | + m.ConsumerBase.pass_cref(self, obj) |
| 110 | + |
| 111 | + |
| 112 | +# roundtrip tests, creating an object in C++ that is passed by reference |
| 113 | +# to a virtual method of a class derived in Python. Thus: |
| 114 | +# C++ -> Python -> C++ |
| 115 | +@pytest.mark.parametrize( |
| 116 | + "f, expected", |
| 117 | + [ |
| 118 | + (m.check_roundtrip_uq_cref, "([0-9]+)_pass_uq_cref"), |
| 119 | + (m.check_roundtrip_valu, "([0-9]+)_"), # modification not passed back to C++ |
| 120 | + (m.check_roundtrip_lref, "([0-9]+)_pass_lref"), |
| 121 | + pytest.param( |
| 122 | + m.check_roundtrip_cref, |
| 123 | + "([0-9]+)_pass_cref", |
| 124 | + marks=pytest.mark.skipif("env.PYPY"), |
| 125 | + ), |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_unique_ptr_consumer1_roundtrip(f, expected): |
| 129 | + c = PyConsumer1() |
| 130 | + check_regex(expected, f(c)) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize( |
| 134 | + "f, expected", |
| 135 | + [ |
| 136 | + pytest.param( # cannot (yet) load unowned const unique_ptr& (for 2nd call) |
| 137 | + m.check_roundtrip_uq_cref, |
| 138 | + "([0-9]+)_pass_uq_cref_\\1", |
| 139 | + marks=pytest.mark.xfail, |
| 140 | + ), |
| 141 | + (m.check_roundtrip_valu, "([0-9]+)_"), # modification not passed back to C++ |
| 142 | + (m.check_roundtrip_lref, "([0-9]+)_pass_lref_\\1"), |
| 143 | + pytest.param( # PYPY always copies the argument instead of passing the reference |
| 144 | + m.check_roundtrip_cref, |
| 145 | + "([0-9]+)_pass_cref_\\1", |
| 146 | + marks=pytest.mark.skipif("env.PYPY"), |
| 147 | + ), |
| 148 | + ], |
| 149 | +) |
| 150 | +def test_unique_ptr_consumer2_roundtrip(f, expected): |
| 151 | + c = PyConsumer2() |
| 152 | + check_regex(expected, f(c)) |
0 commit comments