Skip to content

Allow optional nbytes parameter for recv_into #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 24, 2022
15 changes: 12 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,31 @@ def recv(self, bufsize=0):
gc.collect()
return ret

def recv_into(self, buffer):
def recv_into(self, buffer, nbytes=0):
"""Read some bytes from the connected remote address into a given buffer

:param bytearray buffer: The buffer to read into
:param int nbytes: (Optional) Number of bytes to receive default is 0,
which will receive as many bytes as possible before filling the
buffer or timing out
"""

if not 0 <= nbytes <= len(buffer):
raise ValueError(
"Can only read number of bytes between 0 and length of supplied buffer"
)

stamp = time.monotonic()
to_read = len(buffer)
limit = 0 if nbytes == 0 else to_read - nbytes
received = []
while to_read > 0:
while to_read > limit:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this while loop is evaluating to false and we aren't going inside of it. I added a debugging print above this to print the values of limit and to_read: print("to_read: {} - limit: {}".format(to_read, limit))

And I'm seeing these:

to_read: 32 - limit: 32

I also tried modifying the requests library to not pass the nbytes argument and it seemed to get stuck in an infinite loop, but did eventually finish after a few minutes. I think it took much longer than I was expecting based on previous experience running the simpletest. But I'm not sure if this part is actually related to this issue since requests does pass nbytes and that is how this is intended to work as far as I understand.

# print("Bytes to read:", to_read)
avail = self.available()
if avail:
stamp = time.monotonic()
recv = _the_interface.socket_read(self._socknum, min(to_read, avail))
# received.append(recv)
received.append(recv)
start = len(buffer) - to_read
to_read -= len(recv)
end = len(buffer) - to_read
Expand Down