]> jfr.im git - z_archive/twitter.git/commitdiff
Fix Python 3.x support.
authorMike Verdone <redacted>
Tue, 25 Feb 2014 16:19:50 +0000 (17:19 +0100)
committerMike Verdone <redacted>
Tue, 25 Feb 2014 16:19:50 +0000 (17:19 +0100)
twitter/stream.py

index 19ee3851f430227ea7132eaadd37ed9b948eb78b..c7d929235dbb5bfa2df745824cf53a54628cd1c8 100644 (file)
@@ -12,10 +12,10 @@ import sys, select, time
 
 from .api import TwitterCall, wrap_response, TwitterHTTPError
 
-CRLF = b'\r\n'
-
 PY_3_OR_HIGHER = sys.version_info >= (3, 0)
 
+CRLF = b'\r\n'
+
 Timeout = {'timeout': True}
 Hangup = {'hangup': True}
 HeartbeatTimeout = {'heartbeat_timeout': True, 'hangup': True}
@@ -26,6 +26,8 @@ class ChunkDecodeError(Exception):
 class EndOfStream(Exception):
     pass
 
+range = range if PY_3_OR_HIGHER else xrange
+
 class SocketShim(io.IOBase):
     """
     Adapts a raw socket to fit the IO protocol.
@@ -40,7 +42,7 @@ class SocketShim(io.IOBase):
         return self.sock.recv_into(buf)
 
 def recv_chunk(reader): # -> bytearray:
-    for headerlen in xrange(12):
+    for headerlen in range(12):
         header = reader.peek(headerlen)[:headerlen]
         if header.endswith(CRLF):
             break
@@ -97,7 +99,7 @@ class TwitterJSONIter(object):
 
     def __iter__(self):
         actually_block = self.block and not self.timeout
-        sock_timeout = min(self.timeout, self.heartbeat_timeout) if actually_block else None
+        sock_timeout = min(self.timeout or 1000000, self.heartbeat_timeout) if actually_block else None
         sock = self.handle.fp.raw._sock if PY_3_OR_HIGHER else self.handle.fp._sock.fp._sock
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
         sock.setblocking(actually_block)