From d5cd9af7b502f8cb3b0a6917bf5cc0a741bac454 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Tue, 1 May 2018 19:58:36 +0100 Subject: [PATCH] python/samba/tests: Py2/Py3 port for hexdump Signed-off-by: Noel Power Reviewed-by: Andreas Schneider Reviewed-by: Douglas Bagnall --- python/samba/tests/__init__.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/python/samba/tests/__init__.py b/python/samba/tests/__init__.py index 7061f8b3d14..8eb41cee538 100644 --- a/python/samba/tests/__init__.py +++ b/python/samba/tests/__init__.py @@ -36,6 +36,7 @@ import re import samba.auth import samba.dcerpc.base from samba.compat import PY3, text_type +from samba.compat import string_types from random import randint if not PY3: # Py2 only @@ -50,7 +51,7 @@ except ImportError: class SkipTest(Exception): """Test skipped.""" -HEXDUMP_FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)]) +HEXDUMP_FILTER=bytearray([x if ((len(repr(chr(x)))==3) and (x < 127)) else ord('.') for x in range(256)]) class TestCase(unittest.TestCase): """A Samba test case.""" @@ -80,14 +81,21 @@ class TestCase(unittest.TestCase): def hexdump(self, src): N = 0 result = '' + is_string = isinstance(src, string_types) while src: ll = src[:8] lr = src[8:16] src = src[16:] - hl = ' '.join(["%02X" % ord(x) for x in ll]) - hr = ' '.join(["%02X" % ord(x) for x in lr]) - ll = ll.translate(HEXDUMP_FILTER) - lr = lr.translate(HEXDUMP_FILTER) + if is_string: + hl = ' '.join(["%02X" % ord(x) for x in ll]) + hr = ' '.join(["%02X" % ord(x) for x in lr]) + ll = ll.translate(HEXDUMP_FILTER) + lr = lr.translate(HEXDUMP_FILTER) + else: + hl = ' '.join(["%02X" % x for x in ll]) + hr = ' '.join(["%02X" % x for x in lr]) + ll = ll.translate(HEXDUMP_FILTER).decode('utf8') + lr = lr.translate(HEXDUMP_FILTER).decode('utf8') result += "[%04X] %-*s %-*s %s %s\n" % (N, 8*3, hl, 8*3, hr, ll, lr) N += 16 return result