
{]c           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l 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 m Z m Z m Z m Z m Z m Z m Z m Z m Z e j e j Z Z d d  Z  d d  Z! d   Z" d	   Z# d
   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 d   Z1 d   Z2 d   Z3 d   Z4 d   Z5 d   Z6 d   Z7 d e f d     YZ8 d S(    s  
New, fast version of the CloudPickler.

This new CloudPickler class can now extend the fast C Pickler instead of the
previous Python implementation of the Pickler class. Because this functionality
is only available for Python versions 3.8+, a lot of backward-compatibility
code is also removed.

Note that the C Pickler sublassing API is CPython-specific. Therefore, some
guards present in cloudpickle.py that were written to handle PyPy specificities
are not present in cloudpickle_fast.py
iN(   t   Pickleri   (   t   _is_dynamict   _extract_code_globalst   _BUILTIN_TYPE_NAMESt   DEFAULT_PROTOCOLt   _find_imported_submodulest   _get_cell_contentst
   _is_globalt   _builtin_typet   Enumt   _ensure_trackingt   _make_skeleton_classt   _make_skeleton_enumt   _extract_class_dictt   string_typest   dynamic_subimportt	   subimportc         C   s   t  | d | j |   d S(   sw  Serialize obj as bytes streamed into file

    protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
    pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
    between processes running the same Python version.

    Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
    compatibility with older versions of Python.
    t   protocolN(   t   CloudPicklert   dump(   t   objt   fileR   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR   %   s    
c         C   sB   t  j   0 } t | d | } | j |   | j   SWd QXd S(   s  Serialize obj as a string of bytes allocated in memory

    protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
    pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
    between processes running the same Python version.

    Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
    compatibility with older versions of Python.
    R   N(   t   iot   BytesIOR   R   t   getvalue(   R   R   R   t   cp(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   dumps2   s    
c         C   s~   i  } t  |  d  r% |  j | d <n  |  j j d d   } t | t  rV | | d <n  t |   |  j |  j	 | t
 |   d  f S(   Nt	   __slots__t   __dict__(   t   hasattrR   R   t   gett   Nonet
   isinstancet   propertyt   typet   __name__t	   __bases__R
   (   R   t   type_kwargsR   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _class_getnewargsE   s    c         C   sA   t  d   |  D  } |  j |  j |  j | |  j t |   d  f S(   Nc         s   s!   |  ] } | j  | j f Vq d  S(   N(   t   namet   value(   t   .0t   e(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pys	   <genexpr>S   s    (   t   dictR$   R#   t   __qualname__t
   __module__R
   R   (   R   t   members(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _enum_getnewargsR   s    c         C   s   |  S(   N(    (   t   retval(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _file_reconstructorZ   s    c            s   i   j  d 6  j d 6  j d 6  j d 6  j d 6  j d 6  j d 6  j d 6} t   j	  }   f d	   | D }   j d  k	 r t t t   j   n d } t   j	 t j | j   |   | d
 <| | d <  j } | | f S(   NR#   R,   t   __annotations__t   __kwdefaults__t   __defaults__R-   t   __doc__t   __closure__c            s/   i  |  ]% } |   j  k r   j  | |  q S(    (   t   __globals__(   R)   t   k(   t   func(    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pys
   <dictcomp>s   s   	 t   _cloudpickle_submodulesR7   (    (   R#   R,   R2   R3   R4   R-   R5   R6   R   t   __code__R   t   listt   mapR   R   t	   itertoolst   chaint   valuesR   (   R9   t	   slotstatet   f_globals_reft	   f_globalst   closure_valuest   state(    (   R9   s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _function_getstate`   s"    






-%
	c         C   s   t  |   } | j d d   d | k rf t j |   \ } } } } g  | D] } |   ^ qJ | d <n  t |  d  r t |  j t  r | j |  j  q x$ |  j D] } | j | d   q Wn  | j d d   | i  f S(   Nt   __weakref__t	   _abc_implR   R   (	   R   t   popR   t   abct	   _get_dumpR   R    R   R   (   R   t   clsdictt   registryt   _t   subclass_weakrefR8   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _class_getstate   s     c         C   s   t  |   \ } } t d   |  D  } x- d d d d d g D] } | j | d   q> Wx | D] } | j |  q_ W| | f S(   Nc         s   s!   |  ] } | j  | j f Vq d  S(   N(   R'   R(   (   R)   R*   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pys	   <genexpr>   s    t   _generate_next_value_t   _member_names_t   _member_map_t   _member_type_t   _value2member_map_(   RP   R+   RI   R   (   R   RL   RA   R.   t   attrnamet   member(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _enum_getstate   s    	c         C   ss   |  j  |  j |  j |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j |  j |  j |  j |  j f } t j | f S(   s   codeobject reducer(   t   co_argcountt   co_posonlyargcountt   co_kwonlyargcountt
   co_nlocalst   co_stacksizet   co_flagst   co_codet	   co_constst   co_namest   co_varnamest   co_filenamet   co_namet   co_firstlinenot	   co_lnotabt   co_freevarst   co_cellvarst   typest   CodeType(   R   t   args(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _code_reduce   s    c         C   s@   y |  j  Wn t k
 r( t j d f SXt j |  j  f f Sd S(   s?   Cell (containing values of a function's free variables) reducerN(    (   t   cell_contentst
   ValueErrorRi   t   CellType(   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _cell_reduce   s
    c         C   s   |  j  } t |   | f f S(   N(   t   __func__R"   (   R   t	   orig_func(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _classmethod_reduce   s    	c         C   s  d d l  } t |  d  s, t |  d  r> t j d   n  |  t j k r] t t d f f S|  t j k r| t t d f f S|  t j k r t j d   n  |  j	 r t j d	   n  t |  d
  r |  j
   r t j d   n  d |  j k rd |  j k rt j d |  j   n  |  j } | j   } y6 |  j   } |  j d  |  j   } |  j |  Wn$ t k
 rt j d |   n X| j |  | j |  | | _ t | f f S(   s   Save a fileiNR'   t   modes5   Cannot pickle files that do not map to an actual filet   stdoutt   stderrs   Cannot pickle standard inputs   Cannot pickle closed filest   isattys+   Cannot pickle files that map to tty objectst   rt   +s7   Cannot pickle files that are not opened for reading: %si    s*   Cannot pickle file %s as it cannot be read(   R   R   t   picklet   PicklingErrort   sysRu   t   getattrRv   t   stdint   closedRw   Rt   R'   t   StringIOt   tellt   seekt   readt   IOErrort   writeR1   (   R   R   R'   R0   t   curloct   contents(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _file_reduce   sB     			c         C   s   t  |  j |  j f f S(   N(   R}   t   __objclass__R#   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _getset_descriptor_reduce  s    c         C   s   t  j t |   f f S(   N(   Ri   t   MappingProxyTypeR+   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _mappingproxy_reduce  s    c         C   s   t  |  j   f f S(   N(   t   bytest   tobytes(   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _memoryview_reduce  s    c         C   s9   t  |   r% t |  j t |   f f St |  j f f Sd  S(   N(   R   R   R#   t   varsR   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _module_reduce  s    c         C   s   t  j |  j |  j f f S(   N(   Ri   t
   MethodTypeRq   t   __self__(   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _method_reduce  s    c         C   s   t  j |  j f f S(   N(   t   loggingt	   getLoggerR'   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _logger_reduce  s    c         C   s   t  j d f S(   N(    (   R   R   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _root_logger_reduce"  s    c         C   s   t  j t |   f f S(   N(   t   weakreft   WeakSetR<   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _weakset_reduce&  s    c         C   sc   t  d k	 r= t |  t   r= t t |   t |   d d t f St t |   t	 |   d d t f Sd S(   s   
    Save a class that can't be stored as module global.

    This method is used to serialize classes that are defined inside
    functions, or that otherwise can't be serialized as attribute lookups
    from global modules.
    N(
   R	   R   t
   issubclassR   R/   RX   t   _class_setstateR   R&   RP   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _dynamic_class_reduce*  s
    c         C   s   |  t  d  k r t  d f S|  t  t  k r; t  t f f S|  t  t  k rZ t  t f f S|  t k rw t t |  f f St |   s t |   St S(   sC   Select the reducer depending on the dynamic nature of the class objN(   N(   R"   R   t   Ellipsist   NotImplementedR   R   R   R   (   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _class_reduce>  s    

c   
      C   s   | \ } } |  j  j |  | j d  } | j d  } | j d  |  j j |  t |  j d <| d k	 r xQ t |  D]@ \ } } y | j } Wn t k
 r q} n X| |  j	 | _ q} Wn  x* | j
   D] \ } }	 t |  | |	  q Wd S(   s  Update the state of a dynaamic function.

    As __closure__ and __globals__ are readonly attributes of a function, we
    cannot rely on the native setstate routine of pickle.load_build, that calls
    setattr on items of the slotstate. Instead, we have to modify them inplace.
    R7   R6   R:   t   __builtins__N(   R   t   updateRI   R7   R   R   t	   enumerateRm   Rn   R6   t   itemst   setattr(
   R   RE   RA   t   obj_globalst   obj_closuret   it   cellR(   R8   t   v(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _function_setstateS  s     c         C   s   | \ } } d  } x? | j   D]1 \ } } | d k r@ | } q t |  | |  q W| d  k	 r x | D] } |  j |  qg Wn  |  S(   NRH   (   R   R   R   t   register(   R   RE   RA   RM   RV   t   attrt   subclass(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR   u  s    	R   c           B   s   e  Z d  Z i  Z e e e <e e e j <e	 e e
 j <e e e
 j <e e e <e e e <e e e j <e e e j <e e e j <e e e j <e e e j <e e e j <e e e j  <d d  Z" d   Z# d   Z$ d   Z% d   Z& d   Z' RS(   s  Fast C Pickler extension with additional reducing routines.

    CloudPickler's extensions exist into into:

    * its dispatch_table containing reducers that are called only if ALL
      built-in saving functions were previously discarded.
    * a special callback named "reducer_override", invoked before standard
      function/class builtin-saving method (save_global), to serialize dynamic
      functions
    c         C   sl   | d  k r t } n  t j |  | d | i  |  _ t j j   |  _ |  j j |  j	  t
 |  |  _ d  S(   NR   (   R   R   R    t   __init__t   globals_reft   copyregt   dispatch_tablet   copyR   t   dispatcht   intt   proto(   t   selfR   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR     s    		c         C   sp   t  |  } y t | t   } Wn t k
 r8 t } n X| rI t |  St | t j  rh |  j |  St	 Sd S(   s  Type-agnostic reducing callback for function and classes.

        For performance reasons, subclasses of the C _pickle.Pickler class
        cannot register custom reducers for functions and classes in the
        dispatch_table. Reducer for such types must instead implemented in the
        special reducer_override method.

        Note that method will be called for any object except a few
        builtin-types (int, lists, dicts etc.), which differs from reducers in
        the Pickler's dispatch_table, each of them being invoked for objects of
        a specific type only.

        This property comes in handy for classes: although most classes are
        instances of the ``type`` metaclass, some of them can be instances of
        other custom metaclasses (such as enum.EnumMeta for example). In
        particular, the metaclass will likely not be known in advance, and thus
        cannot be special-cased using an entry in the dispatch_table.
        reducer_override, among other things, allows us to register a reducer
        that will be called for any class, independently of its type.


        Notes:

        * reducer_override has the priority over dispatch_table-registered
          reducers.
        * reducer_override can be used to fix other limitations of cloudpickle
          for other types that suffered from type-specific reducers, such as
          Exceptions. See https://github.com/cloudpipe/cloudpickle/issues/248
        N(
   R"   R   t	   TypeErrort   FalseR   R    Ri   t   FunctionTypet   _function_reduceR   (   R   R   t   tt   is_anyclass(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   reducer_override  s    

c         C   s4   |  j  |  } t |  } t j | | d d t f S(   s>   Reduce a function that is not pickleable via attribute lookup.N(   t   _function_getnewargsRF   Ri   R   R   R   (   R   R9   t   newargsRE   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   _dynamic_function_reduce  s    c         C   s!   t  |  r t S|  j |  Sd S(   s  Reducer for function objects.

        If obj is a top-level attribute of a file-backed module, this
        reducer returns NotImplemented, making the CloudPickler fallback to
        traditional _pickle.Pickler routines to save obj. Otherwise, it reduces
        obj using a custom cloudpickle reducer designed specifically to handle
        dynamic functions.

        As opposed to cloudpickle.py, There no special handling for builtin
        pypy functions because cloudpickle_fast is CPython-specific.
        N(   R   R   R   (   R   R   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR     s    c         C   s   | j  } |  j j t | j  i   } | i  k rv x@ d d d d g D]) } | | j k rF | j | | | <qF qF Wn  | j d  k r d  } n% t d   t t	 | j
   D  } | | d  d  | f S(   Nt   __package__R#   t   __path__t   __file__c         s   s   |  ] } t  j   Vq d  S(   N(   Ri   Ro   (   R)   RN   (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pys	   <genexpr>  s    (   R;   R   t
   setdefaultt   idR7   R6   R   t   tuplet   ranget   lenRg   (   R   R9   t   codet   base_globalsR8   t   closure(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR     s    		"c         C   s\   y t  j |  |  SWnA t k
 rW } d | j d k rQ d } t j |   qX   n Xd  S(   Nt	   recursioni    s?   Could not pickle object as excessively deep recursion required.(   R    R   t   RuntimeErrorRk   Rz   R{   (   R   R   R*   t   msg(    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR     s    N((   R#   R-   R5   R   Rs   t   classmethodR   R   t   TextIOWrapperR   R   t   LoggerR   t
   RootLoggerR   t
   memoryviewt   staticmethodRp   Ri   Ro   Rl   Rj   R   t   GetSetDescriptorTypeR   t
   ModuleTypeR   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyR     s*   



	.			!(9   R5   RJ   R   R   R>   R   t   _pickleRz   R|   Ri   R   R    t   cloudpickleR   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   t   loadt   loadsR   R   R   R&   R/   R1   RF   RP   RX   Rl   Rp   Rs   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s;   lib/python2.7/site-packages/cloudpickle/cloudpickle_fast.pyt   <module>   sL   j				'				
		/											"	