<s> import ufora . native . Hash as HashNative <EOL> import sys <EOL> import hashlib <EOL> import cPickle as pickle <EOL> import weakref <EOL> import sys <EOL> def getClosure ( func ) : <EOL> tr = func . __closure__ <EOL> if tr is None : <EOL> return ( ) <EOL> return [ x . cell_contents for x in tr ] <EOL> def funcEx_ ( ) : <EOL> pass <EOL> funcType_ = type ( funcEx_ ) <EOL> class WeakObjectAssociationDict : <EOL> def __init__ ( self ) : <EOL> self . weakObjectsById = weakref . WeakValueDictionary ( ) <EOL> self . weakValuesById = dict ( ) <EOL> def __setitem__ ( self , k , v ) : <EOL> self . weakObjectsById [ id ( k ) ] = k <EOL> self . weakValuesById [ id ( k ) ] = v <EOL> def __getitem__ ( self , k ) : <EOL> if id ( k ) in self . weakObjectsById : <EOL> return self . weakValuesById [ id ( k ) ] <EOL> def __contains__ ( self , k ) : <EOL> return id ( k ) in self . weakObjectsById <EOL> memoizedHashes_ = WeakObjectAssociationDict ( ) <EOL> badTypes = set ( ) <EOL> def stableHashForIterable_ ( iterable , unhashableObjectPolicy ) : <EOL> return stableHashOfHashes_ ( [ <EOL> stableShaHashForObject ( x , unhashableObjectPolicy ) for x in iterable <EOL> ] <EOL> ) <EOL> def stableHashOfHashes_ ( hashes ) : <EOL> return str ( HashNative . Hash . sha1 ( "<STR_LIT::>" . join ( hashes ) ) ) <EOL> theNoneHash_ = str ( HashNative . Hash . sha1 ( "<STR_LIT>" ) ) <EOL> class AClass : <EOL> pass <EOL> class AClass2 ( object ) : <EOL> pass <EOL> pickleablePrimitiveTypes = set ( [ <EOL> float , <EOL> int , <EOL> long , <EOL> unicode , <EOL> str , <EOL> type , <EOL> type ( AClass ) , <EOL> type ( AClass2 ) , <EOL> type ( HashNative . Hash ) , <EOL> bool <EOL> ] ) <EOL> hashFunctionsByType = { } <EOL> typesUsedByIDMemo = set ( ) <EOL> def pickleUnhashableObjects ( val ) : <EOL> return str ( HashNative . Hash . sha1 ( pickle . dumps ( val , <NUM_LIT:0> ) ) ) <EOL> def useIdForUnhashableObjects ( val ) : <EOL> try : <EOL> if val . __class__ not in typesUsedByIDMemo : <EOL> print "<STR_LIT>" , val , "<STR_LIT>" <EOL> typesUsedByIDMemo . add ( val . __class__ ) <EOL> except AttributeError : <EOL> pass <EOL> return str ( id ( val ) ) <EOL> def allObjectsShouldBeHashable ( val ) : <EOL> assert False , "<STR_LIT>" % ( val , type ( val ) ) <EOL> def stableShaHashForObject ( x , unhashableObjectPolicy = allObjectsShouldBeHashable ) : <EOL> if x in memoizedHashes_ : <EOL> return memoizedHashes_ [ x ] <EOL> assert type ( x ) not in badTypes , type ( x ) <EOL> if type ( x ) in hashFunctionsByType : <EOL> h = hashFunctionsByType [ type ( x ) ] ( x , unhashableObjectPolicy ) <EOL> memoizedHashes_ [ x ] = h <EOL> return h <EOL> if isinstance ( x , funcType_ ) : <EOL> h = stableHashForIterable_ ( <EOL> ( getClosure ( x ) , <EOL> x . func_code . co_code , <EOL> x . func_globals [ '<STR_LIT>' ] if '<STR_LIT>' in x . func_globals else <NUM_LIT:0> <EOL> ) , <EOL> unhashableObjectPolicy <EOL> ) <EOL> memoizedHashes_ [ x ] = h <EOL> return h <EOL> if isinstance ( x , tuple ) or isinstance ( x , list ) : <EOL> return stableHashOfHashes_ ( <EOL> [ stableShaHashForObject ( x , unhashableObjectPolicy ) for x in x ] <EOL> ) <EOL> elif isinstance ( x , dict ) : <EOL> return stableHashOfHashes_ ( <EOL> sorted ( [ stableShaHashForObject ( x , unhashableObjectPolicy ) for x in x . iteritems ( ) ] ) <EOL> ) <EOL> elif isinstance ( x , set ) or isinstance ( x , frozenset ) : <EOL> return stableHashOfHashes_ ( <EOL> sorted ( [ stableShaHashForObject ( elt , unhashableObjectPolicy ) for elt in x ] ) <EOL> ) <EOL> elif x is None : <EOL> return theNoneHash_ <EOL> if isinstance ( x , unicode ) : <EOL> x = str ( x ) <EOL> objectState = None <EOL> try : <EOL> objectState = ( x . __class__ . __getstate__ ( x ) , x . __class__ ) <EOL> except AttributeError : <EOL> pass <EOL> if objectState is None : <EOL> try : <EOL> objectState = ( x . __class__ , x . __class__ . __stable_hash_state__ ( x ) ) <EOL> except AttributeError : <EOL> pass <EOL> if objectState is not None : <EOL> h = stableHashForIterable_ ( objectState , unhashableObjectPolicy ) <EOL> else : <EOL> if type ( x ) in pickleablePrimitiveTypes : <EOL> h = str ( HashNative . Hash . sha1 ( pickle . dumps ( x , <NUM_LIT:0> ) ) ) <EOL> else : <EOL> h = unhashableObjectPolicy ( x ) <EOL> try : <EOL> memoizedHashes_ [ x ] = h <EOL> except TypeError : <EOL> pass <EOL> return h </s>
