Skip to content
Snippets Groups Projects
Commit db86e6f7 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Python 2/3 issues with memoize and unicode should be sorted

parent 482924ac
No related branches found
No related tags found
No related merge requests found
...@@ -55,14 +55,14 @@ def memoize(func): ...@@ -55,14 +55,14 @@ def memoize(func):
try: try:
result = cache[key] result = cache[key]
log.debug('Retrieved from cache[{}]: {}'.format(key, result)) log.debug(u'Retrieved from cache[{}]: {}'.format(key, result))
except KeyError: except KeyError:
result = func(*a, **kwa) result = func(*a, **kwa)
cache[key] = result cache[key] = result
log.debug('Adding to cache[{}]: {}'.format(key, result)) log.debug(u'Adding to cache[{}]: {}'.format(key, result))
return result return result
return wrapper return wrapper
...@@ -82,8 +82,15 @@ def memoizeMD5(func): ...@@ -82,8 +82,15 @@ def memoizeMD5(func):
hashobj = hashlib.md5() hashobj = hashlib.md5()
# Convert each arg to a string
# representation, then encode
# it into a sequence of (utf-8
# compatible) bytes , and take
# the hash of those bytes.
for arg in args: for arg in args:
arg = six.u(str(arg)).encode('utf-8') if not isinstance(arg, six.string_types):
arg = str(arg)
arg = arg.encode('utf-8')
hashobj.update(arg) hashobj.update(arg)
digest = hashobj.hexdigest() digest = hashobj.hexdigest()
...@@ -94,7 +101,7 @@ def memoizeMD5(func): ...@@ -94,7 +101,7 @@ def memoizeMD5(func):
result = func(*args, **kwargs) result = func(*args, **kwargs)
log.debug('Adding to MD5 cache[{}]: {}'.format( log.debug(u'Adding to MD5 cache[{}]: {}'.format(
digest, result)) digest, result))
cache[digest] = result cache[digest] = result
......
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