Skip to content

Commit c6f5a4b

Browse files
committed
Make sage sphinx logger robust against ansi escape sequence
1 parent a9189b3 commit c6f5a4b

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/sage_docbuild/sphinxbuild.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,22 @@ class SageSphinxLogger():
4141
This implements the file object interface to serve as
4242
``sys.stdout``/``sys.stderr`` replacement.
4343
"""
44-
ansi_color = re.compile(r'\x1b\[[0-9;]*m')
45-
ansi_reset = re.compile(r'\x1b\[39;49;00m')
44+
# https://en.wikipedia.org/wiki/ANSI_escape_code
45+
ansi_escape_sequence = re.compile(r'''
46+
\x1b # ESC
47+
\[ # CSI sequence starts
48+
[0-?]* # parameter bytes
49+
[ -/]* # intermediate bytes
50+
[@-~] # final byte
51+
''', re.VERBOSE)
52+
ansi_escape_sequence_color = re.compile(r'''
53+
\x1b # ESC
54+
\[ # CSI sequence starts
55+
[0-9;]* # parameter bytes
56+
# intermediate bytes
57+
m # final byte
58+
''', re.VERBOSE)
59+
4660
prefix_len = 9
4761

4862
def __init__(self, stream, prefix):
@@ -164,7 +178,7 @@ def _filter_out(self, line):
164178
if self._error is not None and self._is_stdout:
165179
# swallow non-errors after an error occurred
166180
return True
167-
line = re.sub(self.ansi_color, '', line)
181+
line = re.sub(self.ansi_escape_sequence, '', line)
168182
line = line.strip()
169183
for regex in self._useless_chatter:
170184
if regex.search(line) is not None:
@@ -233,7 +247,7 @@ def _log_line(self, line):
233247
line = old.sub(new, line)
234248
line = self._prefix + ' ' + line.rstrip() + '\n'
235249
if not self._color:
236-
line = self.ansi_color.sub('', line)
250+
line = self.ansi_escape_sequence_color.sub('', line)
237251
if not skip_this_line:
238252
# sphinx does produce messages in the current locals which
239253
# could be non-ascii

0 commit comments

Comments
 (0)