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.

87 lines
2.8 KiB

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