THRIFT-1857 Python 3 Support
Client: Python
Patch: Thomas Bartelmess, Eevee (Alex Munroe), helgridly, Christian Verkerk, Jeroen Vlek, Nobuaki Sukegawa
This closes #213 and closes #680
diff --git a/lib/py/src/server/TNonblockingServer.py b/lib/py/src/server/TNonblockingServer.py
index 39486cd..79748b7 100644
--- a/lib/py/src/server/TNonblockingServer.py
+++ b/lib/py/src/server/TNonblockingServer.py
@@ -26,13 +26,14 @@
"""
import threading
import socket
-import Queue
import select
import struct
import logging
logger = logging.getLogger(__name__)
+from six.moves import queue
+
from thrift.transport import TTransport
from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
@@ -57,7 +58,7 @@
callback(True, otrans.getvalue())
except Exception:
logger.exception("Exception while processing request")
- callback(False, '')
+ callback(False, b'')
WAIT_LEN = 0
WAIT_MESSAGE = 1
@@ -104,7 +105,7 @@
self.socket.setblocking(False)
self.status = WAIT_LEN
self.len = 0
- self.message = ''
+ self.message = b''
self.lock = threading.Lock()
self.wake_up = wake_up
@@ -132,7 +133,7 @@
logger.error("empty frame, it's really strange")
self.close()
else:
- self.message = ''
+ self.message = b''
self.status = WAIT_MESSAGE
@socket_exception
@@ -162,7 +163,7 @@
sent = self.socket.send(self.message)
if sent == len(self.message):
self.status = WAIT_LEN
- self.message = ''
+ self.message = b''
self.len = 0
else:
self.message = self.message[sent:]
@@ -185,10 +186,10 @@
self.close()
self.wake_up()
return
- self.len = ''
+ self.len = 0
if len(message) == 0:
# it was a oneway request, do not write answer
- self.message = ''
+ self.message = b''
self.status = WAIT_LEN
else:
self.message = struct.pack('!i', len(message)) + message
@@ -236,7 +237,7 @@
self.out_protocol = outputProtocolFactory or self.in_protocol
self.threads = int(threads)
self.clients = {}
- self.tasks = Queue.Queue()
+ self.tasks = queue.Queue()
self._read, self._write = socket.socketpair()
self.prepared = False
self._stop = False
@@ -252,7 +253,7 @@
if self.prepared:
return
self.socket.listen()
- for _ in xrange(self.threads):
+ for _ in range(self.threads):
thread = Worker(self.tasks)
thread.setDaemon(True)
thread.start()
@@ -269,7 +270,7 @@
In this case, we can just write anything to the second socket from
socketpair.
"""
- self._write.send('1')
+ self._write.send(b'1')
def stop(self):
"""Stop the server.
@@ -290,7 +291,7 @@
"""Does select on open connections."""
readable = [self.socket.handle.fileno(), self._read.fileno()]
writable = []
- for i, connection in self.clients.items():
+ for i, connection in list(self.clients.items()):
if connection.is_readable():
readable.append(connection.fileno())
if connection.is_writeable():
@@ -332,7 +333,7 @@
def close(self):
"""Closes the server."""
- for _ in xrange(self.threads):
+ for _ in range(self.threads):
self.tasks.put([None, None, None, None, None])
self.socket.close()
self.prepared = False