Skip to content

Commit 083c581

Browse files
[3.13] gh-125942: Android: set stdout to errors="backslashreplace" (GH-125943) (#125950)
gh-125942: Android: set stdout to `errors="backslashreplace"` (GH-125943) Android stdout/err streams now use `backslashreplace` encoding to ensure readability of the Android log. (cherry picked from commit b08570c) Co-authored-by: Malcolm Smith <[email protected]>
1 parent 904634b commit 083c581

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

Lib/_android_support.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ def init_streams(android_log_write, stdout_prio, stderr_prio):
3131
logcat = Logcat(android_log_write)
3232

3333
sys.stdout = TextLogStream(
34-
stdout_prio, "python.stdout", sys.stdout.fileno(),
35-
errors=sys.stdout.errors)
34+
stdout_prio, "python.stdout", sys.stdout.fileno())
3635
sys.stderr = TextLogStream(
37-
stderr_prio, "python.stderr", sys.stderr.fileno(),
38-
errors=sys.stderr.errors)
36+
stderr_prio, "python.stderr", sys.stderr.fileno())
3937

4038

4139
class TextLogStream(io.TextIOWrapper):
4240
def __init__(self, prio, tag, fileno=None, **kwargs):
41+
# The default is surrogateescape for stdout and backslashreplace for
42+
# stderr, but in the context of an Android log, readability is more
43+
# important than reversibility.
4344
kwargs.setdefault("encoding", "UTF-8")
45+
kwargs.setdefault("errors", "backslashreplace")
46+
4447
super().__init__(BinaryLogStream(prio, tag, fileno), **kwargs)
4548
self._lock = RLock()
4649
self._pending_bytes = []

Lib/test/test_android.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,10 @@ def test_str(self):
123123
self.assertIs(stream.readable(), False)
124124
self.assertEqual(stream.fileno(), fileno)
125125
self.assertEqual("UTF-8", stream.encoding)
126+
self.assertEqual("backslashreplace", stream.errors)
126127
self.assertIs(stream.line_buffering, True)
127128
self.assertIs(stream.write_through, False)
128129

129-
# stderr is backslashreplace by default; stdout is configured
130-
# that way by libregrtest.main.
131-
self.assertEqual("backslashreplace", stream.errors)
132-
133130
def write(s, lines=None, *, write_len=None):
134131
if write_len is None:
135132
write_len = len(s)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
On Android, the ``errors`` setting of :any:`sys.stdout` was changed from
2+
``surrogateescape`` to ``backslashreplace``.

0 commit comments

Comments
 (0)