o
    Td}                     @   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ZddlmZ ddlmZ ddl
mZ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yq   d Z ZZY nw 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, Z-e. dkZ/dZ0e/re e1j2j3Z0e& Z4dd Z5dd Z6dd Z7dd Z8dd Z9dd Z:dd Z;dkddZ<dkdd Z=d!d" Z>d#d$ Z?d%d& Z@d'd( ZAejdd) d*k reA ZBejCd+ ZDejCd, ZEejCd- ZFeDeEeFfZGejHZHejIZIi ZJejKL D ]\ZMZNe eNe u r&eMeJeN< qd.d/ ZOd0d1 ZPd2d3 ZQejdd) d*k rGd4d5 ZRd6d7 ZSndZRdZSd8d9 ZTd:d; ZUd<d= ZVejWZWejXZXd>d? ZYd@dA ZZdBdC Z[dDdE Z\dFdG Z]dHdI Z^e^G dJdK dKZ_dLdM Z`dNdO ZadPdQ Zbe_fdRdSZcdkdTdUZddVdW ZedXdY ZfdZd[ Zgd\d] Zhd^d_ Zid`da Zjdbdc ZkdldedfZldldgdhZmdldidjZndS )maU  
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.
    N   )pickle)OrderedDict)ClassVarGenericUnionTupleCallable)_getattribute)
_find_spec)LiteralFinal)      )CellTypec                     s   d  fdd} | S )Nr   c                          S N r   ar   p/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/cloudpickle/cloudpickle.pygJ      zf.<locals>.gr   )r   r   r   r   fG   s   r   PyPyc                 C   sf   t ' t| }|d u r!t j}|t| < | t|< W d    |S W d    |S 1 s,w   Y  |S r   )_DYNAMIC_CLASS_TRACKER_LOCK_DYNAMIC_CLASS_TRACKER_BY_CLASSgetuuiduuid4hex_DYNAMIC_CLASS_TRACKER_BY_ID)	class_defclass_tracker_idr   r   r   _get_or_create_tracker_idj   s   




r$   c                 C   sl   | d ur4t & |}t| |}| t|< ||ur$t| W d    |S W d    |S 1 s/w   Y  |S r   )r   r!   
setdefaultr   _DYNAMIC_CLASS_TRACKER_REUSINGadd)r#   r"   orig_class_defr   r   r   _lookup_class_or_trackt   s    
	
		r)   c                 C   sJ   t | tjstdt|  d| jtjvrt|  dt	| j dS )a  Register a module to make it functions and classes picklable by value.

    By default, functions and classes that are attributes of an importable
    module are to be pickled by reference, that is relying on re-importing
    the attribute from the module at load time.

    If `register_pickle_by_value(module)` is called, all its functions and
    classes are subsequently to be pickled by value, meaning that they can
    be loaded in Python processes where the module is not importable.

    This is especially useful when developing a module in a distributed
    execution environment: restarting the client Python process with the new
    source code is enough: there is no need to re-install the new version
    of the module on all the worker nodes nor to restart the workers.

    Note: this feature is considered experimental. See the cloudpickle
    README.md file for more details and limitations.
    %Input should be a module object, got  insteadzN was not imported correctly, have you used an `import` statement to access it?N)

isinstancetypes
ModuleType
ValueErrorstr__name__sysmodules_PICKLE_BY_VALUE_MODULESr'   moduler   r   r   register_pickle_by_value   s   r7   c                 C   sH   t | tjstdt|  d| jtvrt|  dt| j dS )z<Unregister that the input module should be pickled by value.r*   r+   z& is not registered for pickle by valueN)r,   r-   r.   r/   r0   r1   r4   remover5   r   r   r   unregister_pickle_by_value   s   
r9   c                   C   s   t  S r   )r4   copyr   r   r   r   list_registry_pickle_by_value      r;   c                 C   sD   | j }|tv r	dS 	 |ddd }||kr	 dS |tv rdS |}q
)NT.r   r   F)r1   r4   rsplit)r6   module_nameparent_namer   r   r   _is_registered_pickle_by_value   s   rA   c              	   C   s   t jdd dk r t| tjr |durtt|d| u rdS d}nt| dd}|dur,|S t j  D ],\}}|dksE|du sEt|t	j
sFq3zt||d | u rU|W   S W q3 ty_   Y q3w d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      typing
__module____main__r   )r2   version_infor,   rE   TypeVargetattrr3   r:   itemsr-   r.   r
   	Exception)objnamer?   r6   r   r   r   _whichmodule   s.   	

rO   c                 C   s|   t | tjstt| tr"t| |d}|du rdS |\}}t| S t | tjr4t| r.dS | jt	j
v S tdt| j)a  Test whether an function or a class should be pickled by reference

     Pickling by reference means by that the object (typically a function or a
     class) is an attribute of a module that is assumed to be importable in the
     target Python environment. Loading will therefore rely on importing the
     module and then calling `getattr` on it to access the function or class.

     Pickling by reference is the only option to pickle functions and classes
     in the standard library. In cloudpickle the alternative option is to
     pickle by value (for instance for interactively or locally defined
     functions and classes or for attributes of modules that have been
     explicitly registered to be pickled by value.
     rN   NFz*cannot check importability of {} instances)r,   r-   FunctionType
issubclasstype_lookup_module_and_qualnamerA   r.   r1   r2   r3   	TypeErrorformat)rM   rN   module_and_namer6   r   r   r   _should_pickle_by_reference   s   
rX   c                 C   s   |d u r
t | dd }|d u rt | dd }t| |}|d u rd S |dkr%d S tj|d }|d u r2d S z	t||\}}W n
 tyE   Y d S w || urLd S ||fS )N__qualname__r1   rG   )rJ   rO   r2   r3   r   r
   AttributeError)rM   rN   r?   r6   obj2parentr   r   r   rT     s(   
rT   c                 C   s\   t | }|du r,dd t| D }| jr(| jD ]}t|tjr'|t| q|t | < |S )zC
    Find all globals names read or written to by codeblock co
    Nc                 S   s   i | ]}|d qS r   r   ).0rN   r   r   r   
<dictcomp>M  s    z)_extract_code_globals.<locals>.<dictcomp>)	_extract_code_globals_cacher   _walk_global_ops	co_constsr,   r-   CodeTypeupdate_extract_code_globals)co	out_namesconstr   r   r   rd   D  s   

rd   c                 C   s   g }|D ]B}t |tjrFt|drF|jrF|jd }ttjD ]'}|durE|	|rEt
|t|d d}|t
| j sE|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__r=   N)r,   r-   r.   hasattrrh   r1   listr2   r3   
startswithsetlensplitco_namesappend)codetop_level_dependencies
subimportsxprefixrN   tokensr   r   r   _find_imported_submodules_  s   
rw   c                 C   s<   t jdd dkr|| _dS 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.
    NrB   rC   	_cell_setr   )r2   rH   cell_contentsr-   rQ   _cell_set_template_code)cellvaluerx   r   r   r   cell_set  s   9
r}   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                      r   r   r   r   r{   r   r   <lambda>      zI_make_cell_set_template_code.<locals>._cell_set_factory.<locals>.<lambda>r   )r|   r   r~   r   _cell_set_factory  s   z7_make_cell_set_template_code.<locals>._cell_set_factoryr   )__code__r-   rb   co_argcountco_kwonlyargcount
co_nlocalsco_stacksizeco_flagsco_codera   ro   co_varnamesco_filenameco_nameco_firstlineno	co_lnotabco_cellvars)r   re   rz   r   r   r   _make_cell_set_template_code  s(   r   rB   rC   STORE_GLOBALDELETE_GLOBALLOAD_GLOBALc                 C   s   | dkrt S tt| S )N	ClassType)rS   rJ   r-   rP   r   r   r   _builtin_type  s   
r   c                 c   s,    t | D ]}|j}|tv r|jV  qdS )zR
    Yield referenced name for all global-referencing instructions in *code*.
    N)disget_instructionsopcode
GLOBAL_OPSargval)rq   instropr   r   r   r`     s   r`   c              	   C   s   t | j}t| jdkr| jd j}ni }t| jD ]}||j qg }| D ]\}}z|| }||u r;|| W q) tyE   Y q)w |D ]}|	| qH|S )zDRetrieve a copy of the dict of a class without the inherited methodsr   r   )
dict__dict__rm   	__bases__reversedrc   rK   rp   KeyErrorpop)clsclsdictinherited_dictbase	to_removerN   r|   
base_valuer   r   r   _extract_class_dict  s&   

r   c                 C   s   t t| dd }|dk}|dk}t | dd d u}t | dd d uo!|}t | dd d uo+|}t | dd d uo5|}t | dd d u}t | dd d u}	t | d	d d uoUt | d
d d u}
t||||||	|
fS )NrF   typing_extensionsrE   
__origin__
__values____type____union_params____tuple_params__
__result____args__)rJ   rS   any)rM   type_modulefrom_typing_extensionsfrom_typing	is_typing
is_literalis_finalis_classvaris_unionis_tupleis_callabler   r   r   _is_parametrized_type_hint&  s(   r   c                 C   s   | | S r   r   )originargsr   r   r   _create_parametrized_type_hintK  r<   r   c                 C   s  t | t tu rt| jf}|S t | t tu rt| jf}|S t | t tu r-t| jf}|S t | t tu r=| j| jf}|S t | t t	u rLt	| jf}|S t | t t
u r[t
| jf}|S t | t tu r| j^ }}t|dkrx|d tu rxt}nt|}t||ff}|S tdt |  )Nr   r   z Cloudpickle Error: Unknown type )rS   r   r   r   r   r   r   r   r   r   r   r	   rm   Ellipsisrj   r   PicklingError)rM   initargsr   resultr   r   r   "parametrized_type_hint_getinitargsR  s8   




r   c                 C   s0   dt jv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)r2   r3   ri   r   funcgenr   r   r   is_tornado_coroutiner  s   



r   c                 C   s   ddl m} || S )Nr   )r   )tornador   	coroutiner   r   r   r   _rebuild_tornado_coroutine  s   
r   c                 C   s   t |  tj|  S r   )
__import__r2   r3   rP   r   r   r   	subimport  s   
r   c                 C   s&   t | }|j| tj|jd< |S N__builtins__)r-   r.   r   rc   builtins)rN   varsmodr   r   r   dynamic_subimport  s   
r   c                   C      t S r   )r   r   r   r   r   _gen_ellipsis  r   r   c                   C   r   r   )NotImplementedr   r   r   r   _gen_not_implemented  r   r   c                 C   s    z| j W S  ty   t Y S w r   )ry   r/   _empty_cell_valuer~   r   r   r   _get_cell_contents  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   instance  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   )r1   r   r   r   r   
__reduce__  s   z_empty_cell_value.__reduce__N)r1   rF   rY   __doc__classmethodr   r   r   r   r   r     s    r   c                  G   sr  t | dkr| d }| d }n;t | dkr)| d }g d}tt|| dd }n!t | dkrC| d }g d}tt|| dd }ntd	| |j|d
  |d |_|d |_d|v re|d |_d|v rn|d |_	d|v rw|d |_
d|v r|d |_d|v r|d |_d|v r|d |_d|v r|d |j}|durt||d D ]\}}|turt|| q|S )zFills in the rest of function data into the skeleton function object

    The skeleton itself is create by _make_skel_func().
    rB   r   r      )globalsdefaultsr   closure_valuesN   )r   r   r   r6   r   z"Unexpected _fill_value arguments: r   r   r   annotationsdocrN   r6   qualname
kwdefaults_cloudpickle_submodulesr   )rm   r   zipr/   __globals__rc   __defaults__r   __annotations__r   r1   rF   rY   __kwdefaults__r   __closure__r   r}   )r   r   statekeyscellsr{   r|   r   r   r   _fill_function  sH   










r   c                 C   s   t |d< t| ||||S r   )r   r-   rQ   )rq   r   rN   argdefsclosurer   r   r   _make_function  s   r   c                      s   	  fddjd S )NFz!this route should not be executedc                      r   r   r   r   r~   r   r   r     r   z"_make_empty_cell.<locals>.<lambda>r   )AssertionErrorr   r   r   r~   r   _make_empty_cell
  s   r   c                 C   s   t  }| turt||  |S r   )r   r   r}   )r|   r{   r   r   r   
_make_cell  s   
r   c                 C   s`   t jdtd |du st|tri }t|d< |dkr%t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 cloudpickle 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   )r]   _r   r   r   	<genexpr>.  s    z"_make_skel_func.<locals>.<genexpr>)
warningswarnUserWarningr,   r0   r   tupleranger-   rQ   )rq   
cell_countbase_globalsr   r   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   )rc   )nstype_kwargsr   r   r   C  s   
 z&_make_skeleton_class.<locals>.<lambda>)r-   	new_classr)   )type_constructorrN   basesr  r#   extraskeleton_classr   r  r   _make_skeleton_class5  s
   


r  c                 C   sP   d}|  D ]\}}|dkr|}qt| || q|dur&|D ]}| | q| S )zwPut attributes from `class_dict` back on `skeleton_class`.

    See CloudPickler.save_dynamic_class for more info.
    N	_abc_impl)rK   setattrregister)r  
class_dictregistryattrnameattrsubclassr   r   r   _rehydrate_skeleton_classH  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__rK   __new__rF   rY   r)   )r  rN   r   membersr6   r#   r  	enum_basemetacls	classdictmember_namemember_value
enum_classr   r   r   _make_skeleton_enumZ  s   

r#  c                 C   s2   t j| g|R |||d}|d urt||S |S )N)bound	covariantcontravariant)rE   rI   r)   )rN   r$  constraintsr%  r&  r#   tvr   r   r   _make_typevarx  s   
r)  c                 C   s   | j | j| j| j| jt| fS r   )r1   	__bound____constraints____covariant____contravariant__r$   )rM   r   r   r   _decompose_typevar  s   r.  c                 C   sB   t | | jd}|d u rtt| fS t|d rtt| fS t|fS )NrP   r   )rT   r1   r)  r.  rA   rJ   )rM   rW   r   r   r   _typevar_reduce  s   r/  c                 C   s$   dt | di v rd}nd}t | |S )N__orig_bases__r   r   )rJ   )typ
bases_attrr   r   r   
_get_bases  s   
r3  Fc                 C   s    |r	t |  S t|  S r   )r   fromkeysr   r   rM   
is_orderedr   r   r   _make_dict_keys  s   r7  c                 C   s4   |rt dd t| D  S dd t| D  S )Nc                 s   s    | ]	\}}||fV  qd S r   r   r]   ir   r   r   r   r     s    z$_make_dict_values.<locals>.<genexpr>c                 S   s   i | ]\}}||qS r   r   r8  r   r   r   r^     s    z%_make_dict_values.<locals>.<dictcomp>)r   	enumeratevaluesr5  r   r   r   _make_dict_values  s   r<  c                 C   s   |rt |  S |  S r   )r   rK   r5  r   r   r   _make_dict_items  s   r=  r   )F)or   r   r   r   platformr2   r-   weakrefr   	threadingrE   r   compatr   collectionsr   r   r   r   r   r	   r
   importlib._bootstrapr   r   _typing_extensionsr   r   ImportErrorrH   r   r   rS   r   HIGHEST_PROTOCOLDEFAULT_PROTOCOLrl   r4   WeakKeyDictionaryr   WeakValueDictionaryr!   Lockr   WeakSetr&   python_implementationPYPYbuiltin_code_typefloatr  r   r_   r$   r)   r7   r9   r;   rA   rO   rX   rT   rd   rw   r}   r   rz   opmapr   r   r   r   HAVE_ARGUMENTEXTENDED_ARG_BUILTIN_TYPE_NAMESr   rK   kvr   r`   r   r   r   r   r   r   loadloadsr   r   r   r   r   r   r   r   r   r   r   r  r  r  r#  r)  r.  r/  r3  r7  r<  r=  r   r   r   r   <module>   s    +

)
.
%(/A



	

% 	A	


