Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix faulthandler for Twisted Logger when used with "--capture=no" #8250

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Anders Hovmöller
Andras Mitzki
Andras Tim
Andrea Cimatoribus
Andreas Motl
Andreas Zeidler
Andrey Paramonov
Andrzej Klajnert
Expand Down
1 change: 1 addition & 0 deletions changelog/8249.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the ``faulthandler`` plugin for occasions when running with ``twisted.logger`` and using ``pytest --capture=no``.
7 changes: 6 additions & 1 deletion src/_pytest/faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def pytest_unconfigure(self, config: Config) -> None:
@staticmethod
def _get_stderr_fileno():
try:
return sys.stderr.fileno()
fileno = sys.stderr.fileno()
# The Twisted Logger will return an invalid file descriptor since it is not backed
# by an FD. So, let's also forward this to the same code path as with pytest-xdist.
if fileno == -1:
raise AttributeError()
return fileno
except (AttributeError, io.UnsupportedOperation):
# pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
# https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
Expand Down
25 changes: 25 additions & 0 deletions testing/test_faulthandler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import sys

import pytest
Expand Down Expand Up @@ -135,3 +136,27 @@ def test():
result.stdout.no_fnmatch_line(warning_line)
result.stdout.fnmatch_lines("*1 passed*")
assert result.ret == 0


def test_get_stderr_fileno_invalid_fd() -> None:
"""Test for faulthandler being able to handle invalid file descriptors for stderr (#8249)."""
from _pytest.faulthandler import FaultHandlerHooks

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.
assert FaultHandlerHooks._get_stderr_fileno() == 2