Skip to content

Commit 731e9a7

Browse files
committedApr 30, 2023
fix: a fake stdout might not have isatty
1 parent 31c216b commit 731e9a7

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed
 

‎CHANGES.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23-
Nothing yet.
23+
- Fix: ``html_report()`` could fail with an AttributeError on ``isatty`` if run
24+
in an unusual environment where sys.stdout had been replaced. This is now
25+
fixed.
2426

2527

2628
.. scriv-start-here

‎coverage/misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def stdout_link(text: str, url: str) -> str:
394394
If attached to a terminal, use escape sequences. Otherwise, just return
395395
the text.
396396
"""
397-
if sys.stdout.isatty():
397+
if hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
398398
return f"\033]8;;{url}\a{text}\033]8;;\a"
399399
else:
400400
return text

‎tests/test_misc.py

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
import sys
9+
from typing import Any
910
from unittest import mock
1011

1112
import pytest
@@ -165,3 +166,19 @@ def test_stdout_link_tty() -> None:
165166
def test_stdout_link_not_tty() -> None:
166167
# Without mocking isatty, it reports False in a pytest suite.
167168
assert stdout_link("some text", "some url") == "some text"
169+
170+
171+
def test_stdout_link_with_fake_stdout() -> None:
172+
# If stdout is another object, we should still be ok.
173+
class FakeStdout:
174+
"""New stdout, has .write(), but not .isatty()."""
175+
def __init__(self, f: Any) -> None:
176+
self.f = f
177+
178+
def write(self, data: str) -> Any:
179+
"""Write through to the underlying file."""
180+
return self.f.write(data)
181+
182+
with mock.patch.object(sys, "stdout", FakeStdout(sys.stdout)):
183+
link = stdout_link("some text", "some url")
184+
assert link == "some text"

0 commit comments

Comments
 (0)
Please sign in to comment.