ó
1¯‚Xc           @   s+  d  Z  d d l Z e j d e ƒ y d d l Z e Z Wn d Z d d l Z n Xd d l m Z d d l	 m
 Z
 m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z e
 rÙ e Z n  e d „ Z d	 „  Z e e d
 „ Z d d „ Z e e d „ Z d e d „ Z  d S(   s*   serialization utilities for apply messagesiÿÿÿÿNsH   ipykernel.serialize is deprecated. It has moved to ipyparallel.serialize(   t   chain(   t   PY3t   buffer_to_bytes_py2(   t   cant   uncant   can_sequencet   uncan_sequencet   CannedObjectt   istypet   sequence_typest   PICKLE_PROTOCOL(   t	   MAX_ITEMSt	   MAX_BYTESc         C   s¾   g  } t  |  t ƒ rº |  j rº x™ t |  j ƒ D]… \ } } t | ƒ | k ri d |  j | <| j | ƒ q. t  | t ƒ rŽ | j ƒ  |  j | <q. t  | t	 ƒ r. t
 | ƒ |  j | <q. q. Wn  | S(   s/   extract buffers larger than a certain thresholdN(   t
   isinstanceR   t   bufferst	   enumeratet   lent   Nonet   appendt
   memoryviewt   tobytest   buffert   bytes(   t   objt	   thresholdR   t   it   buf(    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   _extract_buffers"   s    c         C   sd   t  |  t ƒ r` |  j r` xE t |  j ƒ D]1 \ } } | d k r( | j d ƒ |  j | <q( q( Wn  d S(   s   restore buffers extracted by i    N(   R   R   R   R   R   t   pop(   R   R   R   R   (    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   _restore_buffers3   s    c         C   s  g  } t  |  t ƒ r] t |  ƒ | k  r] t |  ƒ } xº | D] } | j t | | ƒ ƒ q: Wn“ t  |  t ƒ rÎ t |  ƒ | k  rÎ i  } xi t |  ƒ D]6 } t |  | ƒ } | j t | | ƒ ƒ | | | <q‘ Wn" t |  ƒ } | j t | | ƒ ƒ | j	 d t
 j | t ƒ ƒ | S(   s!  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.
    i    (   R   R	   R   R   t   extendR   t   dictt   sortedR   t   insertt   picklet   dumpsR
   (   R   t   buffer_thresholdt   item_thresholdR   t   cobjt   ct   k(    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   serialize_object:   s    !!c         C   s  t  |  ƒ } t | j d ƒ ƒ } t j | ƒ } t | t ƒ r t | ƒ t k  r x | D] } t	 | | ƒ qX Wt
 | | ƒ } n‡ t | t ƒ rì t | ƒ t k  rì i  } x] t | ƒ D]0 } | | } t	 | | ƒ t | | ƒ | | <qµ Wn t	 | | ƒ t | | ƒ } | | f S(   s"  reconstruct an object serialized by serialize_object from data buffers.

    Parameters
    ----------

    bufs : list of buffers/bytes

    g : globals to be used when uncanning

    Returns
    -------

    (newobj, bufs) : unpacked object, and the list of remaining unused buffers.
    i    (   t   listR   R   R"   t   loadsR   R	   R   R   R   R   R   R    R   (   R   t   gt   bufst   pobjt   cannedR'   t   newobjR(   (    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   deserialize_object`   s     !!
c   
         sÞ   t  t j ‡  ‡ f d †  | Dƒ ƒ ƒ } t ˆ j ƒ  ƒ } t  t j ‡  ‡ ‡ f d †  | Dƒ ƒ ƒ } t d t | ƒ d t | ƒ d | ƒ } t j t	 |  ƒ t
 ƒ g }	 |	 j t j | t
 ƒ ƒ |	 j | ƒ |	 j | ƒ |	 S(   sò  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   s!   |  ] } t  | ˆ  ˆ ƒ Vq d  S(   N(   R)   (   t   .0t   arg(   R$   R%   (    s2   lib/python2.7/site-packages/ipykernel/serialize.pys	   <genexpr>“   s    c         3   s%   |  ] } t  ˆ | ˆ  ˆ ƒ Vq d  S(   N(   R)   (   R2   t   key(   R$   R%   t   kwargs(    s2   lib/python2.7/site-packages/ipykernel/serialize.pys	   <genexpr>—   s    t   nargst	   narg_bufst   kw_keys(   R*   R    t   from_iterableR    t   keysR   R   R"   R#   R   R
   R   R   (
   t   ft   argsR5   R$   R%   t   arg_bufsR8   t
   kwarg_bufst   infot   msg(    (   R$   R%   R5   s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   pack_apply_message‚   s    		"'c         C   sT  t  |  ƒ }  t |  ƒ d k s* t d ƒ ‚ t |  j d ƒ ƒ } t t j | ƒ | ƒ } t |  j d ƒ ƒ } t j | ƒ } |  | d  |  | d } } g  }	 x: t | d ƒ D]( }
 t	 | | ƒ \ } } |	 j
 | ƒ q¯ Wt |	 ƒ }	 | sú t d ƒ ‚ i  } x1 | d D]% } t	 | | ƒ \ } } | | | <qW| sGt d ƒ ‚ | |	 | f S(	   sd   unpack f,args,kwargs from buffers packed by pack_apply_message()
    Returns: original f,args,kwargsi   s   not enough buffers!i    R7   R6   s#   Shouldn't be any arg bufs left overR8   s%   Shouldn't be any kwarg bufs left over(   R*   R   t   AssertionErrorR   R   R   R"   R+   t   rangeR1   R   t   tuple(   R-   R,   t   copyt   pfR;   t   pinfoR?   R=   R>   R<   R   R3   R5   R4   t   kwarg(    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   unpack_apply_message¢   s&    (!   t   __doc__t   warningst   warnt   DeprecationWarningt   cPickleR"   R   t	   itertoolsR    t   ipython_genutils.py3compatR   R   t   ipykernel.pickleutilR   R   R   R   R   R   R	   R
   t   jupyter_client.sessionR   R   R   R   R   R   R)   R1   RA   t   TrueRI   (    (    (    s2   lib/python2.7/site-packages/ipykernel/serialize.pyt   <module>   s(   
:		&" 