diff --git a/src/_pytest/faulthandler.py b/src/_pytest/faulthandler.py index 083bcb83739..be6b0b93f3b 100644 --- a/src/_pytest/faulthandler.py +++ b/src/_pytest/faulthandler.py @@ -51,18 +51,19 @@ def pytest_unconfigure(config: Config) -> None: def get_stderr_fileno() -> int: + stderr = sys.stderr try: - 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 + fileno = stderr.fileno() except (AttributeError, ValueError): # 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 # This is potentially dangerous, but the best we can do. return 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: + return sys.__stderr__.fileno() + return fileno def get_timeout_config_value(config: Config) -> float: