Skip to content

Commit 595d68d

Browse files
dnicolodirgommers
authored andcommitted
BUG: suppress all uses of ANSI terminal escapes when not supported
1 parent a32a242 commit 595d68d

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

mesonpy/__init__.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import functools
1919
import importlib.machinery
2020
import io
21+
import itertools
2122
import json
2223
import os
2324
import pathlib
@@ -56,7 +57,7 @@
5657
if typing.TYPE_CHECKING: # pragma: no cover
5758
from typing import Any, Callable, DefaultDict, Dict, List, Literal, Optional, Sequence, TextIO, Tuple, Type, TypeVar, Union
5859

59-
from mesonpy._compat import Collection, Iterator, Mapping, ParamSpec, Path
60+
from mesonpy._compat import Collection, Iterator, Mapping, ParamSpec, Path, Self
6061

6162
P = ParamSpec('P')
6263
T = TypeVar('T')
@@ -194,6 +195,26 @@ def _showwarning(
194195
_log(f'{style.WARNING}meson-python: warning:{style.RESET} {message}')
195196

196197

198+
class _clicounter:
199+
def __init__(self, total: int) -> None:
200+
self._total = total
201+
self._count = itertools.count(start=1)
202+
203+
def __enter__(self) -> Self:
204+
return self
205+
206+
def update(self, description: str) -> None:
207+
line = f'[{next(self._count)}/{self._total}] {description}'
208+
if _use_ansi_escapes():
209+
print('\r', line, sep='', end='\33[0K', flush=True)
210+
else:
211+
print(line)
212+
213+
def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None:
214+
if _use_ansi_escapes():
215+
print()
216+
217+
197218
class Error(RuntimeError):
198219
def __str__(self) -> str:
199220
return str(self.args[0])
@@ -441,7 +462,7 @@ def build(self, directory: Path) -> pathlib.Path:
441462
with mesonpy._wheelfile.WheelFile(wheel_file, 'w') as whl:
442463
self._wheel_write_metadata(whl)
443464

444-
with mesonpy._util.clicounter(sum(len(x) for x in self._manifest.values())) as counter:
465+
with _clicounter(sum(len(x) for x in self._manifest.values())) as counter:
445466

446467
root = 'purelib' if self._pure else 'platlib'
447468

mesonpy/_util.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88

99
import contextlib
1010
import gzip
11-
import itertools
1211
import os
13-
import sys
1412
import tarfile
1513
import typing
1614

1715
from typing import IO
1816

1917

2018
if typing.TYPE_CHECKING: # pragma: no cover
21-
from typing import Any
22-
23-
from mesonpy._compat import Iterator, Path, Self
19+
from mesonpy._compat import Iterator, Path
2420

2521

2622
@contextlib.contextmanager
@@ -53,26 +49,6 @@ def create_targz(path: Path) -> Iterator[tarfile.TarFile]:
5349
yield tar
5450

5551

56-
class clicounter:
57-
def __init__(self, total: int) -> None:
58-
self._total = total
59-
self._count = itertools.count(start=1)
60-
61-
def __enter__(self) -> Self:
62-
return self
63-
64-
def update(self, description: str) -> None:
65-
line = f'[{next(self._count)}/{self._total}] {description}'
66-
if sys.stdout.isatty():
67-
print('\r', line, sep='', end='\33[0K', flush=True)
68-
else:
69-
print(line)
70-
71-
def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None:
72-
if sys.stdout.isatty():
73-
print()
74-
75-
7652
def setup_windows_console() -> bool:
7753
from ctypes import byref, windll # type: ignore
7854
from ctypes.wintypes import DWORD

tests/test_output.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(True, {'TERM': ''}, True),
1919
(True, {'TERM': 'dumb'}, False),
2020
])
21-
def test_use_ansi_colors(mocker, monkeypatch, tty, env, colors):
21+
def test_use_ansi_escapes(mocker, monkeypatch, tty, env, colors):
2222
mocker.patch('sys.stdout.isatty', return_value=tty)
2323
mocker.patch('mesonpy._util.setup_windows_console', return_value=True)
2424
monkeypatch.delenv('NO_COLOR', raising=False)
@@ -27,6 +27,6 @@ def test_use_ansi_colors(mocker, monkeypatch, tty, env, colors):
2727
monkeypatch.setenv(key, value)
2828

2929
# Clear caching by functools.lru_cache().
30-
mesonpy._use_ansi_colors.cache_clear()
30+
mesonpy._use_ansi_escapes.cache_clear()
3131

32-
assert mesonpy._use_ansi_colors() == colors
32+
assert mesonpy._use_ansi_escapes() == colors

0 commit comments

Comments
 (0)