[PATCH 2/2] Output binary data correctly as hexa strings (#986515)
Vratislav Podzimek
vpodzime at redhat.com
Mon Jul 22 12:27:25 UTC 2013
---
meh/dump.py | 12 +++++++++---
meh/safe_string.py | 12 ++++++++++--
tests/handle_binary.py | 19 +++++++++++++++++--
tests/safe_string_test.py | 3 ++-
4 files changed, 38 insertions(+), 8 deletions(-)
diff --git a/meh/dump.py b/meh/dump.py
index 3cb2202..37a0698 100644
--- a/meh/dump.py
+++ b/meh/dump.py
@@ -352,7 +352,9 @@ class ExceptionDump(object):
ret += self._dumpClass(item, level + 1, skipList=skipList)
ret += "]\n"
elif type(value) == types.DictType:
- ret += "%s%s: {" % (pad, curkey)
+ ret += pad
+ ret += curkey
+ ret += ": {"
first = 1
for k, v in value.items():
if not first:
@@ -360,7 +362,9 @@ class ExceptionDump(object):
else:
first = 0
if type(k) == types.StringType:
- ret += "'%s': " % (k,)
+ ret += "'"
+ ret += k
+ ret += "': "
else:
ret += "%s: " % (k,)
@@ -370,7 +374,9 @@ class ExceptionDump(object):
ret += self._dumpClass(v, level + 1, parentkey = curkey, skipList=skipList)
ret += "}\n"
elif __isSimpleType(value):
- ret += "%s%s: %s\n" % (pad, curkey, value)
+ ret += "%s%s: " % (pad, curkey)
+ ret += value
+ ret += "\n"
else:
ret += "%s%s: " % (pad, curkey)
ret += self._dumpClass(value, level + 1, parentkey=curkey, skipList=skipList)
diff --git a/meh/safe_string.py b/meh/safe_string.py
index 558ebb7..d4b13fb 100644
--- a/meh/safe_string.py
+++ b/meh/safe_string.py
@@ -50,7 +50,15 @@ class SafeStr(str):
other.decode("utf-8")
ret = SafeStr(str.__add__(self, other))
except UnicodeDecodeError:
- # binary data
- ret = SafeStr(str.__add__(self, "OMITTED BINARY DATA"))
+ # binary data, get the representation used by Python for
+ # non-ascii bytes
+
+ # hex(255) returns "0xff", we want "\xff"
+ other_hexa = (hex(ord(char)) for char in other)
+ other_backslashed = (hex_num.replace("0x", "\\x")
+ for hex_num in other_hexa)
+ other_repr = "".join(other_backslashed)
+
+ ret = SafeStr(str.__add__(self, other_repr))
return ret
diff --git a/tests/handle_binary.py b/tests/handle_binary.py
index 50f44aa..4f75382 100644
--- a/tests/handle_binary.py
+++ b/tests/handle_binary.py
@@ -3,12 +3,20 @@
from tests.baseclass import BaseTestCase
from meh import Config
-BINARY_DATA = "\xff\xfe\xdd"
+BINARY_DATA = "\xff\x61\xfe\xdd"
+BINARY_DATA2 = "\xfe\x61\xff\xdd"
+BINARY_DATA3 = "\xfe\x62\xff\xdd"
class BinaryExample(object):
def __init__(self):
self.bin_data = BINARY_DATA
+ # crazy, but possible
+ self.dict = dict()
+ self.dict[BINARY_DATA2] = "aaaa"
+
+ self.list = [BINARY_DATA3]
+
class HandleBinary_TestCase(BaseTestCase):
def runTest(self):
binary_example = BinaryExample()
@@ -19,5 +27,12 @@ class HandleBinary_TestCase(BaseTestCase):
# should not raise exception
dump = self.dump(conf, binary_example)
- self.assertIn("OMITTED BINARY DATA", dump)
+ # should contain the attribute name and hexa representation of binary
+ # data ('\x61' == 'a' which shouldn't be translated)
+ self.assertIn("bin_data: \\xff\\x61\\xfe\\xdd\n", dump)
+
+ # should contain the binary-keyed dict
+ self.assertIn("dict: {'\\xfe\\x61\\xff\\xdd': aaaa", dump)
+ # should contain the list with binary item(s)
+ self.assertIn("list: [\\xfe\\x62\\xff\\xdd]", dump)
diff --git a/tests/safe_string_test.py b/tests/safe_string_test.py
index a41e289..70e7695 100755
--- a/tests/safe_string_test.py
+++ b/tests/safe_string_test.py
@@ -32,7 +32,8 @@ class SafeStr_TestCase(BaseTestCase):
# should be included twice -- appended enc_unistr and unistr
self.assertIn(2*self.enc_unistr, self.safestr)
- self.assertIn("OMITTED BINARY DATA", self.safestr)
+
+ self.assertIn("\\xff\\xff\\xfe", self.safestr)
self.assertIn(str(self.test_object), self.safestr)
self.assertIn("OMITTED OBJECT WITHOUT __str__ METHOD", self.safestr)
--
1.7.11.7
More information about the anaconda-patches
mailing list