
    Xh                        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 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y# e$ r ddlmZmZmZmZm	Z	m
Z
mZmZ Y Uw xY w)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                 6   g }t        | t              r| j                  rzt        | j                        D ]b  \  }}t	        |      |kD  r!d| j                  |<   |j                  |       5t        |t              sF|j                         | j                  |<   d |S )z/extract buffers larger than a certain thresholdN)
isinstancer   buffers	enumeratelenappend
memoryviewtobytes)obj	thresholdr   ibufs        G/var/www/html/myenv/lib/python3.12/site-packages/ipykernel/serialize.py_extract_buffersr   0   s~    G#|$, 	/FAs3x)#!%As# C,!$A	/ N    c                     t        | t              rL| j                  r?t        | j                        D ]&  \  }}|	|j	                  d      | j                  |<   ( yyy)zrestore buffers extracted byNr   )r   r   r   r   pop)r   r   r   r   s       r   _restore_buffersr!   @   sP    #|$, 	0FAs{!(QA	0 *5$r   c                     g }t        | t              r<t        |       |k  r.t        |       }|D ]  }|j	                  t        ||              nt        | t              rOt        |       |k  rAi }t        |       D ]0  }t        | |         }|j	                  t        ||             |||<   2 n&t        |       }|j	                  t        ||             |j                  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>"s3x.'@C  	BANN+A/?@A	B	T	s3x.8 	ACFANN+A/?@ADG	
 3x'.>?@NN1fll49:Nr   c                    t        |       }|j                  d      }t        j                  |      }t	        |t
              r5t        |      t        k  r#|D ]  }t        ||        t        ||      }||fS t	        |t              rHt        |      t        k  r6i }t        |      D ]"  }||   }t        ||       t        ||      ||<   $ ||fS 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Q%	&* 4< 
	#f+	"9 	$Aq	AQ%aF1I	$ 4< 	&vq!4<r   c                    t        t        j                  fd|D                    }t        j	                               }t        t        j                  fd|D                    }t        t        |      t        |      |      }t        j                  t        |       t              g}	|	j                  t        j                  |t                     |	j                  |       |	j                  |       |	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   8   K   | ]  }t        |        y wNr.   ).0argr)   r*   s     r   	<genexpr>z%pack_apply_message.<locals>.<genexpr>   s     dX[,S2BNSds   c              3   >   K   | ]  }t        |           y wr:   r;   )r<   keyr)   r*   kwargss     r   r>   z%pack_apply_message.<locals>.<genexpr>   s%      
PSVC[*:NK
s   )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_cddH V[[]#G 
W^
 	
J c$i3x='JD<<A0
1CJJv||D/23JJxJJzJr   c                    t        |       } t        |       dk\  sJ d       | j                  d      }t        t	        j
                  |      |      }| j                  d      }t	        j
                  |      }| d|d    | |d   d }}g }	t        |d         D ]"  }
t        ||      \  }}|	j                  |       $ 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t9>000>	!Bfll2"AHHQKE<<D 3${"34d4;L;N6OjHI4=! *8Q7X D>>><FI .z1=zs 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
      @ 8 N %.  0 ,5Y #L@ :CS\ "J{  	 	 	s   A$ $B ?B 