From 9a0725a6d2f769477ed3724f4e0595f1cab3a0b5 Mon Sep 17 00:00:00 2001 From: Derek Wisong Date: Sat, 1 Oct 2016 11:10:25 -0400 Subject: [PATCH] fix transfer of large data Data larger than the max integer value would fail to transfer. changed size of message read from the IPC protocol from an int to an unsigned int. Also changed to load large data in chunks since stream.read() does not support values larger than max int. --- qpython/qreader.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/qpython/qreader.py b/qpython/qreader.py index 3a602e3..c43b193 100644 --- a/qpython/qreader.py +++ b/qpython/qreader.py @@ -167,7 +167,7 @@ def read_header(self, source = None): # skip 1 byte self._buffer.skip() - message_size = self._buffer.get_int() + message_size = self._buffer.get_uint() return QMessage(None, message_type, message_size, message_compressed) @@ -384,8 +384,28 @@ def _read_bytes(self, length): if length == 0: return b'' - else: + if length <= sys.maxint: data = self._stream.read(length) + else: + try: + from cStringIO import StringIO + except: + from StringIO import StringIO + + # for large messages, read from the stream in chunks + remaining = length + buff = StringIO() + + while remaining > 0: + chunk = self._stream.read(min(remaining, 2048)) + + if chunk: + remaining = remaining - len(chunk) + buff.write(chunk) + else: + break + + data = buff.getvalue() if len(data) == 0: raise QReaderException('Error while reading data') @@ -491,6 +511,15 @@ def get_byte(self): return self.get('b') + def get_uint(self): + ''' + Gets a single unsigned 32-bit integer from the buffer. + + :returns: single unsigned integer + ''' + return self.get('I') + + def get_int(self): ''' Gets a single 32-bit integer from the buffer.