/ lib / ipykernel / serialize.pyc
serialize.pyc
 1  o

 2  0��c��@s�dZddlZejdedd�ddlZddlmZzddlmZm	Z	m
 3  Z
 4  mZmZm
Z
mZddlmZWneyOdd	lmZmZm	Z	m
 5  Z
 6  mZmZm
Z
mZYnwdd
 7  lmZmZefdd�Zd
d�Zeefdd�Zddd�Zeefdd�Zddd�ZdS)z*serialization utilities for apply messages�NzHipykernel.serialize is deprecated. It has moved to ipyparallel.serialize�)�
 8  stacklevel)�chain)�CannedObject�can�can_sequence�istype�sequence_types�uncan�uncan_sequence)�PICKLE_PROTOCOL)rrrrrr	r
 9  r)�	MAX_BYTES�	MAX_ITEMScCsfg}t|t�r1|jr1t|j�D]!\}}t|�|kr$d|j|<|�|�qt|t�r0|��|j|<q|S)z/extract buffers larger than a certain thresholdN)�
10  isinstancer�buffers�	enumerate�len�append�
11  memoryview�tobytes)�obj�	thresholdr�i�buf�r��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ipykernel\serialize.py�_extract_buffers1s
12  
13  �rcCsHt|t�r|jr t|j�D]\}}|dur|�d�|j|<q
dSdSdS)zrestore buffers extracted byNr)rrrr�pop)rrrrrrr�_restore_buffersAs��rcCs�g}t|t�rt|�|krt|�}|D]
14  }|�t||��qn3t|t�rFt|�|krFi}t|�D]}t||�}|�t||��|||<q0nt|�}|�t||��|�	dt
15  �|t��|S)a Serialize an object into a list of sendable buffers.
16  
17      Parameters
18      ----------
19      obj : object
20          The object to be serialized
21      buffer_threshold : int
22          The threshold (in bytes) for pulling out data buffers
23          to avoid pickling them.
24      item_threshold : int
25          The maximum number of items over which canning will iterate.
26          Containers (lists, dicts) larger than this will be pickled without
27          introspection.
28  
29      Returns
30      -------
31      [bufs] : list of buffers representing the serialized object.
32      r)
rr	rr�extendr�dict�sortedr�insert�pickle�dumpsr)r�buffer_threshold�item_thresholdrZcobj�c�krrr�serialize_objectIs"�
33  �r)cCs�t|�}|�d�}t�|�}t|t�r,t|�tkr,|D]}t||�qt	||�}||fSt|t
34  �rTt|�tkrTi}t|�D]}||}t||�t||�||<q=||fSt||�t||�}||fS)a"reconstruct an object serialized by serialize_object from data buffers.
35  
36      Parameters
37      ----------
38      buffers : list of buffers/bytes
39      g : globals to be used when uncanning
40  
41      Returns
42      -------
43      (newobj, bufs) : unpacked object, and the list of remaining unused buffers.
44      r)
�listrr#�loadsrr	rrrrr r!r
45  )r�g�bufsZpobjZcannedr'�newobjr(rrr�deserialize_objectos$
46  
47  
48  �
49  
5051  r/c
52  s�tt���fdd�|D���}t����}tt����fdd�|D���}tt|�t|�|d�}t�t	|�t
53  �g}	|	�t�|t
54  ��|	�|�|	�|�|	S)a�pack up a function, args, and kwargs to be sent over the wire
55  
56      Each element of args/kwargs will be canned for special treatment,
57      but inspection will not go any deeper than that.
58  
59      Any object whose data is larger than `threshold`  will not have their data copied
60      (only numpy arrays and bytes/buffers support zero-copy)
61  
62      Message will be a list of bytes/buffers of the format:
63  
64      [ cf, pinfo, <arg_bufs>, <kwarg_bufs> ]
65  
66      With length at least two + len(args) + len(kwargs)
67      c3s�|]	}t|���VqdS�N�r))�.0�arg)r%r&rr�	<genexpr>�s�z%pack_apply_message.<locals>.<genexpr>c3s �|]}t�|���VqdSr0r1)r2�key�r%r&�kwargsrrr4�s�
68  �)�nargs�	narg_bufs�kw_keys)
r*r�
from_iterabler!�keysr rr#r$rrrr)
69  �f�argsr7r%r&�arg_bufsr:�
70  kwarg_bufs�info�msgrr6r�pack_apply_message�s���
71  
72  rCTcCs�t|�}t|�dksJd��|�d�}tt�|�|�}|�d�}t�|�}|d|d�||dd�}}g}	t|d�D]}
73  t||�\}}|	�|�q>t	|	�}|rWJd��i}
|dD]
}t||�\}}||
|<q]|rqJd	��|||
fS)
74  zdunpack f,args,kwargs from buffers packed by pack_apply_message()
75      Returns: original f,args,kwargsrznot enough buffers!rNr9r8z#Shouldn't be any arg bufs left overr:z%Shouldn't be any kwarg bufs left over)
76  r*rrr
77  r#r+�ranger/r�tuple)r-r,�copy�pfr=�pinforAr?r@�	args_list�_r3r>r7r5�kwargrrr�unpack_apply_message�s&
78  
79  
80  "
81  
82  rLr0)NT)�__doc__�warnings�warn�DeprecationWarningr#�	itertoolsr�ipyparallel.serialize.canningrrrrr	r
83  r�ipyparallel.serialize.serializer�ImportError�ipykernel.pickleutil�jupyter_client.sessionr
rrrr)r/rCrLrrrr�<module>s,�$	,�

84  & %