From cae239d1dc1d379c3acdec081583a56bf5392313 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Thu, 6 Oct 2016 08:44:42 +0100 Subject: [PATCH] memoized: detect hashability by catching TypeError This is a fix for https://github.com/ARM-software/devlib/issues/60 Apprently, being an instance of Hashable does not mean that the object is, in fact, hashable. So rather than testing for hashability, try to hash the object and handle the potential error by falling back to the old method. --- devlib/utils/misc.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py index 7fdbd8c..8421a67 100644 --- a/devlib/utils/misc.py +++ b/devlib/utils/misc.py @@ -33,7 +33,6 @@ import ctypes from operator import itemgetter from itertools import groupby from functools import partial -from collections import Hashable import wrapt @@ -558,9 +557,9 @@ def __get_memo_id(obj): ID string. """ obj_id = id(obj) - if isinstance(obj, Hashable): + try: return '{}/{}'.format(obj_id, hash(obj)) - else: + except TypeError: # obj is not hashable obj_pyobj = ctypes.cast(obj_id, ctypes.py_object) # TODO: Note: there is still a possibility of a clash here. If Two # different objects get assigned the same ID, an are large and are