Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

read from TMemoryBuffer object immediately after writing to it #20

Merged
merged 1 commit into from
Jul 25, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions thriftpy/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,15 @@ def __init__(self, type=UNKNOWN, message=None):


class TMemoryBuffer(TTransportBase):
"""Wraps a BytesIO object as a TTransport.

NOTE: Unlike the C++ version of this class, you cannot write to it
then immediately read from it.
If you want to read from a TMemoryBuffer, you must either pass
a string to the constructor.
TODO(dreiss): Make this work like the C++ version.
"""
"""Wraps a BytesIO object as a TTransport."""

def __init__(self, value=None):
"""value -- a value to read from for stringio
"""value -- a value as the initial value in the BytesIO object.

If value is set, this will be a transport for reading,
otherwise, it is for writing
If value is set, the transport can be read first.
"""
self._buffer = BytesIO(value) if value is not None else BytesIO()
self._pos = 0

def isOpen(self):
return not self._buffer.closed
Expand All @@ -77,10 +70,15 @@ def close(self):
self._buffer.close()

def read(self, sz):
return self._buffer.read(sz)
return self._read(sz)

def _read(self, sz):
return self._buffer.read(sz)
orig_pos = self._buffer.tell()
self._buffer.seek(self._pos)
res = self._buffer.read(sz)
self._buffer.seek(orig_pos)
self._pos += len(res)
return res

def write(self, buf):
self._buffer.write(buf)
Expand All @@ -93,6 +91,7 @@ def getvalue(self):

def setvalue(self, value):
self._buffer = BytesIO(value)
self._pos = 0


class TBufferedTransport(TTransportBase):
Expand Down