Odoo Python3 base64 error: TypeError: expected bytes-like object, not Text

Viewed 3445

I upgraded Odoo v11's python2 to python3. After that I edited my addons. One of my addons, i'm getting error.

result = method(recs, *args, **kwargs)
File "D:\Odoo 11.0\server\addons\addons- 
trbase\l10n_tr_account_einvoice\models\account_einvoice_provider.py", 
line 698, in action_einvoice_get_invoices
self.einvoice_get_invoices()
File "D:\Odoo 11.0\server\addons\addons- unauth\l10n_tr_account_einvoice_provider_isis\models\account_einvoice_provider.py", line 272, in einvoice_get_invoices
bytedata = base64.decodestring(result.ByteData)
File "D:\Odoo 11.0\python\lib\base64.py", line 552, in decodebytes
_input_type_check(s)
File "D:\Odoo 11.0\python\lib\base64.py", line 520, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not Text

python says that;

Deprecated alias of decodebytes(). Deprecated since version 3.1.

I'm trying decodebytes() instead of decodestring() but it's not working.

Here is my class method:

def einvoice_get_invoices(self):
    if self.provider != 'isis':
        return super(AccountEinvoiceProvider, self).einvoice_get_invoices()
    else:
        try:
            client = self.isis_get_client()
            count = 0
            while count < 50:
                count += 1
                result = client.service.GetSingleEnvelope(self.company_id.vat[2:])
                self.isis_check_error(result)
                if result.EnvelopeUUID:
                    bytedata = base64.decodestring(result.ByteData)
                    buffer = io.BytesIO(bytedata)
                    if zipfile.is_zipfile(buffer):
                        file = zipfile.ZipFile(buffer, 'r')
                        for name in file.namelist():
                            bytedata = file.read(name)
                        _logger.debug("Processing Envelope: %s" % bytedata.decode('utf-8'))
                        self.einvoice_process_envelope(bytedata)
                    else:
                        _logger.info("Invalid Zip File! EnvelopeUUID= %s" % result.EnvelopeUUID)
                else:
                    count = 50
            return True
        except WebFault as e:
            _logger.error(_('E-Invoice Provider WebService Error!') + '\n\n' + e.message)
        return False

How can I fix it?

2 Answers

I have solved a similar problem, not exactly yours.

In my case, the error was:

TypeError: expected bytes-like object, not 'str'

and the solution is to encode the (unicode) string before use it:

bytedata = base64.decodestring(result.ByteData.encode())

I don't know at all what kind of variable is result.ByteData, so I am not sure if the encode() method applies here.

I guess that you are not having this problem anymore.

This kind of problems use to happen with python 3.5, try to use python 3.6 or greater.

Related