Add `sphinx.util._strip_escape_sequences`

This commit is contained in:
Adam Turner 2023-09-20 23:53:04 +01:00
parent bf620e9623
commit cb0c6a359b
4 changed files with 19 additions and 7 deletions

View File

@ -1,6 +1,6 @@
from typing import TYPE_CHECKING
from sphinx.util.console import strip_colors
from sphinx.util.console import _strip_escape_sequences
if TYPE_CHECKING:
from typing import Protocol
@ -22,7 +22,7 @@ class TeeStripANSI:
def write(self, text: str, /) -> None:
self.stream_term.write(text)
self.stream_file.write(strip_colors(text))
self.stream_file.write(_strip_escape_sequences(text))
def flush(self) -> None:
if hasattr(self.stream_term, 'flush'):

View File

@ -14,7 +14,15 @@ except ImportError:
colorama = None
_ansi_re: re.Pattern[str] = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm')
_CSI = re.escape('\x1b[') # 'ESC [': Control Sequence Introducer
_ansi_re: re.Pattern[str] = re.compile(
_CSI + r"""
(
(\d\d;){0,2}\d\dm # ANSI colour code
|
\dK # ANSI Erase in Line
)""",
re.VERBOSE | re.ASCII)
codes: dict[str, str] = {}
@ -90,6 +98,10 @@ def strip_colors(s: str) -> str:
return re.compile('\x1b.*?m').sub('', s)
def _strip_escape_sequences(s: str) -> str:
return _ansi_re.sub('', s)
def create_color_func(name: str) -> None:
def inner(text: str) -> str:
return colorize(name, text)

View File

@ -6,7 +6,7 @@ from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING
from sphinx.errors import SphinxParallelError
from sphinx.util.console import strip_colors
from sphinx.util.console import _strip_escape_sequences
if TYPE_CHECKING:
from sphinx.application import Sphinx
@ -31,7 +31,8 @@ def save_traceback(app: Sphinx | None, exc: BaseException) -> str:
last_msgs = exts_list = ''
else:
extensions = app.extensions.values()
last_msgs = '\n'.join(f'# {strip_colors(s).strip()}' for s in app.messagelog)
last_msgs = '\n'.join(f'# {_strip_escape_sequences(s).strip()}'
for s in app.messagelog)
exts_list = '\n'.join(f'# {ext.name} ({ext.version})' for ext in extensions
if ext.version != 'builtin')

View File

@ -26,7 +26,6 @@ from sphinx.builders.linkcheck import (
)
from sphinx.testing.util import strip_escseq
from sphinx.util import requests
from sphinx.util.console import strip_colors
from .utils import CERT_FILE, http_server, https_server
@ -764,7 +763,7 @@ def test_too_many_requests_retry_after_int_delay(app, capsys, status):
"info": "",
}
rate_limit_log = "-rate limited- http://localhost:7777/ | sleeping...\n"
assert rate_limit_log in strip_colors(status.getvalue())
assert rate_limit_log in strip_escseq(status.getvalue())
_stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent(
"""\