
    je3                        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 ddlm	Z	 ddl
mZ  ej                  ded       eZeZt        j$                  Zd+d	Z e       Zd
 Zd Zd Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d de      Z!d+dZ"d  Z#d! Z$d" Z%d# Z&e'e(e)fZ*d$ Z+d+d%Z,d+d&Z-d+d'Z.d(eeee/eee!eeee%iZ0eeure e0e<   ed) e1e-iZ2ejf                  eejh                  f   e5d*<   e0j                         Z6e2j                         Z7y),z>Pickle related utilities. Perhaps this should be called 'can'.    N)FunctionType)
get_logger)import_itemz@ipykernel.pickleutil is deprecated. It has moved to ipyparallel.   )
stacklevelc                 >      fd}t        |j                  d         S )zUthe type of a closure cell doesn't seem to be importable,
    so just create one
    c                       S N )as   6/usr/lib/python3/dist-packages/ipykernel/pickleutil.pyinnerz_get_cell_type.<locals>.inner!   s        r   )type__closure__)r   r   s   ` r   _get_cell_typer      s     
 !!!$%%r   c                     t        | t              rAt        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__)fmainmods     r   interactiver   .   sN     !\"Z(JJJJNN	
 ALHr   c                  ~    ddl } | a	 ddlm} | |_        t
        j                  t        d       y# t        $ r Y &w xY w)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!   s     r   use_dillr(   C   sD      F '  	 KKd#     0 	<<c                  ~    ddl } | a	 ddlm} | |_        t
        j                  t        d       y# t        $ r Y &w xY w)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!   s     r   use_cloudpickler,   \   sD    
  F'' '	 KKd#  r)   c                        e Zd ZdZddZddZy)CannedObjectzA canned object.Nc                     |xs g | _         t        j                  |      | _        t        |      | _        |D ],  }t        | j                  |t        t        ||                   . g | _        y)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)selfr2   r0   r4   keys        r   __init__zCannedObject.__init__y   s]    $ JB	99S>I	 	;CDHHc3wsC'8#9:	; r   c                    |i }| j                   }| j                  D ]#  }t        ||t        t	        ||      |             % | j
                  r-t        | j
                  |      | _        | j                  ||       | j                   S )zGet an object.)r2   r0   r5   uncanr6   r4   )r8   gr2   r9   s       r   
get_objectzCannedObject.get_object   st    9Ahh99 	;CCeGC$5q9:	; 99dii+DIIIc1xxr   )NNr
   r   r   __qualname____doc__r:   r>   r   r   r   r.   r.   v   s    4r   r.   c                   $    e Zd ZdZd Zd ZddZy)	Referencez/object for wrapping a remote reference by name.c                 \    t        |t              st        d|z        || _        g | _        y)zInitialize the reference.zillegal name: %rN)r   str	TypeErrornamer7   )r8   rG   s     r   r:   zReference.__init__   s+    $$.566	r   c                      d| j                   z  S )z%Get the string repr of the reference.z<Reference: %r>)rG   )r8   s    r   __repr__zReference.__repr__   s     499,,r   Nc                 6    |i }t        | j                  |      S )zGet an object in the reference.)evalrG   )r8   r=   s     r   r>   zReference.get_object   s    9ADIIq!!r   r
   )r   r   r@   rA   r:   rI   r>   r   r   r   rC   rC      s    9-"r   rC   c                       e Zd ZdZd ZddZy)
CannedCellzCan a closure cellc                 8    t        |j                        | _        y)zInitialize the canned cell.N)r3   cell_contents)r8   cells     r   r:   zCannedCell.__init__   s     !3!34r   Nc                 X    t        | j                  |      fd}|j                  d   S )zGet an object in the cell.c                       S )zInner function.r   )rO   s   r   r   z$CannedCell.get_object.<locals>.inner   s	      r   r   )r<   rO   r   )r8   r=   r   rO   s      @r   r>   zCannedCell.get_object   s-    d00!4	!   ##r   r
   r?   r   r   r   rM   rM      s    5$r   rM   c                   $    e Zd ZdZd Zd ZddZy)CannedFunctionzCan a function.c                    | j                  |       |j                  | _        |  |j                  r)|j                  D cg c]  }t	        |       c}| _        nd| _        |  |j                  }|rt        d |D              | _        nd| _        |j                  xs d| _
        |j                  | _        g | _        yc c}w )zInitialize the canNc              3   2   K   | ]  }t        |        y wr
   )r3   ).0rP   s     r   	<genexpr>z*CannedFunction.__init__.<locals>.<genexpr>   s      ?tT ?s   r   )_check_typer   coder   r3   defaultsr   tupleclosurer   moduler   r7   )r8   r   fdr]   s       r   r:   zCannedFunction.__init__   s    JJ	>>/0~~>SW>DM DM--  ?w ??DLDLll0j

 ?s   B>c                 2    t        |t              sJ d       y )NzNot a function type)r   r   r8   r2   s     r   rY   zCannedFunction._check_type   s    #|,C.CC,r   Nc                    | j                   j                  d      s<t        | j                          t        j                  | j                      j
                  i | j                  rt        fd| j                  D              nd}| j                  rt        fd| j                  D              nd}t        | j                  | j                  ||      S )zGet an object out of the can.__Nc              3   6   K   | ]  }t        |        y wr
   r<   )rW   cfdr=   s     r   rX   z,CannedFunction.get_object.<locals>.<genexpr>   s     @3sA@   c              3   6   K   | ]  }t        |        y wr
   re   )rW   rP   r=   s     r   rX   z,CannedFunction.get_object.<locals>.<genexpr>   s     @4dA@rg   )r^   
startswithr   sysmodulesr   r[   r\   r]   r   rZ   r   )r8   r=   r[   r]   s    `  r   r>   zCannedFunction.get_object   s     {{%%d+t{{#DKK(11A9ADHMM5@$--@@W[DHLL%@4<<@@VZDIIq$--7KKr   r
   r   r   r@   rA   r:   rY   r>   r   r   r   rT   rT      s    *DLr   rT   c                   $    e Zd ZdZd Zd ZddZy)CannedClasszA canned class object.c                    | j                  |       |j                  | _        t        |t               | _        i | _        |j                  j                         D ]"  \  }}|dvst        |      | j                  |<   $ | j
                  rg n|j                         }|dd D cg c]  }t        |       c}| _        g | _        yc c}w )Initialize the can.)__weakref__r      N)rY   r   rG   r   r   	old_style_canned_dictr   itemsr3   mroparentsr7   )r8   clskvrv   cs         r   r:   zCannedClass.__init__   s    LL	'T22LL&&( 	.DAq33'*1v!!!$	. NNb	(+AB01A0 1s   $Cc                 2    t        |t              sJ d       y )NzNot a class type)r   
class_typera   s     r   rY   zCannedClass._check_type  s    #z*>,>>*r   Nc                     t        fd| j                  D              }t        | j                  |t	        | j
                              S )zGet an object from the can.c              3   6   K   | ]  }t        |        y wr
   re   )rW   pr=   s     r   rX   z)CannedClass.get_object.<locals>.<genexpr>  s     :a:rg   )r=   )r\   rw   r   rG   
uncan_dictrt   )r8   r=   rw   s    ` r   r>   zCannedClass.get_object  s7    :T\\::DIIw
43D3D(JKKr   r
   rl   r   r   r   rn   rn      s     ?Lr   rn   c                       e Zd ZdZd ZddZy)CannedArrayzA canned numpy array.c                 n   ddl m} |j                  | _        |j                  j                  r|j                  j
                  n|j                  j                  | _        d| _        t        |j                        dk(  rd| _        nh|j                  dk(  rd| _        nQ|j                  j                  r;t        d |j                  j                  j                         D              rd| _        | j                  r!t        j                  |t              g| _        y ||d      }t        |      g| _        y)	rp   r   )ascontiguousarrayFTOc              3   ,   K   | ]  \  }}|d k(    yw)r   Nr   )rW   dtszs      r   rX   z'CannedArray.__init__.<locals>.<genexpr>  s     %XFBbCi%Xs   Ndtype)numpyr   shaper   fieldsdescrrE   pickledsumanyvaluesr#   dumpsPICKLE_PROTOCOLr7   buffer)r8   r2   r   s      r   r:   zCannedArray.__init__  s    +YY
(+		(8(8SYY__ciimm
syy>QDLYY#DLYY#%Xcii>N>N>U>U>W%X"XDL<<"LLo>?DL $Ct4C"3K=DLr   Nc                     ddl m} | j                  d   }| j                  rt	        j
                  |      S  ||| j                        j                  | j                        S )zGet the object.r   )
frombufferr   )	r   r   r7   r   r#   loadsr   reshaper   )r8   r=   r   datas       r   r>   zCannedArray.get_object%  sH    $||A<<<<%%$djj199$**EEr   r
   r?   r   r   r   r   r     s    ),Fr   r   c                   l    e Zd ZdZedej                  eeej                  f   defd       Z
d ZddZy)	CannedByteszA canned bytes object.bufreturnc                 |    t        | t              r| j                         S t        | t              st        |       S | S )z+Cast a buffer or memoryview object to bytes)r   
memoryviewtobytesbytes)r   s    r   wrapzCannedBytes.wrap3  s3     c:&;;= #u%:
r   c                     |g| _         y)rp   N)r7   ra   s     r   r:   zCannedBytes.__init__<  s    ur   Nc                 B    | j                   d   }| j                  |      S )zGet the canned object.r   )r7   r   )r8   r=   r   s      r   r>   zCannedBytes.get_object@  s    ||Ayyr   r
   )r   r   r@   rA   staticmethodtypingUnionr   r   SupportsBytesr   r:   r>   r   r   r   r   r   0  sE     &,,z5&2F2FFG E  r   r   c                       e Zd ZdZeZy)CannedBufferzA canned buffer.N)r   r   r@   rA   r   r   r   r   r   r   r   F  s
    Dr   r   c                       e Zd ZdZeZy)CannedMemoryViewzA canned memory view.N)r   r   r@   rA   r   r   r   r   r   r   r   L  s
    Dr   r   c                 R   t               }|j                  d       t        | j                               D ]6  \  }}t	        |t
              s	 t        |      }| j                  |      | |<   8 y# t        $ r. |r||vr|j                  d|d       | j                  |       Y pw xY w)z(import any string-keys in a type mappingzImporting canning mapz canning class not importable: %rT)exc_infoN)
r   debuglistru   r   rE   r   r'   	Exceptionerror)mappingoriginallogr9   _rx   s         r   _import_mappingr   W  s    
,CII%&w}}' 
0Qc30!#&  '{{3/
0  !8 3II@#PTIUC 	!s   A//4B&%B&c                 f     t        |t              rt         fd|D              S t               |u S )zNlike isinstance(obj, check), but strict

    This won't catch subclasses.
    c              3   8   K   | ]  }t              |u   y wr
   )r   )rW   rx   r2   s     r   rX   zistype.<locals>.<genexpr>n  s     549#5s   )r   r\   r   r   )r2   checks   ` r   istyper   h  s/    
 %5u5559r   c                     d}t         j                         D ]/  \  }}t        |t              rd} nt	        | |      s' ||       c S  |rt        t         t               t        |       S | S )zprepare an object for picklingFT)r&   ru   r   rE   r   r   _original_can_mapr3   )r2   import_neededrx   canners       r   r3   r3   r  sh     M}} Vc3 M#s#;  	!233xJr   c                 Z    t        | t              r| j                  dk(  rt        |       S | S )zCan a class object.r   )r   r}   r   rn   )r2   s    r   	can_classr     s'    #z"s~~'C3Jr   c                 z    t        | t              r*i }| j                         D ]  \  }}t        |      ||<    |S | S )zcan the *values* of a dict)r   dictru   r3   )r2   newobjry   rz   s       r   can_dictr     sA    c4IIK 	DAqAF1I	Jr   c                     t        | t              r*t        |       } || D cg c]  }t        |       c}      S | S c c}w )zcan the elements of a sequence)r   sequence_typesr   r3   )r2   tis      r   can_sequencer     s9    c>"I#&Q#a&&''J 's   =c                     d}t         j                         D ]0  \  }}t        |t              rd} nt        | |      s' || |      c S  |r t	        t         t
               t        | |      S | S )zinvert canningFT)	uncan_mapru   r   rE   r   _original_uncan_mapr<   )r2   r=   r   rx   uncanners        r   r<   r<     so     M"* $Xc3 Mc3C##$  		#67S!}Jr   c                 |    t        | t              r+i }| j                         D ]  \  }}t        ||      ||<    |S | S )zUncan a dict object.)r   r   ru   r<   )r2   r=   r   ry   rz   s        r   r   r     sC    c4IIK 	$DAqaF1I	$Jr   c           	          t        | t              r+t        |       } || D cg c]  }t        ||       c}      S | S c c}w )zUncan a sequence.)r   r   r   r<   )r2   r=   r   r   s       r   uncan_sequencer     s;    c>"Is+!%1++,,J ,s   >znumpy.ndarrayc                 $    | j                  |      S r
   )r>   )r2   r=   s     r   <lambda>r     s    !2 r   r   r
   )8rA   r1   r#   rj   r   warningstypesr   traitlets.logr   traitlets.utils.importstringr   warnDeprecationWarningr   r   r   r}   DEFAULT_PROTOCOLr   r   	cell_typer   r(   r,   r.   rC   rM   rT   rn   r   r   r   r   r   r   r3   r   r   r   r\   setr   r   r<   r   r   r   r&   r   r   DictAny__annotations__r   r   r   r   r   <module>r      s   D   
    % 4 F 

))& 	*$2$4( (V" ",$ $$&L\ &LRL, L4!F, !FH, ,; { 0"* s#( [.	; z	 
"GFO 2*,	6;;tVZZ'(  LLN nn& r   