o
    fR                     @   s  d Z daddlZddlZddlZddlZddlZejdkZer(ddl	Z
ddlZnddlZ
er3ddgZndgZedd eD sAJ ed ZeeZdeZd	d
 eD ZdZdZee Zdd Ze Zdd Zdd Zdd Zerwdd Zndd Zdd Zdd Zdd Zdd Z d d! Z!erd"d# Z"nd$d# Z"dpd&d'Z#e$e#j%Z&d('d)d*d+ Z(e)*e(d*Z+d,Z,d-Z-d.gZ.ere./d/ e0 Z1d0gZ2e2 Z3Z4dqdd1d2d3Z5d4d5 Z6d6d7 Z7d8d9 Z8d:d; Z9d<d= Z:d>d? Z;d@dA Z<dBdC Z=dDdE Z>drdFdGZ?dsdHdIZ@dtdKdLZAdMdN ZBeC ZDdqdeDdOdPdQZEG dRdS dSZFG dTdU dUZGG dVdW dWeGZHG dXdY dYZIG dZd[ d[eIeHZJG d\d] d]eIeGZKG d^d_ d_eIeGZLG d`da daZMG dbdc dcZNG ddde deZOG dfdg dgZPdqdhdiZQdjdk ZRdldm ZSdndo ZTdS )ua^  Core implementation of path-based import.

This module is NOT meant to be directly imported! It has been designed such
that it can be bootstrapped into Python as the implementation of import. As
such it requires the injection of specific modules and attributes in order to
work. One should use importlib as the public-facing version of this module.

N    win32\/c                 c   s    | ]	}t |d kV  qdS )   N)len).0sep r	   4/usr/lib/python3.10/importlib/_bootstrap_external.py	<genexpr>.   s    r    c                 C   s   h | ]}d | qS ):r	   )r   sr	   r	   r
   	<setcomp>2   s    r   )win)cygwindarwinc                     s>   t jtrt jtrd nd  fdd} | S dd } | S )NPYTHONCASEOKs   PYTHONCASEOKc                      s   t jj o	 tjv S )z^True if filenames must be checked case-insensitively and ignore environment flags are not set.)sysflagsignore_environment_osenvironr	   keyr	   r
   _relax_caseC      z%_make_relax_case.<locals>._relax_casec                   S      dS )z5True if filenames must be checked case-insensitively.Fr	   r	   r	   r	   r
   r   G      )r   platform
startswith_CASE_INSENSITIVE_PLATFORMS#_CASE_INSENSITIVE_PLATFORMS_STR_KEY)r   r	   r   r
   _make_relax_case<   s   r#   c                 C   s   t | d@ ddS )z*Convert a 32-bit integer to little-endian.       little)intto_bytes)xr	   r	   r
   _pack_uint32O   r   r*   c                 C      t | dksJ t| dS )z/Convert 4 bytes in little-endian to an integer.r%   r&   r   r'   
from_bytesdatar	   r	   r
   _unpack_uint32T      r0   c                 C   r+   )z/Convert 2 bytes in little-endian to an integer.   r&   r,   r.   r	   r	   r
   _unpack_uint16Y   r1   r3   c                  G   s   | sdS t | dkr| d S d}g }ttj| D ]=\}}|ts&|tr3|tp,|}t	| g}q|drL|
 |
 krF|}|g}q|| q|pO|}|| qdd |D }t |dkrk|d sk|t	 S |t	| S )Replacement for os.path.join().r   r   r   r   c                 S      g | ]	}|r| tqS r	   rstrippath_separatorsr   pr	   r	   r
   
<listcomp>w   s    _path_join.<locals>.<listcomp>)r   mapr   _path_splitrootr    path_sep_tupleendswithr7   r8   path_sepcasefoldappendjoin)
path_partsrootpathnew_roottailr	   r	   r
   
_path_join`   s*   
rJ   c                  G   s   t dd | D S )r4   c                 S   r5   r	   r6   )r   partr	   r	   r
   r;      s    r<   )rA   rD   )rE   r	   r	   r
   rJ   ~   s   
c                    sB   t  fddtD }|dk rd fS  d|  |d d fS )z Replacement for os.path.split().c                 3   s    | ]}  |V  qd S N)rfindr9   rG   r	   r
   r      s    z_path_split.<locals>.<genexpr>r   r   Nr   )maxr8   )rG   ir	   rN   r
   _path_split   s   rQ   c                 C   s
   t | S )z~Stat the path.

    Made a separate function to make it easier to override in experiments
    (e.g. cache stat results).

    )r   statrN   r	   r	   r
   
_path_stat   s   
rS   c                 C   s0   zt | }W n
 ty   Y dS w |jd@ |kS )z1Test whether the path is the specified mode type.Fi   )rS   OSErrorst_mode)rG   mode	stat_infor	   r	   r
   _path_is_mode_type   s   rX   c                 C   s
   t | dS )zReplacement for os.path.isfile.i   )rX   rN   r	   r	   r
   _path_isfile      
rY   c                 C   s   | st  } t| dS )zReplacement for os.path.isdir.i @  )r   getcwdrX   rN   r	   r	   r
   _path_isdir   s   
r\   c                 C   s>   | sdS t | d dd}t|dko|dp|dS )Replacement for os.path.isabs.Fr   r   r   r   z\\)r   r>   replacer   r    r@   )rG   rF   r	   r	   r
   _path_isabs   s    r_   c                 C   s
   |  tS )r]   )r    r8   rN   r	   r	   r
   r_      rZ     c                 C   s   d | t| }t|tjtjB tjB |d@ }z$t|d}|	| W d   n1 s/w   Y  t
||  W dS  tyT   zt| W   tyS   Y  w w )zBest-effort function to write data to a path atomically.
    Be prepared to handle a FileExistsError if concurrent writing of the
    temporary file is attempted.{}.{}r`   wbN)formatidr   openO_EXCLO_CREATO_WRONLY_ioFileIOwriter^   rT   unlink)rG   r/   rV   path_tmpfdfiler	   r	   r
   _write_atomic   s$   rp   io  r2   r&   s   
__pycache__zopt-z.pyz.pywz.pyc)optimizationc                C   sP  |durt dt |durd}t||rdnd}t| } t| \}}|d\}}}tj	j
}	|	du r9tdd|r?|n|||	g}
|du rVtjjdkrRd}ntjj}t|}|dkrp| sitd	|d
|
t|}
|
td  }tjdurt|stt |}|d dkr|d tvr|dd }ttj|t|S t|t|S )a  Given the path to a .py file, return the path to its .pyc file.

    The .py file does not need to exist; this simply returns the path to the
    .pyc file calculated as if the .py file were imported.

    The 'optimization' parameter controls the presumed optimization level of
    the bytecode file. If 'optimization' is not None, the string representation
    of the argument is taken and verified to be alphanumeric (else ValueError
    is raised).

    The debug_override parameter is deprecated. If debug_override is not None,
    a True value is the same as setting 'optimization' to the empty string
    while a False value is equivalent to setting 'optimization' to '1'.

    If sys.implementation.cache_tag is None then NotImplementedError is raised.

    NzFthe debug_override parameter is deprecated; use 'optimization' insteadz2debug_override or optimization must be set to Noner   r   .$sys.implementation.cache_tag is Noner   z{!r} is not alphanumericz{}.{}{}r   r2   )	_warningswarnDeprecationWarning	TypeErrorr   fspathrQ   
rpartitionr   implementation	cache_tagNotImplementedErrorrD   r   optimizestrisalnum
ValueErrorrc   _OPTBYTECODE_SUFFIXESpycache_prefixr_   rJ   r[   r8   lstrip_PYCACHE)rG   debug_overriderr   messageheadrI   baser   resttagalmost_filenamefilenamer	   r	   r
   cache_from_source|  sH   

	r   c           
      C   s(  t jjdu r
tdt| } t| \}}d}t jdur3t jt	}|
|t r3|t|d }d}|sHt|\}}|tkrHtt d| |d}|dvrXtd||d	kr|dd
d }|
tsptdt|ttd }| std|d|dd }	t||	td  S )an  Given the path to a .pyc. file, return the path to its .py file.

    The .pyc file does not need to exist; this simply returns the path to
    the .py file calculated to correspond to the .pyc file.  If path does
    not conform to PEP 3147/488 format, ValueError will be raised. If
    sys.implementation.cache_tag is None then NotImplementedError is raised.

    Nrt   FTz not bottom-level directory in rs   >   r2      zexpected only 2 or 3 dots in r   r2   z5optimization portion of filename does not start with zoptimization level z is not an alphanumeric valuer   )r   r{   r|   r}   r   ry   rQ   r   r7   r8   r    rA   r   r   r   countrsplitr   r   	partitionrJ   SOURCE_SUFFIXES)
rG   r   pycache_filenamefound_in_pycache_prefixstripped_pathpycache	dot_countrr   	opt_levelbase_filenamer	   r	   r
   source_from_cache  s<   	



r   c              	   C   s|   t | dkrdS | d\}}}|r| dd dkr| S zt| }W n ttfy5   | dd }Y nw t|r<|S | S )zConvert a bytecode file path to a source path (if possible).

    This function exists purely for backwards-compatibility for
    PyImport_ExecCodeModuleWithFilenames() in the C API.

    r   Nrs   py)r   rz   lowerr   r}   r   rY   )bytecode_pathr   _	extensionsource_pathr	   r	   r
   _get_sourcefile  s   r   c                 C   sD   |  ttrzt| W S  ty   Y d S w |  ttr | S d S rL   )r@   tupler   r   r}   r   )r   r	   r	   r
   _get_cached  s   
r   c                 C   s2   zt | j}W n ty   d}Y nw |dO }|S )z3Calculate the mode permissions for a bytecode file.r`      )rS   rU   rT   )rG   rV   r	   r	   r
   
_calc_mode
  s   r   c                    s4   d fdd	}t durt j}ndd }||  |S )zDecorator to verify that the module being requested matches the one the
    loader can handle.

    The first argument (self) must define _name which the second argument is
    compared against. If the comparison fails then ImportError is raised.

    Nc                    sH   |d u r| j }n| j |krtd| j |f |d | |g|R i |S )Nzloader for %s cannot handle %sname)r   ImportError)selfr   argskwargsmethodr	   r
   _check_name_wrapper  s   
z(_check_name.<locals>._check_name_wrapperc                 S   s8   dD ]}t ||rt| |t|| q| j|j d S )N)
__module____name____qualname____doc__)hasattrsetattrgetattr__dict__update)newoldr^   r	   r	   r
   _wrap+  s
   
z_check_name.<locals>._wraprL   )
_bootstrapr   )r   r   r   r	   r   r
   _check_name  s   

r   c                 C   sH   t dt | |\}}|du r"t|r"d}t ||d t |S )zTry to find a loader for the specified module by delegating to
    self.find_loader().

    This method is deprecated in favor of finder.find_spec().

    zZfind_module() is deprecated and slated for removal in Python 3.12; use find_spec() insteadNz,Not importing directory {}: missing __init__r   )ru   rv   rw   find_loaderr   rc   ImportWarning)r   fullnameloaderportionsmsgr	   r	   r
   _find_module_shim5  s   r   c                 C   s   | dd }|t kr d|d|}td| t|fi |t| dk r5d|}td| t|t| dd }|d	@ rQd
|d|}t|fi ||S )aT  Perform basic validity checking of a pyc header and return the flags field,
    which determines how the pyc should be further validated against the source.

    *data* is the contents of the pyc file. (Only the first 16 bytes are
    required, though.)

    *name* is the name of the module being imported. It is used for logging.

    *exc_details* is a dictionary passed to ImportError if it raised for
    improved debugging.

    ImportError is raised when the magic number is incorrect or when the flags
    field is invalid. EOFError is raised when the data is found to be truncated.

    Nr%   zbad magic number in z: {}   z(reached EOF while reading pyc header of    zinvalid flags z in )MAGIC_NUMBERr   _verbose_messager   r   EOFErrorr0   )r/   r   exc_detailsmagicr   r   r	   r	   r
   _classify_pycI  s   
r   c                 C   s|   t | dd |d@ krd|}td| t|fi ||dur:t | dd |d@ kr<td|fi |dS dS )a  Validate a pyc against the source last-modified time.

    *data* is the contents of the pyc file. (Only the first 16 bytes are
    required.)

    *source_mtime* is the last modified timestamp of the source file.

    *source_size* is None or the size of the source file in bytes.

    *name* is the name of the module being imported. It is used for logging.

    *exc_details* is a dictionary passed to ImportError if it raised for
    improved debugging.

    An ImportError is raised if the bytecode is stale.

    r      r$   zbytecode is stale for r   Nr   )r0   r   r   r   )r/   source_mtimesource_sizer   r   r   r	   r	   r
   _validate_timestamp_pycj  s   
r   c                 C   s*   | dd |krt d|fi |dS )a  Validate a hash-based pyc by checking the real source hash against the one in
    the pyc header.

    *data* is the contents of the pyc file. (Only the first 16 bytes are
    required.)

    *source_hash* is the importlib.util.source_hash() of the source file.

    *name* is the name of the module being imported. It is used for logging.

    *exc_details* is a dictionary passed to ImportError if it raised for
    improved debugging.

    An ImportError is raised if the bytecode is stale.

    r   r   z.hash in bytecode doesn't match hash of source N)r   )r/   source_hashr   r   r	   r	   r
   _validate_hash_pyc  s   r   c                 C   sL   t | }t|trtd| |durt|| |S td	|||d)z#Compile bytecode as found in a pyc.zcode object from {!r}NzNon-code object in {!r}r   rG   )
marshalloads
isinstance
_code_typer   r   _imp_fix_co_filenamer   rc   )r/   r   r   r   coder	   r	   r
   _compile_bytecode  s   


r   c                 C   sF   t t}|td |t| |t| |t|  |S )z+Produce the data for a timestamp-based pyc.r   )	bytearrayr   extendr*   r   dumps)r   mtimer   r/   r	   r	   r
   _code_to_timestamp_pyc  s   r   Tc                 C   sP   t t}d|d> B }|t| t|dksJ || |t|  |S )z&Produce the data for a hash-based pyc.r   r   )r   r   r   r*   r   r   r   )r   r   checkedr/   r   r	   r	   r
   _code_to_hash_pyc  s   
r   c                 C   s>   ddl }t| j}||}tdd}|| |d S )zyDecode bytes representing source code and return the string.

    Universal newline support is used in the decoding.
    r   NT)tokenizeri   BytesIOreadlinedetect_encodingIncrementalNewlineDecoderdecode)source_bytesr   source_bytes_readlineencodingnewline_decoderr	   r	   r
   decode_source  s
   
r   r   submodule_search_locationsc          	      C   s6  |du rd}t |drz|| }W n& ty   Y nw nt|}t|s9z	tt |}W n	 ty8   Y nw t	j
| ||d}d|_|du rct D ]\}}|t|r`|| |}||_ nqKdS |tu rt |drz|| }W n	 ty|   Y n
w |rg |_n||_|jg kr|rt|d }|j| |S )a=  Return a module spec based on a file location.

    To indicate that the module is a package, set
    submodule_search_locations to a list of directory paths.  An
    empty list is sufficient, though its not otherwise useful to the
    import system.

    The loader must take a spec as its only __init__() arg.

    Nz	<unknown>get_filenameoriginT
is_packager   )r   r   r   r   ry   r_   rJ   r[   rT   r   
ModuleSpec_set_fileattr_get_supported_file_loadersr@   r   r   	_POPULATEr   r   rQ   rC   )	r   locationr   r   specloader_classsuffixesr   dirnamer	   r	   r
   spec_from_file_location  sT   

	


r  c                   @   sX   e Zd ZdZdZdZeodev Ze	dd Z
edd Zedd
dZedddZd	S )WindowsRegistryFinderz>Meta path finder for modules declared in the Windows registry.z;Software\Python\PythonCore\{sys_version}\Modules\{fullname}zASoftware\Python\PythonCore\{sys_version}\Modules\{fullname}\Debugz_d.pydc                 C   s2   zt t j| W S  ty   t t j|  Y S w rL   )winregOpenKeyHKEY_CURRENT_USERrT   HKEY_LOCAL_MACHINEr   r	   r	   r
   _open_registry&  s
   z$WindowsRegistryFinder._open_registryc                 C   s   | j r| j}n| j}|j|dtjd d  d}z | |}t|d}W d    W |S 1 s1w   Y  W |S  t	yB   Y d S w )Nz%d.%dr2   )r   sys_versionr   )
DEBUG_BUILDREGISTRY_KEY_DEBUGREGISTRY_KEYrc   r   version_infor	  r  
QueryValuerT   )clsr   registry_keyr   hkeyfilepathr	   r	   r
   _search_registry-  s    z&WindowsRegistryFinder._search_registryNc                 C   sx   |  |}|d u rd S zt| W n
 ty   Y d S w t D ]\}}|t|r9tj|||||d}|  S qd S )Nr   )r  rS   rT   r   r@   r   r   spec_from_loader)r  r   rG   targetr  r   r  r   r	   r	   r
   	find_spec<  s"   
zWindowsRegistryFinder.find_specc                 C   s*   t dt | ||}|dur|jS dS )zjFind module named in the registry.

        This method is deprecated.  Use find_spec() instead.

        zpWindowsRegistryFinder.find_module() is deprecated and slated for removal in Python 3.12; use find_spec() insteadNru   rv   rw   r  r   r  r   rG   r   r	   r	   r
   find_moduleL  s   z!WindowsRegistryFinder.find_moduleNNrL   )r   r   r   r   r  r  _MS_WINDOWSEXTENSION_SUFFIXESr  staticmethodr	  classmethodr  r  r  r	   r	   r	   r
   r    s    

r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_LoaderBasicszSBase class of common code needed by both SourceLoader and
    SourcelessFileLoader.c                 C   s@   t | |d }|ddd }|dd }|dko|dkS )zConcrete implementation of InspectLoader.is_package by checking if
        the path returned by get_filename has a filename of '__init__.py'.r   rs   r   r2   __init__)rQ   r   r   rz   )r   r   r   filename_base	tail_namer	   r	   r
   r   b  s   z_LoaderBasics.is_packagec                 C   r   z*Use default semantics for module creation.Nr	   r   r   r	   r	   r
   create_modulej      z_LoaderBasics.create_modulec                 C   s8   |  |j}|du rtd|jtt||j dS )zExecute the module.Nz4cannot load module {!r} when get_code() returns None)get_coder   r   rc   r   _call_with_frames_removedexecr   )r   moduler   r	   r	   r
   exec_modulem  s   z_LoaderBasics.exec_modulec                 C   s   t | |S )zThis method is deprecated.)r   _load_module_shimr   r   r	   r	   r
   load_moduleu  s   z_LoaderBasics.load_moduleN)r   r   r   r   r   r&  r,  r/  r	   r	   r	   r
   r   ]  s    r   c                   @   sJ   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZddddZdd Z	dS )SourceLoaderc                 C   s   t )zOptional method that returns the modification time (an int) for the
        specified path (a str).

        Raises OSError when the path cannot be handled.
        )rT   r   rG   r	   r	   r
   
path_mtime}  s   zSourceLoader.path_mtimec                 C   s   d|  |iS )a  Optional method returning a metadata dict for the specified
        path (a str).

        Possible keys:
        - 'mtime' (mandatory) is the numeric timestamp of last source
          code modification;
        - 'size' (optional) is the size in bytes of the source code.

        Implementing this method allows the loader to read bytecode files.
        Raises OSError when the path cannot be handled.
        r   )r2  r1  r	   r	   r
   
path_stats  s   zSourceLoader.path_statsc                 C   s   |  ||S )zOptional method which writes data (bytes) to a file path (a str).

        Implementing this method allows for the writing of bytecode files.

        The source path is needed in order to correctly transfer permissions
        )set_data)r   r   
cache_pathr/   r	   r	   r
   _cache_bytecode  s   zSourceLoader._cache_bytecodec                 C   r   )zOptional method which writes data (bytes) to a file path (a str).

        Implementing this method allows for the writing of bytecode files.
        Nr	   )r   rG   r/   r	   r	   r
   r4    r'  zSourceLoader.set_datac              
   C   sF   |  |}z
| |}W t|S  ty" } ztd|d|d}~ww )z4Concrete implementation of InspectLoader.get_source.z'source not available through get_data()r   N)r   get_datarT   r   r   )r   r   rG   r   excr	   r	   r
   
get_source  s   
zSourceLoader.get_sourcer   )	_optimizec                C   s   t jt||dd|dS )zReturn the code object compiled from source.

        The 'data' argument can be any object type that compile() supports.
        r*  T)dont_inheritr~   )r   r)  compile)r   r/   rG   r:  r	   r	   r
   source_to_code  s   zSourceLoader.source_to_codec              	   C   s  |  |}d}d}d}d}d}zt|}W n ty    d}Y nw z| |}	W n	 ty1   Y nw t|	d }z| |}
W n	 tyH   Y nhw ||d}zGt|
||}t|
dd }|d@ dk}|r|d	@ dk}t	j
d
kr|swt	j
dkr| |}t	t|}t|
||| n
t|
||	d || W n ttfy   Y nw td|| t||||dS |du r| |}| ||}td| tjs|dur|dur|r|du rt	|}t|||}
nt||t|}
z
| |||
 W |S  ty   Y |S w |S )zConcrete implementation of InspectLoader.get_code.

        Reading of bytecode requires path_stats to be implemented. To write
        bytecode, set_data must also be implemented.

        NFTr   r   r   r   r   r2   neveralwayssizez{} matches {})r   r   r   zcode object from {})r   r   r}   r3  rT   r'   r7  r   
memoryviewr   check_hash_based_pycsr   _RAW_MAGIC_NUMBERr   r   r   r   r   r   r   r=  r   dont_write_bytecoder   r   r   r6  )r   r   r   r   r   r   
hash_basedcheck_sourcer   str/   r   r   
bytes_datacode_objectr	   r	   r
   r(    s   





zSourceLoader.get_codeN)
r   r   r   r2  r3  r6  r4  r9  r=  r(  r	   r	   r	   r
   r0  {  s    

r0  c                       s\   e Zd ZdZdd Zdd Zdd Ze fdd	Zed
d Z	dd Z
edd Z  ZS )
FileLoaderzgBase file loader class which implements the loader protocol methods that
    require file system usage.c                 C   s   || _ || _dS )zKCache the module name and the path to the file found by the
        finder.Nr   )r   r   rG   r	   r	   r
   r!    s   
zFileLoader.__init__c                 C      | j |j ko| j|jkS rL   	__class__r   r   otherr	   r	   r
   __eq__     
zFileLoader.__eq__c                 C      t | jt | jA S rL   hashr   rG   r   r	   r	   r
   __hash__     zFileLoader.__hash__c                    s   t t| |S )zdLoad a module from a file.

        This method is deprecated.  Use exec_module() instead.

        )superrJ  r/  r.  rM  r	   r
   r/    s   
zFileLoader.load_modulec                 C      | j S z:Return the path to the source file as found by the finder.rN   r.  r	   r	   r
   r   )     zFileLoader.get_filenamec                 C   s   t | ttfr$tt|}| W  d   S 1 sw   Y  dS t|d}| W  d   S 1 s9w   Y  dS )z'Return the data from path as raw bytes.Nr)r   r0  ExtensionFileLoaderri   	open_coder   readrj   )r   rG   ro   r	   r	   r
   r7  .  s   $$zFileLoader.get_datac                 C   s   ddl m} || S )Nr   )
FileReader)importlib.readersra  )r   r+  ra  r	   r	   r
   get_resource_reader7  s   zFileLoader.get_resource_reader)r   r   r   r   r!  rP  rV  r   r/  r   r7  rc  __classcell__r	   r	   rY  r
   rJ    s    
	rJ  c                   @   s.   e Zd ZdZdd Zdd Zdddd	Zd
S )SourceFileLoaderz>Concrete implementation of SourceLoader using the file system.c                 C   s   t |}|j|jdS )z!Return the metadata for the path.)r   r@  )rS   st_mtimest_size)r   rG   rG  r	   r	   r
   r3  A  s   zSourceFileLoader.path_statsc                 C   s   t |}| j|||dS )N_mode)r   r4  )r   r   r   r/   rV   r	   r	   r
   r6  F  s   z SourceFileLoader._cache_bytecoder`   rh  c          	      C   s   t |\}}g }|rt|st |\}}|| |rt|rt|D ]1}t||}zt| W q# ty:   Y q# tyT } zt	
d|| W Y d}~ dS d}~ww zt||| t	
d| W dS  ty~ } zt	
d|| W Y d}~dS d}~ww )zWrite bytes data to a file.zcould not create {!r}: {!r}Nzcreated {!r})rQ   r\   rC   reversedrJ   r   mkdirFileExistsErrorrT   r   r   rp   )	r   rG   r/   ri  parentr   rE   rK   r8  r	   r	   r
   r4  K  s8   

zSourceFileLoader.set_dataN)r   r   r   r   r3  r6  r4  r	   r	   r	   r
   re  =  s
    re  c                   @   s    e Zd ZdZdd Zdd ZdS )SourcelessFileLoaderz-Loader which handles sourceless file imports.c                 C   sD   |  |}| |}||d}t||| tt|dd  ||dS )Nr   r   )r   r   )r   r7  r   r   rA  )r   r   rG   r/   r   r	   r	   r
   r(  n  s   

zSourcelessFileLoader.get_codec                 C   r   )z'Return None as there is no source code.Nr	   r.  r	   r	   r
   r9  ~  r   zSourcelessFileLoader.get_sourceN)r   r   r   r   r(  r9  r	   r	   r	   r
   rn  j  s    rn  c                   @   s\   e Zd Z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edd ZdS )r^  z]Loader for extension modules.

    The constructor is designed to work with FileFinder.

    c                 C   s   || _ || _d S rL   r   )r   r   rG   r	   r	   r
   r!    s   
zExtensionFileLoader.__init__c                 C   rK  rL   rL  rN  r	   r	   r
   rP    rQ  zExtensionFileLoader.__eq__c                 C   rR  rL   rS  rU  r	   r	   r
   rV    rW  zExtensionFileLoader.__hash__c                 C   s$   t tj|}t d|j| j |S )z&Create an unitialized extension modulez&extension module {!r} loaded from {!r})r   r)  r   create_dynamicr   r   rG   )r   r   r+  r	   r	   r
   r&    s   z!ExtensionFileLoader.create_modulec                 C   s$   t tj| t d| j| j dS )zInitialize an extension modulez(extension module {!r} executed from {!r}N)r   r)  r   exec_dynamicr   r   rG   r   r+  r	   r	   r
   r,    s   zExtensionFileLoader.exec_modulec                    s$   t | jd  t fddtD S )z1Return True if the extension module is a package.r   c                 3   s    | ]	} d | kV  qdS )r!  Nr	   r   suffix	file_namer	   r
   r     s    z1ExtensionFileLoader.is_package.<locals>.<genexpr>)rQ   rG   anyr  r.  r	   rt  r
   r     s   zExtensionFileLoader.is_packagec                 C   r   )z?Return None as an extension module cannot create a code object.Nr	   r.  r	   r	   r
   r(    r   zExtensionFileLoader.get_codec                 C   r   )z5Return None as extension modules have no source code.Nr	   r.  r	   r	   r
   r9    r   zExtensionFileLoader.get_sourcec                 C   rZ  r[  rN   r.  r	   r	   r
   r     r\  z ExtensionFileLoader.get_filenameN)r   r   r   r   r!  rP  rV  r&  r,  r   r(  r9  r   r   r	   r	   r	   r
   r^    s    r^  c                   @   sl   e Zd ZdZ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S )_NamespacePatha&  Represents a namespace package's path.  It uses the module name
    to find its parent module, and from there it looks up the parent's
    __path__.  When this changes, the module's own path is recomputed,
    using path_finder.  For top-level modules, the parent module's path
    is sys.path.r   c                 C   s,   || _ || _t|  | _| j| _|| _d S rL   )_name_pathr   _get_parent_path_last_parent_path_epoch_last_epoch_path_finderr   r   rG   path_finderr	   r	   r
   r!    s
   
z_NamespacePath.__init__c                 C   s&   | j d\}}}|dkrdS |dfS )z>Returns a tuple of (parent-module-name, parent-path-attr-name)rs   r   )r   rG   __path__)rx  rz   )r   rm  dotmer	   r	   r
   _find_parent_path_names  s   z&_NamespacePath._find_parent_path_namesc                 C   s   |   \}}ttj| |S rL   )r  r   r   modules)r   parent_module_namepath_attr_namer	   r	   r
   rz    s   z_NamespacePath._get_parent_pathc                 C   sd   t |  }|| jks| j| jkr/| | j|}|d ur(|jd u r(|jr(|j| _	|| _| j| _| j	S rL   )
r   rz  r{  r|  r}  r~  rx  r   r   ry  )r   parent_pathr   r	   r	   r
   _recalculate  s   z_NamespacePath._recalculatec                 C      t |  S rL   )iterr  rU  r	   r	   r
   __iter__     z_NamespacePath.__iter__c                 C   s   |   | S rL   r  )r   indexr	   r	   r
   __getitem__  r  z_NamespacePath.__getitem__c                 C   s   || j |< d S rL   )ry  )r   r  rG   r	   r	   r
   __setitem__  s   z_NamespacePath.__setitem__c                 C   r  rL   )r   r  rU  r	   r	   r
   __len__  r  z_NamespacePath.__len__c                 C      d | jS )Nz_NamespacePath({!r}))rc   ry  rU  r	   r	   r
   __repr__  r  z_NamespacePath.__repr__c                 C   s   ||   v S rL   r  r   itemr	   r	   r
   __contains__  r  z_NamespacePath.__contains__c                 C   s   | j | d S rL   )ry  rC   r  r	   r	   r
   rC        z_NamespacePath.appendN)r   r   r   r   r|  r!  r  rz  r  r  r  r  r  r  r  rC   r	   r	   r	   r
   rw    s    
rw  c                   @   sX   e Zd Zdd Ze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S )_NamespaceLoaderc                 C   s   t |||| _d S rL   )rw  ry  r  r	   r	   r
   r!    s   z_NamespaceLoader.__init__c                 C   s   t dt d| jS )zsReturn repr for the module.

        The method is deprecated.  The import machinery does the job itself.

        zR_NamespaceLoader.module_repr() is deprecated and slated for removal in Python 3.12z<module {!r} (namespace)>)ru   rv   rw   rc   r   )r+  r	   r	   r
   module_repr  s   z_NamespaceLoader.module_reprc                 C   r   )NTr	   r.  r	   r	   r
   r        z_NamespaceLoader.is_packagec                 C   r   )Nr   r	   r.  r	   r	   r
   r9    r  z_NamespaceLoader.get_sourcec                 C   s   t dddddS )Nr   z<string>r*  T)r;  )r<  r.  r	   r	   r
   r(    r  z_NamespaceLoader.get_codec                 C   r   r$  r	   r%  r	   r	   r
   r&    r'  z_NamespaceLoader.create_modulec                 C   s   d S rL   r	   rq  r	   r	   r
   r,    r  z_NamespaceLoader.exec_modulec                 C   s   t d| j t | |S )zbLoad a namespace module.

        This method is deprecated.  Use exec_module() instead.

        z&namespace module loaded with path {!r})r   r   ry  r-  r.  r	   r	   r
   r/    s   z_NamespaceLoader.load_modulec                 C   s   ddl m} || jS )Nr   )NamespaceReader)rb  r  ry  )r   r+  r  r	   r	   r
   rc  '  s   
z$_NamespaceLoader.get_resource_readerN)r   r   r   r!  r  r  r   r9  r(  r&  r,  r/  rc  r	   r	   r	   r
   r    s    

r  c                   @   sv   e Zd ZdZedd Zedd Zedd Zedd	 Z	edddZ
edddZedddZedd Zd
S )
PathFinderz>Meta path finder for sys.path and package __path__ attributes.c                  C   sN   t tj D ]\} }|du rtj| = qt|dr|  qt jd7  _dS )z}Call the invalidate_caches() method on all path entry finders
        stored in sys.path_importer_caches (where implemented).Ninvalidate_cachesr   )listr   path_importer_cacheitemsr   r  rw  r|  )r   finderr	   r	   r
   r  2  s   

zPathFinder.invalidate_cachesc              	   C   sL   t jdurt jstdt t jD ]}z|| W   S  ty#   Y qw dS )z.Search sys.path_hooks for a finder for 'path'.Nzsys.path_hooks is empty)r   
path_hooksru   rv   r   r   )rG   hookr	   r	   r
   _path_hooks?  s   
zPathFinder._path_hooksc                 C   sd   |dkrzt  }W n
 ty   Y dS w ztj| }W |S  ty1   | |}|tj|< Y |S w )zGet the finder for the path entry from sys.path_importer_cache.

        If the path entry is not in the cache, find the appropriate finder
        and cache it. If no finder is available, store None.

        r   N)r   r[   FileNotFoundErrorr   r  KeyErrorr  )r  rG   r  r	   r	   r
   _path_importer_cacheL  s   
zPathFinder._path_importer_cachec                 C   s   t |drt| d}t|t ||\}}nt| d}t|t ||}g }|d ur:t||S t	|d }||_
|S )Nr   z5.find_spec() not found; falling back to find_loader()z5.find_spec() not found; falling back to find_module())r   r   _object_nameru   rv   r   r   r  r  r   r   )r  r   r  r   r   r   r   r	   r	   r
   _legacy_get_specb  s   

zPathFinder._legacy_get_specNc           	      C   s   g }|D ]C}t |ttfsq| |}|durGt|dr#|||}n| ||}|du r.q|jdur7|  S |j}|du rBt	d|
| qt|d}||_|S )z?Find the loader or namespace_path for this module/package name.Nr  zspec missing loader)r   r   bytesr  r   r  r  r   r   r   r   r   r   )	r  r   rG   r  namespace_pathentryr  r   r   r	   r	   r
   	_get_specw  s*   



zPathFinder._get_specc                 C   s^   |du rt j}| |||}|du rdS |jdu r-|j}|r+d|_t||| j|_|S dS |S )zTry to find a spec for 'fullname' on sys.path or 'path'.

        The search is based on sys.path_hooks and sys.path_importer_cache.
        N)r   rG   r  r   r   r   rw  )r  r   rG   r  r   r  r	   r	   r
   r    s   
zPathFinder.find_specc                 C   s*   t dt | ||}|du rdS |jS )zfind the module on sys.path or 'path' based on sys.path_hooks and
        sys.path_importer_cache.

        This method is deprecated.  Use find_spec() instead.

        zePathFinder.find_module() is deprecated and slated for removal in Python 3.12; use find_spec() insteadNr  r  r	   r	   r
   r    s   zPathFinder.find_modulec                  O   s   ddl m} |j| i |S )a   
        Find distributions.

        Return an iterable of all Distribution instances capable of
        loading the metadata for packages matching ``context.name``
        (or all names if ``None`` indicated) along the paths in the list
        of directories ``context.path``.
        r   )MetadataPathFinder)importlib.metadatar  find_distributions)r   r   r  r	   r	   r
   r    s   
zPathFinder.find_distributionsrL   r  )r   r   r   r   r  r  r  r  r  r  r  r  r  r  r	   r	   r	   r
   r  .  s$    



r  c                   @   sZ   e Zd ZdZdd Zdd ZeZdd Zdd	 Z	dddZ
dd Zedd Zdd Zd
S )
FileFinderzFile-based finder.

    Interactions with the file system are cached for performance, being
    refreshed when the directory the finder is handling has been modified.

    c                    sp   g }|D ]\ }|  fdd|D  q|| _|pd| _t| js+tt | j| _d| _t | _	t | _
dS )zInitialize with the path to search on and a variable number of
        2-tuples containing the loader and the file suffixes the loader
        recognizes.c                 3   s    | ]}| fV  qd S rL   r	   rr  r   r	   r
   r     s    z&FileFinder.__init__.<locals>.<genexpr>rs   r   N)r   _loadersrG   r_   rJ   r   r[   _path_mtimeset_path_cache_relaxed_path_cache)r   rG   loader_detailsloadersr  r	   r  r
   r!    s   

zFileFinder.__init__c                 C   s
   d| _ dS )zInvalidate the directory mtime.r   N)r  rU  r	   r	   r
   r    rZ   zFileFinder.invalidate_cachesc                 C   s6   t dt | |}|du rdg fS |j|jpg fS )zTry to find a loader for the specified module, or the namespace
        package portions. Returns (loader, list-of-portions).

        This method is deprecated.  Use find_spec() instead.

        zeFileFinder.find_loader() is deprecated and slated for removal in Python 3.12; use find_spec() insteadN)ru   rv   rw   r  r   r   )r   r   r   r	   r	   r
   r     s   
zFileFinder.find_loaderc                 C   s   |||}t ||||dS )Nr   )r  )r   r   r   rG   smslr  r   r	   r	   r
   r    s   
zFileFinder._get_specNc              	   C   sz  d}| dd }zt| jpt j}W n ty    d}Y nw || jkr-|   || _t	 r8| j
}| }n| j}|}||v rlt| j|}| jD ]\}	}
d|	 }t||}t|rg| |
|||g|  S qJt|}| jD ]7\}	}
z
t| j||	 }W n ty   Y  dS w tjd|dd ||	 |v rt|r| |
||d|  S qo|rtd	| t|d}|g|_|S dS )
zoTry to find a spec for the specified module.

        Returns the matching spec, or None if not found.
        Frs   r2   r   r!  Nz	trying {})	verbosityzpossible namespace for {})rz   rS   rG   r   r[   rf  rT   r  _fill_cacher   r  r   r  rJ   r  rY   r  r\   r   r   r   r   r   )r   r   r  is_namespacetail_moduler   cachecache_module	base_pathrs  r   init_filename	full_pathr   r	   r	   r
   r     sV   



zFileFinder.find_specc           	   
   C   s   | j }zt|pt }W n tttfy   g }Y nw tj	ds)t
|| _n%t
 }|D ]}|d\}}}|rCd|| }n|}|| q.|| _tj	tr^dd |D | _dS dS )zDFill the cache of potential modules and packages for this directory.r   rs   ra   c                 S   s   h | ]}|  qS r	   )r   )r   fnr	   r	   r
   r   P  s    z)FileFinder._fill_cache.<locals>.<setcomp>N)rG   r   listdirr[   r  PermissionErrorNotADirectoryErrorr   r   r    r  r  r   rc   r   addr!   r  )	r   rG   contentslower_suffix_contentsr  r   r  rs  new_namer	   r	   r
   r  3  s&   zFileFinder._fill_cachec                    s    fdd}|S )a  A class method which returns a closure to use on sys.path_hook
        which will return an instance using the specified loaders and the path
        called on the closure.

        If the path called on the closure is not a directory, ImportError is
        raised.

        c                    s$   t | s
td| d | gR  S )z-Path hook for importlib.machinery.FileFinder.zonly directories are supportedrN   )r\   r   rN   r  r  r	   r
   path_hook_for_FileFinder\  s   z6FileFinder.path_hook.<locals>.path_hook_for_FileFinderr	   )r  r  r  r	   r  r
   	path_hookR  s   
zFileFinder.path_hookc                 C   r  )NzFileFinder({!r}))rc   rG   rU  r	   r	   r
   r  d  r  zFileFinder.__repr__rL   )r   r   r   r   r!  r  r   r  r   r  r  r  r  r  r  r	   r	   r	   r
   r    s    
3
r  c                 C   s   |  d}|  d}|s!|r|j}n||krt||}nt||}|s*t|||d}z|| d< || d< || d< || d< W d S  tyG   Y d S w )N
__loader____spec__r  __file__
__cached__)getr   rn  re  r  	Exception)nsr   pathname	cpathnamer   r   r	   r	   r
   _fix_up_modulej  s$   


r  c                  C   s&   t t f} ttf}ttf}| ||gS )z_Returns a list of file-based module loaders.

    Each item is a tuple (loader, suffixes).
    )r^  r   extension_suffixesre  r   rn  r   )
extensionssourcebytecoder	   r	   r
   r     s   
r   c                 C   s   | a d S rL   )r   )_bootstrap_moduler	   r	   r
   _set_bootstrap_module  s   r  c                 C   s2   t |  t }tjtj| g tjt	 dS )z)Install the path-based import components.N)
r  r   r   r  r   r  r  	meta_pathrC   r  )r  supported_loadersr	   r	   r
   _install  s   r  )r`   rL   )NNN)r   r   )T)Ur   r   r   ri   r   ru   r   r   r  ntr   r  posixr8   allrA   r   r?   rD   _pathseps_with_colonr"   %_CASE_INSENSITIVE_PLATFORMS_BYTES_KEYr!   r#   r   r*   r0   r3   rJ   rQ   rS   rX   rY   r\   r_   rp   type__code__r   r(   r   r'   r-   rC  r   r   r   rC   r  r  r   DEBUG_BYTECODE_SUFFIXESOPTIMIZED_BYTECODE_SUFFIXESr   r   r   r   r   r   r   r   r   r   r   r   r   r   objectr   r  r  r   r0  rJ  re  rn  r^  rw  r  r  r  r  r   r  r  r	   r	   r	   r
   <module>   s    





	


 
G(!



IC 2-5E1   
