B
    \                 @   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"G dd de#Z$G dd de#Z%dd Z&dd Z'd3ddZ(G dd de#Z)G dd  d e#Z*G d!d" d"e#Z+G d#d$ d$e,Z-d4d%d&Z.d'd( Z/d5d)d*Z d+d, Z0d6d-d.Z1e,d d dd dddd d d/dd d i i d0d0d1d1dd0d0ddddddd0d2Z2dS )7    )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_]*)*$c               @   s   e Zd ZdS )CompilationDataN)__name__
__module____qualname__ r   r   3lib/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 | _tjtjtjtjttjjd}||g | _|d k	r| | d | _d S )Nr   )BuiltinCythonScope__builtin__ZcythonIncludes) r   r   Zbuiltin_scopemodulesZcreate_cython_scopecython_scopeinclude_directoriessetfuture_directivescompiler_directivescppoptionspxds	_internedospathabspathnormpathjoindirname__file__pardirset_language_levelZgdb_debug_outputwriter)	selfr"   r%   r&   language_levelr'   r   r   standard_include_pathr   r   r   __init__H   s"     
zContext.__init__c             C   s   ddl m}m}m}m} g }|dkr@|||g}| j| d}nt|}|dkr\||||g}|| _|rr| j	| |dkr| j
d | j
d< d S )Nr   )print_functionunicode_literalsr   divisionZ3strr   r   builtins)ZFuturer7   r8   r   r9   r$   discardintr4   updater    )r3   levelr7   r8   r   r9   r$   r   r   r   r2   d   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)r3   valuer@   keyr   r   r   intern_ustringu   s    


zContext.intern_ustringc             G   s>   t ||f| }y
| j| S  tk
r.   Y nX || j|< |S )N)typer)   r?   )r3   rA   rB   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   )PipelineZpyx)r   rF   
isinstancer   Z
_file_typeCompilationSourcer*   getcwdcreate_default_resultobjr'   Zcreate_pyx_as_pxd_pipelinerun_pipelineZcreate_pxd_pipeline)	r3   source_descscopemodule_namerF   sourceZresult_sinkpipelineresultr   r   r   process_pxd   s    zContext.process_pxdc             C   s
   t |S )N)r   Zreport_error)r3   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   replacer*   sepr+   splitextr   rR   r(   )r3   rN   Zrelative_toposZneed_pxdZabsolute_fallbackZdebug_find_modulerM   Zpxd_pathnamerX   nameZpackage_pathnamerel_pathrL   errrQ   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)rV   Zpython)stdlibstdiostlr   Z
Deprecatedz!'%s' is deprecated, use 'cpython'r   )ri   rj   z!'%s' is deprecated, use 'libc.%s'rk   z$'%s' is deprecated, use 'libcpp.*.*')r_   
startswithr*   r+   r,   r-   r.   r/   r0   r1   r"   appendpopr   r   Zcimport_from_pyxfind_pyx_file)r3   rX   rd   rV   pxdr5   Zdeprecated_include_pathre   r   r   r   r\      s,    	
 
zContext.find_pxd_filec             C   s   |  |d|S )Nz.pyx)r_   )r3   rX   rd   r   r   r   ro     s    zContext.find_pyx_filec             C   s(   | j |d|dd}|s$t|d|  |S )Nr   T)includez'%s' not found)r_   r   )r3   filenamerd   r+   r   r   r   find_include_file  s
    
zContext.find_include_filec             C   s   t t| j|||||S )N)r   r_   tupler"   )r3   rX   suffixrd   rq   rV   r   r   r   r_   #  s    z"Context.search_include_directoriesc             C   s
   t |S )N)r   find_root_package_dir)r3   Z	file_pathr   r   r   rv   (  s    zContext.find_root_package_dirc             C   s   t |t|S )N)r   check_package_dirrt   )r3   dirZpackage_namesr   r   r   rw   +  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cimportrq   r   )
r*   r+   existsr   Zmodification_timeZfile_newer_thanreplace_suffixread_dependency_filer\   r_   )	r3   source_pathZoutput_pathZc_timerd   Zpxd_pathkindre   dep_pathr   r   r   c_file_out_of_date.  s$    
zContext.c_file_out_of_datec             C   s   dd |  |D S )Nc             S   s   g | ]\}}|d kr|qS )ry   r   ).0r~   re   r   r   r   
<listcomp>D  s    z7Context.find_cimported_module_names.<locals>.<listcomp>)r|   )r3   r}   r   r   r   find_cimported_module_namesC  s    z#Context.find_cimported_module_namesc             C   s
   t |S )N)r   is_package_dir)r3   Zdir_pathr   r   r   r   G  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   )stripr^   )r   liner   r   r   r   N  s   z0Context.read_dependency_file.<locals>.<listcomp>r   )r   r{   r*   r+   rz   open	readlinesclose)r3   r}   r   fZchunksr   r   r   r|   J  s    
zContext.read_dependency_filec             C   s   | j |d S )N)r    get)r3   re   r   r   r   r[   V  s    zContext.lookup_submodulec             C   s*   |  |}|s&t|d | d}|| j|< |S )N)parent_modulecontext)r[   r   r    )r3   re   rM   r   r   r   r]   Z  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_encodingrM   r   r   )ConcreteSyntaxTreezLFormal grammar can only be used with compiled Cython with an available pgen.)rG   r   RuntimeErrorrr   r&   r   
num_errorsr   Zopen_source_filer   r   r
   r@   Zp_moduler'   formal_grammarZParserr   ImportErrorUnicodeDecodeError_report_decode_errorr   )r3   rL   rM   rp   full_module_namesource_filenamer   r   r   sZtreer   er   r   r   parsec  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   )r@   newlinezqDecoding error, missing or incorrect coding=<encoding-name> at top of source (cannot decode with encoding %r: %s))argsior   rr   	enumeratelenr   )r3   rL   rS   msgZpositionr@   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 )NrU   )r*   r+   r^   rc   r   rm   reverser.   )
r3   r+   r'   rx   rr   rN   _namesparentZ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)r3   r'   rQ   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_sourcerL   rG   r   r   r   Zclose_listing_filer   c_filer   Zcastrate_filer*   statrr   EnvironmentError)r3   rg   r'   rQ   rL   r   r   r   teardown_errors  s    


zContext.teardown_errors)FNN)N)NNr   T)T)FF)r   r   r   r!   r4   r6   r2   rC   rE   rR   rT   rh   r\   ro   rs   r_   rv   rw   r   r   r   r|   r[   r]   r   r   r   r   r   r   r   r   r   r   9   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_filer*   r+   r.   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)	CompilationResultrL   rr   r   r   r   r   r   embedded_metadata)r   r'   rQ   rL   r   r   r   rJ     s    
rJ   c          	   C   sz  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||||
}|||
 ||| \}}||||
 |
S )Nr   )rF   rU   r   z.htmlr   zUTF-8)r@   z<!-- Generated by Cythond   Tz.py)r   rF   r*   r+   rc   configure_language_defaultscreate_contextrI   r,   r   r   Z$raise_error_if_module_name_forbidden'relative_path_in_code_position_commentsra   rb   r`   r   rH   rJ   annotater   rz   r   r   readlowerZcreate_py_pipelineZcreate_pyx_pipeliner   rK   r   )rO   r'   r   r   rF   Z
source_extr   Zabs_pathrf   rL   rQ   Zhtml_filenameZ	html_filerP   rg   Zenddatar   r   r   rK     s<    



rK   c               @   s   e Zd ZdZdd ZdS )rH   zk
    Contains the data necessary to start up a compilation pipeline for
    a single compilation unit.
    c             C   s   || _ || _|| _d S )N)rL   r   r   )r3   rL   r   r   r   r   r   r6     s    zCompilationSource.__init__N)r   r   r   __doc__r6   r   r   r   r   rH     s   rH   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!Tr4   r   cacheZcompiler)r   rG   r   __dict__default_optionsdictr=   r#   difference_updater   r.   
ValueErrorr   Zget_directive_defaultsr   warningswarnr*   r+   r   Zget_cython_cache_dir)
r3   defaultskwr'   Zunknown_optionsmessageZdirective_defaultsZ
directivesZunknown_directivesr   r   r   r   r6     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   )r3   Zsource_extensionr   r   r   r   L  s    z.CompilationOptions.configure_language_defaultsc             C   s   t | j| j| j| j| dS )N)r'   )r   r   r%   r   r4   )r3   r   r   r   r   Q  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   r4   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   rB   rA   )to_fingerprintr   r   r     s    zNCompilationOptions.get_fingerprint.<locals>.to_fingerprint.<locals>.<listcomp>)rG   r   sorteditemsr   )item)r   r   r   r     s    
z:CompilationOptions.get_fingerprint.<locals>.to_fingerprint)r   r   r   NotImplementedErrorr   )r3   r   rB   rA   r   )r   r   get_fingerprintU  sJ    






	z"CompilationOptions.get_fingerprint)N)r   r   r   r   r6   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   )r3   r   r   r   r6     s    zCompilationResult.__init__N)r   r   r   r   r6   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   )r3   rO   rQ   r   r   r   add  s    zCompilationResultSet.addN)r   r   r   r   r   r   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.
    )rK   )rO   r'   r   r   r   r   compile_single  s    r   c             C   s   dd | D } t  }t }|j}|j}d}t }x| D ]x}||kr8|dkrT| }t|||}	|||	}
|rt|
r|rt	j
d|  t|||d}||| d}|| q8W |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.
    c             S   s   g | ]}t j|qS r   )r*   r+   r,   )r   rO   r   r   r   r     s    z$compile_multiple.<locals>.<listcomp>NzCompiling %s
)r   )r#   r   r   r   r*   rI   r   r   r   sysstderrwriterK   r   )sourcesr'   Z	processedZresultsr   r   r   r   rO   Zoutput_filenameZout_of_daterQ   r   r   r   compile_multiple  s*    

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   rG   r   r   r   r   )rO   r'   r   kwdsr   r   r   compile  s    
r   c               C   s
   t ddS )Nr   )command_line)mainr   r   r   r   setuptools_main  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   
)r   argvZCmdLiner   r   r   r   r   r   versionr   r*   chdirr   r   r   r   strexit)r   r   Zany_failuresr   r'   r   rQ   r   r   r   r   r     s(    


r   r   FT)r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r   r   r   r   r   r4   r   r   r   r   r   r   r   r   r   )NN)N)NN)r   )3Z
__future__r   r*   rer   r   version_infor   r   rt   r  r   r   r   r   r   r   ZStringEncodingr	   ZScanningr
   r   r   r   r   r   ZSymtabr   r   r   r   r   r   rY   r   objectr   r   r   rJ   rK   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   4


   
5 
$

