MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_python_alreadyset_in_destructor

Function test_python_alreadyset_in_destructor

tests/test_exceptions.py:107–129  ·  view source on GitHub ↗
(monkeypatch, capsys)

Source from the content-addressed store, hash-verified

105@pytest.mark.xfail(env.PYPY, reason="Failure on PyPy 3.8 (7.3.7)", strict=False)
106@ignore_pytest_unraisable_warning
107def test_python_alreadyset_in_destructor(monkeypatch, capsys):
108 triggered = False
109
110 # Don't take `sys.unraisablehook`, as that's overwritten by pytest
111 default_hook = sys.__unraisablehook__
112
113 def hook(unraisable_hook_args):
114 exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
115 if obj == "already_set demo":
116 nonlocal triggered
117 triggered = True
118 default_hook(unraisable_hook_args)
119 return
120
121 # Use monkeypatch so pytest can apply and remove the patch as appropriate
122 monkeypatch.setattr(sys, "unraisablehook", hook)
123
124 assert m.python_alreadyset_in_destructor("already_set demo") is True
125 assert triggered is True
126
127 _, captured_stderr = capsys.readouterr()
128 assert captured_stderr.startswith("Exception ignored in: 'already_set demo'")
129 assert captured_stderr.rstrip().endswith("KeyError: 'bar'")
130
131
132def test_exception_matches():

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected