Skip to content

Commit 7a65a47

Browse files
[3.14] Make Android streams respect the unbuffered (-u) option (GH-138806) (#139108)
Co-authored-by: Malcolm Smith <[email protected]>
1 parent 997e471 commit 7a65a47

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

Lib/_android_support.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ def init_streams(android_log_write, stdout_prio, stderr_prio):
2929

3030
global logcat
3131
logcat = Logcat(android_log_write)
32-
33-
sys.stdout = TextLogStream(
34-
stdout_prio, "python.stdout", sys.stdout.fileno())
35-
sys.stderr = TextLogStream(
36-
stderr_prio, "python.stderr", sys.stderr.fileno())
32+
sys.stdout = TextLogStream(stdout_prio, "python.stdout", sys.stdout)
33+
sys.stderr = TextLogStream(stderr_prio, "python.stderr", sys.stderr)
3734

3835

3936
class TextLogStream(io.TextIOWrapper):
40-
def __init__(self, prio, tag, fileno=None, **kwargs):
37+
def __init__(self, prio, tag, original=None, **kwargs):
38+
# Respect the -u option.
39+
if original:
40+
kwargs.setdefault("write_through", original.write_through)
41+
fileno = original.fileno()
42+
else:
43+
fileno = None
44+
4145
# The default is surrogateescape for stdout and backslashreplace for
4246
# stderr, but in the context of an Android log, readability is more
4347
# important than reversibility.

Lib/test/test_android.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,38 @@ def tearDown(self):
9191
self.logcat_thread = None
9292

9393
@contextmanager
94-
def unbuffered(self, stream):
95-
stream.reconfigure(write_through=True)
94+
def reconfigure(self, stream, **settings):
95+
original_settings = {key: getattr(stream, key, None) for key in settings.keys()}
96+
stream.reconfigure(**settings)
9697
try:
9798
yield
9899
finally:
99-
stream.reconfigure(write_through=False)
100+
stream.reconfigure(**original_settings)
100101

101-
# In --verbose3 mode, sys.stdout and sys.stderr are captured, so we can't
102-
# test them directly. Detect this mode and use some temporary streams with
103-
# the same properties.
104102
def stream_context(self, stream_name, level):
105-
# https://developer.android.com/ndk/reference/group/logging
106-
prio = {"I": 4, "W": 5}[level]
107-
108103
stack = ExitStack()
109104
stack.enter_context(self.subTest(stream_name))
105+
106+
# In --verbose3 mode, sys.stdout and sys.stderr are captured, so we can't
107+
# test them directly. Detect this mode and use some temporary streams with
108+
# the same properties.
110109
stream = getattr(sys, stream_name)
111110
native_stream = getattr(sys, f"__{stream_name}__")
112111
if isinstance(stream, io.StringIO):
112+
# https://developer.android.com/ndk/reference/group/logging
113+
prio = {"I": 4, "W": 5}[level]
113114
stack.enter_context(
114115
patch(
115116
f"sys.{stream_name}",
116-
TextLogStream(
117-
prio, f"python.{stream_name}", native_stream.fileno(),
118-
errors="backslashreplace"
117+
stream := TextLogStream(
118+
prio, f"python.{stream_name}", native_stream,
119119
),
120120
)
121121
)
122+
123+
# The tests assume the stream is initially buffered.
124+
stack.enter_context(self.reconfigure(stream, write_through=False))
125+
122126
return stack
123127

124128
def test_str(self):
@@ -145,7 +149,7 @@ def write(s, lines=None, *, write_len=None):
145149
self.assert_logs(level, tag, lines)
146150

147151
# Single-line messages,
148-
with self.unbuffered(stream):
152+
with self.reconfigure(stream, write_through=True):
149153
write("", [])
150154

151155
write("a")
@@ -192,7 +196,7 @@ def write(s, lines=None, *, write_len=None):
192196

193197
# However, buffering can be turned off completely if you want a
194198
# flush after every write.
195-
with self.unbuffered(stream):
199+
with self.reconfigure(stream, write_through=True):
196200
write("\nx", ["", "x"])
197201
write("\na\n", ["", "a"])
198202
write("\n", [""])

0 commit comments

Comments
 (0)