Change default value of read()
From `n = -1` to `n = None`, to comply with trio API
This commit is contained in:
parent
6e01a7da31
commit
5b4b65faa8
@ -8,7 +8,7 @@ class Closer(ABC):
|
||||
|
||||
class Reader(ABC):
|
||||
@abstractmethod
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
...
|
||||
|
||||
|
||||
|
@ -54,7 +54,7 @@ class MsgIOReader(ReadCloser):
|
||||
self.read_closer = read_closer
|
||||
self.next_length = None
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
return await self.read_msg()
|
||||
|
||||
async def read_msg(self) -> bytes:
|
||||
|
@ -26,22 +26,13 @@ class TrioTCPStream(ReadWriteCloser):
|
||||
await self.stream.send_all(data)
|
||||
except (trio.ClosedResourceError, trio.BrokenResourceError) as error:
|
||||
raise IOException from error
|
||||
except trio.BusyResourceError as error:
|
||||
# This should never happen, since we already access streams with read/write locks.
|
||||
raise Exception(
|
||||
"this should never happen "
|
||||
"since we already access streams with read/write locks."
|
||||
) from error
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
async with self.read_lock:
|
||||
if n == 0:
|
||||
# Checkpoint
|
||||
await trio.hazmat.checkpoint()
|
||||
if n is not None and n == 0:
|
||||
return b""
|
||||
max_bytes = n if n != -1 else None
|
||||
try:
|
||||
return await self.stream.receive_some(max_bytes)
|
||||
return await self.stream.receive_some(n)
|
||||
except (trio.ClosedResourceError, trio.BrokenResourceError) as error:
|
||||
raise IOException from error
|
||||
except trio.BusyResourceError as error:
|
||||
|
@ -20,7 +20,7 @@ class RawConnection(IRawConnection):
|
||||
except IOException as error:
|
||||
raise RawConnError(error)
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
"""
|
||||
Read up to ``n`` bytes from the underlying stream. This call is
|
||||
delegated directly to the underlying ``self.reader``.
|
||||
|
@ -37,7 +37,7 @@ class NetStream(INetStream):
|
||||
"""
|
||||
self.protocol_id = protocol_id
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
"""
|
||||
reads from stream.
|
||||
|
||||
|
@ -39,7 +39,7 @@ class InsecureSession(BaseSession):
|
||||
await self.conn.write(data)
|
||||
return len(data)
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
return await self.conn.read(n)
|
||||
|
||||
async def close(self) -> None:
|
||||
|
@ -94,7 +94,7 @@ class SecureSession(BaseSession):
|
||||
|
||||
data = self.buf.getbuffer()[self.low_watermark : self.high_watermark]
|
||||
|
||||
if n < 0:
|
||||
if n is None:
|
||||
n = len(data)
|
||||
result = data[:n].tobytes()
|
||||
self.low_watermark += len(result)
|
||||
@ -111,7 +111,7 @@ class SecureSession(BaseSession):
|
||||
self.low_watermark = 0
|
||||
self.high_watermark = len(msg)
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
if n == 0:
|
||||
return bytes()
|
||||
|
||||
|
@ -81,22 +81,23 @@ class MplexStream(IMuxedStream):
|
||||
break
|
||||
return buf
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
"""
|
||||
Read up to n bytes. Read possibly returns fewer than `n` bytes, if
|
||||
there are not enough bytes in the Mplex buffer. If `n == -1`, read
|
||||
there are not enough bytes in the Mplex buffer. If `n is None`, read
|
||||
until EOF.
|
||||
|
||||
:param n: number of bytes to read
|
||||
:return: bytes actually read
|
||||
"""
|
||||
if n < 0 and n != -1:
|
||||
if n is not None and n < 0:
|
||||
raise ValueError(
|
||||
f"the number of bytes to read `n` must be positive or -1 to indicate read until EOF"
|
||||
f"the number of bytes to read `n` must be non-negative or "
|
||||
"`None` to indicate read until EOF"
|
||||
)
|
||||
if self.event_reset.is_set():
|
||||
raise MplexStreamReset
|
||||
if n == -1:
|
||||
if n is None:
|
||||
return await self._read_until_eof()
|
||||
if len(self._buf) == 0:
|
||||
data: bytes
|
||||
|
@ -84,10 +84,7 @@ class DaemonStream(ReadWriteCloser):
|
||||
async def close(self) -> None:
|
||||
await self.stream.close()
|
||||
|
||||
async def read(self, n: int = -1) -> bytes:
|
||||
if n == -1:
|
||||
return await self.stream.receive_some()
|
||||
else:
|
||||
async def read(self, n: int = None) -> bytes:
|
||||
return await self.stream.receive_some(n)
|
||||
|
||||
async def write(self, data: bytes) -> None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user