o
    —ôjÑ)  ã                   @   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Z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.
é    Nc                   @   ó   e Zd ZdZdd„ ZdS )Ú_ConsistentSetz\Class used to ensure the hash of Sets is preserved
    whatever the order of its items.
    c              	   C   s@   zt |ƒ| _W d S  ttjfy   t dd„ |D ƒƒ| _Y d S w )Nc                 s   s   � | ]}t |ƒV  qd S ©N©Úhash)Ú.0Úe© r	   úa/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/joblib/hashing.pyÚ	<genexpr>%   s   € z*_ConsistentSet.__init__.<locals>.<genexpr>)ÚsortedZ	_sequenceÚ	TypeErrorÚdecimalÚInvalidOperation)ÚselfÚset_sequencer	   r	   r
   Ú__init__   s
   ýz_ConsistentSet.__init__N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r	   r	   r
   r      s    r   c                   @   r   )Ú_MyHashz5Class used to hash objects that won't normally picklec                 G   s
   || _ d S r   )Úargs)r   r   r	   r	   r
   r   +   s   
z_MyHash.__init__Nr   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 ¡ 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 )ÚHasherzÉA subclass of pickler, to do cryptographic hashing, rather than
    pickling. This is used to produce a unique hash of the given
    Python object that is not necessarily cryptographically secure.
    Úmd5c                 C   s4   t  ¡ | _d}tj| | j|d� tj|dd�| _d S )Né   )ÚprotocolF)Úusedforsecurity)ÚioÚBytesIOÚstreamÚPicklerr   ÚhashlibÚnewÚ_hash)r   Ú	hash_namer   r	   r	   r
   r   5   s   
zHasher.__init__Tc              
   C   sl   z|   |¡ W n tjy! } z| jd||f f7  _‚ d }~ww | j ¡ }| j |¡ |r4| j ¡ S d S )Nz"PicklingError while hashing %r: %r)	ÚdumpÚpickleÚPicklingErrorr   r!   Úgetvaluer%   ÚupdateÚ	hexdigest)r   ÚobjZreturn_digestr   Údumpsr	   r	   r
   r   >   s   €þ

ÿzHasher.hashc                 C   sˆ   t |tjtjfƒr<t|dƒr|jj}n|j}|j}t|ƒtt	ƒu r(t
||jƒ}n|d u r2t
||ƒ}n
|jj}t
|||ƒ}t | |¡ d S )NÚ__func__)Ú
isinstanceÚtypesÚ
MethodTypeÚBuiltinMethodTypeÚhasattrr/   r   Ú__self__Útyper(   r   Ú	__class__r"   Úsave)r   r-   Ú	func_nameÚinstÚclsr	   r	   r
   r8   I   s   

zHasher.savec                 C   s"   t |ttfƒr	d S t | |¡ d S r   )r0   ÚbytesÚstrr"   Úmemoize)r   r-   r	   r	   r
   r>   \   s   zHasher.memoizeNc                 C   sª   t ||d�}|d= ztj| |fi |¤Ž W d S  tjyT   tj| |fi |¤Ž t|dd ƒ}|dkrN|}|d u r;|j}tj| }t	||ƒsQt
|||ƒ Y d S Y d S Y d S w )N)ÚnameÚpackr@   r   Ú__main__)Údictr"   Úsave_globalr(   r)   Úgetattrr   ÚsysÚmodulesr4   Úsetattr)r   r-   r?   r@   ÚkwargsÚmoduleÚmy_nameÚmodr	   r	   r
   rC   g   s"   

øøzHasher.save_globalc              	   G   s^   zt j| tt|ƒƒg|¢R Ž  W d S  ty.   t j| ttdd„ |D ƒƒƒg|¢R Ž  Y d S w )Nc                 s   s    � | ]\}}t |ƒ|fV  qd S r   r   )r   ÚkÚvr	   r	   r
   r   ”   s   € z)Hasher._batch_setitems.<locals>.<genexpr>)r"   Ú_batch_setitemsÚiterr   r   )r   Úitemsr   r	   r	   r
   rN   ˆ   s   "ÿÿýzHasher._batch_setitemsc                 C   s   t  | t|ƒ¡ d S r   )r"   r8   r   )r   Z	set_itemsr	   r	   r
   Úsave_set—   s   zHasher.save_set)r   )T)r   r   r   r   r   r   r8   r>   Ústructr@   rC   r"   ÚdispatchÚcopyr6   ÚlenÚobjectr(   r'   rN   rQ   Úsetr	   r	   r	   r
   r   /   s    

	
r   c                   @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
ÚNumpyHasherz1Special case the hasher for when numpy is loaded.r   Fc                 C   sB   || _ tj| |d� ddl}|| _t|dƒr|j| _dS t| _dS )zå
        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Únpr4   rZ   Ú
_getbufferÚ
memoryview)r   r&   r[   r]   r	   r	   r
   r   ¡   s   


zNumpyHasher.__init__c                 C   sî   t || jjƒrT|jjsT|jdkr| ¡ }n|jjr|}n|jj	r$|j
}n| ¡ }| j |  | | jj¡¡¡ | jrEt || jjƒrE| jj}n|j}|d|j|j|jff}nt || jjƒro| j d d¡¡ | j t |¡¡ dS t | |¡ 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.
        r	   ZHASHEDZ_HASHED_DTYPEzutf-8N)r0   r]   ÚndarrayÚdtypeÚ	hasobjectÚshapeÚflattenÚflagsÚc_contiguousÚf_contiguousÚTr%   r+   r^   ÚviewÚuint8r[   Úmemmapr7   ÚstridesÚencoder(   r.   r   r8   )r   r-   Zobj_c_contiguousÚklassr	   r	   r
   r8   ¶   s$   


zNumpyHasher.saveN©r   F)r   r   r   r   r   r8   r	   r	   r	   r
   rX   ž   s    
rX   r   Fc                 C   sH   d}||vrt d ||¡ƒ‚dtjv rt||d�}nt|d�}| | ¡S )aI  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
    )r   Úsha1zAValid options for 'hash_name' are {}. Got hash_name={!r} instead.r\   )r&   r[   rY   )Ú
ValueErrorÚformatrE   rF   rX   r   r   )r-   r&   r[   Zvalid_hash_namesZhasherr	   r	   r
   r   ÷   s   ÿÿ


r   ro   )r   r   r#   r   r(   rR   rE   r1   Ú_Picklerr"   rV   r   r   r   rX   r   r	   r	   r	   r
   Ú<module>   s    	oY