From: Aditya Agarwal Date: Mon, 30 Jul 2007 23:58:37 +0000 (+0000) Subject: -- Adding TMemoryBuffer.py X-Git-Tag: 0.2.0~1288 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=0dd0916f9a519f9664c416d9d6b6885560299b1f;p=common%2Fthrift.git -- Adding TMemoryBuffer.py Summary: Submitted by Ben Maurer Reviewed By: mcslee, aditya git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665184 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py index 305c10bf..3c18221e 100644 --- a/lib/py/src/transport/TTransport.py +++ b/lib/py/src/transport/TTransport.py @@ -112,6 +112,40 @@ class TBufferedTransport(TTransportBase): self.__trans.flush() self.__buf = StringIO() +class TMemoryBuffer(TTransportBase): + """Wraps a string object as a TTransport""" + + def __init__(self, value=None): + """value -- a value to read from for stringio + + If value is set, this will be a transport for reading, + otherwise, it is for writing""" + if value is not None: + self._buffer = StringIO(value) + else: + self._buffer = StringIO() + + def isOpen(self): + return not self._buffer.closed + + def open(self): + pass + + def close(self): + self._buffer.close() + + def read(self, sz): + return self._buffer.read(sz) + + def write(self, buf): + self._buffer.write(buf) + + def flush(self): + pass + + def getvalue(self): + return self._buffer.getvalue() + class TFramedTransportFactory: """Factory transport that builds framed transports"""