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.

74 lines
2.5 KiB

  1. import pytest
  2. def test_error_already_set(msg):
  3. from pybind11_tests import throw_already_set
  4. with pytest.raises(RuntimeError) as excinfo:
  5. throw_already_set(False)
  6. assert msg(excinfo.value) == "Unknown internal error occurred"
  7. with pytest.raises(ValueError) as excinfo:
  8. throw_already_set(True)
  9. assert msg(excinfo.value) == "foo"
  10. def test_python_call_in_catch():
  11. from pybind11_tests import python_call_in_destructor
  12. d = {}
  13. assert python_call_in_destructor(d) is True
  14. assert d["good"] is True
  15. def test_custom(msg):
  16. from pybind11_tests import (MyException, MyException5, MyException5_1,
  17. throws1, throws2, throws3, throws4, throws5, throws5_1,
  18. throws_logic_error)
  19. # Can we catch a MyException?"
  20. with pytest.raises(MyException) as excinfo:
  21. throws1()
  22. assert msg(excinfo.value) == "this error should go to a custom type"
  23. # Can we translate to standard Python exceptions?
  24. with pytest.raises(RuntimeError) as excinfo:
  25. throws2()
  26. assert msg(excinfo.value) == "this error should go to a standard Python exception"
  27. # Can we handle unknown exceptions?
  28. with pytest.raises(RuntimeError) as excinfo:
  29. throws3()
  30. assert msg(excinfo.value) == "Caught an unknown exception!"
  31. # Can we delegate to another handler by rethrowing?
  32. with pytest.raises(MyException) as excinfo:
  33. throws4()
  34. assert msg(excinfo.value) == "this error is rethrown"
  35. # "Can we fall-through to the default handler?"
  36. with pytest.raises(RuntimeError) as excinfo:
  37. throws_logic_error()
  38. assert msg(excinfo.value) == "this error should fall through to the standard handler"
  39. # Can we handle a helper-declared exception?
  40. with pytest.raises(MyException5) as excinfo:
  41. throws5()
  42. assert msg(excinfo.value) == "this is a helper-defined translated exception"
  43. # Exception subclassing:
  44. with pytest.raises(MyException5) as excinfo:
  45. throws5_1()
  46. assert msg(excinfo.value) == "MyException5 subclass"
  47. assert isinstance(excinfo.value, MyException5_1)
  48. with pytest.raises(MyException5_1) as excinfo:
  49. throws5_1()
  50. assert msg(excinfo.value) == "MyException5 subclass"
  51. with pytest.raises(MyException5) as excinfo:
  52. try:
  53. throws5()
  54. except MyException5_1:
  55. raise RuntimeError("Exception error: caught child from parent")
  56. assert msg(excinfo.value) == "this is a helper-defined translated exception"