impyla: TypeError: can't concat bytes to str (Python 3.5, thrift-sasl with pure-sasl)

Due to issue #237 , I removed sasl 0.2.1 and replaced it with pure-sasl 0.3.0 based on this commit by @nonsleepr : https://github.com/cloudera/impyla/commit/5e386d6495b580ae88efd81dd0f8927af5157bb8

but then I got below error:

>>> conn = connect(host='<host>', port=21050, database='<db>', auth_mechanism='PLAIN', user='<user>', password='<pass>')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/python/miniconda3/lib/python3.5/site-packages/impala/dbapi.py", line 147, in connect
    auth_mechanism=auth_mechanism)
  File "/tmp/python/miniconda3/lib/python3.5/site-packages/impala/hiveserver2.py", line 758, in connect
    transport.open()
  File "/tmp/python/miniconda3/lib/python3.5/site-packages/thrift_sasl-0.2.1-py3.5.egg/thrift_sasl/__init__.py", line 75, in open
  File "/tmp/python/miniconda3/lib/python3.5/site-packages/thrift_sasl-0.2.1-py3.5.egg/thrift_sasl/__init__.py", line 94, in _send_message
TypeError: can't concat bytes to str

I am using:

  • CDH 5.8 / 5.9
  • Anaconda Python 3.5
  • impyla 0.14.0
  • bitarray 0.8.1
  • thriftpy 0.3.9
  • six 1.10.0
  • thrift-sasl 0.2.1 (installed via pip with ‘–no-deps’ option)
  • pure-sasl 0.3.0

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 17 (3 by maintainers)

Commits related to this issue

Most upvoted comments

By changing line 94 in thrift_sasl/__init__.py to

print('Type of header:' + str(type(header)))
print('Type of body:' + str(type(body)))
self._trans.write(header + body.encode())

The output shows that on the first time _send_message is called body is passed as str. The second time it is passed correctly as bytes

Type of header:<class 'bytes'>
Type of body:<class 'str'>
Type of header:<class 'bytes'>
Type of body:<class 'bytes'>

Here’s a fix for the problem After installing:

  • thrift-sasl 0.2.1 (installed via pip with ‘–no-deps’ option)
  • pure-sasl 0.3.0

Go to directory <site-packages path>\thrift_sasl Remove __pycache__ if exists Append below codes before line 94 self._trans.write(header + body) in __init__.py:

    if (type(body) is str):
      body = body.encode()
modify thrift_sasl\__init__.py
...
  def _send_message(self, status, body):
    header = struct.pack(">BI", status, len(body))
------
#add code
    if (type(body) is str):
      body = body.encode()
------
    self._trans.write(header + body)
    self._trans.flush()

it works for me

  • python3.7
  • thrift-sasl==0.4a1
  • thriftpy2==0.4.0
  • pure-sasl==0.6.1
  • impyla==0.15.0