Skip to content

Commit 201e925

Browse files
committed
Fix default nbytes and behavior of recv_into
1 parent 6f3d99f commit 201e925

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,24 @@ def recv(self, bufsize=0):
161161
gc.collect()
162162
return ret
163163

164-
def recv_into(self, buffer, nbytes=None):
164+
def recv_into(self, buffer, nbytes=0):
165165
"""Read some bytes from the connected remote address into a given buffer
166166
167167
:param bytearray buffer: The buffer to read into
168-
:param int nbytes: (Optional) Number of bytes to receive
169-
default is as many as possible before filling the
168+
:param int nbytes: (Optional) Number of bytes to receive default is 0,
169+
which will receive as many bytes as possible before filling the
170170
buffer or timing out
171171
"""
172172

173+
if not (0 <= nbytes <= len(buffer)):
174+
raise ValueError(
175+
"Can only read number of bytes between 0 and length of supplied buffer"
176+
)
177+
173178
stamp = time.monotonic()
174179
to_read = len(buffer)
175-
limit = 0 if nbytes is None else to_read - nbytes
176180
received = []
177-
while to_read > limit:
181+
while to_read > nbytes:
178182
# print("Bytes to read:", to_read)
179183
avail = self.available()
180184
if avail:

0 commit comments

Comments
 (0)