]> jfr.im git - z_archive/twitter.git/commitdiff
Add comments detailing why we can avoid handling certain edge cases in the Twitter...
authorAndrew W. Donoho <redacted>
Wed, 29 Jan 2014 15:30:41 +0000 (09:30 -0600)
committerAndrew W. Donoho <redacted>
Wed, 29 Jan 2014 15:30:41 +0000 (09:30 -0600)
twitter/stream.py

index 96e92d739d229580c8876036b8f3768f59a91957..44fd1052459106bf9f0c71878c62d855a25655ff 100644 (file)
@@ -26,12 +26,16 @@ def recv_chunk(sock):  # -> bytearray:
 
         chunk = bytearray(remaining)
 
-        if end < remaining:
+        if remaining <= 2:  # E.g. an HTTP chunk with just a keep-alive delimiter.
+            chunk[:remaining] = buf[start:start + remaining]
+        # There are several edge cases (remaining == [3-6]) as the chunk size exceeds the length
+        # of the initial read of 8 bytes. With Twitter, these do not, in practice, occur. The
+        # shortest real message JSON starts with '{"limit":{'. Hence, it exceeds in size the
+        # edge cases and we do not need to address them.
+        else:  # There is more to read in the chunk.
             chunk[:end] = buf[start:]
             chunk[end:] = sock.recv(remaining - end)
             sock.recv(2)  # Read the trailing CRLF pair. Throw it away.
-        else:  # E.g. an HTTP chunk with just a keep-alive delimiter.
-            chunk[:remaining] = buf[start:start + remaining]
 
         return chunk
 
@@ -76,8 +80,7 @@ class TwitterJSONIter(object):
                         buf += recv_chunk(sock).decode('utf-8')  # This is a non-blocking read.
                         if time.time() - timer > self.timeout:
                             yield {'timeout': True}
-                    else:
-                        yield {'timeout': True}
+                    else: yield {'timeout': True}
                 else:
                     buf += recv_chunk(sock).decode('utf-8')
                 if not buf and self.block: