
    Mh3                        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	 n# e
$ r Y nw xY w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Z5ej6        eej7        f         e8d+<   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                 B      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   T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/ipykernel/pickleutil.pyinnerz_get_cell_type.<locals>.inner%   s        r   )type__closure__)r   r   s   ` r   _get_cell_typer       s4    
     !!$%%%r   c                     t          | t                    r5t          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   sU     !\"" 
Z((JJN	
 
 ALHr   c                      ddl } | a	 ddlm} | |_        n# t          $ r Y nw xY 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"   s     r   use_dillr)   G   sv     KKK F ''''''  	     KKd#####    
##c                      ddl } | a	 ddlm} | |_        n# t          $ r Y nw xY w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"   s     r   use_cloudpickler-   `   sy    
  F''''''' '	     KKd#####r*   c                   "    e Zd ZdZddZddZdS )CannedObjectzA canned object.Nc                     |pg | _         t          j        |          | _        t          |          | _        |D ]3}t          | j        |t          t          ||                               4g | _        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)selfr3   r1   r5   keys        r   __init__zCannedObject.__init__}   sn    $ JB	9S>>II	 	; 	;CDHc3wsC'8'8#9#9::::r   c                    |i }| j         }| j        D ]/}t          ||t          t	          ||          |                     0| j        r0t          | j        |          | _        |                     ||           | j         S )zGet an object.)r3   r1   r6   uncanr7   r5   )r9   gr3   r:   s       r   
get_objectzCannedObject.get_object   s    9Ah9 	; 	;CCeGC$5$5q99::::9 	di++DIIIc1xr   )NNr   r   r   __qualname____doc__r;   r?   r   r   r   r/   r/   z   sB           4     r   r/   c                   &    e Zd ZdZd Zd ZddZdS )	Referencez/object for wrapping a remote reference by name.c                 p    t          |t                    st          d|z            || _        g | _        dS )zInitialize the reference.zillegal name: %rN)r   str	TypeErrornamer8   )r9   rH   s     r   r;   zReference.__init__   s:    $$$ 	7.56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     49,,r   Nc                 4    |i }t          | j        |          S )zGet an object in the reference.)evalrH   )r9   r>   s     r   r?   zReference.get_object   s    9ADIq!!!r   r   )r   r   rA   rB   r;   rJ   r?   r   r   r   rD   rD      sL        99  - - -" " " " " "r   rD   c                        e Zd ZdZd ZddZdS )
CannedCellzCan a closure cellc                 8    t          |j                  | _        dS )zInitialize the canned cell.N)r4   cell_contents)r9   cells     r   r;   zCannedCell.__init__   s     !344r   Nc                 R    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!44	! 	! 	! 	! 	!  ##r   r   r@   r   r   r   rN   rN      s=        5 5 5$ $ $ $ $ $r   rN   c                   &    e Zd ZdZd Zd ZddZdS )CannedFunctionzCan a function.c                 :   |                      |           |j        | _        |  |j        rd |j        D             | _        nd| _        |  |j        }|rt          d |D                       | _        nd| _        |j        pd| _	        |j
        | _
        g | _        dS )zInitialize the canc                 ,    g | ]}t          |          S r   r4   ).0fds     r   
<listcomp>z+CannedFunction.__init__.<locals>.<listcomp>   s    >>>SWW>>>r   Nc              3   4   K   | ]}t          |          V  d S r   rX   )rY   rQ   s     r   	<genexpr>z*CannedFunction.__init__.<locals>.<genexpr>   s(       ? ?tT ? ? ? ? ? ?r   r   )_check_typer   coder   defaultsr   tupleclosurer   moduler   r8   )r9   r   rb   s      r   r;   zCannedFunction.__init__   s    J	?> 	!>>q~>>>DMM DM - 	   ? ?w ? ? ???DLLDLl0j
r   c                 D    t          |t                    s
J d            d S )NzNot a function type)r   r   r9   r3   s     r   r^   zCannedFunction._check_type   s'    #|,,CC.CCCCCCr   Nc                 ~   | j                             d          s0t          | 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   8   K   | ]}t          |          V  d S r   r=   )rY   cfdr>   s     r   r]   z,CannedFunction.get_object.<locals>.<genexpr>   s+      @@3sA@@@@@@r   c              3   8   K   | ]}t          |          V  d S r   ri   )rY   rQ   r>   s     r   r]   z,CannedFunction.get_object.<locals>.<genexpr>   s+      @@4dA@@@@@@r   )rc   
startswithr   sysmodulesr   r`   ra   rb   r   r_   r   )r9   r>   r`   rb   s    `  r   r?   zCannedFunction.get_object   s     {%%d++ 	2t{###DK(1A9ADHM[5@@@@$-@@@@@@W[DHLZ%@@@@4<@@@@@@VZDIq$-7KKKr   r   r   r   rA   rB   r;   r^   r?   r   r   r   rU   rU      sU          *D D DL L L L L Lr   rU   c                   &    e Zd ZdZd Zd ZddZdS )CannedClasszA canned class object.c                 z   |                      |           |j        | _        t          |t                     | _        i | _        |j                                        D ] \  }}|dvrt          |          | j        |<   !| j        rg n|
                                }d |dd         D             | _        g | _        dS )Initialize the can.)__weakref__r   c                 ,    g | ]}t          |          S r   rX   )rY   cs     r   r[   z(CannedClass.__init__.<locals>.<listcomp>  s    0001A000r      N)r^   r   rH   r   r   	old_style_canned_dictr   itemsr4   mroparentsr8   )r9   clskvr{   s        r   r;   zCannedClass.__init__   s    L	'T222L&&(( 	. 	.DAq333'*1vv!!$N1bb		00ABB000r   c                 D    t          |t                    s
J d            d S )NzNot a class type)r   
class_typere   s     r   r^   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   8   K   | ]}t          |          V  d S r   ri   )rY   pr>   s     r   r]   z)CannedClass.get_object.<locals>.<genexpr>  s+      ::a::::::r   )r>   )ra   r|   r   rH   
uncan_dictry   )r9   r>   r|   s    ` r   r?   zCannedClass.get_object
  sK    ::::T\:::::DIw
43D(J(J(JKKKr   r   ro   r   r   r   rq   rq      sR            ? ? ?L L L L L Lr   rq   c                        e Zd ZdZd ZddZdS )CannedArrayzA canned numpy array.c                    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| _        nH|j        j        r<t          d |j        j        
                                D                       rd| _        | j        r"t          j        |t                    g| _        dS  ||d          }t          |          g| _        dS )	rs   r   )ascontiguousarrayFTOc              3   (   K   | ]\  }}|d k    V  dS )r   Nr   )rY   dtszs      r   r]   z'CannedArray.__init__.<locals>.<genexpr>  s*      %X%XFBbCi%X%X%X%X%X%Xr   Ndtype)numpyr   shaper   fieldsdescrrF   pickledsumanyvaluesr$   dumpsPICKLE_PROTOCOLr8   buffer)r9   r3   r   s      r   r;   zCannedArray.__init__  s   ++++++Y
(+	(8KSY__cim
sy>>QDLLY#DLLY 	 #%X%Xci>N>U>U>W>W%X%X%X"X"X 	 DL< 	)"Lo>>?DLLL $#Ct444C"3KK=DLLLr   Nc                     ddl m} | j        d         }| j        rt	          j        |          S  ||| 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)  s`    $$$$$$|A< 	&<%%%z$dj11199$*EEEr   r   r@   r   r   r   r   r     sC        ) ) ),F F F F F Fr   r   c                   f    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dS )	CannedByteszA canned bytes object.bufreturnc                     t          | t                    r|                                 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  sG     c:&& 	!;;== #u%% 	::
r   c                     |g| _         dS )rs   N)r8   re   s     r   r;   zCannedBytes.__init__@  s    ur   Nc                 F    | j         d         }|                     |          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  sz          &,z5&2FFG E    \       r   r   c                       e Zd ZdZeZdS )CannedBufferzA canned buffer.N)r   r   rA   rB   r   r   r   r   r   r   r   J  s        DDDr   r   c                       e Zd ZdZeZdS )CannedMemoryViewzA canned memory view.N)r   r   rA   rB   r   r   r   r   r   r   r   P  s        DDDr   r   c                    t                      }|                    d           t          |                                           D ]\  }}t	          |t
                    rl	 t          |          }|                     |          | |<   C# t          $ r6 |r||vr|	                    d|d           |                     |           Y w xY wdS )z(import any string-keys in a type mappingzImporting canning mapz canning class not importable: %rT)exc_infoN)
r   debuglistrz   r   rF   r   r(   	Exceptionerror)mappingoriginallogr:   _r}   s         r   _import_mappingr   [  s    
,,CII%&&&w}}'' 
0 
0Qc3 		00!#&&  '{{3//  ! ! ! V8 3 3II@#PTIUUUC     	!		0
0 
0s    B=CCc                      t          |t                    rt           fd|D                       S t                     |u S )zNlike isinstance(obj, check), but strict

    This won't catch subclasses.
    c              3   :   K   | ]}t                    |u V  d S r   )r   )rY   r}   r3   s     r   r]   zistype.<locals>.<genexpr>r  s.      55499#555555r   )r   ra   r   r   )r3   checks   ` r   istyper   l  sK    
 % 65555u55555599r   c                 
   d}t                                           D ];\  }}t          |t                    rd} nt	          | |          r ||           c S <|r)t          t           t                     t          |           S | S )zprepare an object for picklingFT)r'   rz   r   rF   r   r   _original_can_mapr4   )r3   import_neededr}   canners       r   r4   r4   v  s     M}}  Vc3 	 ME#s 	6#;;	   	!23333xxJr   c                 d    t          | t                    r| j        dk    rt          |           S | S )zCan a class object.r   )r   r   r   rq   )r3   s    r   	can_classr     s3    #z""  s~'C'C3Jr   c                     t          | t                    r0i }|                                 D ]\  }}t          |          ||<   |S | S )zcan the *values* of a dict)r   dictrz   r4   )r3   newobjr~   r   s       r   can_dictr     sO    c4 IIKK 	 	DAqAF1IIJr   c                 x    t          | t                    r$t          |           } |d | D                       S | S )zcan the elements of a sequencec                 ,    g | ]}t          |          S r   rX   )rY   is     r   r[   z can_sequence.<locals>.<listcomp>  s    &&&Q#a&&&&&r   r   sequence_typesr   )r3   ts     r   can_sequencer     sC    c>"" (IIq&&#&&&'''Jr   c                    d}t                                           D ]<\  }}t          |t                    rd} nt          | |          r || |          c S =|r*t	          t           t
                     t          | |          S | S )zinvert canningFT)	uncan_maprz   r   rF   r   _original_uncan_mapr=   )r3   r>   r   r}   uncanners        r   r=   r=     s     M"** $ $Xc3 	 MEc3 	$8C#####	$   		#6777S!}}Jr   c                     t          | t                    r1i }|                                 D ]\  }}t          ||          ||<   |S | S )zUncan a dict object.)r   r   rz   r=   )r3   r>   r   r~   r   s        r   r   r     sQ    c4 IIKK 	$ 	$DAqaF1IIJr   c                 ~    t          | t                    r&t          |           } |fd| D                       S | S )zUncan a sequence.c                 0    g | ]}t          |          S r   ri   )rY   r   r>   s     r   r[   z"uncan_sequence.<locals>.<listcomp>  s!    +++!%1+++++r   r   )r3   r>   r   s    ` r   uncan_sequencer     sJ    c>"" -IIq++++s+++,,,Jr   znumpy.ndarrayc                 ,    |                      |          S r   )r?   )r3   r>   s     r   <lambda>r     s    !2!2 r   r   r   );rB   r2   r$   rm   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   rq   r   r   r   r   r   r   r4   r   r   r   ra   setr   r   r=   r   r   r   r'   r   r   DictAny__annotations__r   r   r   r   r   <module>r      s   D D D   



        	....... 	 	 	D	 $ $ $ $ $ $ 4 4 4 4 4 4 F    

)& & & & N	  *$ $ $2$ $ $4( ( ( ( ( ( ( (V" " " " " " " ",$ $ $ $ $ $ $ $$&L &L &L &L &L\ &L &L &LRL L L L L, L L L4!F !F !F !F !F, !F !F !FH    ,   ,    ;       {   0 0 0 0"    *     s#     (       [.	; z	 
"GFO 22*,	6;tVZ'(    LLNN nn&&   s   & ..