o
    l¨Êh”L  ã                   @   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d8dd„Zd8dd	„Z d
d„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(dd„ Z)dd„ Z*dd„ Z+d d!„ Z,d"d#„ Z-d$d%„ Z.d&d'„ Z/d(d)„ Z0d*d+„ Z1d,d-„ Z2d.d/„ Z3d0d1„ Z4d2d3„ Z5d4d5„ Z6G d6d7„ d7eƒZ7dS )9aì  
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
é    N)ÚPickleré   )Ú_is_dynamicÚ_extract_code_globalsÚ_BUILTIN_TYPE_NAMESÚDEFAULT_PROTOCOLÚ_find_imported_submodulesÚ_get_cell_contentsÚ
_is_globalÚ_builtin_typeÚEnumÚ_ensure_trackingÚ_make_skeleton_classÚ_make_skeleton_enumÚ_extract_class_dictÚstring_typesÚdynamic_subimportÚ	subimportc                 C   s   t ||d� | ¡ dS )aw  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.
    ©ÚprotocolN)ÚCloudPicklerÚdump)ÚobjÚfiler   © r   ú_/var/www/html/env/lib/python3.10/site-packages/joblib/externals/cloudpickle/cloudpickle_fast.pyr   %   s   
r   c                 C   sJ   t  ¡ �}t||d�}| | ¡ | ¡ W  d  ƒ S 1 sw   Y  dS )a„  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)ÚioÚBytesIOr   r   Úgetvalue)r   r   r   Úcpr   r   r   Údumps2   s
   


$ýr    c                 C   sT   i }t | dƒr| j|d< | j dd ¡}t|tƒr||d< t| ƒ| j| j|t	| ƒd fS )NÚ	__slots__Ú__dict__)
Úhasattrr!   r"   ÚgetÚ
isinstanceÚpropertyÚtypeÚ__name__Ú	__bases__r   )r   Útype_kwargsr"   r   r   r   Ú_class_getnewargsE   s   


ÿr+   c                 C   s0   t dd„ | D ƒƒ}| j| j| j|| jt| ƒd fS )Nc                 s   ó   � | ]	}|j |jfV  qd S ©N©ÚnameÚvalue©Ú.0Úer   r   r   Ú	<genexpr>S   ó   € z#_enum_getnewargs.<locals>.<genexpr>)Údictr)   r(   Ú__qualname__Ú
__module__r   )r   Úmembersr   r   r   Ú_enum_getnewargsR   s   ÿr:   c                 C   s   | S r-   r   )Úretvalr   r   r   Ú_file_reconstructorZ   s   r<   c              	      s’   ˆ j ˆ jˆ jˆ jˆ jˆ jˆ jˆ jdœ}tˆ j	ƒ}‡ fdd„|D ƒ}ˆ jd ur.t
ttˆ jƒƒnd}tˆ j	t | ¡ |¡ƒ|d< ||d< ˆ j}||fS )N)r(   r7   Ú__annotations__Ú__kwdefaults__Ú__defaults__r8   Ú__doc__Ú__closure__c                    s"   i | ]}|ˆ j v r|ˆ j | “qS r   )Ú__globals__)r2   Úk©Úfuncr   r   Ú
<dictcomp>s   s   
 ÿz&_function_getstate.<locals>.<dictcomp>r   Ú_cloudpickle_submodulesrB   )r(   r7   r=   r>   r?   r8   r@   rA   r   Ú__code__ÚlistÚmapr	   r   Ú	itertoolsÚchainÚvaluesr"   )rE   Ú	slotstateÚf_globals_refÚ	f_globalsÚclosure_valuesÚstater   rD   r   Ú_function_getstate`   s*   ø

ÿþ	ÿrS   c                 C   s�   t | ƒ}| dd ¡ d|v r t | ¡\}}}}dd„ |D ƒ|d< t| dƒr>t| jtƒr2| | j¡ n| jD ]}| |d ¡ q5| dd ¡ |i fS )NÚ__weakref__Ú	_abc_implc                 S   s   g | ]}|ƒ ‘qS r   r   )r2   Úsubclass_weakrefr   r   r   Ú
<listcomp>�   s    
ÿz#_class_getstate.<locals>.<listcomp>r!   r"   )r   ÚpopÚabcÚ	_get_dumpr#   r%   r!   r   )r   ÚclsdictÚregistryÚ_rC   r   r   r   Ú_class_getstate‡   s   
ÿ

r^   c                 C   sP   t | ƒ\}}tdd„ | D ƒƒ}dD ]}| |d ¡ q|D ]}| |¡ q||fS )Nc                 s   r,   r-   r.   r1   r   r   r   r4   ¤   r5   z!_enum_getstate.<locals>.<genexpr>)Ú_generate_next_value_Ú_member_names_Ú_member_map_Ú_member_type_Ú_value2member_map_)r^   r6   rX   )r   r[   rN   r9   ÚattrnameÚmemberr   r   r   Ú_enum_getstate¡   s   rf   c                 C   sN   | j | j| j| j| j| j| j| j| j| j	| j
| j| j| j| j| jf}tj|fS )zcodeobject reducer)Úco_argcountÚco_posonlyargcountÚco_kwonlyargcountÚ
co_nlocalsÚco_stacksizeÚco_flagsÚco_codeÚ	co_constsÚco_namesÚco_varnamesÚco_filenameÚco_nameÚco_firstlinenoÚ	co_lnotabÚco_freevarsÚco_cellvarsÚtypesÚCodeType)r   Úargsr   r   r   Ú_code_reduce¼   s   ú
rz   c                 C   s6   z| j  W n ty   tjdf Y S w tj| j ffS )z?Cell (containing values of a function's free variables) reducerr   )Úcell_contentsÚ
ValueErrorrw   ÚCellType©r   r   r   r   Ú_cell_reduceÉ   s   
ÿr   c                 C   s   | j }t| ƒ|ffS r-   )Ú__func__r'   )r   Ú	orig_funcr   r   r   Ú_classmethod_reduceÓ   s   r‚   c                 C   s.  ddl }t| dƒrt| dƒst d¡‚| tju rttdffS | tju r)ttdffS | tju r3t d¡‚| j	r;t d	¡‚t| d
ƒrI|  
¡ rIt d¡‚d| jvr[d| jvr[t d| j ¡‚| j}| ¡ }z|  ¡ }|  d¡ |  ¡ }|  |¡ W n ty„   t d| ¡‚w | |¡ | |¡ ||_t|ffS )zSave a filer   Nr/   Úmodez5Cannot pickle files that do not map to an actual fileÚstdoutÚstderrzCannot pickle standard inputzCannot pickle closed filesÚisattyz+Cannot pickle files that map to tty objectsÚrú+z7Cannot pickle files that are not opened for reading: %sz*Cannot pickle file %s as it cannot be read)r   r#   ÚpickleÚPicklingErrorÚsysr„   Úgetattrr…   ÚstdinÚclosedr†   rƒ   r/   ÚStringIOÚtellÚseekÚreadÚIOErrorÚwriter<   )r   r   r/   r;   ÚcurlocÚcontentsr   r   r   Ú_file_reduceØ   sN   ÿ




ÿÿÿ
ÿÿ


r—   c                 C   s   t | j| jffS r-   )rŒ   Ú__objclass__r(   r~   r   r   r   Ú_getset_descriptor_reduce  ó   r™   c                 C   ó   t jt| ƒffS r-   )rw   ÚMappingProxyTyper6   r~   r   r   r   Ú_mappingproxy_reduce  rš   r�   c                 C   s   t |  ¡ ffS r-   )ÚbytesÚtobytesr~   r   r   r   Ú_memoryview_reduce  ó   r    c                 C   s&   t | ƒrt| jt| ƒffS t| jffS r-   )r   r   r(   Úvarsr   r~   r   r   r   Ú_module_reduce  s   r£   c                 C   s   t j| j| jffS r-   )rw   Ú
MethodTyper€   Ú__self__r~   r   r   r   Ú_method_reduce  s   r¦   c                 C   s   t j| jffS r-   )ÚloggingÚ	getLoggerr/   r~   r   r   r   Ú_logger_reduce  r¡   r©   c                 C   s
   t jdfS )Nr   )r§   r¨   r~   r   r   r   Ú_root_logger_reduce"  s   
rª   c                 C   r›   r-   )ÚweakrefÚWeakSetrI   r~   r   r   r   Ú_weakset_reduce&  rš   r­   c                 C   sB   t durt| t ƒrtt| ƒt| ƒddtfS tt| ƒt| ƒddtfS )zæ
    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   Ú
issubclassr   r:   rf   Ú_class_setstater   r+   r^   r~   r   r   r   Ú_dynamic_class_reduce*  s   þþr°   c                 C   sj   | t dƒu r
t dfS | t tƒu rt tffS | t tƒu r t tffS | tv r+tt|  ffS t| ƒs3t| ƒS tS )zCSelect the reducer depending on the dynamic nature of the class objNr-   )r'   ÚEllipsisÚNotImplementedr   r   r
   r°   r~   r   r   r   Ú_class_reduce>  s   

r³   c           
   	   C   s®   |\}}| j  |¡ | d¡}| d¡}| d¡ | j |¡ t| jd< |durFt|ƒD ]\}}z|j}W n	 ty>   Y q,w || j| _q,| 	¡ D ]
\}}	t
| ||	ƒ qJdS )a  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.
    rB   rA   rG   Ú__builtins__N)r"   ÚupdaterX   rB   r´   Ú	enumerater{   r|   rA   ÚitemsÚsetattr)
r   rR   rN   Úobj_globalsÚobj_closureÚiÚcellr0   rC   Úvr   r   r   Ú_function_setstateS  s$   




ÿÿr¾   c                 C   sX   |\}}d }|  ¡ D ]\}}|dkr|}q
t| ||ƒ q
|d ur*|D ]}|  |¡ q"| S )NrU   )r·   r¸   Úregister)r   rR   rN   r\   rd   ÚattrÚsubclassr   r   r   r¯   u  s   r¯   c                   @   sÂ   e Zd 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d„Z"dd„ Z#dd„ Z$d	d
„ Z%dd„ Z&dd„ Z'dS )r   aŸ  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
    Nc                 C   sJ   |d u rt }tj| ||d� i | _tj ¡ | _| j | j¡ t	|ƒ| _
d S )Nr   )r   r   Ú__init__Úglobals_refÚcopyregÚdispatch_tableÚcopyrµ   ÚdispatchÚintÚproto)Úselfr   r   r   r   r   rÂ   ¢  s   zCloudPickler.__init__c                 C   sT   t |ƒ}zt|t ƒ}W n ty   d}Y nw |rt|ƒS t|tjƒr(|  |¡S tS )aº  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
        F)	r'   r®   Ú	TypeErrorr³   r%   rw   ÚFunctionTypeÚ_function_reducer²   )rÊ   r   ÚtÚis_anyclassr   r   r   Úreducer_override±  s   ÿ
zCloudPickler.reducer_overridec                 C   s$   |   |¡}t|ƒ}tj||ddtfS )z>Reduce a function that is not pickleable via attribute lookup.N)Ú_function_getnewargsrS   rw   rÌ   r¾   )rÊ   rE   ÚnewargsrR   r   r   r   Ú_dynamic_function_reduceß  s
   
ÿz%CloudPickler._dynamic_function_reducec                 C   s   t |ƒrtS |  |¡S )a  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.
        )r
   r²   rÓ   )rÊ   r   r   r   r   rÍ   æ  s   
zCloudPickler._function_reducec                 C   s~   |j }| j t|jƒi ¡}|i kr"dD ]}||jv r!|j| ||< q|jd u r*d }ntdd„ tt|j	ƒƒD ƒƒ}||d d |fS )N)Ú__package__r(   Ú__path__Ú__file__c                 s   s   � | ]}t  ¡ V  qd S r-   )rw   r}   )r2   r]   r   r   r   r4     s   € 
ÿz4CloudPickler._function_getnewargs.<locals>.<genexpr>)
rH   rÃ   Ú
setdefaultÚidrB   rA   ÚtupleÚrangeÚlenru   )rÊ   rE   ÚcodeÚbase_globalsrC   Úclosurer   r   r   rÑ   ÷  s   
€
ÿz!CloudPickler._function_getnewargsc              
   C   sF   zt  | |¡W S  ty" } zd|jd v rd}t |¡‚‚ d }~ww )NÚ	recursionr   z?Could not pickle object as excessively deep recursion required.)r   r   ÚRuntimeErrorry   r‰   rŠ   )rÊ   r   r3   Úmsgr   r   r   r     s   ÿ
€øzCloudPickler.dumpr-   )(r(   r8   r7   r@   rÇ   r‚   Úclassmethodr—   r   ÚTextIOWrapperr©   r§   ÚLoggerrª   Ú
RootLoggerr    Ú
memoryviewÚstaticmethodr   rw   r}   rz   rx   r™   ÚGetSetDescriptorTyper£   Ú
ModuleTyper¦   r¤   r�   rœ   r­   r«   r¬   rÂ   rÐ   rÓ   rÍ   rÑ   r   r   r   r   r   r   „  s,    










.!r   r-   )8r@   rY   rÄ   r   rK   r§   Ú_pickler‰   r‹   rw   r«   r   Úcloudpickler   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   ÚloadÚloadsr   r    r+   r:   r<   rS   r^   rf   rz   r   r‚   r—   r™   r�   r    r£   r¦   r©   rª   r­   r°   r³   r¾   r¯   r   r   r   r   r   Ú<module>   sN    H

'
/"