From 79b1268a75be215725417178c3c38e6b56072b1e Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Sat, 26 Mar 2016 10:02:16 -0400 Subject: [PATCH] Tolerate cp437 format text UEFI output may still be cp437. Tolerate through attempting to use it. UTF-8 continues to be preferred. --- confluent_client/confluent/tlvdata.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/confluent_client/confluent/tlvdata.py b/confluent_client/confluent/tlvdata.py index 5a13f901..9676a35a 100644 --- a/confluent_client/confluent/tlvdata.py +++ b/confluent_client/confluent/tlvdata.py @@ -19,15 +19,25 @@ import confluent.tlv as tlv import json import struct +def decodestr(value): + ret = None + try: + ret = value.decode('utf-8') + except UnicodeDecodeError: + try: + ret = value.decode('cp437') + except UnicodeDecodeError: + ret = value + return ret def unicode_dictvalues(dictdata): for key in dictdata: if isinstance(dictdata[key], str): - dictdata[key] = dictdata[key].decode('utf-8') + dictdata[key] = decodestr(dictdata[key]) elif isinstance(dictdata[key], list): for i in xrange(len(dictdata[key])): if isinstance(dictdata[key][i], str): - dictdata[key][i] = dictdata[key][i].decode('utf-8') + dictdata[key][i] = decodestr(dictdata[key][i]) elif isinstance(dictdata[key][i], dict): unicode_dictvalues(dictdata[key][i]) elif isinstance(dictdata[key], dict):