From d9c47b62c3ff87bf5136ca96f6ba833d57de50c8 Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph.weston@cea.fr> Date: Fri, 7 Aug 2015 16:10:25 +0200 Subject: [PATCH] update digest --- kwant/digest.py | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/kwant/digest.py b/kwant/digest.py index ecc90f9d..7d2d16d9 100644 --- a/kwant/digest.py +++ b/kwant/digest.py @@ -35,30 +35,22 @@ BPF_MASK = 2**53 - 1 RECIP_BPF = 2**-BPF -# TODO: Remove the following workaround for Python 2.6 once we do not support it -# anymore. - -if sys.version_info < (2, 7): - def uniform2(input, salt=''): - """Return two independent [0,1)-distributed numbers.""" - try: - input = bytes(buffer(input)) + salt - except TypeError: - # Tinyarray does not provide the old buffer protocol, so buffer does - # not work. However, bytearray does work! - input = bytearray(input) + salt - a, b = unpack('qq', md5(input).digest()) - a &= BPF_MASK - b &= BPF_MASK - return a * RECIP_BPF, b * RECIP_BPF -else: - def uniform2(input, salt=''): - """Return two independent [0,1)-distributed numbers.""" - input = memoryview(input).tobytes() + salt - a, b = unpack('qq', md5(input).digest()) - a &= BPF_MASK - b &= BPF_MASK - return a * RECIP_BPF, b * RECIP_BPF +def str_to_bytes(s): + """Return bytes if the input is a string, else return the object as-is.""" + try: + return s.encode('utf8') + except AttributeError: + return s + +def uniform2(input, salt=''): + """Return two independent [0,1)-distributed numbers.""" + input = str_to_bytes(input) + salt = str_to_bytes(salt) + input = memoryview(input).tobytes() + salt + a, b = unpack('qq', md5(input).digest()) + a &= BPF_MASK + b &= BPF_MASK + return a * RECIP_BPF, b * RECIP_BPF def uniform(input, salt=''): -- GitLab