
    Mh                        d Z ddlZddlZddlmZ 	 ddlmZmZmZm	Z	m
Z
mZmZ ddlmZ n# e$ r ddlmZmZmZmZm	Z	m
Z
mZmZ Y nw xY wddlmZmZ  ej        ded	
           efdZd ZeefdZddZeefdZddZdS )z*serialization utilities for apply messages    N)chain)CannedObjectcancan_sequenceistypesequence_typesuncanuncan_sequence)PICKLE_PROTOCOL)r   r   r   r   r   r   r	   r
   )	MAX_BYTES	MAX_ITEMSzHipykernel.serialize is deprecated. It has moved to ipyparallel.serialize   )
stacklevelc                 >   g }t          | t                    r| j        r~t          | j                  D ]i\  }}t	          |          |k    r d| j        |<   |                    |           8t          |t                    r|                                | j        |<   j|S )z/extract buffers larger than a certain thresholdN)
isinstancer   buffers	enumeratelenappend
memoryviewtobytes)obj	thresholdr   ibufs        S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/ipykernel/serialize.py_extract_buffersr   0   s    G#|$$ 	/ 	/,, 	/ 	/FAs3xx)##!%As#### C,, /!$AN    c                     t          | t                    r@| j        r;t          | j                  D ](\  }}||                    d          | j        |<   %dS dS dS )zrestore buffers extracted byNr   )r   r   r   r   pop)r   r   r   r   s       r   _restore_buffersr!   @   ss    #|$$ 0 0,, 	0 	0FAs{!(QA0 0 0 0	0 	0r   c                 ~   g }t          | t                    rKt          |           |k     r8t          |           }|D ]%}|                    t          ||                     &nt          | t                    ret          |           |k     rRi }t          |           D ]?}t          | |                   }|                    t          ||                     |||<   @n2t          |           }|                    t          ||                     |	                    dt          j        |t                               |S )a   Serialize an object into a list of sendable buffers.

    Parameters
    ----------
    obj : object
        The object to be serialized
    buffer_threshold : int
        The threshold (in bytes) for pulling out data buffers
        to avoid pickling them.
    item_threshold : int
        The maximum number of items over which canning will iterate.
        Containers (lists, dicts) larger than this will be pickled without
        introspection.

    Returns
    -------
    [bufs] : list of buffers representing the serialized object.
    r   )r   r   r   r   extendr   dictsortedr   insertpickledumpsr   )r   buffer_thresholditem_thresholdr   cobjcks          r   serialize_objectr.   H   s;   & Gc>"" As3xx.'@'@C   	B 	BANN+A/?@@AAAA	B	T		 As3xx.88 	 	ACFANN+A/?@@AAADGG	
 3xx'.>??@@@NN1fl499:::Nr   c                 :   t          |           }|                    d          }t          j        |          }t	          |t
                    r>t          |          t          k     r&|D ]}t          ||           t          ||          }nt	          |t                    rXt          |          t          k     r@i }t          |          D ]-}||         }t          ||           t          ||          ||<   .n t          ||           t          ||          }||fS )a"  reconstruct an object serialized by serialize_object from data buffers.

    Parameters
    ----------
    buffers : list of buffers/bytes
    g : globals to be used when uncanning

    Returns
    -------
    (newobj, bufs) : unpacked object, and the list of remaining unused buffers.
    r   )listr    r'   loadsr   r   r   r   r!   r
   r$   r%   r	   )r   gbufspobjcannedr,   newobjr-   s           r   deserialize_objectr7   n   s    ==D88A;;D\$Ffn%% "#f++	*A*A 	& 	&AQ%%%%**			 "#f++	"9"9 	$ 	$Aq	AQ%%%aF1II	$
 	&&&vq!!4<r   c                 ^   t          t          j        fd|D                                 }t                                                    }t          t          j        fd|D                                 }t          t          |          t          |          |          }t          j        t          |           t                    g}	|	                    t          j        |t                               |	                    |           |	                    |           |	S )a  pack up a function, args, and kwargs to be sent over the wire

    Each element of args/kwargs will be canned for special treatment,
    but inspection will not go any deeper than that.

    Any object whose data is larger than `threshold`  will not have their data copied
    (only numpy arrays and bytes/buffers support zero-copy)

    Message will be a list of bytes/buffers of the format:

    [ cf, pinfo, <arg_bufs>, <kwarg_bufs> ]

    With length at least two + len(args) + len(kwargs)
    c              3   :   K   | ]}t          |          V  d S Nr.   ).0argr)   r*   s     r   	<genexpr>z%pack_apply_message.<locals>.<genexpr>   s2      ddX[,S2BNSSddddddr   c              3   F   K   | ]}t          |                   V  d S r:   r;   )r<   keyr)   r*   kwargss     r   r>   z%pack_apply_message.<locals>.<genexpr>   sF       
 
PSVC[*:NKK
 
 
 
 
 
r   )nargs	narg_bufskw_keys)r0   r   from_iterabler%   keysr$   r   r'   r(   r   r   r   r#   )
fargsrA   r)   r*   arg_bufsrD   
kwarg_bufsinfomsgs
     ```     r   pack_apply_messagerM      s&     ddddd_cddddd H V[[]]##G 
 
 
 
 
 
W^
 
 
 	
 	
 J c$ii3x=='JJJD<A00
1CJJv|D/22333JJxJJzJr   Tc                    t          |           } t          |           dk    s
J d            |                     d          }t          t	          j        |          |          }|                     d          }t	          j        |          }| d|d                  | |d         d         }}g }	t          |d                   D ]*}
t          ||          \  }}|	                    |           +t          |	          }|r
J d            i }|d         D ]}t          ||          \  }}|||<   |r
J d	            |||fS )
zdunpack f,args,kwargs from buffers packed by pack_apply_message()
    Returns: original f,args,kwargsr   znot enough buffers!r   NrC   rB   z#Shouldn't be any arg bufs left overrD   z%Shouldn't be any kwarg bufs left over)
r0   r   r    r	   r'   r1   ranger7   r   tuple)r3   r2   copypfrG   pinforK   rI   rJ   	args_list_r=   rH   rA   r@   kwargs                   r   unpack_apply_messagerW      s\    ::Dt99>>>0>>>	!Bfl2""AHHQKKE<D 3${"3 34d4;L;N;N6OjHI4=!!  *8Q77XD>>>>>>FI  .z1==zsBBBBBBdF?r   r:   )NT)__doc__r'   warnings	itertoolsr   ipyparallel.serialize.canningr   r   r   r   r   r	   r
   ipyparallel.serialize.serializer   ImportErroripykernel.pickleutiljupyter_client.sessionr   r   warnDeprecationWarningr   r!   r.   r7   rM   rW    r   r   <module>rc      s   0 0
                          @??????   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 8 7 7 7 7 7 7 7 N    %.     0 0 0 ,5Y # # # #L   @ :CS\ " " " "J     s   + AA