| 239 | |
| 240 | |
| 241 | def test_cross_module_calls(): |
| 242 | import pybind11_cross_module_tests as cm |
| 243 | |
| 244 | v1 = m.LocalVec() |
| 245 | v1.append(m.LocalType(1)) |
| 246 | v2 = cm.LocalVec() |
| 247 | v2.append(cm.LocalType(2)) |
| 248 | |
| 249 | # Returning the self pointer should get picked up as returning an existing |
| 250 | # instance (even when that instance is of a foreign, non-local type). |
| 251 | assert m.return_self(v1) is v1 |
| 252 | assert cm.return_self(v2) is v2 |
| 253 | assert m.return_self(v2) is v2 |
| 254 | assert cm.return_self(v1) is v1 |
| 255 | |
| 256 | assert m.LocalVec is not cm.LocalVec |
| 257 | # Returning a copy, on the other hand, always goes to the local type, |
| 258 | # regardless of where the source type came from. |
| 259 | assert type(m.return_copy(v1)) is m.LocalVec |
| 260 | assert type(m.return_copy(v2)) is m.LocalVec |
| 261 | assert type(cm.return_copy(v1)) is cm.LocalVec |
| 262 | assert type(cm.return_copy(v2)) is cm.LocalVec |
| 263 | |
| 264 | # Test the example given in the documentation (which also tests inheritance casting): |
| 265 | mycat = m.Cat("Fluffy") |
| 266 | mydog = cm.Dog("Rover") |
| 267 | assert mycat.get_name() == "Fluffy" |
| 268 | assert mydog.name() == "Rover" |
| 269 | assert m.Cat.__base__.__name__ == "Pet" |
| 270 | assert cm.Dog.__base__.__name__ == "Pet" |
| 271 | assert m.Cat.__base__ is not cm.Dog.__base__ |
| 272 | assert m.pet_name(mycat) == "Fluffy" |
| 273 | assert m.pet_name(mydog) == "Rover" |
| 274 | assert cm.pet_name(mycat) == "Fluffy" |
| 275 | assert cm.pet_name(mydog) == "Rover" |
| 276 | |
| 277 | assert m.MixGL is not cm.MixGL |
| 278 | a = m.MixGL(1) |
| 279 | b = cm.MixGL(2) |
| 280 | assert m.get_gl_value(a) == 11 |
| 281 | assert m.get_gl_value(b) == 12 |
| 282 | assert cm.get_gl_value(a) == 101 |
| 283 | assert cm.get_gl_value(b) == 102 |
| 284 | |
| 285 | c, d = m.MixGL2(3), cm.MixGL2(4) |
| 286 | with pytest.raises(TypeError) as excinfo: |
| 287 | m.get_gl_value(c) |
| 288 | assert "incompatible function arguments" in str(excinfo.value) |
| 289 | with pytest.raises(TypeError) as excinfo: |
| 290 | m.get_gl_value(d) |
| 291 | assert "incompatible function arguments" in str(excinfo.value) |