4

I am trying out Apache Thrift using python 3.4 which Thrift seems to have support, since it has lib.linux-x86_64-3.4 under build directory. But I am keeping getting this error message

  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/transport/TTransport.py", line 163, in write
self.__wbuf.write(buf)
TypeError: string argument expected, got 'bytes'

Anybody knows what's going on, and possibly with a solution?

Here below is the relevant code

socket = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Algo.Client(protocol)
transport.open()
ping_req = PingRequest()
ping_resp = client.ping(ping_req)

the whole Traceback:

  File "py3client.py", line 36, in <module>
    ping_resp = client.ping(ping_req)
  File "/home/qunzi/Projects/test/sample_Test/py3.4_thrift/ib/Algo.py", line 66, in ping
    self.send_ping(request)
  File "/home/qunzi/Projects/test/sample_Test/py3.4_thrift/ib/Algo.py", line 70, in send_ping
    self._oprot.writeMessageBegin('ping', TMessageType.CALL, self._seqid)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/protocol/TBinaryProtocol.py", line 46, in writeMessageBegin
    self.writeI32(TBinaryProtocol.VERSION_1 | type)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/protocol/TBinaryProtocol.py", line 111, in writeI32
    self.trans.write(buff)
  File "/home/qunzi/Downloads/thrift-0.9.2/lib/py/build/lib.linux-x86_64-3.4/thrift/transport/TTransport.py", line 163, in write
    self.__wbuf.write(buf)
TypeError: string argument expected, got 'bytes'
JensG
  • 12,102
  • 4
  • 40
  • 51
Allanqunzi
  • 3,038
  • 1
  • 21
  • 49
  • Under what circumstance do you receive that error? Is that the full trace? – selllikesybok Aug 07 '15 at 04:09
  • No, that's not the full trace. I am just simply calling `ping`, they are defined in `thrift` file as empty structs `struct PingRequest {}`, `struct PingResponse {}`, and `PingResponse ping (1:required PingRequest request);` – Allanqunzi Aug 07 '15 at 04:14
  • `string` and `binary` have a lot of common in Thrift, mostly for historical reasons. That still does not explain the issue, but narrows it down. If nobody comes up with a solution or advice, I'd recommend to throw that into the mailing list. Maybe you get an answer there. – JensG Aug 07 '15 at 18:27

1 Answers1

2

Although the question is rather dead, it looks like I have an answer :)

The current python generator of thrift (as of 0.9.3) generates code, specific to python2. At least I've just switched the gears and the code which gave me the same error, now works like a charm.

The error is caused by (implicit) treating of all string-like objects within the library like byte objects. However, in Python3 StringIO class expects char-oriented strings (str) and those byte-oriented strings cause the exception.

One should file a bug to the Thrift tracker, and for now he/she should use Python2 instead.

user3159253
  • 14,913
  • 3
  • 24
  • 43