Skip to content
Snippets Groups Projects
Commit 3d739054 authored by Christoph Groth's avatar Christoph Groth
Browse files

digest: add work-around for python 2.6

parent d5f9f776
No related branches found
No related tags found
No related merge requests found
...@@ -21,9 +21,10 @@ this module. ...@@ -21,9 +21,10 @@ this module.
from __future__ import division from __future__ import division
from math import pi, log, sqrt, sin, cos from math import pi, log, sqrt, cos
from hashlib import md5 from hashlib import md5
from struct import unpack from struct import unpack
import sys
__all__ = ['uniform', 'gauss', 'test'] __all__ = ['uniform', 'gauss', 'test']
...@@ -34,13 +35,30 @@ BPF_MASK = 2**53 - 1 ...@@ -34,13 +35,30 @@ BPF_MASK = 2**53 - 1
RECIP_BPF = 2**-BPF RECIP_BPF = 2**-BPF
def uniform2(input, salt=''): # TODO: Remove the following workaround for Python 2.6 once we do not support it
"""Return two independent [0,1)-distributed numbers.""" # anymore.
input = memoryview(input).tobytes() + salt
a, b = unpack('qq', md5(input).digest()) if sys.version_info < (2, 7):
a &= BPF_MASK def uniform2(input, salt=''):
b &= BPF_MASK """Return two independent [0,1)-distributed numbers."""
return a * RECIP_BPF, b * RECIP_BPF 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=''): def uniform(input, salt=''):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment