diff options
author | Wyatt Hepler <hepler@google.com> | 2023-07-07 18:37:15 +0000 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-07-07 18:37:15 +0000 |
commit | 00e82706bfd82662c3db081280e8217b2b8e547e (patch) | |
tree | c70f4554efba071defd3fa186e259c3930c7ba0c | |
parent | 3382261ffdee80d068043e5f6736fdcb5e9373f1 (diff) | |
download | pigweed-00e82706bfd82662c3db081280e8217b2b8e547e.tar.gz |
pw_hdlc: Handle sys.stdout without a buffer attribute
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>
-rw-r--r-- | pw_hdlc/py/pw_hdlc/rpc.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/pw_hdlc/py/pw_hdlc/rpc.py b/pw_hdlc/py/pw_hdlc/rpc.py index 76a6e9605..b1998830e 100644 --- a/pw_hdlc/py/pw_hdlc/rpc.py +++ b/pw_hdlc/py/pw_hdlc/rpc.py @@ -133,7 +133,14 @@ class DataReaderAndExecutor: 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() |