(self, startpos)
| 49 | return len(self.buf) > 0 |
| 50 | |
| 51 | def parse_array(self, startpos): |
| 52 | assert(self.inbuf[startpos] == ord('*')) |
| 53 | endrange = self.inbuf[startpos+1:].find(ord('\r')) + 1 + startpos |
| 54 | assert(endrange > 0) |
| 55 | numargs = int(self.inbuf[startpos+1:endrange]) |
| 56 | if numargs == -1: # Nil array, used in some returns |
| 57 | startpos = endrange + 2 |
| 58 | return startpos, [] |
| 59 | assert(numargs > 0) |
| 60 | args = list() |
| 61 | startpos = endrange + 2 # plus 1 gets us to the '\n' and the next gets us to the start char |
| 62 | |
| 63 | while len(args) < numargs: |
| 64 | # We're parsing entries of the form "$N\r\nnnnnnn\r\n" |
| 65 | if startpos >= len(self.inbuf): |
| 66 | return # Not the full response |
| 67 | if self.inbuf[startpos] == ord('*'): |
| 68 | startpos, arr = self.parse_array(startpos) |
| 69 | args.append(arr) |
| 70 | else: |
| 71 | assert(self.inbuf[startpos] == ord('$')) |
| 72 | startpos = startpos + 1 |
| 73 | endrange = self.inbuf[startpos:].find(b'\r') |
| 74 | if endrange < 0: |
| 75 | return |
| 76 | endrange += startpos |
| 77 | assert(endrange <= len(self.inbuf)) |
| 78 | length = int(self.inbuf[startpos:endrange]) |
| 79 | if length < 0: |
| 80 | return |
| 81 | startpos = endrange + 2 |
| 82 | assert((startpos + length) <= len(self.inbuf)) |
| 83 | assert(self.inbuf[startpos+length] == ord('\r')) |
| 84 | assert(self.inbuf[startpos+length+1] == ord('\n')) |
| 85 | args.append(self.inbuf[startpos:(startpos+length)]) |
| 86 | startpos += length + 2 |
| 87 | assert(len(args) == numargs) |
| 88 | return startpos, args |
| 89 | |
| 90 | def parse_response(self): |
| 91 | if len(self.inbuf) == 0: |
no test coverage detected