From 3d739054b0a35ff7d815f21a3d8411930bb6d566 Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Thu, 5 Sep 2013 20:43:05 +0200
Subject: [PATCH] digest: add work-around for python 2.6

---
 kwant/digest.py | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/kwant/digest.py b/kwant/digest.py
index 014ec63..2933138 100644
--- a/kwant/digest.py
+++ b/kwant/digest.py
@@ -21,9 +21,10 @@ this module.
 
 from __future__ import division
 
-from math import pi, log, sqrt, sin, cos
+from math import pi, log, sqrt, cos
 from hashlib import md5
 from struct import unpack
+import sys
 
 __all__ = ['uniform', 'gauss', 'test']
 
@@ -34,13 +35,30 @@ BPF_MASK = 2**53 - 1
 RECIP_BPF = 2**-BPF
 
 
-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
+# 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 uniform(input, salt=''):
-- 
GitLab