Skip to content

Commit

Permalink
Add sphinx.util._strip_escape_sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Sep 20, 2023
1 parent bf620e9 commit cb0c6a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions sphinx/util/_io.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +22,7 @@ def __init__(

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'):
Expand Down
14 changes: 13 additions & 1 deletion sphinx/util/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
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] = {}


Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions sphinx/util/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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
Expand All @@ -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')

Expand Down
3 changes: 1 addition & 2 deletions tests/test_build_linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
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

Expand Down Expand Up @@ -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(
"""\
Expand Down

0 comments on commit cb0c6a3

Please sign in to comment.