test_callbacks.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import pytest
  2. from pybind11_tests import callbacks as m
  3. def test_callbacks():
  4. from functools import partial
  5. def func1():
  6. return "func1"
  7. def func2(a, b, c, d):
  8. return "func2", a, b, c, d
  9. def func3(a):
  10. return "func3({})".format(a)
  11. assert m.test_callback1(func1) == "func1"
  12. assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5)
  13. assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
  14. assert m.test_callback1(partial(func3, "partial")) == "func3(partial)"
  15. assert m.test_callback3(lambda i: i + 1) == "func(43) = 44"
  16. f = m.test_callback4()
  17. assert f(43) == 44
  18. f = m.test_callback5()
  19. assert f(number=43) == 44
  20. def test_bound_method_callback():
  21. # Bound Python method:
  22. class MyClass:
  23. def double(self, val):
  24. return 2 * val
  25. z = MyClass()
  26. assert m.test_callback3(z.double) == "func(43) = 86"
  27. z = m.CppBoundMethodTest()
  28. assert m.test_callback3(z.triple) == "func(43) = 129"
  29. def test_keyword_args_and_generalized_unpacking():
  30. def f(*args, **kwargs):
  31. return args, kwargs
  32. assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
  33. assert m.test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
  34. assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20})
  35. assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
  36. assert m.test_unpacking_and_keywords2(f) == (
  37. ("positional", 1, 2, 3, 4, 5),
  38. {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
  39. )
  40. with pytest.raises(TypeError) as excinfo:
  41. m.test_unpacking_error1(f)
  42. assert "Got multiple values for keyword argument" in str(excinfo.value)
  43. with pytest.raises(TypeError) as excinfo:
  44. m.test_unpacking_error2(f)
  45. assert "Got multiple values for keyword argument" in str(excinfo.value)
  46. with pytest.raises(RuntimeError) as excinfo:
  47. m.test_arg_conversion_error1(f)
  48. assert "Unable to convert call argument" in str(excinfo.value)
  49. with pytest.raises(RuntimeError) as excinfo:
  50. m.test_arg_conversion_error2(f)
  51. assert "Unable to convert call argument" in str(excinfo.value)
  52. def test_lambda_closure_cleanup():
  53. m.test_cleanup()
  54. cstats = m.payload_cstats()
  55. assert cstats.alive() == 0
  56. assert cstats.copy_constructions == 1
  57. assert cstats.move_constructions >= 1
  58. def test_cpp_function_roundtrip():
  59. """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
  60. assert m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2"
  61. assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
  62. "matches dummy_function: eval(1) = 2")
  63. assert m.roundtrip(None, expect_none=True) is None
  64. assert (m.test_dummy_function(lambda x: x + 2) ==
  65. "can't convert to function pointer: eval(1) = 3")
  66. with pytest.raises(TypeError) as excinfo:
  67. m.test_dummy_function(m.dummy_function2)
  68. assert "incompatible function arguments" in str(excinfo.value)
  69. with pytest.raises(TypeError) as excinfo:
  70. m.test_dummy_function(lambda x, y: x + y)
  71. assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
  72. "takes exactly 2 arguments"))
  73. def test_function_signatures(doc):
  74. assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
  75. assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]"
  76. def test_movable_object():
  77. assert m.callback_with_movable(lambda _: None) is True