2021-01-18 02:23:57 +08:00
|
|
|
import io
|
2019-06-13 05:49:51 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import pytest
|
2020-11-10 01:07:34 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2019-06-13 05:49:51 +08:00
|
|
|
|
2020-11-12 01:35:06 +08:00
|
|
|
|
2020-11-10 01:07:34 +08:00
|
|
|
def test_enabled(pytester: Pytester) -> None:
|
2019-06-13 05:49:51 +08:00
|
|
|
"""Test single crashing test displays a traceback."""
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-13 05:49:51 +08:00
|
|
|
"""
|
|
|
|
import faulthandler
|
|
|
|
def test_crash():
|
|
|
|
faulthandler._sigabrt()
|
|
|
|
"""
|
|
|
|
)
|
2020-11-10 01:07:34 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-06-13 05:49:51 +08:00
|
|
|
result.stderr.fnmatch_lines(["*Fatal Python error*"])
|
|
|
|
assert result.ret != 0
|
|
|
|
|
|
|
|
|
2021-01-25 23:28:00 +08:00
|
|
|
def setup_crashing_test(pytester: Pytester) -> None:
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-13 05:49:51 +08:00
|
|
|
"""
|
2021-01-25 23:28:00 +08:00
|
|
|
import faulthandler
|
|
|
|
import atexit
|
|
|
|
def test_ok():
|
|
|
|
atexit.register(faulthandler._sigabrt)
|
|
|
|
"""
|
2019-06-13 05:49:51 +08:00
|
|
|
)
|
2021-01-25 23:28:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_crash_during_shutdown_captured(pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Re-enable faulthandler if pytest encountered it enabled during configure.
|
|
|
|
We should be able to then see crashes during interpreter shutdown.
|
|
|
|
"""
|
|
|
|
setup_crashing_test(pytester)
|
|
|
|
args = (sys.executable, "-Xfaulthandler", "-mpytest")
|
|
|
|
result = pytester.run(*args)
|
2019-06-13 05:49:51 +08:00
|
|
|
result.stderr.fnmatch_lines(["*Fatal Python error*"])
|
|
|
|
assert result.ret != 0
|
|
|
|
|
|
|
|
|
2021-01-25 23:28:00 +08:00
|
|
|
def test_crash_during_shutdown_not_captured(pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Check that pytest leaves faulthandler disabled if it was not enabled during configure.
|
|
|
|
This prevents us from seeing crashes during interpreter shutdown (see #8260).
|
|
|
|
"""
|
|
|
|
setup_crashing_test(pytester)
|
|
|
|
args = (sys.executable, "-mpytest")
|
|
|
|
result = pytester.run(*args)
|
|
|
|
result.stderr.no_fnmatch_line("*Fatal Python error*")
|
|
|
|
assert result.ret != 0
|
|
|
|
|
|
|
|
|
2020-11-10 01:07:34 +08:00
|
|
|
def test_disabled(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Test option to disable fault handler in the command line."""
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-13 05:49:51 +08:00
|
|
|
"""
|
|
|
|
import faulthandler
|
|
|
|
def test_disabled():
|
|
|
|
assert not faulthandler.is_enabled()
|
|
|
|
"""
|
|
|
|
)
|
2020-11-10 01:07:34 +08:00
|
|
|
result = pytester.runpytest_subprocess("-p", "no:faulthandler")
|
2019-06-13 05:49:51 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-04-09 22:23:22 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"enabled",
|
|
|
|
[
|
|
|
|
pytest.param(
|
|
|
|
True, marks=pytest.mark.skip(reason="sometimes crashes on CI (#7022)")
|
|
|
|
),
|
|
|
|
False,
|
|
|
|
],
|
|
|
|
)
|
2020-11-10 01:07:34 +08:00
|
|
|
def test_timeout(pytester: Pytester, enabled: bool) -> None:
|
2019-06-13 05:49:51 +08:00
|
|
|
"""Test option to dump tracebacks after a certain timeout.
|
2020-07-18 17:35:13 +08:00
|
|
|
|
2019-06-13 05:49:51 +08:00
|
|
|
If faulthandler is disabled, no traceback will be dumped.
|
|
|
|
"""
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-13 05:49:51 +08:00
|
|
|
"""
|
2020-02-21 19:52:31 +08:00
|
|
|
import os, time
|
2019-06-13 05:49:51 +08:00
|
|
|
def test_timeout():
|
2020-04-07 15:49:10 +08:00
|
|
|
time.sleep(1 if "CI" in os.environ else 0.1)
|
2019-06-13 05:49:51 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makeini(
|
2019-06-23 06:22:43 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
2019-10-27 09:43:49 +08:00
|
|
|
faulthandler_timeout = 0.01
|
2019-06-23 06:22:43 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = ["-p", "no:faulthandler"] if not enabled else []
|
2019-06-13 05:49:51 +08:00
|
|
|
|
2020-11-10 01:07:34 +08:00
|
|
|
result = pytester.runpytest_subprocess(*args)
|
2019-06-13 05:49:51 +08:00
|
|
|
tb_output = "most recent call first"
|
|
|
|
if enabled:
|
|
|
|
result.stderr.fnmatch_lines(["*%s*" % tb_output])
|
|
|
|
else:
|
|
|
|
assert tb_output not in result.stderr.str()
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", ["pytest_enter_pdb", "pytest_exception_interact"])
|
2020-11-11 12:02:32 +08:00
|
|
|
def test_cancel_timeout_on_hook(monkeypatch, hook_name) -> None:
|
2019-06-13 05:49:51 +08:00
|
|
|
"""Make sure that we are cancelling any scheduled traceback dumping due
|
2020-07-18 17:35:13 +08:00
|
|
|
to timeout before entering pdb (pytest-dev/pytest-faulthandler#12) or any
|
|
|
|
other interactive exception (pytest-dev/pytest-faulthandler#14)."""
|
2019-06-13 05:49:51 +08:00
|
|
|
import faulthandler
|
2021-01-20 21:05:36 +08:00
|
|
|
from _pytest import faulthandler as faulthandler_plugin
|
2019-06-13 05:49:51 +08:00
|
|
|
|
|
|
|
called = []
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
faulthandler, "cancel_dump_traceback_later", lambda: called.append(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
# call our hook explicitly, we can trust that pytest will call the hook
|
|
|
|
# for us at the appropriate moment
|
2021-01-20 21:05:36 +08:00
|
|
|
hook_func = getattr(faulthandler_plugin, hook_name)
|
|
|
|
hook_func()
|
2019-06-13 05:49:51 +08:00
|
|
|
assert called == [1]
|
2020-02-12 18:06:20 +08:00
|
|
|
|
|
|
|
|
2021-01-20 21:05:36 +08:00
|
|
|
def test_already_initialized_crash(pytester: Pytester) -> None:
|
|
|
|
"""Even if faulthandler is already initialized, we still dump tracebacks on crashes (#8258)."""
|
2020-11-10 01:07:34 +08:00
|
|
|
pytester.makepyfile(
|
2020-02-12 18:06:20 +08:00
|
|
|
"""
|
|
|
|
def test():
|
|
|
|
import faulthandler
|
2021-01-20 21:05:36 +08:00
|
|
|
faulthandler._sigabrt()
|
2020-02-12 18:06:20 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-10 01:07:34 +08:00
|
|
|
result = pytester.run(
|
2020-02-12 18:06:20 +08:00
|
|
|
sys.executable,
|
|
|
|
"-X",
|
|
|
|
"faulthandler",
|
|
|
|
"-mpytest",
|
2020-11-11 11:45:57 +08:00
|
|
|
pytester.path,
|
2020-02-12 18:06:20 +08:00
|
|
|
)
|
2021-01-20 21:05:36 +08:00
|
|
|
result.stderr.fnmatch_lines(["*Fatal Python error*"])
|
|
|
|
assert result.ret != 0
|
2021-01-18 02:23:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_stderr_fileno_invalid_fd() -> None:
|
|
|
|
"""Test for faulthandler being able to handle invalid file descriptors for stderr (#8249)."""
|
2021-01-20 21:05:36 +08:00
|
|
|
from _pytest.faulthandler import get_stderr_fileno
|
2021-01-18 02:23:57 +08:00
|
|
|
|
|
|
|
class StdErrWrapper(io.StringIO):
|
|
|
|
"""
|
|
|
|
Mimic ``twisted.logger.LoggingFile`` to simulate returning an invalid file descriptor.
|
|
|
|
|
|
|
|
https://github.com/twisted/twisted/blob/twisted-20.3.0/src/twisted/logger/_io.py#L132-L139
|
|
|
|
"""
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
return -1
|
|
|
|
|
|
|
|
wrapper = StdErrWrapper()
|
|
|
|
|
|
|
|
with pytest.MonkeyPatch.context() as mp:
|
|
|
|
mp.setattr("sys.stderr", wrapper)
|
|
|
|
|
|
|
|
# Even when the stderr wrapper signals an invalid file descriptor,
|
|
|
|
# ``_get_stderr_fileno()`` should return the real one.
|
2021-01-20 21:05:36 +08:00
|
|
|
assert get_stderr_fileno() == 2
|