Skip to content

Commit

Permalink
Merge pull request #33 from tannewt/fix_5.3.1
Browse files Browse the repository at this point in the history
Fix 5.3.1 by providing find and json in one big chunk
  • Loading branch information
tannewt authored Sep 22, 2020
2 parents 2a2c20e + 93030cd commit 9aaf781
Showing 1 changed file with 25 additions and 3 deletions.
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

0 comments on commit 9aaf781

Please sign in to comment.