Skip to content

Fix 5.3.1 by providing find and json in one big chunk #33

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 1 commit into from
Sep 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,31 @@ def _recv_into(self, buf, size=0):
return read_size
return self.socket.recv_into(buf, size)

@staticmethod
def _find(buf, needle, start, end):
if hasattr(buf, "find"):
return buf.find(needle, start, end)
result = -1
i = start
while i < end:
j = 0
while j < len(needle) and i + j < end and buf[i + j] == needle[j]:
j += 1
if j == len(needle):
result = i
break
i += 1

return result

def _readto(self, first, second=b""):
buf = self._receive_buffer
end = self._received_length
while True:
firsti = buf.find(first, 0, end)
firsti = self._find(buf, first, 0, end)
secondi = -1
if second:
secondi = buf.find(second, 0, end)
secondi = self._find(buf, second, 0, end)

i = -1
needle_len = 0
Expand Down Expand Up @@ -318,7 +335,12 @@ def json(self):
if not self._raw:
self._raw = _RawResponse(self)

obj = json.load(self._raw)
try:
obj = json.load(self._raw)
except OSError:
# <5.3.1 doesn't piecemeal load json from any object with readinto so load the whole
# string.
obj = json.loads(self._raw.read())
if not self._cached:
self._cached = obj
self.close()
Expand Down