From fb53edbc0466e87d6e08b6ccf7d33689e6b5d6ac Mon Sep 17 00:00:00 2001 From: mhchia Date: Fri, 7 Feb 2020 18:33:15 +0800 Subject: [PATCH] Change `async def write` To return `None` instead of `int. `Writer.write` *does* write all data in all use case. --- libp2p/io/abc.py | 2 +- libp2p/io/msgio.py | 3 +-- libp2p/network/stream/net_stream.py | 4 ++-- libp2p/security/noise/transport.py | 4 ++-- libp2p/security/secio/transport.py | 3 +-- libp2p/stream_muxer/mplex/mplex.py | 4 +--- libp2p/stream_muxer/mplex/mplex_stream.py | 4 ++-- 7 files changed, 10 insertions(+), 14 deletions(-) diff --git a/libp2p/io/abc.py b/libp2p/io/abc.py index 8f2c758..265663a 100644 --- a/libp2p/io/abc.py +++ b/libp2p/io/abc.py @@ -14,7 +14,7 @@ class Reader(ABC): class Writer(ABC): @abstractmethod - async def write(self, data: bytes) -> int: + async def write(self, data: bytes) -> None: ... diff --git a/libp2p/io/msgio.py b/libp2p/io/msgio.py index 32c0d09..82c55ac 100644 --- a/libp2p/io/msgio.py +++ b/libp2p/io/msgio.py @@ -31,9 +31,8 @@ class MsgIOWriter(WriteCloser): def __init__(self, write_closer: WriteCloser) -> None: self.write_closer = write_closer - async def write(self, data: bytes) -> int: + async def write(self, data: bytes) -> None: await self.write_msg(data) - return len(data) async def write_msg(self, msg: bytes) -> None: data = encode_msg_with_length(msg) diff --git a/libp2p/network/stream/net_stream.py b/libp2p/network/stream/net_stream.py index 72d5c6a..dab1920 100644 --- a/libp2p/network/stream/net_stream.py +++ b/libp2p/network/stream/net_stream.py @@ -51,14 +51,14 @@ class NetStream(INetStream): except MuxedStreamReset as error: raise StreamReset() from error - async def write(self, data: bytes) -> int: + async def write(self, data: bytes) -> None: """ write to stream. :return: number of bytes written """ try: - return await self.muxed_stream.write(data) + await self.muxed_stream.write(data) except MuxedStreamClosed as error: raise StreamClosed() from error diff --git a/libp2p/security/noise/transport.py b/libp2p/security/noise/transport.py index a567eb2..51b00d0 100644 --- a/libp2p/security/noise/transport.py +++ b/libp2p/security/noise/transport.py @@ -27,8 +27,8 @@ class NoiseConnection(BaseSession): async def read(self, n: int = None) -> bytes: return await self.conn.read(n) - async def write(self, data: bytes) -> int: - return await self.conn.write(data) + async def write(self, data: bytes) -> None: + await self.conn.write(data) async def close(self) -> None: await self.conn.close() diff --git a/libp2p/security/secio/transport.py b/libp2p/security/secio/transport.py index 9e98873..d8216b8 100644 --- a/libp2p/security/secio/transport.py +++ b/libp2p/security/secio/transport.py @@ -135,9 +135,8 @@ class SecureSession(BaseSession): raise DecryptionFailedException() from e return decrypted_msg - async def write(self, data: bytes) -> int: + async def write(self, data: bytes) -> None: await self.write_msg(data) - return len(data) async def write_msg(self, msg: bytes) -> None: encrypted_data = self.local_encrypter.encrypt(msg) diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index 4f62e15..defe0b8 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -160,7 +160,7 @@ class Mplex(IMuxedConn): return await self.write_to_stream(_bytes) - async def write_to_stream(self, _bytes: bytes) -> int: + async def write_to_stream(self, _bytes: bytes) -> None: """ writes a byte array to a secured connection. @@ -174,8 +174,6 @@ class Mplex(IMuxedConn): "failed to write message to the underlying connection" ) from e - return len(_bytes) - async def handle_incoming(self) -> None: """Read a message off of the secured connection and add it to the corresponding message buffer.""" diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index 7967574..9c724cc 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -134,7 +134,7 @@ class MplexStream(IMuxedStream): self._buf = self._buf[len(payload) :] return bytes(payload) - async def write(self, data: bytes) -> int: + async def write(self, data: bytes) -> None: """ write to stream. @@ -147,7 +147,7 @@ class MplexStream(IMuxedStream): if self.is_initiator else HeaderTags.MessageReceiver ) - return await self.muxed_conn.send_message(flag, data, self.stream_id) + await self.muxed_conn.send_message(flag, data, self.stream_id) async def close(self) -> None: """Closing a stream closes it for writing and closes the remote end for