Skip to content

Commit 5a73c57

Browse files
committed
terminal: add a TerminalReporter.write_raw method
The `write` method does some processing on the input, namely markup and adding to the current line. `write_raw` skips these parts. It is useful for emitting invisible escape sequences.
1 parent 9913ced commit 5a73c57

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/_pytest/_io/terminalwriter.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,23 @@ def write(self, msg: str, *, flush: bool = False, **markup: bool) -> None:
161161

162162
msg = self.markup(msg, **markup)
163163

164-
try:
165-
self._file.write(msg)
166-
except UnicodeEncodeError:
167-
# Some environments don't support printing general Unicode
168-
# strings, due to misconfiguration or otherwise; in that case,
169-
# print the string escaped to ASCII.
170-
# When the Unicode situation improves we should consider
171-
# letting the error propagate instead of masking it (see #7475
172-
# for one brief attempt).
173-
msg = msg.encode("unicode-escape").decode("ascii")
174-
self._file.write(msg)
175-
176-
if flush:
177-
self.flush()
164+
self.write_raw(msg, flush=flush)
165+
166+
def write_raw(self, msg: str, *, flush: bool = False) -> None:
167+
try:
168+
self._file.write(msg)
169+
except UnicodeEncodeError:
170+
# Some environments don't support printing general Unicode
171+
# strings, due to misconfiguration or otherwise; in that case,
172+
# print the string escaped to ASCII.
173+
# When the Unicode situation improves we should consider
174+
# letting the error propagate instead of masking it (see #7475
175+
# for one brief attempt).
176+
msg = msg.encode("unicode-escape").decode("ascii")
177+
self._file.write(msg)
178+
179+
if flush:
180+
self.flush()
178181

179182
def line(self, s: str = "", **markup: bool) -> None:
180183
self.write(s, **markup)

src/_pytest/terminal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ def wrap_write(
508508
def write(self, content: str, *, flush: bool = False, **markup: bool) -> None:
509509
self._tw.write(content, flush=flush, **markup)
510510

511+
def write_raw(self, content: str, *, flush: bool = False) -> None:
512+
self._tw.write_raw(content, flush=flush)
513+
511514
def flush(self) -> None:
512515
self._tw.flush()
513516

0 commit comments

Comments
 (0)