o
    Zh3                     @   s  U d Z ddlZddladdlZddlZddlZddlmZ zddlm	Z	 W n	 e
y.   Y nw ddlmZ ddlmZ ejdedd	 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Z0d:d4d5Z1d6e!eee2e"ee$eeee(iZ3eeure#e3e< ed7d8 e4e0iZ5ej6eej7f e8d9< e3 Z9e5 Z:dS );z>Pickle related utilities. Perhaps this should be called 'can'.    N)FunctionType)codeutil)
get_logger)import_itemz@ipykernel.pickleutil is deprecated. It has moved to ipyparallel.   )
stacklevelc                    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 N r
   ar
   K/var/www/html/lang_env/lib/python3.10/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 )zdecorator 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   interactive2   s   
r   c                  C   F   ddl } | azddlm} W n	 ty   Y nw | |_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_dillG   s   r%   c                  C   r   )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r+t| j|| _| || | j S )zGet an object.N)r+   r)   r.   uncanr/   r-   )r1   gr+   r2   r
   r
   r   
get_object   s   
zCannedObject.get_object)NNr	   r   r   __qualname____doc__r3   r6   r
   r
   r
   r   r(   z   s    
r(   c                   @   *   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namer0   )r1   r>   r
   r
   r   r3      s   

zReference.__init__c                 C   s
   d| j  S )z%Get the string repr of the reference.z<Reference: %r>)r>   )r1   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>   )r1   r5   r
   r
   r   r6      s   zReference.get_objectr	   )r   r   r8   r9   r3   r?   r6   r
   r
   r
   r   r;      s
    r;   c                   @   "   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)r1   cellr
   r
   r   r3      s   zCannedCell.__init__Nc                    s"   t | j|  fdd}|jd S )zGet an object in the cell.c                      r   )zInner function.r
   r
   rC   r
   r   r      s   z$CannedCell.get_object.<locals>.innerr   )r4   rC   r   )r1   r5   r   r
   rE   r   r6      s   
zCannedCell.get_objectr	   r7   r
   r
   r
   r   rB      s    rB   c                   @   r:   )
CannedFunctionzCan a function.c                 C   s~   |  | |j| _|  |jrdd |jD | _nd| _|  |j}|r-tdd |D | _nd| _|jp4d| _	|j
| _
g | _dS )zInitialize the canc                 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	   rH   rI   rD   r
   r
   r   	<genexpr>   s    z*CannedFunction.__init__.<locals>.<genexpr>r   )_check_typer   coder   defaultsr   tupleclosurer   moduler   r0   )r1   r   rS   r
   r
   r   r3      s   

zCannedFunction.__init__c                 C      t |ts	J dd S )NzNot a function type)r   r   r1   r+   r
   r
   r   rO         zCannedFunction._check_typeNc                    s   | j dst| j  tj| j  j  du ri  | jr't fdd| jD nd}| jr8t fdd| jD nd}t	| j
 | j||S )zGet an object out of the can.__Nc                 3       | ]}t | V  qd S r	   r4   )rI   Zcfdr5   r
   r   rN          z,CannedFunction.get_object.<locals>.<genexpr>c                 3   rY   r	   rZ   rM   r[   r
   r   rN      r\   )rT   
startswithr   sysmodulesr   rQ   rR   rS   r   rP   r   )r1   r5   rQ   rS   r
   r[   r   r6      s   
""zCannedFunction.get_objectr	   r   r   r8   r9   r3   rO   r6   r
   r
   r
   r   rF      s
    rF   c                   @   r:   )
CannedClasszA canned class object.c                 C   s   |  | |j| _t|t | _i | _|j D ]\}}|dvr't	|| j|< q| jr-g n|
 }dd |dd D | _g | _dS )Initialize the can.)__weakref__r   c                 S   rG   r
   rH   )rI   cr
   r
   r   rK     rL   z(CannedClass.__init__.<locals>.<listcomp>   N)rO   r   r>   r   r   Z	old_style_canned_dictr   itemsr,   mroparentsr0   )r1   clskvrh   r
   r
   r   r3      s   

zCannedClass.__init__c                 C   rU   )NzNot a class type)r   
class_typerV   r
   r
   r   rO     rW   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   rY   r	   rZ   )rI   pr[   r
   r   rN     r\   z)CannedClass.get_object.<locals>.<genexpr>r[   )rR   ri   r   r>   
uncan_dictrf   )r1   r5   ri   r
   r[   r   r6   
  s   zCannedClass.get_objectr	   r`   r
   r
   r
   r   ra      s
    ra   c                   @   rA   )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r%d| _n|jdkr.d| _n|jjrBt	dd |jj
 D rBd| _| jrOt|tg| _dS ||dd	}t|g| _dS )
rb   r   )ascontiguousarrayFTOc                 s   s    | ]	\}}|d kV  qdS )rr   Nr
   )rI   dtszr
   r
   r   rN         z'CannedArray.__init__.<locals>.<genexpr>Ndtype)numpyrq   shaperw   fieldsdescrr<   pickledsumanyvaluesr    dumpsPICKLE_PROTOCOLr0   buffer)r1   r+   rq   r
   r
   r   r3     s   
"zCannedArray.__init__Nc                 C   s<   ddl m} | jd }| jrt|S ||| jd| jS )zGet the object.r   )
frombufferrv   )	rx   r   r0   r|   r    loadsrw   Zreshapery   )r1   r5   r   datar
   r
   r   r6   )  s
   

zCannedArray.get_objectr	   r7   r
   r
   r
   r   rp     s    rp   c                   @   sF   e Zd ZdZedejeeej	f def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 )rb   N)r0   rV   r
   r
   r   r3   @  s   zCannedBytes.__init__Nc                 C   s   | j d }| |S )zGet the canned object.r   )r0   r   )r1   r5   r   r
   r
   r   r6   D  s   

zCannedBytes.get_objectr	   )r   r   r8   r9   staticmethodtypingUnionr   r   SupportsBytesr   r3   r6   r
   r
   r
   r   r   4  s    "r   c                   @      e Zd ZdZeZdS )CannedBufferzA canned buffer.N)r   r   r8   r9   r   r   r
   r
   r
   r   r   J      r   c                   @   r   )CannedMemoryViewzA canned memory view.N)r   r   r8   r9   r   r   r
   r
   r
   r   r   P  r   r   c              	   C   s   t  }|d t|  D ]3\}}t|trAzt|}W n ty9   |r2||vr2|jd|dd | 	| Y qw | 	|| |< qdS )z(import any string-keys in a type mappingzImporting canning mapz canning class not importable: %rT)exc_infoN)
r   debuglistrg   r   r<   r   	Exceptionerrorr$   )mappingoriginallogr2   _rj   r
   r
   r   _import_mapping[  s   

r   c                    s,   t |trt fdd|D S t |u S )zNlike isinstance(obj, check), but strict

    This won't catch subclasses.
    c                 3   s    | ]	}t  |u V  qd S r	   )r   )rI   rj   r+   r
   r   rN   r  ru   zistype.<locals>.<genexpr>)r   rR   r~   r   )r+   checkr
   r   r   istypel  s   
r   c                 C   sX   d}t  D ]\}}t|trd} nt| |r||   S q|r*tt t t| S | S )zprepare an object for picklingFT)r#   rg   r   r<   r   r   _original_can_mapr,   )r+   import_neededrj   Zcannerr
   r
   r   r,   v  s   


r,   c                 C   s    t | tr| jdkrt| S | S )zCan a class object.r   )r   rm   r   ra   r   r
   r
   r   	can_class  s   r   c                 C   s4   t | tri }|  D ]
\}}t|||< q|S | S )zcan the *values* of a dict)r   dictrg   r,   )r+   newobjrk   rl   r
   r
   r   can_dict  s   
r   c                 C   s(   t | trt| }|dd | D S | S )zcan the elements of a sequencec                 S   rG   r
   rH   rI   ir
   r
   r   rK     rL   z can_sequence.<locals>.<listcomp>r   sequence_typesr   )r+   tr
   r
   r   can_sequence  s   
r   c                 C   s\   d}t  D ]\}}t|trd} nt| |r|| |  S q|r,tt t t| |S | S )zinvert canningFT)	uncan_maprg   r   r<   r   _original_uncan_mapr4   )r+   r5   r   rj   Zuncannerr
   r
   r   r4     s   



r4   c                 C   s6   t | tri }|  D ]\}}t||||< q|S | S )zUncan a dict object.)r   r   rg   r4   )r+   r5   r   rk   rl   r
   r
   r   ro     s   
ro   c                    s,   t | trt| }| fdd| D S | S )zUncan a sequence.c                    s   g | ]}t | qS r
   rZ   r   r[   r
   r   rK     s    z"uncan_sequence.<locals>.<listcomp>r   )r+   r5   r   r
   r[   r   uncan_sequence  s   
r   znumpy.ndarrayc                 C   s
   |  |S r	   )r6   )r+   r5   r
   r
   r   <lambda>  s   
 r   r   r	   );r9   r*   r    r^   r   warningstypesr   Zipyparallel.serializer   r"   Ztraitlets.logr   Ztraitlets.utils.importstringr   warnDeprecationWarningr   r   r   rm   DEFAULT_PROTOCOLr   r   Z	cell_typer   r%   r'   r(   r;   rB   rF   ra   rp   r   r   r   r   r   r,   r   r   r   rR   setr   r   r4   ro   r   r   r#   r   r   DictAny__annotations__r   r   r
   r
   r
   r   <module>   sx    
+)$







