Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Bytes cleanups
  • Loading branch information
mhsmith committed Apr 30, 2024
commit b2e2ef6f6e317eae0c11749d70723867cf03a86e
25 changes: 11 additions & 14 deletions Lib/_android_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,20 @@ def writable(self):
return True

def write(self, b):
try:
memoryview(b)
except TypeError:
raise TypeError(
f"write() argument must be bytes-like, not {type(b).__name__}"
) from None

b_out = bytes(b)
b_len = len(b_out) # May be different from len(b) if b is an array.
if type(b) is not bytes:
try:
b = bytes(memoryview(b))
except TypeError:
raise TypeError(
f"write() argument must be bytes-like, not {type(b).__name__}"
) from None

# Writing an empty string to the stream should have no effect.
if b_out:
if b:
# Encode null bytes using "modified UTF-8" to avoid truncating the
# message. This should not affect the return value, as the caller
# may be expecting it to match the length of the input.
b_out = b_out.replace(b"\x00", b"\xc0\x80")

self.android_log_write(self.prio, self.tag, b_out)
self.android_log_write(self.prio, self.tag,
b.replace(b"\x00", b"\xc0\x80"))

return b_len
return len(b)
Comment thread
serhiy-storchaka marked this conversation as resolved.
16 changes: 8 additions & 8 deletions Lib/test/test_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,16 @@ def write(b, lines=None, *, write_len=None):
write(b"\xf0\x9f\x98\x80")

# Null bytes are logged using "modified UTF-8".
write(b"\x00", ["\\xc0\\x80"])
write(b"a\x00", ["a\\xc0\\x80"])
write(b"\x00b", ["\\xc0\\x80b"])
write(b"a\x00b", ["a\\xc0\\x80b"])
write(b"\x00", [r"\xc0\x80"])
write(b"a\x00", [r"a\xc0\x80"])
write(b"\x00b", [r"\xc0\x80b"])
write(b"a\x00b", [r"a\xc0\x80b"])

# Invalid UTF-8
write(b"\xff", ["\\xff"])
write(b"a\xff", ["a\\xff"])
write(b"\xffb", ["\\xffb"])
write(b"a\xffb", ["a\\xffb"])
write(b"\xff", [r"\xff"])
write(b"a\xff", [r"a\xff"])
write(b"\xffb", [r"\xffb"])
write(b"a\xffb", [r"a\xffb"])

# Log entries containing newlines are shown differently by
# `logcat -v tag`, `logcat -v long`, and Android Studio. We
Expand Down