U
    ,dw                     @   sr  d Z ddlm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ZddlmZ ddlmZmZmZmZ ddlmZ ddlmZ zddlZdd	lmZmZ W n  ek
r   d Z ZZY nX ejd
krddlmZ ndZejdkrddlmZ ndd Ze e j!d Zej"Z#e	$ Z%e	& Z'e( Z)e	* Z+e, dkZ-dZ.e-rxe e/j0j1Z.e	$ Z2dd Z3dd Z4dd Z5dbddZ6dcddZ7dd Z8dd Z9dd  Z:d!d" Z;ejdd# d$k re; Z<ej=d% Z>ej=d& Z?ej=d' Z@e>e?e@fZAejBZBejCZCi ZDejEF D ] \ZGZHe eHe kr(eGeDeH< q(d(d) ZId*d+ ZJd,d- ZKejdd# d$k rd.d/ ZLd0d1 ZMndZLdZMd2d3 ZNd4d5 ZOd6d7 ZPejQZQejRZRd8d9 ZSd:d; ZTd<d= ZUd>d? ZVd@dA ZWdBdC ZXeXG dDdE dEeYZZdFdG Z[dHdI Z\eZfdJdKZ]dddLdMZ^dNdO Z_dPdQ Z`dRdS ZadTdU ZbdVdW ZcdXdY ZddZd[ Zed\d] Zfd^d_ Zgd`da ZhdS )eaU  
This class is defined to override standard pickle functionality

The goals of it follow:
-Serialize lambdas and nested functions to compiled byte code
-Deal with main module correctly
-Deal with other non-serializable objects

It does not include an unpickler, as standard python unpickling suffices.

This module was extracted from the `cloud` package, developed by `PiCloud, Inc.
<https://web.archive.org/web/20140626004012/http://www.picloud.com/>`_.

Copyright (c) 2012, Regents of the University of California.
Copyright (c) 2009 `PiCloud, Inc. <https://web.archive.org/web/20140626004012/http://www.picloud.com/>`_.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the University of California, Berkeley nor the
      names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    )print_functionN   )pickle)GenericUnionTupleCallable)_getattribute)
_find_spec)LiteralFinal      r   )ClassVar)r      )CellTypec                     s   d  fdd} | S )Nr   c                      s    S N r   ar   A/tmp/pip-unpacked-wheel-eu7e0c37/numba/cloudpickle/cloudpickle.pygO   s    zf.<locals>.gr   )r   r   r   r   fL   s    r   PyPyc              	   C   s@   t 2 t| }|d kr2t j}|t| < | t|< W 5 Q R X |S r   )_DYNAMIC_CLASS_TRACKER_LOCK_DYNAMIC_CLASS_TRACKER_BY_CLASSgetuuidZuuid4hex_DYNAMIC_CLASS_TRACKER_BY_ID)	class_defclass_tracker_idr   r   r   _get_or_create_tracker_idl   s    

r#   c              	   C   sF   | d k	rBt 0 |}t| |}| t|< ||k	r8t| W 5 Q R X |S r   )r   r    
setdefaultr   _DYNAMIC_CLASS_TRACKER_REUSINGadd)r"   r!   Zorig_class_defr   r   r   _lookup_class_or_trackv   s     r'   c              	   C   s   t jdd dk r$t| tjr$d}nt| dd}|dk	r<|S t j  D ]\\}}|dksJ|dksJt|t	j
spqJz t||d | kr|W   S W qJ tk
r   Y qJX qJdS )aU  Find the module an object belongs to.

    This function differs from ``pickle.whichmodule`` in two ways:
    - it does not mangle the cases where obj's module is __main__ and obj was
      not found in any module.
    - Errors arising during module introspection are ignored, as those errors
      are considered unwanted side effects.
    N   r      
__module____main__r   )sysversion_info
isinstancetypingTypeVargetattrmodulescopyitemstypes
ModuleTyper	   	Exception)objnamemodule_namemoduler   r   r   _whichmodule   s&    	
r=   c                 C   sj   t | tjrt| |ddk	S tt| tr:t| |ddk	S t | tjrR| jtj	kS t
dt| jdS )zCDispatcher utility to test the importability of various constructs.r:   Nz*cannot check importability of {} instances)r/   r6   FunctionType_lookup_module_and_qualname
issubclasstyper7   __name__r-   r3   	TypeErrorformat)r9   r:   r   r   r   _is_importable   s    rF   c                 C   s   |d krt | dd }|d kr(t | dd }t| |}|d kr>d S |dkrJd S tj|d }|d krdd S zt||\}}W n tk
r   Y d S X || k	rd S ||fS )N__qualname__rC   r,   )r2   r=   r-   r3   r   r	   AttributeError)r9   r:   r;   r<   obj2parentr   r   r   r@      s&    
r@   c                    sd   t | }|dkr`| j  fddt| D }| jrX| jD ]}t|tjr:|t|O }q:|t | < |S )zC
    Find all globals names read or written to by codeblock co
    Nc                    s   h | ]\}} | qS r   r   ).0_Zopargnamesr   r   	<setcomp>   s     z(_extract_code_globals.<locals>.<setcomp>)	_extract_code_globals_cacher   co_names_walk_global_ops	co_constsr/   r6   CodeType_extract_code_globals)coZ	out_namesconstr   rM   r   rU      s    

rU   c                 C   s   g }|D ]}t |tjrt|dr|jr|jd }ttjD ]N}|dk	r<|	|r<t
|t|d d}|t
| j s<|tj|  q<q|S )a&  
    Find currently imported submodules used by a function.

    Submodules used by a function need to be detected and referenced for the
    function to work correctly at depickling time. Because submodules can be
    referenced as attribute of their parent package (``package.submodule``), we
    need a special introspection technique that does not rely on GLOBAL-related
    opcodes to find references of them in a code object.

    Example:
    ```
    import concurrent.futures
    import cloudpickle
    def func():
        x = concurrent.futures.ThreadPoolExecutor
    if __name__ == '__main__':
        cloudpickle.dumps(func)
    ```
    The globals extracted by cloudpickle in the function's state include the
    concurrent package, but not its submodule (here, concurrent.futures), which
    is the module used by func. Find_imported_submodules will detect the usage
    of concurrent.futures. Saving this module alongside with func will ensure
    that calling func once depickled does not fail due to concurrent.futures
    not being imported
    __package__.N)r/   r6   r7   hasattrrX   rC   listr-   r3   
startswithsetlensplitrQ   append)codeZtop_level_dependenciesZ
subimportsxprefixr:   tokensr   r   r   _find_imported_submodules  s    
re   c                 C   s:   t jdd dkr|| _ntti dd| f}|| dS )aZ
  Set the value of a closure cell.

    The point of this function is to set the cell_contents attribute of a cell
    after its creation. This operation is necessary in case the cell contains a
    reference to the function the cell belongs to, as when calling the
    function's constructor
    ``f = types.FunctionType(code, globals, name, argdefs, closure)``,
    closure will not be able to contain the yet-to-be-created f.

    In Python3.7, cell_contents is writeable, so setting the contents of a cell
    can be done simply using
    >>> cell.cell_contents = value

    In earlier Python3 versions, the cell_contents attribute of a cell is read
    only, but this limitation can be worked around by leveraging the Python 3
    ``nonlocal`` keyword.

    In Python2 however, this attribute is read only, and there is no
    ``nonlocal`` keyword. For this reason, we need to come up with more
    complicated hacks to set this attribute.

    The chosen approach is to create a function with a STORE_DEREF opcode,
    which sets the content of a closure variable. Typically:

    >>> def inner(value):
    ...     lambda: cell  # the lambda makes cell a closure
    ...     cell = value  # cell is a closure, so this triggers a STORE_DEREF

    (Note that in Python2, A STORE_DEREF can never be triggered from an inner
    function. The function g for example here
    >>> def f(var):
    ...     def g():
    ...         var += 1
    ...     return g

    will not modify the closure variable ``var```inplace, but instead try to
    load a local variable var and increment it. As g does not assign the local
    variable ``var`` any initial value, calling f(1)() will fail at runtime.)

    Our objective is to set the value of a given cell ``cell``. So we need to
    somewhat reference our ``cell`` object into the ``inner`` function so that
    this object (and not the smoke cell of the lambda function) gets affected
    by the STORE_DEREF operation.

    In inner, ``cell`` is referenced as a cell variable (an enclosing variable
    that is referenced by the inner function). If we create a new function
    cell_set with the exact same code as ``inner``, but with ``cell`` marked as
    a free variable instead, the STORE_DEREF will be applied on its closure -
    ``cell``, which we can specify explicitly during construction! The new
    cell_set variable thus actually sets the contents of a specified cell!

    Note: we do not make use of the ``nonlocal`` keyword to set the contents of
    a cell in early python3 versions to limit possible syntax errors in case
    test and checker libraries decide to parse the whole file.
    Nr(   r)   	_cell_setr   )r-   r.   cell_contentsr6   r?   _cell_set_template_code)cellvaluerf   r   r   r   cell_set3  s    9    rk   c                  C   sT   dd } | j }t|j|j|j|j|j|j|j	|j
|j|j|j|j|j|jd}|S )Nc                    s    fdd |  d S )Nc                      s    S r   r   r   ri   r   r   <lambda>v      zI_make_cell_set_template_code.<locals>._cell_set_factory.<locals>.<lambda>r   )rj   r   rl   r   _cell_set_factoryu  s    z7_make_cell_set_template_code.<locals>._cell_set_factoryr   )__code__r6   rT   co_argcountco_kwonlyargcount
co_nlocalsco_stacksizeco_flagsco_coderS   rQ   co_varnamesco_filenameco_nameco_firstlineno	co_lnotabco_cellvars)ro   rV   rh   r   r   r   _make_cell_set_template_codet  s(    r}   r(   r)   STORE_GLOBALDELETE_GLOBALLOAD_GLOBALc                 C   s   | dkrt S tt| S )NZ	ClassType)rB   r2   r6   r>   r   r   r   _builtin_type  s    r   c                 c   s.   t | D ]}|j}|tkr
||jfV  q
dS )zg
    Yield (opcode, argument number) tuples for all
    global-referencing instructions in *code*.
    N)disget_instructionsopcode
GLOBAL_OPSarg)ra   instropr   r   r   rR     s    rR   c              	   C   s   t | j}t| jdkr&| jd j}n i }t| jD ]}||j q4g }| D ]<\}}z|| }||krv|| W qR tk
r   Y qRX qR|D ]}|	| q|S )zDRetrieve a copy of the dict of a class without the inherited methodsr   r   )
dict__dict__r^   	__bases__reversedupdater5   r`   KeyErrorpop)clsZclsdictZinherited_dictbaseZ	to_remover:   rj   Z
base_valuer   r   r   _extract_class_dict  s"    
r   c                 C   s   t | dd d k	}t | dd d k	}t | dd d k	}t | dd d k	}t | dd d k	}t | dd d k	ont | dd d k	}t||||||fS )N
__origin__
__values____type____union_params____tuple_params__
__result____args__)r2   any)r9   Z	is_typingZis_litteralis_finalZis_unionZis_tupleZis_callabler   r   r   _is_parametrized_type_hint  s    r   c                 C   s   | | S r   r   )originargsr   r   r   _create_parametrized_type_hint  s    r   c                 C   s  t | t tkrt| jf}nt | t tkr<t| jf}nlt | t tkrZt| jf}nNt | t tkr| j}t| jdkr| j	|f}n| j	| j
f}nt | t tkrtjdk rt| jf}n
t| j
f}nt | t tkrtjdk rt| jf}n
t| j
f}nt | t tkrtjdk rP| j
}| j}|tkrt|trHt|}n|g}n6| j
^ }}t|dkr~|d tkr~t}nt|}t||ff}ntdt | |S )Nr   r   r   z"Cloudpickle Error: Unknown type {})rB   r   r   r   r   r   r   __parameters__r^   r   r   r   r-   r.   r   r   r   r   r   Ellipsisr/   tupler[   r   PicklingErrorrE   )r9   Zinitargs
parametersr   resultr   r   r   "parametrized_type_hint_getinitargs  sH    



r   c                 C   s0   dt jkrdS t jd }t|ds&dS || S )zj
    Return whether *func* is a Tornado coroutine function.
    Running coroutines are not supported.
    ztornado.genFis_coroutine_function)r-   r3   rZ   r   funcgenr   r   r   is_tornado_coroutine  s    


r   c                 C   s   ddl m} || S )Nr   )r   )Ztornador   	coroutiner   r   r   r   _rebuild_tornado_coroutine,  s    r   c                 C   s   t |  tj|  S r   )
__import__r-   r3   r>   r   r   r   	subimport7  s    r   c                 C   s&   t | }|j| tj|jd< |S )N__builtins__)r6   r7   r   r   builtins)r:   varsmodr   r   r   dynamic_subimport<  s    
r   c                   C   s   t S r   )r   r   r   r   r   _gen_ellipsisC  s    r   c                   C   s   t S r   )NotImplementedr   r   r   r   _gen_not_implementedG  s    r   c                 C   s&   z| j W S  tk
r    t Y S X d S r   )rg   
ValueError_empty_cell_valuerl   r   r   r   _get_cell_contentsK  s    r   c                 C   s   |  S )zCreate a new instance of a class.

    Parameters
    ----------
    cls : type
        The class to create an instance of.

    Returns
    -------
    instance : cls
        A new instance of ``cls``.
    r   r   r   r   r   instanceS  s    r   c                   @   s   e Zd ZdZedd ZdS )r   z sentinel for empty closures
    c                 C   s   | j S r   )rC   r   r   r   r   
__reduce__g  s    z_empty_cell_value.__reduce__N)rC   r+   rG   __doc__classmethodr   r   r   r   r   r   c  s   r   c                  G   s  t | dkr| d }| d }nt | dkrV| d }ddddg}tt|| dd	 }nHt | d
kr| d }dddddg}tt|| dd	 }ntd| f |j|d  |d |_|d |_d|kr|d |_d|kr|d |_	d|kr|d |_
d|kr|d |_d|kr |d |_d|kr4|d |_d|krH|d |j}|d	k	rt||d D ]\}}|tk	rft|| qf|S )zFills in the rest of function data into the skeleton function object

    The skeleton itself is create by _make_skel_func().
    r(   r   r   r   globalsdefaultsr   Zclosure_valuesN   r<   z$Unexpected _fill_value arguments: %rannotationsdocr:   qualname
kwdefaultsZ_cloudpickle_submodules)r^   r   zipr   __globals__r   __defaults__r   __annotations__r   rC   r+   rG   __kwdefaults__r   __closure__r   rk   )r   r   statekeyscellsri   rj   r   r   r   _fill_functionl  sF    















r   c                      s    fddjd S )Nc                      s    S r   r   r   rl   r   r   rm     rn   z"_make_empty_cell.<locals>.<lambda>r   )AssertionErrorr   r   r   rl   r   _make_empty_cell  s    r   c                 C   s   t  }| tk	rt||  |S r   )r   r   rk   )rj   ri   r   r   r   
_make_cell  s    
r   c                 C   s`   t jdtd |dks t|tr$i }t|d< |dkrJtdd t|D nd}t	| |dd|S )z Creates a skeleton function object that contains just the provided
        code and the correct number of cells in func_closure.  All other
        func attributes (e.g. func_globals) are empty.
    zA pickle file created using an old (<=1.4.1) version of cloudpicke is currently being loaded. This is not supported by cloudpickle and will break in cloudpickle 1.7)categoryNr   r   c                 s   s   | ]}t  V  qd S r   )r   )rK   rL   r   r   r   	<genexpr>  s     z"_make_skel_func.<locals>.<genexpr>)
warningswarnUserWarningr/   strr   r   ranger6   r?   )ra   Z
cell_countbase_globalsclosurer   r   r   _make_skel_func  s    r   c                    s&   t ||d| i fdd}t||S )a  Build dynamic class with an empty __dict__ to be filled once memoized

    If class_tracker_id is not None, try to lookup an existing class definition
    matching that id. If none is found, track a newly reconstructed class
    definition under that id so that other instances stemming from the same
    class id will also reuse this class definition.

    The "extra" variable is meant to be a dict (or None) that can be used for
    forward compatibility shall the need arise.
    	metaclassc                    s
   |   S r   )r   )nstype_kwargsr   r   rm     rn   z&_make_skeleton_class.<locals>.<lambda>)r6   	new_classr'   )Ztype_constructorr:   basesr   r"   extraskeleton_classr   r   r   _make_skeleton_class  s      
r   c                 C   sP   d}|  D ]"\}}|dkr"|}qt| || q|dk	rL|D ]}| | q<| S )zwPut attributes from `class_dict` back on `skeleton_class`.

    See CloudPickler.save_dynamic_class for more info.
    N	_abc_impl)r5   setattrregister)r   
class_dictregistryattrnameattrsubclassr   r   r   _rehydrate_skeleton_class  s    r   c                 C   sZ   | d }|j }||| }	| D ]\}
}||	|
< q"|||| |	}||_||_t||S )a6  Build dynamic enum with an empty __dict__ to be filled once memoized

    The creation of the enum class is inspired by the code of
    EnumMeta._create_.

    If class_tracker_id is not None, try to lookup an existing enum definition
    matching that id. If none is found, track a newly reconstructed enum
    definition under that id so that other instances stemming from the same
    class id will also reuse this enum definition.

    The "extra" variable is meant to be a dict (or None) that can be used for
    forward compatibility shall the need arise.
    )	__class____prepare__r5   __new__r+   rG   r'   )r   r:   r   membersr<   r"   r   Z	enum_basemetacls	classdictmember_namemember_value
enum_classr   r   r   _make_skeleton_enum  s    
r   c                 C   s4   t j| f||||d}|d k	r,t||S |S d S )N)bound	covariantcontravariant)r0   r1   r'   )r:   r   constraintsr   r   r"   tvr   r   r   _make_typevar  s     
r   c                 C   s@   zt | }W n tk
r$   d }Y nX | j| j| j| j| j|fS r   )r#   rD   rC   	__bound____constraints____covariant____contravariant__)r9   r"   r   r   r   _decompose_typevar(  s    
   r  c                 C   s*   t | | jd}|d kr"tt| fS t|fS )Nr>   )r@   rC   r   r  r2   )r9   Zmodule_and_namer   r   r   _typevar_reduce5  s    r  c                 C   s   t | drd}nd}t| |S )N__orig_bases__r   )rZ   r2   )typZ
bases_attrr   r   r   
_get_bases=  s    
r	  c                 C   s   t |  S r   )r   fromkeysr   r9   r   r   r   _make_dict_keysG  s    r  c                 C   s   dd t | D  S )Nc                 S   s   i | ]\}}||qS r   r   )rK   irL   r   r   r   
<dictcomp>L  s      z%_make_dict_values.<locals>.<dictcomp>)	enumeratevaluesr  r   r   r   _make_dict_valuesK  s    r  c                 C   s   |   S r   )r5   r  r   r   r   _make_dict_itemsO  s    r  )N)N)N)ir   
__future__r   r   r   r   platformr-   r6   weakrefr   	threadingr0   r   compatr   r   r   r   r   r	   Zimportlib._bootstrapr
   Ztyping_extensionsZ_typing_extensionsr   r   ImportErrorr.   r   r   r   rB   r   HIGHEST_PROTOCOLDEFAULT_PROTOCOLWeakKeyDictionaryr   WeakValueDictionaryr    Lockr   WeakSetr%   python_implementationZPYPYZbuiltin_code_typefloatr   rp   rP   r#   r'   r=   rF   r@   rU   re   rk   r}   rh   opmapr~   r   r   r   HAVE_ARGUMENTEXTENDED_ARGZ_BUILTIN_TYPE_NAMESr   r5   kvr   rR   r   r   r   r   r   r   loadloadsr   r   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r  r  r	  r  r  r  r   r   r   r   <module>   s   *

+

(/A



	
6A	

