test_exceptions.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import pytest
  4. from pybind11_tests import exceptions as m
  5. import pybind11_cross_module_tests as cm
  6. def test_std_exception(msg):
  7. with pytest.raises(RuntimeError) as excinfo:
  8. m.throw_std_exception()
  9. assert msg(excinfo.value) == "This exception was intentionally thrown."
  10. def test_error_already_set(msg):
  11. with pytest.raises(RuntimeError) as excinfo:
  12. m.throw_already_set(False)
  13. assert msg(excinfo.value) == "Unknown internal error occurred"
  14. with pytest.raises(ValueError) as excinfo:
  15. m.throw_already_set(True)
  16. assert msg(excinfo.value) == "foo"
  17. def test_cross_module_exceptions():
  18. with pytest.raises(RuntimeError) as excinfo:
  19. cm.raise_runtime_error()
  20. assert str(excinfo.value) == "My runtime error"
  21. with pytest.raises(ValueError) as excinfo:
  22. cm.raise_value_error()
  23. assert str(excinfo.value) == "My value error"
  24. with pytest.raises(ValueError) as excinfo:
  25. cm.throw_pybind_value_error()
  26. assert str(excinfo.value) == "pybind11 value error"
  27. with pytest.raises(TypeError) as excinfo:
  28. cm.throw_pybind_type_error()
  29. assert str(excinfo.value) == "pybind11 type error"
  30. with pytest.raises(StopIteration) as excinfo:
  31. cm.throw_stop_iteration()
  32. def test_python_call_in_catch():
  33. d = {}
  34. assert m.python_call_in_destructor(d) is True
  35. assert d["good"] is True
  36. def ignore_pytest_unraisable_warning(f):
  37. unraisable = "PytestUnraisableExceptionWarning"
  38. if hasattr(pytest, unraisable): # Python >= 3.8 and pytest >= 6
  39. dec = pytest.mark.filterwarnings("ignore::pytest.{}".format(unraisable))
  40. return dec(f)
  41. else:
  42. return f
  43. @ignore_pytest_unraisable_warning
  44. def test_python_alreadyset_in_destructor(monkeypatch, capsys):
  45. hooked = False
  46. triggered = [False] # mutable, so Python 2.7 closure can modify it
  47. if hasattr(sys, "unraisablehook"): # Python 3.8+
  48. hooked = True
  49. # Don't take `sys.unraisablehook`, as that's overwritten by pytest
  50. default_hook = sys.__unraisablehook__
  51. def hook(unraisable_hook_args):
  52. exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
  53. if obj == "already_set demo":
  54. triggered[0] = True
  55. default_hook(unraisable_hook_args)
  56. return
  57. # Use monkeypatch so pytest can apply and remove the patch as appropriate
  58. monkeypatch.setattr(sys, "unraisablehook", hook)
  59. assert m.python_alreadyset_in_destructor("already_set demo") is True
  60. if hooked:
  61. assert triggered[0] is True
  62. _, captured_stderr = capsys.readouterr()
  63. # Error message is different in Python 2 and 3, check for words that appear in both
  64. assert "ignored" in captured_stderr and "already_set demo" in captured_stderr
  65. def test_exception_matches():
  66. assert m.exception_matches()
  67. assert m.exception_matches_base()
  68. assert m.modulenotfound_exception_matches_base()
  69. def test_custom(msg):
  70. # Can we catch a MyException?
  71. with pytest.raises(m.MyException) as excinfo:
  72. m.throws1()
  73. assert msg(excinfo.value) == "this error should go to a custom type"
  74. # Can we translate to standard Python exceptions?
  75. with pytest.raises(RuntimeError) as excinfo:
  76. m.throws2()
  77. assert msg(excinfo.value) == "this error should go to a standard Python exception"
  78. # Can we handle unknown exceptions?
  79. with pytest.raises(RuntimeError) as excinfo:
  80. m.throws3()
  81. assert msg(excinfo.value) == "Caught an unknown exception!"
  82. # Can we delegate to another handler by rethrowing?
  83. with pytest.raises(m.MyException) as excinfo:
  84. m.throws4()
  85. assert msg(excinfo.value) == "this error is rethrown"
  86. # Can we fall-through to the default handler?
  87. with pytest.raises(RuntimeError) as excinfo:
  88. m.throws_logic_error()
  89. assert (
  90. msg(excinfo.value) == "this error should fall through to the standard handler"
  91. )
  92. # OverFlow error translation.
  93. with pytest.raises(OverflowError) as excinfo:
  94. m.throws_overflow_error()
  95. # Can we handle a helper-declared exception?
  96. with pytest.raises(m.MyException5) as excinfo:
  97. m.throws5()
  98. assert msg(excinfo.value) == "this is a helper-defined translated exception"
  99. # Exception subclassing:
  100. with pytest.raises(m.MyException5) as excinfo:
  101. m.throws5_1()
  102. assert msg(excinfo.value) == "MyException5 subclass"
  103. assert isinstance(excinfo.value, m.MyException5_1)
  104. with pytest.raises(m.MyException5_1) as excinfo:
  105. m.throws5_1()
  106. assert msg(excinfo.value) == "MyException5 subclass"
  107. with pytest.raises(m.MyException5) as excinfo:
  108. try:
  109. m.throws5()
  110. except m.MyException5_1:
  111. raise RuntimeError("Exception error: caught child from parent")
  112. assert msg(excinfo.value) == "this is a helper-defined translated exception"
  113. def test_nested_throws(capture):
  114. """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
  115. def throw_myex():
  116. raise m.MyException("nested error")
  117. def throw_myex5():
  118. raise m.MyException5("nested error 5")
  119. # In the comments below, the exception is caught in the first step, thrown in the last step
  120. # C++ -> Python
  121. with capture:
  122. m.try_catch(m.MyException5, throw_myex5)
  123. assert str(capture).startswith("MyException5: nested error 5")
  124. # Python -> C++ -> Python
  125. with pytest.raises(m.MyException) as excinfo:
  126. m.try_catch(m.MyException5, throw_myex)
  127. assert str(excinfo.value) == "nested error"
  128. def pycatch(exctype, f, *args):
  129. try:
  130. f(*args)
  131. except m.MyException as e:
  132. print(e)
  133. # C++ -> Python -> C++ -> Python
  134. with capture:
  135. m.try_catch(
  136. m.MyException5,
  137. pycatch,
  138. m.MyException,
  139. m.try_catch,
  140. m.MyException,
  141. throw_myex5,
  142. )
  143. assert str(capture).startswith("MyException5: nested error 5")
  144. # C++ -> Python -> C++
  145. with capture:
  146. m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
  147. assert capture == "this error is rethrown"
  148. # Python -> C++ -> Python -> C++
  149. with pytest.raises(m.MyException5) as excinfo:
  150. m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
  151. assert str(excinfo.value) == "this is a helper-defined translated exception"
  152. # This can often happen if you wrap a pybind11 class in a Python wrapper
  153. def test_invalid_repr():
  154. class MyRepr(object):
  155. def __repr__(self):
  156. raise AttributeError("Example error")
  157. with pytest.raises(TypeError):
  158. m.simple_bool_passthrough(MyRepr())