Skip to content

Commit

Permalink
pw_hdlc: Handle sys.stdout without a buffer attribute
Browse files Browse the repository at this point in the history
The buffer attribute is not required in TextIOBase instances. Support
wrapped versions of sys.stdout that do not offer it by deferring to
sys.__stdout__.buffer.

Change-Id: I9677da0cb16e889562f1d2737d3b275f16ac97ad
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/154496
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
  • Loading branch information
255 authored and CQ Bot Account committed Jul 7, 2023
1 parent 3382261 commit 00e8270
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pw_hdlc/py/pw_hdlc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ def run(self) -> NoReturn:
executor.submit(self._frame_handler, frame)


def write_to_file(data: bytes, output: BinaryIO = sys.stdout.buffer):
# Writes to stdout by default, but sys.stdout.buffer is not guaranteed to exist
# (see https://docs.python.org/3/library/io.html#io.TextIOBase.buffer). Defer
# to sys.__stdout__.buffer if sys.stdout is wrapped with something that does not
# offer it.
def write_to_file(
data: bytes,
output: BinaryIO = getattr(sys.stdout, 'buffer', sys.__stdout__.buffer),
) -> None:
output.write(data + b'\n')
output.flush()

Expand Down

0 comments on commit 00e8270

Please sign in to comment.