U
    nuf!                     @   s*  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
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d
d Zdd Zdd Zdd Zd/ddZdd Zdd Zd0ddZdd Zdd Zdd Zd d! Z d1d#d$Z!d2d%d&Z"d'd( Z#d)d* Z$d+d, Z%G d-d. d.Z&dS )3    N)Path)ZipFile)zipimporterZipImportError)all_suffixes)access)debug)parser_utils)KnownContentFileIO	ZipFileIOc                   C   s   t jS N)syspath r   r   P/tmp/pip-unpacked-wheel-ol4ehs9f/jedi/inference/compiled/subprocess/functions.pyget_sys_path   s    r   c                 K   s   t j| f|S r   )r   load_module)inference_statekwargsr   r   r   r      s    r   c                 O   s   | j |}t|j|||S r   )Zcompiled_subprocessZget_access_handlegetattrr   )r   id	attributeargsr   handler   r   r   get_compiled_method_return   s    r   c                 C   s   t | |S r   )r   Zcreate_access_path)r   objr   r   r   create_simple_object   s    r   c                 K   sd   |dk	r|t j t _}z8ztf d|i|W W  S  tk
rJ   Y W 
dS X W 5 |dk	r^|t _X dS )zK
    Returns Tuple[Union[NamespaceInfo, FileIO, None], Optional[bool]]
    N	full_name)NN)r   r   _find_moduleImportError)r   sys_pathr   r   tempr   r   r   get_module_info!   s    r"   c                 C   s   t jS r   )r   builtin_module_names)r   r   r   r   get_builtin_module_names0   s    r$   c                 C   s   |dS )zE
    Raise an error to simulate certain problems for unit tests.
    Nr   )r   Zexception_typer   r   r   _test_raise_error4   s    r%   c                 C   s>   |dk	r t |tjd tj  |dk	r:t | tj  dS )zL
    Force some prints in the subprocesses. This exists for unit tests.
    N)file)printr   stderrflushstdout)r   r(   r*   r   r   r   _test_print;   s    
r+   c                 C   s6   t  D ]*}tj| d| }tj|r|  S qdS )z^
    The __init__ file can be searched in a directory. If found return it, else
    None.
    __init__N)r   osr   joinexists)Zdirectory_pathsuffixr   r   r   r   _get_init_pathG   s
    

r1   c                 C   s
   t |S r   )r	   safe_literal_eval)r   valuer   r   r   r2   S   s    r2   c                  O   s   t t| |S r   )list_iter_module_names)r   r   r   r   r   iter_module_namesW   s    r6   c                 c   s   |D ]}zdd t |D }W nV tk
rv   zt|}t|j|j}W n& tk
rp   t	d| Y Y qY nX Y nX |D ]`\}}|r|dkr|
 r|V  q||dr|d d }n
t|}|r|d|kr||dkr||V  q|qd S )	Nc                 s   s   | ]}|j | fV  qd S r   )nameis_dir).0entryr   r   r   	<genexpr>_   s     z%_iter_module_names.<locals>.<genexpr>z"Not possible to list directory: %s__pycache__z.pyi.r,   )r-   scandirOSErrorr   _zip_list_subdirectoryarchiveprefixr   r   warningisidentifierendswithinspectgetmodulename)r   pathsr   Zdir_entriesZzip_import_infor7   r8   modnamer   r   r   r5   [   s.     

r5   Tc           
   	   C   s   d}d}t jD ]}|r(|tjjkr(d}n|}z
|j}W n tk
rN   Y qY nX || |}|dk	r|jdkrnq|j}|dkr|j	s|s| n|}t
||jj}	|	df  S  qqt| ||S )a  
    Provides information about a module.

    This function isolates the differences in importing libraries introduced with
    python 3.3 on; it gets a module name and optionally a path. It will return a
    tuple containin an open file for the module (if not builtin), the filename
    or the name of the module if it is a builtin one and a boolean indicating
    if the module is contained in a package.
    NfrozenT)r   	meta_path	importlib	machinery
PathFinder	find_specAttributeErrororiginloaderhas_locationImplicitNSInfosubmodule_search_locations_path_find_module_py33)
stringr   r   is_global_searchspecrS   finderprP   Zimplicit_ns_infor   r   r   r   ~   s*    





r   c              
   C   s   |s"t jj| |}|d k	r"|j}|d kr|d krzt j| }|d k	rN|j}W n2 tk
r } ztdt| W 5 d }~X Y nX |d krtd	| t
|| S )NzOriginally  zCouldn't find a loader for {})rM   rN   rO   rP   rS   util
ValueErrorr   reprformat_from_loader)rY   r   rS   r   rZ   r[   er   r   r   rX      s    
"rX   c                 C   s   z
| j }W n tk
r"   d}Y n
X ||}z
| j}W n tk
rR   d |f Y S X ||}zt| j}W n tk
r   tdY nX |tjjjk	r| |}n
t	| |}|d krd |fS t
| trt||t| j|fS t|||fS )NFz$get_source was not defined on loader)
is_packagerQ   get_filenametype
get_sourcer   rM   rN   SourceFileLoader_get_source
isinstancer   r   r   rB   r
   )rS   rY   Zis_package_methodrd   re   module_pathfcoder   r   r   rb      s,    




rb   c                 C   s<   |  |}z| |W S  tk
r6   td|dY nX dS )z
    This method is here as a replacement for SourceLoader.get_source. That
    method returns unicode, but we prefer bytes.
    z'source not available through get_data())r7   N)re   get_datar@   r   )rS   fullnamer   r   r   r   ri      s    
ri   c                 c   sT   t | }t|}| }|D ]2}t|}|j|kr||}|j|dfV  qd S )N/)r   r   namelistparentrelative_tor7   rF   )zip_pathZzip_subdir_pathzip_fileZzip_content_file_pathsZraw_file_name	file_pathr   r   r   rA      s    

rA   c                   @   s   e Zd ZdZdd ZdS )rU   z;Stores information returned from an implicit namespace specc                 C   s   || _ || _d S r   )r7   rI   )selfr7   rI   r   r   r   r,      s    zImplicitNSInfo.__init__N)__name__
__module____qualname____doc__r,   r   r   r   r   rU      s   rU   )NN)NN)NNT)NNNT)'r   r-   rG   rM   pathlibr   zipfiler   	zipimportr   r   Zimportlib.machineryr   Zjedi.inference.compiledr   Zjedir   r	   Zjedi.file_ior
   r   r   r   r   r   r"   r$   r%   r+   r1   r2   r6   r5   r   rX   rb   ri   rA   rU   r   r   r   r   <module>   s:   

#
*
'