You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.3 KiB

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