U
    {hu                  	   @   s  d dl Z d dlZd dlZd dlZejdd dk rZejdeejdd   ed ddl	m
Z
 ddlmZ ddlmZmZ dd	l
mZmZmZ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lmZ ddlmZmZmZm Z  dd Z!d"ee!eee!e Z#e$d"e#Z#e j%&e j%'e j%(e j%(e)dZ*G dd dZ+dd Z,dd Z-dd Z.dd Z/dd  Z0G d!d" d"Z1G d#d$ d$Z2G d%d& d&e3Z4d'd( Z5d9d)d*Z6d:d+d,Z7d;d-d.Z$ej8d<d1d2Z9ej8d3d4 Z:d5d6 Z;d=d7d8Z<dS )>    N   )      z0Sorry, Cython requires Python 3.8+, found %d.%d
   )Errors)EncodedString)PyrexScannerFileSourceDescriptor)
PyrexErrorCompileErrorerrorwarning)ModuleScope)Utils)Options)CompilationOptionsdefault_options)parse_command_line)unicode_start_ch_anyunicode_continuation_ch_anyunicode_start_ch_rangeunicode_continuation_ch_rangec              	   C   sB   g }t dt| dD ]"}|d| | | |d   qd|S )Nr   r   z{}-{}r    )rangelenappendformatjoin)Zchrsouti r    8/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Compiler/Main.py_make_range_re!   s     r"   z[{0}{1}][{0}{2}{1}{3}]*z{0}(\.{0})*$ZIncludesc                   @   s  e Zd ZdZdZd=ddZedd Zedd Z	d	d
 Z
d>ddZdd Zdd Zd?ddZedd Zedd Zd@ddZdAddZdBddZdCdd ZdDd!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdEd1d2Zd3d4 Zd5d6 Z d7d8 Z!d9d: Z"d;d< Z#dS )FContextNFc                 C   s   ddl m}m} d|ji| _|| | _| j| jd< || _t | _	|| _
|| _|| _i | _i | _i | _|d k	rx| | | j
dd| _d | _d S )Nr   )BuiltinCythonScope__builtin__Zcythonlegacy_implicit_noexceptF)r   r$   r%   Zbuiltin_scopemodulesZcreate_cython_scopecython_scopeinclude_directoriessetfuture_directivescompiler_directivescppoptionspxdsZutility_pxds	_internedset_language_levelgetr'   Zgdb_debug_outputwriter)selfr*   r-   r.   language_levelr/   r$   r%   r    r    r!   __init__B   s     
zContext.__init__c                 C   s   | |j |j|j|j|dS )N)r/   )Zinclude_pathr-   cplusr5   )clsr/   r    r    r!   from_options]   s
    
  zContext.from_optionsc                 C   s   | j r| j jS d S N)r/   shared_utility_qualified_namer4   r    r    r!   r;   b   s    z%Context.shared_utility_qualified_namec                 C   s|   ddl m}m}m}m}m} t }|dkr0d}nt|}|dkrT||||||g || _	|| _
|dkrx| jd | jd< d S )Nr   )print_functionunicode_literalsabsolute_importdivisiongenerator_stopZ3strr   r&   builtins)ZFuturer=   r>   r?   r@   rA   r+   intupdater5   r,   r(   )r4   levelr=   r>   r?   r@   rA   r,   r    r    r!   r2   f   s    zContext.set_language_levelc                 C   sL   t ||f}z| j| W S  tk
r*   Y nX t |}|r>||_|| j|< |S r:   )r   r1   KeyErrorencoding)r4   valuerG   keyr    r    r!   intern_ustringt   s    

zContext.intern_ustringc                 C   s   ddl m} t|trV|jdkrVt||t }t|| j	}|
| |}|||}nd|jrddl m}	 |	| }
|| ||}|j||
|	jd}|||}n|| ||}|||}|S )Nr   PipelineZpyx)ParseTreeTransforms)before)r   rL   
isinstancer	   Z
_file_typeCompilationSourceosgetcwdcreate_default_resultobjr/   Zcreate_pyx_as_pxd_pipelinerun_pipelineZin_utility_coderM   ZCnameDirectivesTransformZcreate_pxd_pipelineZinsert_into_pipelineZInterpretCompilerDirectives)r4   source_descscopemodule_namerL   sourceZresult_sinkpipelineresultrM   Z	transformr    r    r!   process_pxd   s&    
 zContext.process_pxdc                 C   s
   t |S r:   )r   Zreport_error)r4   excr    r    r!   nonfatal_error   s    zContext.nonfatal_errorc                 C   sp   | d}| }dd |D }|dkrld}dD ],}| j||d d | d}|r0| |} q^q0|||f |S )N.c                 S   s   g | ]}|d fqS )Tr    ).0pr    r    r!   
<listcomp>   s     z1Context._split_qualified_name.<locals>.<listcomp>r6   F).py.pyx)suffix
source_possource_file_pathsys_path)splitpopsearch_include_directories_is_init_filer   )r4   qualified_namerelative_importZqualified_name_partsZ	last_part
is_packagerd   pathr    r    r!   _split_qualified_name   s$    
    
zContext._split_qualified_namec                 C   s   | rt j| dkS dS )N)z__init__.pyxz__init__.pyz__init__.pxdF)rQ   ro   basename)ro   r    r    r!   rk      s    zContext._is_init_filec                 C   sF   |sd S t j|}d|krB|t j|d krBt| d| dd d S )Nr^   r   ^Dotted filenames ('%s') are deprecated. Please use the normal Python package directory layout.r   rE   )rQ   ro   rq   splitextr   )pospxd_pathnamerl   Zpxd_filenamer    r    r!   _check_pxd_filename   s    zContext._check_pxd_filenamer   Tc                 C   s|  d}|rt d||||f  d }d }	|rH|r8||}
qL|j}
|}d }n|}
t|
snt|pd|ddfd| |r|r~t d ||}|s| j|
|| d}	| ||	|
 |	r| 	|	}|j
||d}|s|rt d |r|}
| }| j|
|dD ]\}}|j
||d}q|rt d	|  |jsx|r4t d
 |	s|rHt d | j|
||oZ| d}	| ||	|
 |rt d|	  |	s|rd|_| j|
d|| d}|r|tjrnt|d|
dtj  |	rxd|_z|rt d|	  |dtjtj|	d  }|	|s"|	}t|	|}| |||
\}}|rH||\}}||f| j|< W n tk
rv   Y nX |S )Nr   zPContext.find_module: module_name = %s, from_module = %s, pos = %s, need_pxd = %sz'%s' is not a valid module namez...trying relative importrg   )
as_packagez...trying absolute import)rm   z...scope = %sz...pxd not loadedz...looking for pxd filez......found %sTrb   )rd   re   rg   z'%s.pxd' not foundr^   zContext.find_module: Parsing %sr   )printZqualify_namerl   module_name_patternmatchr   lookup_submodulefind_pxd_filerw   rk   find_submodulerp   Zpxd_file_loadedrj   endswithr   ZPACKAGE_FILESr   replacerQ   sepro   rt   r	   r[   r0   )r4   rW   Zfrom_moduleru   Zneed_pxdZabsolute_fallbackrm   Zdebug_find_modulerV   rv   rl   rn   nameZpackage_pathnamerel_pathrU   errrZ   Zpxd_codenodesZ	pxd_scoper    r    r!   find_module   s    	   


   
zContext.find_modulec                 C   s6   | j |d|||d}|d kr2tjr2| j|||dS |S )N.pxdrd   re   rg   rf   rx   )rj   r   Zcimport_from_pyxfind_pyx_file)r4   rl   ru   rg   rf   pxdr    r    r!   r~     s    	    zContext.find_pxd_filec                 C   s   | j |d|||dS )Nrc   r   )rj   )r4   rl   ru   rg   rf   r    r    r!   r   $  s        zContext.find_pyx_filec                 C   s(   | j ||d|d}|s$t|d|  |S )NT)re   includerf   z'%s' not found)rj   r   )r4   filenameru   rf   ro   r    r    r!   find_include_file*  s       zContext.find_include_filec                 C   s8   | j }|r|tj }t|tg }t|||p.d|||S )Nr   )r*   sysro   tuplestandard_include_pathrj   )r4   rl   rd   re   r   rg   rf   include_dirsr    r    r!   rj   3  s    
     z"Context.search_include_directoriesc                 C   s
   t |S r:   )r   find_root_package_dir)r4   	file_pathr    r    r!   r   =  s    zContext.find_root_package_dirc                 C   s   t |t|S r:   )r   check_package_dirr   )r4   dirpackage_namesr    r    r!   r   @  s    zContext.check_package_dirc                 C   s   t j|sdS t|}t||r*dS t|d}t j|rRt||rRdS | |D ]P\}}|dkr|| j||d}n|dkr\| j	||d}nq\|r\t||r\ dS q\dS )Nr   r   cimport)rf   r   r   )
rQ   ro   existsr   Zmodification_timeZfile_newer_thanreplace_suffixread_dependency_filer~   rj   )r4   source_pathZoutput_pathZc_timeZpxd_pathkindr   dep_pathr    r    r!   c_file_out_of_dateC  s"    
zContext.c_file_out_of_datec                 C   s   dd |  |D S )Nc                 S   s   g | ]\}}|d kr|qS )r   r    )r_   r   r   r    r    r!   ra   X  s    z7Context.find_cimported_module_names.<locals>.<listcomp>)r   )r4   r   r    r    r!   find_cimported_module_namesW  s    z#Context.find_cimported_module_namesc                 C   s
   t |S r:   )r   is_package_dir)r4   Zdir_pathr    r    r!   r   [  s    zContext.is_package_dirc              	   C   sP   t |d}tj|rHt|}dd dd |D D }W 5 Q R X |S dS d S )Nz.depc                 S   s    g | ]}d |kr| d dqS ) r   )rh   )r_   liner    r    r!   ra   b  s   z0Context.read_dependency_file.<locals>.<listcomp>c                 s   s   | ]}|  V  qd S r:   )strip)r_   lr    r    r!   	<genexpr>c  s     z/Context.read_dependency_file.<locals>.<genexpr>r    )r   r   rQ   ro   r   open)r4   r   r   fchunksr    r    r!   r   ^  s    
zContext.read_dependency_filec                 C   s   | j |d S r:   )r(   r3   )r4   r   r    r    r!   r}   i  s    zContext.lookup_submodulec                 C   s,   |  |}|s(t|d | |d}|| j|< |S )N)parent_modulecontextrn   )r}   r   r(   )r4   r   ry   rV   r    r    r!   r   m  s    
  
zContext.find_submodulec              
   C   s   t |tstd| j|_t }z| v}ddlm} t	|||j
|| d}||||}	| jjrzddlm}
 W n tk
r   tdY nX |
|j W 5 Q R X W n. tk
r } z| ||W 5 d }~X Y nX t |krt |	S )N$Only file sources for code supportedr   )Parsing)Zsource_encodingrV   r   r   )ConcreteSyntaxTreezLFormal grammar can only be used with compiled Cython with an available pgen.)rO   r	   RuntimeErrorr.   r   get_errors_countZget_file_objectr   r   r   rG   Zp_moduler/   Zformal_grammarParserr   ImportErrorr   UnicodeDecodeError_report_decode_errorr   )r4   rU   rV   r   full_module_name
num_errorsr   r   streer   er    r    r!   parsev  s2    


 
zContext.parsec              	   C   s   |j d }|j d }|j d }d}d }}t|jdddF}	t|	dD ]4\}}
|t|
7 }||krF||t|
  d } q|qFW 5 Q R X t|||fd||f S )	Nr   r   r   z	iso8859-1r   )rG   newlinezqDecoding error, missing or incorrect coding=<encoding-name> at top of source (cannot decode with encoding %r: %s))argsr   r   	enumerater   r   )r4   rU   r\   msgpositionrG   r   columnidxr   datar    r    r!   r     s     



zContext._report_decode_errorc           
      C   sx   t j|\}}t j|\}}d|kr,|S |g}| |rft j|\}}	||krVqf||	 |}q2|  d|S Nr^   )rQ   ro   rh   rt   r   r   reverser   )
r4   ro   r/   r   r   rW   _namesparentpackage_namer    r    r!   extract_module_name  s    

zContext.extract_module_namec                 C   s<   t   |jr$t|jd }|_nd }t j||jd d S )Nz.lis)ro   Zecho_to_stderr)	r   Zinit_threadZuse_listing_filer   r   main_source_filelisting_fileZopen_listing_fileZerrors_to_stderr)r4   r/   rZ   ro   r    r    r!   setup_errors  s
    zContext.setup_errorsc                 C   s~   |j j}t|tstdt  t |_|jdkr:d}|rz|j	rzzt
|j	t|j W n tk
rr   Y nX d |_	d S )Nr   r   T)compilation_sourcerU   rO   r	   r   r   Zclose_listing_filer   r   c_filer   Zcastrate_filerQ   statr   OSError)r4   r   r/   rZ   rU   r    r    r!   teardown_errors  s    



zContext.teardown_errors)FNN)N)F)NNr   TF)NTN)NTN)NN)NNFFN)F)$__name__
__module____qualname__r)   r5   r6   classmethodr9   propertyr;   r2   rJ   r[   r]   rp   staticmethodrk   rw   r   r~   r   r   rj   r   r   r   r   r   r   r}   r   r   r   r   r   r   r    r    r    r!   r#   3   sZ       






    
\



         


	r#   c                 C   sb   |j rd}nd}t| |}|jrZtj||j}tj|rTtj|tj|S |S n|S d S )Nz.cppz.c)	r7   r   r   Zoutput_filerQ   ro   r   isdirrq   )Zsource_filenamecwdr/   Zc_suffixZsuggested_file_nameZout_pathr    r    r!   get_output_filename  s    r   c                 C   s:   t  }| jj|_| |_| j}t|j| j||_|j|_|S r:   )	CompilationResultrU   r   r   r   r   r   r   Zembedded_metadata)r   r/   rZ   rU   r    r    r!   rS     s    
 rS   c           	      C   sx   t  }t j| }|p"|| |}t|}t| |jr^|	dt j
| }||sb| }n|}t||}t|||S r   )rQ   rR   ro   abspathr   r   r   Z$raise_error_if_module_name_forbiddenZ'relative_path_in_code_position_commentsr   r   r   r	   rP   )	rX   
source_extr   r/   r   r   abs_pathr   rU   r    r    r!   setup_source_object  s    


r   c                 C   s   t  }t| ||}|||}|rl||| t j| d }	||	dd   t| |	|||} t	| |S t
| |||}
|r||||
 |
S )Nr   )rQ   rR   r   Zlookup_cacheZload_from_cachero   rt   configure_language_defaultsr   rS   rT   Zstore_to_cache)rX   r/   r   r   cachefingerprintr   output_filenamecachedr   rZ   r    r    r!   run_cached_pipeline  s    
r   c              	   C   s  ddl m} |jr"tjd|   tj| d }tj	| }|
|dd   t| ||||} t| |}|jd krtj|jd d }tj|rt|dd}	d|	d	krd
|_W 5 Q R X | dks|s||||}
n||||}
||| d| jkrHdtjtj|d krHt| jddfdtj| dd td|jtjjr||js|t| jddfddd ||
| \}}| ||| |d kr|j!rddl"m#} ||$|j%}t&'|j|j%| |S )Nr   rK   zCompiling %s
r   z.htmlzUTF-8)rG   z<!-- Generated by Cythond   Trb   r^   rr   rs   z[.]c(pp|[+][+]|xx)$z:Filename implies a c++ file but Cython is not in c++ mode.r   create_dependency_tree)(r   rL   verboser   stderrwriterQ   ro   rt   r   r   r   rS   annotater   r   r   readlowerZcreate_py_pipelineZcreate_pyx_pipeliner   r   rq   r   rU   research	RegexFlag
IGNORECASEr.   rT   r   ZdepfileBuild.Dependenciesr   all_dependenciesr   r   Zwrite_depfile)rX   r/   r   r   rL   r   r   rZ   Zhtml_filenameZ	html_filerY   r   Zenddatar   dependenciesr    r    r!   rT     sJ    

*
rT   c                   @   s   e Zd ZdZdd ZdS )rP   zk
    Contains the data necessary to start up a compilation pipeline for
    a single compilation unit.
    c                 C   s   || _ || _|| _d S r:   )rU   r   r   )r4   rU   r   r   r    r    r!   r6   G  s    zCompilationSource.__init__N)r   r   r   __doc__r6   r    r    r    r!   rP   B  s   rP   c                   @   s<   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdd ZdS )r   af  
    Results from the Cython compiler:

    c_file           string or None   The generated C source file
    h_file           string or None   The generated C header file
    i_file           string or None   The generated .pxi file
    api_file         string or None   The generated C API .h file
    listing_file     string or None   File of error messages
    object_file      string or None   Result of compiling the C file
    extension_file   string or None   Result of linking the object file
    num_errors       integer          Number of compilation errors
    compilation_source CompilationSource
    Nr   c                 C   s   dd | j | j| j| jfD S )Nc                 S   s   g | ]}|r|qS r    r    )r_   Zsource_filer    r    r!   ra   g  s   z@CompilationResult.get_generated_source_files.<locals>.<listcomp>)r   h_filei_fileapi_filer<   r    r    r!   get_generated_source_filesf  s    z,CompilationResult.get_generated_source_files)r   r   r   r   r   r   r   r   r   Zobject_fileZextension_filer   r   r   r    r    r    r!   r   M  s   r   c                   @   s   e Zd ZdZdZdd ZdS )CompilationResultSetz
    Results from compiling multiple Pyrex source files. A mapping
    from source file paths to CompilationResult instances. Also
    has the following attributes:

    num_errors   integer   Total number of compilation errors
    r   c                 C   s   || |< |  j |j 7  _ d S r:   )r   )r4   rX   rZ   r    r    r!   addx  s    zCompilationResultSet.addN)r   r   r   r   r   r   r    r    r    r!   r   m  s   r   c              	   C   sT   ddl m} ddlm} t|}||}| ||||||jrFdnd|j	dS )Nr   r   )FingerprintFlagszc++c)
np_pythran)
r   r   Build.Cacher   r#   r9   Ztransitive_fingerprintr   r7   r   )r   rX   r/   r   r   r   r   r    r    r!   get_fingerprint}  s    
  r  c                 C   sJ   |dkrt |}|r8|p$t|| |}t| |||||S t| |||S dS )a  
    compile_single(source, options, full_module_name, cache, context, fingerprint)

    Compile the given Pyrex implementation file and return a CompilationResult.
    Always compiles a single file; does not perform timestamp checking or
    recursion.
    N)r#   r9   r  r   rT   )rX   r/   r   r   r   r   r    r    r!   compile_single  s    	
r  c                 C   s   t | dkr|jrtddd | D } t }t }|j}d}t }| D ]l}||krJt|||}	|dkrtt	
|}|||	}
|r|
rt|||j||d}||| d}|| qJ|r|  |S )z
    compile_multiple(sources, options, cache)

    Compiles the given sequence of Pyrex implementation files and returns
    a CompilationResultSet. Performs timestamp checking, caching and/or recursion
    if these are specified in the options.
    r   z>Full module name can only be set for single source compilationc                 S   s   g | ]}t j|qS r    )rQ   ro   r   )r_   rX   r    r    r!   ra     s     z$compile_multiple.<locals>.<listcomp>N)r   r   r   )r   rW   r   r+   r   
timestampsrQ   rR   r   r#   r9   r   r  r   Zcleanup_cache)sourcesr/   r   	processedresultsr  r   r   rX   r   Zout_of_daterZ   r    r    r!   compile_multiple  s,    
r  c                 K   s   t f d|i|}d}|jrd|js(tjr<|jrdtjd n(ddlm	} |jdkrVdn|j}||}t
| tr|jst| |||S | g} t| ||S )a  
    compile(source [, options], [, <option> = <value>]...)

    Compile one or more Pyrex implementation files, with optional timestamp
    checking and recursing on dependencies.  The source argument may be a string
    or a sequence of strings.  If it is a string and no recursion or timestamp
    checking is requested, a CompilationResult is returned, otherwise a
    CompilationResultSet is returned.
    defaultsNz/Cache is ignored when annotations are enabled.
r   )CacheT)r   r   r   r   r   r   r   r   r   r	  rO   strr  r  r  )rX   r/   r   kwdsr   r	  
cache_pathr    r    r!   compile  s    

r  r   Fc                 C   s0  |r(|s(|d }t |ts"td|j}|rT|rDtj|f|  } nt|f|  } |}|rd||7 }| D ]&}tj	||}	tj
|	rh|	  S qh|s,|d}
t|
dd }|
d }g }| D ]D}t||\}}|dk	r|r|| qt|||}	|	r|	  S q|D ] }t|||}	|	r
|	  S q
dS )a)  
    Search the list of include directories for the given file name.

    If a source file path or position is given, first searches the directory
    containing that file.  Returns None if not found, but does not report an error.

    The 'include' option will disable package dereferencing.
    r   r   r^   Nr   )rO   r	   r   r   rQ   ro   dirnamer   r   r   r   rh   r   r   r   search_module_in_dir)dirsrl   rd   ru   r   rf   Z	file_descZdotted_filenamer  ro   r   r   rW   Znamespace_dirspackage_diris_namespacer    r    r!   rj     sF    





rj   c                 C   s2   t | ||}|s.|r.t tj| |d|}|S )Nr6   )r   Zfind_versioned_filerQ   ro   r   )r  rW   rd   ro   r    r    r!   r    s    r  c                   C   s
   t ddS )Nr   )command_line)mainr    r    r    r!   setuptools_main1  s    r  c              
   C   s6  t jdd  }d}| rtzt|\}}W q tk
rp } z,tdt jd |jt jd t d W 5 d }~X Y qX nt	t
}|}|jrt  |jdkrt|j z<|jrddlm} || W d S t||}|jdkrd}W n@ ttfk
r  } zt jt|d  d}W 5 d }~X Y nX |r2t d d S )	Nr   r   z#{}: No such file or directory: '{}')filer   r   )generate_shared_module
)r   argvr   FileNotFoundErrorrz   r   r   r   exitr   r   Zshow_versionr   print_versionZworking_pathrQ   chdirZshared_c_file_pathZBuild.SharedModuler  r  r   r   r
   r   r
  )r  r   Zany_failuresr/   r  r   r  rZ   r    r    r!   r  5  s6    


r  )NNN)N)NN)r   NFN)r   )=rQ   r   r   ioversion_infor   r   r   r  r   r   ZStringEncodingr   ZScanningr   r	   r
   r   r   r   ZSymtabr   r   r   r   r   ZCmdLiner   ZLexiconr   r   r   r   r"   r   r{   r  ro   r   r   r  __file__r   r#   r   rS   r   r   rT   rP   r   dictr   r  r  r  Zcached_functionrj   r  r  r  r    r    r    r!   <module>   sb   
    4 

%
!:
