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.

104 lines
3.9 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_keyword_args_and_generalized_unpacking():
  22. from pybind11_tests import (test_tuple_unpacking, test_dict_unpacking, test_keyword_args,
  23. test_unpacking_and_keywords1, test_unpacking_and_keywords2,
  24. test_unpacking_error1, test_unpacking_error2,
  25. test_arg_conversion_error1, test_arg_conversion_error2)
  26. def f(*args, **kwargs):
  27. return args, kwargs
  28. assert test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
  29. assert test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
  30. assert test_keyword_args(f) == ((), {"x": 10, "y": 20})
  31. assert test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
  32. assert test_unpacking_and_keywords2(f) == (
  33. ("positional", 1, 2, 3, 4, 5),
  34. {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
  35. )
  36. with pytest.raises(TypeError) as excinfo:
  37. test_unpacking_error1(f)
  38. assert "Got multiple values for keyword argument" in str(excinfo.value)
  39. with pytest.raises(TypeError) as excinfo:
  40. test_unpacking_error2(f)
  41. assert "Got multiple values for keyword argument" in str(excinfo.value)
  42. with pytest.raises(RuntimeError) as excinfo:
  43. test_arg_conversion_error1(f)
  44. assert "Unable to convert call argument" in str(excinfo.value)
  45. with pytest.raises(RuntimeError) as excinfo:
  46. test_arg_conversion_error2(f)
  47. assert "Unable to convert call argument" in str(excinfo.value)
  48. def test_lambda_closure_cleanup():
  49. from pybind11_tests import test_cleanup, payload_cstats
  50. test_cleanup()
  51. cstats = payload_cstats()
  52. assert cstats.alive() == 0
  53. assert cstats.copy_constructions == 1
  54. assert cstats.move_constructions >= 1
  55. def test_cpp_function_roundtrip():
  56. """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
  57. from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
  58. assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
  59. assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
  60. assert roundtrip(None, expect_none=True) is None
  61. assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
  62. with pytest.raises(TypeError) as excinfo:
  63. test_dummy_function(dummy_function2)
  64. assert "incompatible function arguments" in str(excinfo.value)
  65. with pytest.raises(TypeError) as excinfo:
  66. test_dummy_function(lambda x, y: x + y)
  67. assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
  68. "takes exactly 2 arguments"))
  69. def test_function_signatures(doc):
  70. from pybind11_tests import test_callback3, test_callback4
  71. assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
  72. assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"
  73. def test_movable_object():
  74. from pybind11_tests import callback_with_movable
  75. assert callback_with_movable(lambda _: None) is True