
    Ch                        S r SSKrSSKrSSKJr   SSKJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJrJr  \R(                  " S\S	S
9  \4S jrS r\\4S jrSS jr\\4S jrSS jrg! \ a    SSKJrJrJrJrJ	r	J
r
JrJr   NXf = f)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                 \   / n[        U [        5      (       a  U R                  (       a  [        U R                  5       Hj  u  p4[	        U5      U:  a"  SU R                  U'   UR                  U5        M6  [        U[        5      (       d  MM  UR                  5       U R                  U'   Ml     U$ )z/extract buffers larger than a certain thresholdN)
isinstancer   buffers	enumeratelenappend
memoryviewtobytes)obj	thresholdr   ibufs        fC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\ipykernel/serialize.py_extract_buffersr   0   s}    G#|$$,FA3x)#!%As# C,,!$A - N    c                     [        U [        5      (       aT  U R                  (       aB  [        U R                  5       H(  u  p#Ub  M
  UR	                  S5      U R                  U'   M*     ggg)zrestore buffers extracted byNr   )r   r   r   r   pop)r   r   r   r   s       r   _restore_buffersr!   @   sL    #|$$,FA{!(QA - *5$r   c                    / n[        U [        5      (       a>  [        U 5      U:  a/  [        U 5      nU H  nUR	                  [        XQ5      5        M     O[        U [        5      (       aO  [        U 5      U:  a@  0 n[        U 5       H.  n[        X   5      nUR	                  [        XQ5      5        XTU'   M0     O%[        U 5      nUR	                  [        XA5      5        UR                  S[        R                  " U[        5      5        U$ )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 ANN+A@A 	T		s3x.8ACFANN+A@AG 
 3x'?@NN1fll49:Nr   c                    [        U 5      nUR                  S5      n[        R                  " U5      n[	        U[
        5      (       a5  [        U5      [        :  a"  U H  n[        XR5        M     [        XA5      nXb4$ [	        U[        5      (       aF  [        U5      [        :  a3  0 n[        U5       H  nXG   n[        XR5        [        XQ5      Xg'   M!     Xb4$ [        XB5        [        XA5      nXb4$ )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% * < 
		#f+	"9A	AQ%aFI   < 	&v!<r   c                   ^^^ [        [        R                  " UU4S jU 5       5      5      n[        TR	                  5       5      n[        [        R                  " UUU4S jU 5       5      5      n[        [        U5      [        U5      US9n[        R                  " [        U 5      [        5      /n	U	R                  [        R                  " U[        5      5        U	R                  U5        U	R                  U5        U	$ )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   >   >#    U  H  n[        UTT5      v   M     g 7fNr.   ).0argr)   r*   s     r   	<genexpr>%pack_apply_message.<locals>.<genexpr>   s"     d_cX[,S2BNSS_cs   c              3   D   >#    U  H  n[        TU   TT5      v   M     g 7fr:   r;   )r<   keyr)   r*   kwargss     r   r>   r?      s(      
W^PSVC[*:NKKW^s    )nargs	narg_bufskw_keys)r0   r   from_iterabler%   keysr$   r   r'   r(   r   r   r   r#   )
fargsrB   r)   r*   arg_bufsrE   
kwarg_bufsinfomsgs
     ```     r   pack_apply_messagerN      s      d_cddH V[[]#G 
W^
 	
J c$i3x='JD<<A0
1CJJv||D/23JJxJJzJr   c                    [        U 5      n [        U 5      S:  d   S5       eU R                  S5      n[        [        R
                  " U5      U5      nU R                  S5      n[        R
                  " U5      nU SUS    XS   S p/ n	[        US   5       H!  n
[        Xq5      u  pU	R                  U5        M#     [        U	5      nU(       a   S5       e0 nUS    H  n[        X5      u  pXU'   M     U(       a   S	5       eXLU4$ )
z`unpack f,args,kwargs from buffers packed by pack_apply_message()
Returns: original f,args,kwargsr   znot enough buffers!r   NrD   rC   z#Shouldn't be any arg bufs left overrE   z%Shouldn't be any kwarg bufs left over)
r0   r   r    r	   r'   r1   ranger7   r   tuple)r3   r2   copypfrH   pinforL   rJ   rK   	args_list_r=   rI   rB   rA   kwargs                   r   unpack_apply_messagerX      s    :Dt9>000>	!Bfll2"AHHQKE<<D 3${"34d;L;N6OjI4=!*87 " D>>><FI.z=s  BBB>F?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   rN   rX    r   r   <module>rd      s    0
      @ 8 N %.  0 ,5Y #L@ :CS\ "J{  	 	 	s   A& &BB