From 07c35cc8bf7b89756c7a0d07f073ee6718ab1d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 1 Apr 2019 14:27:37 +0200 Subject: [PATCH] Operate on a byte level instead of utf8 strings only --- pipe_io_server/pipe_reader_thread.py | 8 ++++---- pipe_io_server/pipe_writer_thread.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pipe_io_server/pipe_reader_thread.py b/pipe_io_server/pipe_reader_thread.py index 70df33e..2687fcd 100644 --- a/pipe_io_server/pipe_reader_thread.py +++ b/pipe_io_server/pipe_reader_thread.py @@ -7,8 +7,8 @@ from .terminate_process_on_failure import terminate_process_on_failure class PipeReaderThread(Thread): - eof = '' - stop_sequence = 'stop_reading' + eof = b'' + stop_sequence = b'stop_reading' def __init__(self, pipe_path, stop_event, message_handler): super().__init__() @@ -18,7 +18,7 @@ class PipeReaderThread(Thread): @terminate_process_on_failure def run(self): - with open(self._pipe_path, 'r') as pipe: + with open(self._pipe_path, 'rb') as pipe: while True: message = pipe.readline().rstrip() if message in (self.eof, self.stop_sequence): @@ -33,5 +33,5 @@ class PipeReaderThread(Thread): def unblock(self): with suppress(OSError): fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK) - write(fd, f'{self.stop_sequence}\n'.encode()) + write(fd, self.stop_sequence + b'\n') close(fd) diff --git a/pipe_io_server/pipe_writer_thread.py b/pipe_io_server/pipe_writer_thread.py index d0e61c4..77d3518 100644 --- a/pipe_io_server/pipe_writer_thread.py +++ b/pipe_io_server/pipe_writer_thread.py @@ -17,13 +17,13 @@ class PipeWriterThread(Thread): @terminate_process_on_failure def run(self): try: - with open(self._pipe_path, 'w') as pipe: + with open(self._pipe_path, 'wb') as pipe: while True: message = self._write_queue.get(block=True) if message is None: self._stop_event.set() break - pipe.write(f'{message}\n') + pipe.write(message + b'\n') pipe.flush() except BrokenPipeError: self._stop_event.set() @@ -34,4 +34,4 @@ class PipeWriterThread(Thread): def unblock(self): self._write_queue.put(None) - open(self._pipe_path, 'r').close() + open(self._pipe_path, 'rb').close()