B
    d              !   @   s\  d dl mZ d dlZd dlZd dlZd dlZejdd dk s`dejdd   kr\dk rn n(ejde	ejdd   e
d yd d	lmZ W n ek
r   eZY nX 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 ejZe dZ!d Z"ej#$ej#%ej#&e'ej#j(dZ)G dd de*Z+G dd de*Z,dd Z-dd Z.d6ddZ/G dd de*Z0G d d! d!e*Z1G d"d# d#e*Z2G d$d% d%e3Z4d7d&d'Z5d(d) Z6d8d*d+Z ej7d9d-d.Z8d/d0 Z9d:d1d2Z:e3d d dd ddddd d d3dd d i i d,d,d4d4dd,d,dddddddd,d5Z;dS );    )absolute_importN   )r      )   r   )r   r   z8Sorry, Cython requires Python 2.6+ or 3.3+, found %d.%d
   )
basestring)Errors)EncodedString)PyrexScannerFileSourceDescriptor)
PyrexErrorCompileErrorerrorwarning)ModuleScope)Utils)Options)Versionz2[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$Includesc               @   s   e Zd ZdS )CompilationDataN)__name__
__module____qualname__ r   r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/Cython/Compiler/Main.pyr   ,   s   r   c               @   s   e Zd ZdZdZd5ddZdd Zd6ddZd	d
 Zdd Z	dd Z
d7ddZd8ddZdd Zdd Zd9d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/d0 Zd1d2 Zd3d4 ZdS ):ContextNFc             C   s|   ddl m}m} d|ji| _|| | _| j| jd< || _t | _	|| _
|| _|| _i | _i | _|d k	rr| | d | _d S )Nr   )BuiltinCythonScope__builtin__cython) r   r   Zbuiltin_scopemodulesZcreate_cython_scopecython_scopeinclude_directoriessetfuture_directivescompiler_directivescppoptionspxds	_internedset_language_levelZgdb_debug_outputwriter)selfr#   r&   r'   language_levelr(   r   r   r   r   r   __init__J   s    
zContext.__init__c             C   s   ddl m}m}m}m} t }|dkr,d}nt|}|dkrF|| |dkr^||||g || _	|| _
|dkr| jd | jd< d S )Nr   )print_functionunicode_literalsr   divisionZ3strr   r   builtins)Futurer/   r0   r   r1   r$   intaddupdater-   r%   r!   )r,   levelr/   r0   r   r1   r%   r   r   r   r+   b   s    
zContext.set_language_levelc             C   sJ   t ||f}y
| j| S  tk
r(   Y nX t |}|r<||_|| j|< |S )N)r	   r*   KeyErrorencoding)r,   valuer9   keyr   r   r   intern_ustringr   s    


zContext.intern_ustringc             G   s>   t ||f| }y
| j| S  tk
r.   Y nX || j|< |S )N)typer*   r8   )r,   r:   r;   r   r   r   intern_value~   s    

zContext.intern_valuec       	      C   st   ddl m} t|trV|jdkrVt||t }t|| j	}|
| |}|||}n|| ||}|||}|S )Nr   )Pipelinepyx)r    r?   
isinstancer   Z
_file_typeCompilationSourceosgetcwdcreate_default_resultobjr(   Zcreate_pyx_as_pxd_pipelinerun_pipelineZcreate_pxd_pipeline)	r,   source_descscopemodule_namer?   sourceZresult_sinkpipelineresultr   r   r   process_pxd   s    zContext.process_pxdc             C   s
   t |S )N)r   Zreport_error)r,   excr   r   r   nonfatal_error   s    zContext.nonfatal_errorr   Tc             C   s0  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| |	|}|r||}|s|rt d |r|}	| }x|		dD ]}
||
}qW |rt d|  |j
s,|rt d |s|rt d	 | j|	||d
}|r<t d|  |s|rd|_
| |	d|}|rp|drpnt|d|	dtj  |r,d|_
y|rt d|  |dtjtj|d  }||s|}t||}| |||	\}}|r||\}}||f| j|< W n tk
r*   Y nX |S )Nr   zPContext.find_module: module_name = %s, relative_to = %s, pos = %s, need_pxd = %sz'%s' is not a valid module namez...trying relative importz...trying absolute import.z...scope = %sz...pxd not loadedz...looking for pxd file)sys_pathz......found %sTz.pyz__init__.pyz'%s.pxd' not foundzContext.find_module: Parsing %sr   )printZqualify_namequalified_namemodule_name_patternmatchr   lookup_submodulefind_pxd_filefind_submodulesplitZpxd_file_loadedsearch_include_directoriesendswithr   replacerC   seppathsplitextr   rM   r)   )r,   rI   relative_toposZneed_pxdZabsolute_fallbackZdebug_find_modulerH   Zpxd_pathnamerS   nameZpackage_pathnamerel_pathrG   errrL   Zpxd_codenodesZ	pxd_scoper   r   r   find_module   s    	




zContext.find_modulec          	   C   s  | j |d||d}|d kr|ds,|dkrtjtjtjtjttjj	d}tj|d}| j
| z|  |d|}W d | j
  X |r|}|drt|d| d n6|d	krt|d
||f d n|dkrt|d| d |d kr
tjr
| ||S |S )Nz.pxd)rQ   python)stdlibstdiostlr   Z
Deprecatedz!'%s' is deprecated, use 'cpython'r   )rg   rh   z!'%s' is deprecated, use 'libc.%s'ri   z$'%s' is deprecated, use 'libcpp.*.*')rZ   
startswithrC   r^   abspathnormpathjoindirname__file__pardirr#   appendpopr   r   Zcimport_from_pyxfind_pyx_file)r,   rS   ra   rQ   pxdstandard_include_pathZdeprecated_include_pathrb   r   r   r   rW      s,    	
 
zContext.find_pxd_filec             C   s   |  |d|S )Nz.pyx)rZ   )r,   rS   ra   r   r   r   rs     s    zContext.find_pyx_filec             C   s(   | j |d|dd}|s$t|d|  |S )Nr    T)includez'%s' not found)rZ   r   )r,   filenamera   r^   r   r   r   find_include_file  s
    
zContext.find_include_filec             C   s2   | j }|r|tj }t|tg }t|||||S )N)r#   sysr^   tupleru   rZ   )r,   rS   suffixra   rv   rQ   include_dirsr   r   r   rZ      s    
z"Context.search_include_directoriesc             C   s
   t |S )N)r   find_root_package_dir)r,   	file_pathr   r   r   r}   *  s    zContext.find_root_package_dirc             C   s   t |t|S )N)r   check_package_dirrz   )r,   dirpackage_namesr   r   r   r   -  s    zContext.check_package_dirc       	      C   s   t j|sdS t|}t||r*dS |g}t|d}t j|rXt||rXdS xX| |D ]J\}}|dkr| ||}n|dkrd| 	||}nqd|rdt||rddS qdW dS )Nr   z.pxdcimportrv   r   )
rC   r^   existsr   Zmodification_timeZfile_newer_thanreplace_suffixread_dependency_filerW   rZ   )	r,   source_pathZoutput_pathZc_timera   Zpxd_pathkindrb   dep_pathr   r   r   c_file_out_of_date0  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   ).0r   rb   r   r   r   
<listcomp>F  s    z7Context.find_cimported_module_names.<locals>.<listcomp>)r   )r,   r   r   r   r   find_cimported_module_namesE  s    z#Context.find_cimported_module_namesc             C   s
   t |S )N)r   is_package_dir)r,   Zdir_pathr   r   r   r   I  s    zContext.is_package_dirc             C   sH   t |d}tj|r@t|d}dd | D }|  |S dS d S )Nz.depZrUc             S   s(   g | ] }d |  kr|  d dqS ) r   )striprY   )r   liner   r   r   r   P  s   z0Context.read_dependency_file.<locals>.<listcomp>r   )r   r   rC   r^   r   open	readlinesclose)r,   r   r   fchunksr   r   r   r   L  s    
zContext.read_dependency_filec             C   s   | j |d S )N)r!   get)r,   rb   r   r   r   rV   X  s    zContext.lookup_submodulec             C   s*   |  |}|s&t|d | d}|| j|< |S )N)parent_modulecontext)rV   r   r!   )r,   rb   rH   r   r   r   rX   \  s    


zContext.find_submodulec          
   C   s   t |tstd|j}| j|_tj}yt|t}ddl	m
} t|||j|| d}	||	||}
| jjryddlm} W n tk
r   tdY nX || W d Q R X W n. tk
r } z| ||W d d }~X Y nX tj|krt |
S )Nz$Only file sources for code supportedr   )Parsing)Zsource_encodingrH   r   r   )ConcreteSyntaxTreezLFormal grammar can only be used with compiled Cython with an available pgen.)rA   r   RuntimeErrorrw   r'   r   
num_errorsr   Zopen_source_filer    r   r
   r9   Zp_moduler(   formal_grammarParserr   ImportErrorUnicodeDecodeError_report_decode_errorr   )r,   rG   rH   rt   full_module_namesource_filenamer   r   r   streer   er   r   r   parsee  s.    



zContext.parsec          	   C   s   |j d }|j d }|j d }d}d }}tj|jddddH}	x@t|	dD ]2\}}
|t|
7 }||krL||t|
  d }P qLW W d Q R X t|||fd	||f S )
Nr   r   r   rz	iso8859-1r    )r9   newlinezqDecoding error, missing or incorrect coding=<encoding-name> at top of source (cannot decode with encoding %r: %s))argsior   rw   	enumeratelenr   )r,   rG   rN   msgpositionr9   r   columnidxr   datar   r   r   r     s    



zContext._report_decode_errorc       
      C   s|   t j|\}}t j|\}}d|kr,|S |g}x6| |rht j|\}}	||krXP ||	 |}q4W |  d|S )NrP   )rC   r^   rY   r_   r   rq   reverserm   )
r,   r^   r(   r   rw   rI   _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)r^   Zecho_to_stderr)	r   resetuse_listing_filer   r   main_source_filelisting_fileZopen_listing_fileerrors_to_stderr)r,   r(   rL   r^   r   r   r   setup_errors  s    zContext.setup_errorsc             C   s|   |j j}t|tstdt  tj|_|jdkr8d}|rx|jrxyt	
|jt|j W n tk
rp   Y nX d |_d S )Nz$Only file sources for code supportedr   T)compilation_sourcerG   rA   r   r   r   Zclose_listing_filer   c_filer   Zcastrate_filerC   statrw   EnvironmentError)r,   rd   r(   rL   rG   r   r   r   teardown_errors  s    


zContext.teardown_errors)FNN)N)NNr   T)T)FF)r   r   r   r"   r-   r.   r+   r<   r>   rM   rO   re   rW   rs   rx   rZ   r}   r   r   r   r   r   rV   rX   r   r   r   r   r   r   r   r   r   r   ;   s8    

 
X
!

			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)	cplusr   r   output_filerC   r^   rm   isdirbasename)r   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 )N)	CompilationResultrG   rw   r   r   r   r   r   embedded_metadata)r   r(   rL   rG   r   r   r   rE     s    
rE   c          	   C   s  ddl m} tj| d }||dd   |d kr>| }t }tj| }|p`|	| |}t
| |jr|dtj| }||s| }n|}t||}	t|	||} t| |}
|jd krtj|
jd d }tj|rtj|ddd}d	|d
krd|_W d Q R X | dks.|s>||||
}n||||
}|||
 ||| \}}||||
 |d kr|jrddl m!} ||"|
j#}t
$|
j|
j#| |
S )Nr   )r?   rP   r   z.htmlr   zUTF-8)r9   z<!-- Generated by Cythond   Tz.pyr   )create_dependency_tree)%r    r?   rC   r^   r_   configure_language_defaultscreate_contextrD   rk   r   r   Z$raise_error_if_module_name_forbidden'relative_path_in_code_position_commentsr\   r]   r[   r   rB   rE   annotater   r   r   r   readlowerZcreate_py_pipelineZcreate_pyx_pipeliner   rF   r   depfileZBuild.Dependenciesr   Zall_dependenciesr   Zwrite_depfile)rJ   r(   r   r   r?   Z
source_extr   abs_pathrc   rG   rL   Zhtml_filenameZ	html_filerK   rd   Zenddatar   dependenciesr   r   r   rF     sD    



rF   c               @   s   e Zd ZdZdd ZdS )rB   zk
    Contains the data necessary to start up a compilation pipeline for
    a single compilation unit.
    c             C   s   || _ || _|| _d S )N)rG   r   r   )r,   rG   r   r   r   r   r   r.     s    zCompilationSource.__init__N)r   r   r   __doc__r.   r   r   r   r   rB     s   rB   c               @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )CompilationOptionsz
    See default_options at the end of this module for a list of all possible
    options and CmdLine.usage and CmdLine.parse_command_line() for their
    meaning.
    Nc       
      K   s  g | _ |rt|tr |j}nt}t|}|| t|tt }|dg |r|dt	|dkrddndd
|f }t|t }t|d }t|t| }|rdt	|dkrdndd
|f }t|||d< |d	d
r|d sdd l}	|	d d|d< d|kr*d|kr*|d |d< n|dsD|d|d< d|krdd|krd|d |d< |d dkrtj
t d|d< | j| d S )Ninclude_pathz3got unknown compilation option%s, please remove: %sr   r   r    z, r&   z$got unknown compiler directive%s: %s
np_pythranFr   r   z%C++ mode forced when in Pythran mode!Tr-   r   cachecompiler)r   rA   r   __dict__default_optionsdictr6   r$   difference_updater   rm   
ValueErrorr   Zget_directive_defaultsr   warningswarnrC   r^   r   Zget_cython_cache_dir)
r,   defaultskwr(   Zunknown_optionsmessageZdirective_defaultsZ
directivesZunknown_directivesr   r   r   r   r.   #  sH    


zCompilationOptions.__init__c             C   s&   |dkr"| j dd kr"d| j d< d S )NpyZbindingT)r&   r   )r,   Zsource_extensionr   r   r   r   R  s    z.CompilationOptions.configure_language_defaultsc             C   s   t | j| j| j| j| dS )N)r(   )r   r   r&   r   r-   )r,   r   r   r   r   W  s    
z!CompilationOptions.create_contextc                s&  i }x
| j  D ]\}}|dkr&qq|dkr2qq|dkr>qq|dkrJqq|dkrVqq|dkrbqq|dkrnqq|dkrzqq|d	krqq|d
kr|||< q|dkr|||< q|dkr|||< q|dkr|||< q|dgkr| jrtdq|dgkr| jrtdq|||< qW  fdd  |S )zi
        Return a string that contains all the options that are relevant for cache invalidation.
        )show_versionr   verbosequiet)r   
output_dir)
timestamps)r   )r&   )r   )working_path)create_extension)	build_dir)r   generate_pxir   annotate_coverage_xml)r   evaluate_tree_assertions)r   emit_linenumsc_line_in_traceback	gdb_debugr   )r   r-   compile_time_envr   capi_reexport_cincludesz=capi_reexport_cincludes is not compatible with Cython cachingcommon_utility_include_dirzDcommon_utility_include_dir is not compatible with Cython caching yetc                s,   t | tr$t fdd|  D } t| S )z
            Recursively turn item into a string, turning dicts into lists with
            deterministic ordering.
            c                s    g | ]\}}t | |fqS r   )repr)r   r;   r:   )to_fingerprintr   r   r     s    zNCompilationOptions.get_fingerprint.<locals>.to_fingerprint.<locals>.<listcomp>)rA   r   sorteditemsr   )item)r   r   r   r     s    
z:CompilationOptions.get_fingerprint.<locals>.to_fingerprint)r   r   r   NotImplementedErrorr   )r,   r   r;   r:   r   )r   r   get_fingerprint[  sJ    






	z"CompilationOptions.get_fingerprint)N)r   r   r   r   r.   r   r   r   r   r   r   r   r     s
   
/r   c               @   s   e 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
    c             C   s4   d | _ d | _d | _d | _d | _d | _d | _d | _d S )N)r   Zh_fileZi_fileZapi_filer   Zobject_fileZextension_filer   )r,   r   r   r   r.     s    zCompilationResult.__init__N)r   r   r   r   r.   r   r   r   r   r     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 )N)r   )r,   rJ   rL   r   r   r   r5     s    zCompilationResultSet.addN)r   r   r   r   r   r5   r   r   r   r   r    s   r  c             C   s   t | ||S )z
    compile_single(source, options, full_module_name)

    Compile the given Pyrex implementation file and return a CompilationResult.
    Always compiles a single file; does not perform timestamp checking or
    recursion.
    )rF   )rJ   r(   r   r   r   r   compile_single  s    r  c             C   s   |j rt| dkrtddd | D } t }t }|j}|j}d}t }x| D ]|}||krR|dkrn|	 }t
|||}	|||	}
|r|
r|rtjd|  t|||j |d}||| d}|| qRW |S )z
    compile_multiple(sources, options)

    Compiles the given sequence of Pyrex implementation files and returns
    a CompilationResultSet. Performs timestamp checking 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   )rC   r^   rk   )r   rJ   r   r   r   r     s    z$compile_multiple.<locals>.<listcomp>NzCompiling %s
)r   r   )rI   r   r   r$   r  r   r   rC   rD   r   r   r   ry   stderrwriterF   r5   )sourcesr(   	processedresultsr   r   r   r   rJ   Zoutput_filenameZout_of_daterL   r   r   r   compile_multiple  s2    
r  c             K   s<   t f d|i|}t| tr.|js.t| ||S t| |S d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.
    r   N)r   rA   r   r   r  r  )rJ   r(   r   kwdsr   r   r   compile  s    
r
  Fc             C   s  |rJ|d }t |tstd|r8tj|jf|  } nt|jf|  } |}|rZ||7 }|s|	d}t
|dd }|d }	|	| }
d| }x| D ]x}tj||}tj|r|S |st||}|dk	rtj||
}tj|r|S tj||	|}tj|r|S qW dS )a$  
    Search the list of include directories for the given file name.

    If a source file 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   z$Only file sources for code supportedrP   Nr   r.   )rA   r   r   rC   r^   rn   rw   r   r}   rY   rz   rm   r   r   )dirsrS   r{   ra   rv   Z	file_descZdotted_filenamer   r   rI   Zmodule_filenameZpackage_filenamern   r^   package_dirr   r   r   rZ     s>    



rZ   c               C   s
   t ddS )Nr   )command_line)mainr   r   r   r   setuptools_mainO  s    r  c          
   C   s   t jdd  }d}| r0ddlm} ||\}}ntt}|}|jrRt jdt	  |j
dkrht|j
 yt||}|jdkrd}W n> ttfk
r } zt jt|d  d}W d d }~X Y nX |rt d d S )Nr   r   )parse_command_linezCython version %s
r    
)ry   argvZCmdLiner  r   r   r   r  r  versionr   rC   chdirr
  r   r   r   strexit)r  r   Zany_failuresr  r(   r  rL   r   r   r   r   r  S  s(    


r  r    T)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r   r   r   r   r   r-   r   r   r   r   r   r   r   r   rI   r   )NN)N)NN)F)r   )<
__future__r   rC   rery   r   version_infor  r  rz   r  r   r   r   r  r    r   ZStringEncodingr	   ZScanningr
   r   r   r   r   r   ZSymtabr   r   r   r   r  r
  rT   r   r^   rk   rm   rn   ro   rp   ru   objectr   r   r   rE   rF   rB   r   r   r   r  r  r  Zcached_functionrZ   r  r  r   r   r   r   r   <module>   s   4


   
9 
(
7
