
    Xh3                        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 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'Z0d,d(Z1d)e!eee2e"ee$eeee(iZ3eeure#e3e<   ed* e4e0iZ5e4eejl                  f   e7d+<   e3j                         Z8e5j                         Z9y# e
$ r Y *w xY w)-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                 >      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   H/var/www/html/myenv/lib/python3.12/site-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   2   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)   G   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)selfr3   r1   r5   keys        r   __init__zCannedObject.__init__}   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.)r3   r1   r6   uncanr7   r5   )r9   gr3   r:   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/   z   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namer8   )r9   rH   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>)rH   )r9   s    r   __repr__zReference.__repr__   s     499,,r   Nc                 6    |i }t        | j                  |      S )zGet an object in the reference.)evalrH   )r9   r>   s     r   r?   zReference.get_object   s    9ADIIq!!r   r   )r   r   rA   rB   r;   rJ   r?   r   r   r   rD   rD      s    9-"r   rD   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)r4   cell_contents)r9   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   )rP   s   r   r   z$CannedCell.get_object.<locals>.inner   s	      r   r   )r=   rP   r   )r9   r>   r   rP   s      @r   r?   zCannedCell.get_object   s-    d00!4	!   ##r   r   r@   r   r   r   rN   rN      s    5$r   rN   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   )r4   ).0rQ   s     r   	<genexpr>z*CannedFunction.__init__.<locals>.<genexpr>   s      ?tT ?s   r   )_check_typer   coder   r4   defaultsr   tupleclosurer   moduler   r8   )r9   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   r9   r3   s     r   rZ   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=   )rX   cfdr>   s     r   rY   z,CannedFunction.get_object.<locals>.<genexpr>   s     @3sA@   c              3   6   K   | ]  }t        |        y wr   rf   )rX   rQ   r>   s     r   rY   z,CannedFunction.get_object.<locals>.<genexpr>   s     @4dA@rh   )r_   
startswithr   sysmodulesr   r\   r]   r^   r   r[   r   )r9   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   rA   rB   r;   rZ   r?   r   r   r   rU   rU      s    *DLr   rU   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)rZ   r   rH   r   r   	old_style_canned_dictr   itemsr4   mroparentsr8   )r9   clskvrw   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_typerb   s     r   rZ   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   rf   )rX   pr>   s     r   rY   z)CannedClass.get_object.<locals>.<genexpr>  s     :a:rh   )r>   )r]   rx   r   rH   
uncan_dictru   )r9   r>   rx   s    ` r   r?   zCannedClass.get_object
  s7    :T\\::DIIw
43D3D(JKKr   r   rm   r   r   r   ro   ro      s     ?Lr   ro   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)	rq   r   )ascontiguousarrayFTOc              3   ,   K   | ]  \  }}|d k(    yw)r   Nr   )rX   dtszs      r   rY   z'CannedArray.__init__.<locals>.<genexpr>  s     %XFBbCi%Xs   Ndtype)numpyr   shaper   fieldsdescrrF   pickledsumanyvaluesr$   dumpsPICKLE_PROTOCOLr8   buffer)r9   r3   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   r8   r   r$   loadsr   reshaper   )r9   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.wrap7  s3     c:&;;= #u%:
r   c                     |g| _         y)rq   N)r8   rb   s     r   r;   zCannedBytes.__init__@  s    ur   Nc                 B    | j                   d   }| j                  |      S )zGet the canned object.r   )r8   r   )r9   r>   r   s      r   r?   zCannedBytes.get_objectD  s    ||Ayyr   r   )r   r   rA   rB   staticmethodtypingUnionr   r   SupportsBytesr   r;   r?   r   r   r   r   r   4  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   rA   rB   r   r   r   r   r   r   r   J  s
    Dr   r   c                       e Zd ZdZeZy)CannedMemoryViewzA canned memory view.N)r   r   rA   rB   r   r   r   r   r   r   r   P  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listrv   r   rF   r   r(   	Exceptionerror)mappingoriginallogr:   _ry   s         r   _import_mappingr   [  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   )rX   ry   r3   s     r   rY   zistype.<locals>.<genexpr>r  s     549#5s   )r   r]   r   r   )r3   checks   ` r   istyper   l  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'   rv   r   rF   r   r   _original_can_mapr4   )r3   import_neededry   canners       r   r4   r4   v  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   ro   )r3   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   dictrv   r4   )r3   newobjrz   r{   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   r4   )r3   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_maprv   r   rF   r   _original_uncan_mapr=   )r3   r>   r   ry   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   rv   r=   )r3   r>   r   rz   r{   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=   )r3   r>   r   r   s       r   uncan_sequencer     s;    c>"Is+!%1++,,J ,s   >znumpy.ndarrayc                 $    | j                  |      S r   )r?   )r3   r>   s     r   <lambda>r     s    !2 r   r   r   ):rB   r2   r$   rk   r   warningstypesr   ipyparallel.serializer   r&   traitlets.logr   traitlets.utils.importstringr   warnDeprecationWarningr   r   r   r~   DEFAULT_PROTOCOLr   r   	cell_typer   r)   r-   r/   rD   rN   rU   ro   r   r   r   r   r   r   r4   r   r   r   r]   setr   r   r=   r   r   r   r'   r   r   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*%	4fjj !  LLN nn& m  		s   E EE