3
QfX#                 @   s   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mZ ddl	Z	ej
d dkZerbejZnejZG dd deZG dd	 d	eZG d
d deZG dd deZdddZdS )zb
Fast cryptographic hash of Python objects, with a special case for fast
hashing of numpy arrays.
    N   )_bytes_or_unicode3c               @   s   e Zd ZdZdd ZdS )_ConsistentSetza Class used to ensure the hash of Sets is preserved
        whatever the order of its items.
    c             C   s   t || _d S )N)sortedZ	_sequence)selfZset_sequence r   U/home/psgendb/BIRCHDEV/pkg/SPAdes-3.15.4/linux-x86_64/share/spades/joblib3/hashing.py__init__    s    z_ConsistentSet.__init__N)__name__
__module____qualname____doc__r
   r   r   r   r	   r      s   r   c               @   s   e Zd ZdZdd ZdS )_MyHashz7 Class used to hash objects that won't normally pickle c             G   s
   || _ d S )N)args)r   r   r   r   r	   r
   '   s    z_MyHash.__init__N)r   r   r   r   r
   r   r   r   r	   r   $   s   r   c               @   s   e Zd ZdZdddZdddZdd	 Zd
d Zdej	fddZ
ejj Ze
eee< e
eee< e
eee< e
eeej< dd Zdd Zeeee < dS )HasherzW A subclass of pickler, to do cryptographic hashing, rather than
        pickling.
    md5c             C   s<   t j | _trtjntj}tj| | j|d t	j
|| _d S )N)protocol)ioBytesIOstreamPY3pickleDEFAULT_PROTOCOLHIGHEST_PROTOCOLPicklerr
   hashlibnew_hash)r   	hash_namer   r   r   r	   r
   0   s
    

zHasher.__init__Tc             C   sn   y| j | W n6 tjk
rD } ztjd||f  W Y d d }~X nX | jj }| jj| |rj| jj	 S d S )Nz"PicklingError while hashing %r: %r)
dumpr   PicklingErrorwarningswarnr   getvaluer   update	hexdigest)r   objZreturn_digestedumpsr   r   r	   hash:   s    $
zHasher.hashc             C   s   t |tjti jfr|t|dr*|jj}n|j}|j}t|tt	krTt
||j}n(|d krht
||}n|jj}t
|||}tj| | d S )N__func__)
isinstancetypes
MethodTypetypepophasattrr+   r   __self__r   r   	__class__r   save)r   r'   	func_nameinstclsr   r   r	   r4   D   s    

zHasher.savec             C   s   t |trd S tj| | d S )N)r,   r   r   memoize)r   r'   r   r   r	   r8   W   s    
zHasher.memoizeNc             C   s   t ||d}tjdkr|d= ytj| |f| W nl tjk
r   tj| |f| t|dd }|dkr|}|d krx|j}tj	| }t
||st||| Y nX d S )N)namepack      r:   r   __main__)r;   r<   )dictsysversion_infor   save_globalr   r!   getattrr   modulesr1   setattr)r   r'   r9   r:   kwargsmoduleZmy_namemodr   r   r	   rA   b   s    


zHasher.save_globalc             C   s   t j| tt| d S )N)r   _batch_setitemsiterr   )r   itemsr   r   r	   rH      s    zHasher._batch_setitemsc             C   s   t j| t| d S )N)r   r4   r   )r   Z	set_itemsr   r   r	   save_set   s    zHasher.save_set)r   )T)r   r   r   r   r
   r*   r4   r8   structr:   rA   r   dispatchcopyr/   lenobjectr   r    rH   rK   setr   r   r   r	   r   +   s   




r   c               @   s"   e Zd ZdZd	ddZdd ZdS )
NumpyHasherz7 Special case the hasher for when numpy is loaded.
    r   Fc             C   s@   || _ tj| |d ddl}|| _t|dr6|j| _nt| _dS )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npr1   rS   
_getbuffer
memoryview)r   r   rT   rV   r   r   r	   r
      s    


zNumpyHasher.__init__c             C   s   t || jjr|jj ry$|j| jj}| jj| j	| W n< t
tfk
rx   |j j| jj}| jj| j	| Y nX | jrt || jjr| jj}n|j}|d|j|j|jff}n"t || jjr|j}|d|jff}tj| | dS )z Subclass the save method, to hash ndarray subclass, rather
            than pickling them. Off course, this is a total abuse of
            the Pickler class.
        ZHASHEDN)r,   rV   ZndarrayZdtypeZ	hasobjectviewZuint8r   r%   rW   
ValueErrorBufferErrorZflattenrT   Zmemmapr3   shapestridesZdescrr   r4   )r   r'   Zobj_bytes_viewklassr   r   r	   r4      s    
zNumpyHasher.saveN)r   F)r   r   r   r   r
   r4   r   r   r   r	   rR      s   
rR   r   Fc             C   s,   dt jkrt||d}n
t|d}|j| S )al   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
    rU   )r   rT   )r   )r?   rC   rR   r   r*   )r'   r   rT   Zhasherr   r   r	   r*      s    

r*   )r   F)r   r"   r   r   r?   r-   rL   _compatr   r   versionr   _Picklerr   rP   r   r   r   rR   r*   r   r   r   r	   <module>   s"   bQ