
    sKg')                         d Z ddlZddlZddlZddlZddlZddlZddlZej                  Z	 G d de
      Z G d de
      Z G d de	      Z G d	 d
e      ZddZy)zb
Fast cryptographic hash of Python objects, with a special case for fast
hashing of numpy arrays.
    Nc                       e Zd ZdZd Zy)_ConsistentSetza Class used to ensure the hash of Sets is preserved
        whatever the order of its items.
    c                     	 t        |      | _        y # t        t        j                  f$ r t        d |D              | _        Y y w xY w)Nc              3   2   K   | ]  }t        |        y wNhash).0es     R/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/joblib/hashing.py	<genexpr>z*_ConsistentSet.__init__.<locals>.<genexpr>%   s     $ClT!Wls   )sorted	_sequence	TypeErrordecimalInvalidOperation)selfset_sequences     r   __init__z_ConsistentSet.__init__   sF    		E
 $L1DN7334 	E $$Cl$CDDN	Es    0AAN__name__
__module____qualname____doc__r        r   r   r      s    Er   r   c                       e Zd ZdZd Zy)_MyHashz7 Class used to hash objects that won't normally pickle c                     || _         y r   )args)r   r    s     r   r   z_MyHash.__init__+   s	    	r   Nr   r   r   r   r   r   (   s
    Ar   r   c                      e Zd ZdZd
dZddZd Zd Zdej                  fdZ
ej                  j                         Ze
e ee      <   e
e ee      <   e
e ee      <   e
e eej$                        <   d Zd	 Zee e e             <   y)HasherzW A subclass of pickler, to do cryptographic hashing, rather than
        pickling.
    c                     t        j                         | _        d}t        j	                  | | j                  |       t        j                  |      | _        y )N   )protocol)ioBytesIOstreamPicklerr   hashlibnew_hash)r   	hash_namer%   s      r   r   zHasher.__init__4   s?    jjl t{{X>[[+
r   c                 :   	 | j                  |       | j                  j                         }| j                  j                  |       |r| j                  j                         S y # t        j                  $ r"}|xj                  d|d|fz  c_         d }~ww xY w)NzPicklingError while hashing z: )	dumppicklePicklingErrorr    r(   getvaluer,   update	hexdigest)r   objreturn_digestr   dumpss        r   r	   zHasher.hash=   s    	IIcN $$&

% ::''))  ## 	FFsAFHHF	s   A% %B8BBc                    t        |t        j                  t        i j                        f      rt        |d      r|j                  j                  }n|j                  }|j                  }t        |      t        t              u rt        ||j                        }n2|t        ||      }n#|j                  j                  }t        |||      }t        j                  | |       y )N__func__)
isinstancetypes
MethodTypetypepophasattrr9   r   __self__r0   r   	__class__r)   save)r   r5   	func_nameinstclss        r   rB   zHasher.saveH   s    cE,,d266l;< sJ'LL11	LL	<<DDzT&\)i7i.ll,,is3T3r   c                 ^    t        |t        t        f      ry t        j	                  | |       y r   )r:   bytesstrr)   memoize)r   r5   s     r   rI   zHasher.memoize[   s#    
 cE3<(c"r   Nc                 X   t        ||      }|d= 	 t        j                  | |fi | y # t        j                  $ rl t        j                  | |fi | t        |dd       }|dk(  r@|}||j                  }t        j                  |   }t        ||      st        |||       Y y Y y Y y w xY w)N)namepackrL   r   __main__)dictr)   save_globalr0   r1   getattrr   sysmodulesr?   setattr)r   r5   rK   rL   kwargsmodulemy_namemods           r   rO   zHasher.save_globalf   s     4d+6N	/c4V4## 	/c4V4S,5F#?!llGkk&)sG, C#. - $	/s   * A8B)(B)c           
          	 t         j                  | t        t        |                   y # t        $ r2 t         j                  | t        t        d |D                           Y y w xY w)Nc              3   <   K   | ]  \  }}t        |      |f  y wr   r   )r
   kvs      r   r   z)Hasher._batch_setitems.<locals>.<genexpr>   s%      6IBG$!Q 8<AwlBGs   )r)   _batch_setitemsiterr   r   )r   itemss     r   r\   zHasher._batch_setitems   sf    
	K
 ##D$ve}*=> 	K ##D$v 6IBG6I 0I +J K	Ks   (+ 8A&%A&c                 B    t         j                  | t        |             y r   )r)   rB   r   )r   	set_itemss     r   save_setzHasher.save_set   s    T>)45r   )md5)T)r   r   r   r   r   r	   rB   rI   structrL   rO   r)   dispatchcopyr=   lenobjectr0   r/   r\   ra   setr   r   r   r"   r"   /   s    ,	* &# %)v{{ /* $$&H%HT#Y(HT&\)HT']"-HT&++K6 %HT#%[r   r"   c                       e Zd ZdZddZd Zy)NumpyHasherz7 Special case the hasher for when numpy is loaded.
    c                     || _         t        j                  | |       ddl}|| _        t        |d      r|j                  | _        yt        | _        y)a  
            Parameters
            ----------
            hash_name: string
                The hash algorithm to be used
            coerce_mmap: boolean
                Make no difference between np.memmap and np.ndarray
                objects.
        r-   r   N	getbuffer)	coerce_mmapr"   r   numpynpr?   rm   
_getbuffer
memoryview)r   r-   rn   rp   s       r   r   zNumpyHasher.__init__   sB     '	22{# llDO(DOr   c                    t        || j                  j                        rF|j                  j                  s/|j
                  dk(  r|j                         }nL|j                  j                  r|}n3|j                  j                  r|j                  }n|j                         }| j                  j                  | j                  |j                  | j                  j                                     | j                   r7t        || j                  j"                        r| j                  j                  }n|j$                  }|d|j                  |j
                  |j&                  ff}nyt        || j                  j                        rY| j                  j                  dj)                  d             | j                  j                  t+        j,                  |             yt.        j1                  | |       y)z Subclass the save method, to hash ndarray subclass, rather
            than pickling them. Off course, this is a total abuse of
            the Pickler class.
        r   HASHED_HASHED_DTYPEzutf-8N)r:   rp   ndarraydtype	hasobjectshapeflattenflagsc_contiguousf_contiguousTr,   r3   rq   viewuint8rn   memmaprA   stridesencoder0   r7   r"   rB   )r   r5   obj_c_contiguousklasss       r   rB   zNumpyHasher.save   s\   
 c477??+CII4G4G yyB $';;= ''#& ''#&55 
 $';;=  JJ 0 5 5dggmm DEG JsDGGNN$C 
 8SYY		3;;GHCTWW]]+  JJo44W=>JJfll3/0D#r   Nrb   F)r   r   r   r   r   rB   r   r   r   rj   rj      s    )(?r   rj   c                     d}||vrt        dj                  ||            dt        j                  v rt	        ||      }nt        |      }|j                  |       S )aj   Quick calculation of a hash to identify uniquely Python objects
        containing numpy arrays.

        Parameters
        ----------
        hash_name: 'md5' or 'sha1'
            Hashing algorithm used. sha1 is supposedly safer, but md5 is
            faster.
        coerce_mmap: boolean
            Make no difference between np.memmap and np.ndarray
    )rb   sha1zAValid options for 'hash_name' are {}. Got hash_name={!r} instead.ro   )r-   rn   rl   )
ValueErrorformatrQ   rR   rj   r"   r	   )r5   r-   rn   valid_hash_nameshashers        r   r	   r	      se     '(( 7 &!19=? 	? #++ykJ),;;sr   r   )r   r0   r*   rQ   r;   rc   r&   r   _Picklerr)   rg   r   r   r"   rj   r	   r   r   r   <module>r      si      
   	  //EV E$f h%W h%VW& Wtr   