a
    ø£BeB4  ã                   @   sò  U d Z ddlZddlZejdedd� ddlZddladdlZddlm	Z	 ddl
mZ ddlmZ dd	lmZ eZeZtjZd:d
d„Zeƒ Zdd„ Zdd„ Zdd„ ZG dd„ dƒ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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&d'„ Z%d(d)„ Z&d*d+„ Z'd,d-„ Z(e)e*e+fZ,d.d/„ Z-d<d0d1„Z.d=d2d3„Z/d>d4d5„Z0d6e e	ee1e!ee#eeee'iZ2eeu�rºe"e2e< ed7d8„ e3e/iZ4ej5eej6f e7d9< e2 ¡ Z8e4 ¡ Z9dS )?z>Pickle related utilities. Perhaps this should be called 'can'.é    Nz@ipykernel.pickleutil is deprecated. It has moved to ipyparallel.é   )Ú
stacklevel)ÚFunctionType)Úcodeutil)Ú
get_logger)Úimport_itemc                    s   ‡ fdd„}t |jd ƒS )zUthe type of a closure cell doesn't seem to be importable,
    so just create one
    c                      s   ˆ S ©N© r	   ©Úar	   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/pickleutil.pyÚinner#   s    z_get_cell_type.<locals>.innerr   )ÚtypeÚ__closure__)r   r   r	   r
   r   Ú_get_cell_type   s    r   c                 C   s2   t | tƒr(tdƒ}t| j|j| j| jƒ} d| _| S )z³decorator for making functions appear as interactively defined.
    This results in the function being linked to the user_ns as globals()
    instead of the module globals().
    Ú__main__)Ú
isinstancer   Ú
__import__Ú__code__Ú__dict__Ú__name__Ú__defaults__Ú
__module__)ÚfZmainmodr	   r	   r   Úinteractive0   s    
ür   c                  C   sF   ddl } | azddlm} W n ty.   Y n0 | |_t td¡ dS )zquse dill to expand serialization support

    adds support for object methods and closures to serialization.
    r   N©Ú	serialize)ÚdillÚpickleÚ	ipykernelr   ÚImportErrorÚcan_mapÚpopr   )r   r   r	   r	   r   Úuse_dillE   s    r#   c                  C   sF   ddl } | azddlm} W n ty.   Y n0 | |_t td¡ dS )zxuse cloudpickle to expand serialization support

    adds support for object methods and closures to serialization.
    r   Nr   )Úcloudpickler   r   r   r    r!   r"   r   )r$   r   r	   r	   r   Úuse_cloudpickle^   s    r%   c                   @   s$   e Zd ZdZddd„Zddd„ZdS )	ÚCannedObjectzA canned object.Nc                 C   sL   |pg | _ t |¡| _t|ƒ| _|D ]}t| j|tt||ƒƒƒ q$g | _dS )aü  can an object for safe pickling

        Parameters
        ----------
        obj
            The object to be canned
        keys : list (optional)
            list of attribute names that will be explicitly canned / uncanned
        hook : callable (optional)
            An optional extra callable,
            which can do additional processing of the uncanned object.

        Notes
        -----
        large data may be offloaded into the buffers list,
        used for zero-copy transfers.
        N)ÚkeysÚcopyÚobjÚcanÚhookÚsetattrÚgetattrÚbuffers)Úselfr)   r'   r+   Úkeyr	   r	   r   Ú__init__{   s    

zCannedObject.__init__c                 C   s\   |du ri }| j }| jD ]}t||tt||ƒ|ƒƒ q| jrVt| j|ƒ| _|  ||¡ | j S )zGet an object.N)r)   r'   r,   Úuncanr-   r+   )r/   Úgr)   r0   r	   r	   r   Ú
get_object•   s    
zCannedObject.get_object)NN)N©r   r   Ú__qualname__Ú__doc__r1   r4   r	   r	   r	   r   r&   x   s   
r&   c                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
Ú	Referencez/object for wrapping a remote reference by name.c                 C   s&   t |tƒstd| ƒ‚|| _g | _dS )zInitialize the reference.zillegal name: %rN)r   ÚstrÚ	TypeErrorÚnamer.   )r/   r;   r	   r	   r   r1   ¦   s    
zReference.__init__c                 C   s
   d| j  S )z%Get the string repr of the reference.z<Reference: %r>)r;   )r/   r	   r	   r   Ú__repr__­   s    zReference.__repr__Nc                 C   s   |du ri }t | j|ƒS )zGet an object in the reference.N)Úevalr;   )r/   r3   r	   r	   r   r4   ±   s    zReference.get_object)N)r   r   r6   r7   r1   r<   r4   r	   r	   r	   r   r8   £   s   r8   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )Ú
CannedCellzCan a closure cellc                 C   s   t |jƒ| _dS )zInitialize the canned cell.N)r*   Úcell_contents)r/   Úcellr	   r	   r   r1   ¼   s    zCannedCell.__init__Nc                    s"   t | j|ƒ‰ ‡ fdd„}|jd S )zGet an object in the cell.c                      s   ˆ S )zInner function.r	   r	   ©r?   r	   r   r   Ä   s    z$CannedCell.get_object.<locals>.innerr   )r2   r?   r   )r/   r3   r   r	   rA   r   r4   À   s    zCannedCell.get_object)Nr5   r	   r	   r	   r   r>   ¹   s   r>   c                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚCannedFunctionzCan a function.c                 C   s~   |   |¡ |j| _|  |jr0dd„ |jD ƒ| _nd| _|  |j}|rZtdd„ |D ƒƒ| _nd| _|jphd| _	|j
| _
g | _dS )zInitialize the canc                 S   s   g | ]}t |ƒ‘qS r	   ©r*   )Ú.0Úfdr	   r	   r   Ú
<listcomp>Ô   ó    z+CannedFunction.__init__.<locals>.<listcomp>Nc                 s   s   | ]}t |ƒV  qd S r   rC   ©rD   r@   r	   r	   r   Ú	<genexpr>Û   rG   z*CannedFunction.__init__.<locals>.<genexpr>r   )Ú_check_typer   Úcoder   Údefaultsr   ÚtupleÚclosurer   Úmoduler   r.   )r/   r   rN   r	   r	   r   r1   Î   s    
zCannedFunction.__init__c                 C   s   t |tƒsJ dƒ‚d S )NzNot a function type)r   r   ©r/   r)   r	   r	   r   rJ   ã   s    zCannedFunction._check_typeNc                    sŒ   | j  d¡s$t| j ƒ tj| j  j‰ ˆ du r0i ‰ | jrNt‡ fdd„| jD ƒƒnd}| jrpt‡ fdd„| jD ƒƒnd}t	| j
ˆ | j||ƒ}|S )zGet an object out of the can.Ú__Nc                 3   s   | ]}t |ˆ ƒV  qd S r   ©r2   )rD   Zcfd©r3   r	   r   rI   ï   rG   z,CannedFunction.get_object.<locals>.<genexpr>c                 3   s   | ]}t |ˆ ƒV  qd S r   rR   rH   rS   r	   r   rI   ð   rG   )rO   Ú
startswithr   ÚsysÚmodulesr   rL   rM   rN   r   rK   r   )r/   r3   rL   rN   ZnewFuncr	   rS   r   r4   æ   s    
""zCannedFunction.get_object)N©r   r   r6   r7   r1   rJ   r4   r	   r	   r	   r   rB   Ë   s   rB   c                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚCannedClasszA canned class object.c                 C   s„   |   |¡ |j| _t|tƒ | _i | _|j ¡ D ]\}}|dvr0t	|ƒ| j|< q0| jrZg n| 
¡ }dd„ |dd… D ƒ| _g | _dS )úInitialize the can.)Ú__weakref__r   c                 S   s   g | ]}t |ƒ‘qS r	   rC   )rD   Úcr	   r	   r   rF     rG   z(CannedClass.__init__.<locals>.<listcomp>é   N)rJ   r   r;   r   r   Z	old_styleÚ_canned_dictr   Úitemsr*   ÚmroÚparentsr.   )r/   ÚclsÚkÚvr_   r	   r	   r   r1   ø   s    
zCannedClass.__init__c                 C   s   t |tƒsJ dƒ‚d S )NzNot a class type)r   Ú
class_typerP   r	   r	   r   rJ     s    zCannedClass._check_typeNc                    s0   t ‡ fdd„| jD ƒƒ}t| j|t| jˆ d�ƒS )zGet an object from the can.c                 3   s   | ]}t |ˆ ƒV  qd S r   rR   )rD   ÚprS   r	   r   rI     rG   z)CannedClass.get_object.<locals>.<genexpr>rS   )rM   r`   r   r;   Ú
uncan_dictr]   )r/   r3   r`   r	   rS   r   r4   	  s    zCannedClass.get_object)NrW   r	   r	   r	   r   rX   õ   s   rX   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )ÚCannedArrayzA canned numpy array.c                 C   s¸   ddl m} |j| _|jjr$|jjn|jj| _d| _t|jƒdkrJd| _n:|jdkr\d| _n(|jjr„t	dd„ |jj 
¡ D ƒƒr„d| _| jrœt |t¡g| _n||dd	�}t|ƒg| _dS )
rY   r   )ÚascontiguousarrayFTÚOc                 s   s   | ]\}}|d kV  qdS )ri   Nr	   )rD   ÚdtÚszr	   r	   r   rI     rG   z'CannedArray.__init__.<locals>.<genexpr>N©Údtype)Únumpyrh   Úshaperm   ÚfieldsÚdescrr9   ÚpickledÚsumÚanyÚvaluesr   ÚdumpsÚPICKLE_PROTOCOLr.   Úbuffer)r/   r)   rh   r	   r	   r   r1     s    
"zCannedArray.__init__Nc                 C   s@   ddl m} | jd }| jr&t |¡S ||| jd� | j¡S dS )zGet the object.r   )Ú
frombufferrl   N)	rn   ry   r.   rr   r   Úloadsrm   Zreshapero   )r/   r3   ry   Údatar	   r	   r   r4   (  s
    

zCannedArray.get_object)Nr5   r	   r	   r	   r   rg     s   rg   c                   @   sD   e Zd ZdZeejeeej	f edœdd„ƒZ
dd„ Zd
dd	„ZdS )ÚCannedByteszA canned bytes object.)ÚbufÚreturnc                 C   s(   t | tƒr|  ¡ S t | tƒs$t| ƒS | S )z+Cast a buffer or memoryview object to bytes)r   Ú
memoryviewÚtobytesÚbytes)r}   r	   r	   r   Úwrap7  s
    

zCannedBytes.wrapc                 C   s   |g| _ dS )rY   N)r.   rP   r	   r	   r   r1   @  s    zCannedBytes.__init__Nc                 C   s   | j d }|  |¡S )zGet the canned object.r   )r.   r‚   )r/   r3   r{   r	   r	   r   r4   D  s    
zCannedBytes.get_object)N)r   r   r6   r7   ÚstaticmethodÚtypingÚUnionr   r�   ÚSupportsBytesr‚   r1   r4   r	   r	   r	   r   r|   4  s
    r|   c                   @   s   e Zd ZdZeZdS )ÚCannedBufferzA canned buffer.N)r   r   r6   r7   rx   r‚   r	   r	   r	   r   r‡   J  s   r‡   c                   @   s   e Zd ZdZeZdS )ÚCannedMemoryViewzA canned memory view.N)r   r   r6   r7   r   r‚   r	   r	   r	   r   rˆ   P  s   rˆ   c              	   C   sˆ   t ƒ }| d¡ t|  ¡ ƒD ]f\}}t|tƒrzt|ƒ}W n8 tyr   |rd||vrd|jd|dd� |  	|¡ Y q0 |  	|¡| |< qdS )z(import any string-keys in a type mappingzImporting canning mapz canning class not importable: %rT)Úexc_infoN)
r   ÚdebugÚlistr^   r   r9   r   Ú	ExceptionÚerrorr"   )ÚmappingÚoriginalÚlogr0   Ú_ra   r	   r	   r   Ú_import_mapping[  s    

r’   c                    s0   t |tƒr t‡ fdd„|D ƒƒS tˆ ƒ|u S dS )zNlike isinstance(obj, check), but strict

    This won't catch subclasses.
    c                 3   s   | ]}t ˆ ƒ|u V  qd S r   )r   )rD   ra   ©r)   r	   r   rI   r  rG   zistype.<locals>.<genexpr>N)r   rM   rt   r   )r)   Úcheckr	   r“   r   Úistypel  s    
r•   c                 C   sZ   d}t  ¡ D ]2\}}t|tƒr(d} q@qt| |ƒr|| ƒ  S q|rVtt tƒ t| ƒS | S )zprepare an object for picklingFT)r!   r^   r   r9   r•   r’   Ú_original_can_mapr*   )r)   Úimport_neededra   Zcannerr	   r	   r   r*   w  s    


r*   c                 C   s$   t | tƒr| jdkrt| ƒS | S dS )zCan a class object.r   N)r   rd   r   rX   r“   r	   r	   r   Ú	can_classŒ  s    r˜   c                 C   s8   t | tƒr0i }|  ¡ D ]\}}t|ƒ||< q|S | S dS )zcan the *values* of a dictN)r•   Údictr^   r*   )r)   Únewobjrb   rc   r	   r	   r   Úcan_dict”  s    
r›   c                 C   s,   t | tƒr$t| ƒ}|dd„ | D ƒƒS | S dS )zcan the elements of a sequencec                 S   s   g | ]}t |ƒ‘qS r	   rC   ©rD   Úir	   r	   r   rF   ¦  rG   z can_sequence.<locals>.<listcomp>N©r•   Úsequence_typesr   )r)   Útr	   r	   r   Úcan_sequence¢  s    
r¡   c                 C   s^   d}t  ¡ D ]4\}}t|tƒr(d} qBqt| |ƒr|| |ƒ  S q|rZtt tƒ t| |ƒS | S )zinvert canningFT)Ú	uncan_mapr^   r   r9   r’   Ú_original_uncan_mapr2   )r)   r3   r—   ra   Zuncannerr	   r	   r   r2   «  s    



r2   c                 C   s:   t | tƒr2i }|  ¡ D ]\}}t||ƒ||< q|S | S dS )zUncan a dict object.N)r•   r™   r^   r2   )r)   r3   rš   rb   rc   r	   r	   r   rf   ¿  s    
rf   c                    s0   t | tƒr(t| ƒ}|‡ fdd„| D ƒƒS | S dS )zUncan a sequence.c                    s   g | ]}t |ˆ ƒ‘qS r	   rR   rœ   rS   r	   r   rF   Î  rG   z"uncan_sequence.<locals>.<listcomp>Nrž   )r)   r3   r    r	   rS   r   Úuncan_sequenceÊ  s    
r¤   znumpy.ndarrayc                 C   s
   |   |¡S r   )r4   )r)   r3   r	   r	   r   Ú<lambda>å  rG   r¥   r¢   )N)N)N)N)N):r7   r„   ÚwarningsÚwarnÚDeprecationWarningr(   r   rU   Útypesr   Zipyparallel.serializer   Ztraitlets.logr   Ztraitlets.utils.importstringr   r   rx   r   rd   ÚDEFAULT_PROTOCOLrw   r   Z	cell_typer   r#   r%   r&   r8   r>   rB   rX   rg   r|   r‡   rˆ   r’   r•   r*   r˜   r›   r‹   rM   ÚsetrŸ   r¡   r2   rf   r¤   r�   r!   r™   r¢   ÚDictÚAnyÚ__annotations__r–   r£   r	   r	   r	   r   Ú<module>   sn   ý
+*%

	


ú
þ