Patch for handling base64 encoding on Py 2 and 3, to address #567

This commit is contained in:
Garret Wassermann 2017-12-05 02:51:15 -05:00
parent 43035afc07
commit 32cc5e2496

View File

@ -356,10 +356,15 @@ def ticket_from_message(message, queue, logger):
if isinstance(payload, list): if isinstance(payload, list):
payload = payload.pop().as_string() payload = payload.pop().as_string()
payloadToWrite = payload payloadToWrite = payload
# check version of python to ensure use of only the correct error type
if six.PY2:
non_b64_err = binascii.Error
else:
non_b64_err = TypeError
try: try:
logger.debug("Try to base64 decode the attachment payload") logger.debug("Try to base64 decode the attachment payload")
payloadToWrite = base64.decodestring(payload) payloadToWrite = base64.decodestring(payload)
except (binascii.Error, TypeError): except non_b64_err:
logger.debug("Payload was not base64 encoded, using raw bytes") logger.debug("Payload was not base64 encoded, using raw bytes")
payloadToWrite = payload payloadToWrite = payload
files.append(SimpleUploadedFile(name, part.get_payload(decode=True), mimetypes.guess_type(name)[0])) files.append(SimpleUploadedFile(name, part.get_payload(decode=True), mimetypes.guess_type(name)[0]))