Skip to content

gh-125942: Android: set stdout to errors="backslashreplace" #125943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Lib/_android_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ def init_streams(android_log_write, stdout_prio, stderr_prio):
logcat = Logcat(android_log_write)

sys.stdout = TextLogStream(
stdout_prio, "python.stdout", sys.stdout.fileno(),
errors=sys.stdout.errors)
stdout_prio, "python.stdout", sys.stdout.fileno())
sys.stderr = TextLogStream(
stderr_prio, "python.stderr", sys.stderr.fileno(),
errors=sys.stderr.errors)
stderr_prio, "python.stderr", sys.stderr.fileno())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand what is going on here - why is the errors kwarg no longer required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's been added as a default in TextLogStream itself.

Copy link
Member Author

@mhsmith mhsmith Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that because of how the output is being logged, we want to explicitly use backslashreplace, rather than copying the existing errors setting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - I was misreading what sys.stdout.errors was passing in.



class TextLogStream(io.TextIOWrapper):
def __init__(self, prio, tag, fileno=None, **kwargs):
# The default is surrogateescape for stdout and backslashreplace for
# stderr, but in the context of an Android log, readability is more
# important than reversibility.
kwargs.setdefault("encoding", "UTF-8")
kwargs.setdefault("errors", "backslashreplace")

super().__init__(BinaryLogStream(prio, tag, fileno), **kwargs)
self._lock = RLock()
self._pending_bytes = []
Expand Down
5 changes: 1 addition & 4 deletions Lib/test/test_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,10 @@ def test_str(self):
self.assertIs(stream.readable(), False)
self.assertEqual(stream.fileno(), fileno)
self.assertEqual("UTF-8", stream.encoding)
self.assertEqual("backslashreplace", stream.errors)
self.assertIs(stream.line_buffering, True)
self.assertIs(stream.write_through, False)

# stderr is backslashreplace by default; stdout is configured
# that way by libregrtest.main.
self.assertEqual("backslashreplace", stream.errors)

def write(s, lines=None, *, write_len=None):
if write_len is None:
write_len = len(s)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
On Android, the ``errors`` setting of :any:`sys.stdout` was changed from
``surrogateescape`` to ``backslashreplace``.
Loading