a
    b                     @   sV  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 zd d	lmZ W n ey   eZY n0 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 d3dd d i i d,d,d4d4d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   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/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 urr| | d | _d S )Nr   )BuiltinCythonScope__builtin__Z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.   r/   r   r0   r#   intaddupdater,   r$   r    )r+   levelr.   r/   r   r0   r$   r   r   r   r*   b   s    
zContext.set_language_levelc                 C   sJ   t ||f}z| j| W S  ty(   Y n0 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| }z| j| W S  ty.   Y n0 || j|< |S r7   )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   PipelineZ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 r7   )r   Zreport_error)r+   excr   r   r   nonfatal_error   s    zContext.nonfatal_errorr   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| |	|}|r||}|s|rt d |r|}	| }|		dD ]}
||
}q|rt d|  |j
s&|rt d |s|rt d	 | j|	||d
}|r8t d|  |s|rd|_
| |	d|}|rl|drlnt|d|	dtj  |r&d|_
z|rt d|  |dtjtj|d  }||s|}t||}| |||	\}}|r||\}}||f| j|< W n ty$   Y n0 |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 %sT.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_pathnamerU   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 u r|ds,|dv rtjtjtjtjttjj	d}tj|d}| j
| z|  |d|}W | j
  n| j
  0 |r|}|drt|d| d n6|d	v rt|d
||f d n|dv rt|d| d |d u rtjr| ||S |S )N.pxdrQ   python)stdlibstdiostlr   Z
Deprecatedz!'%s' is deprecated, use 'cpython'r   )rj   rk   z!'%s' is deprecated, use 'libc.%s'rl   z$'%s' is deprecated, use 'libcpp.*.*')r\   
startswithrC   r`   abspathnormpathjoindirname__file__pardirr"   appendpopr   r   Zcimport_from_pyxfind_pyx_file)r+   rU   rc   rR   pxdstandard_include_pathZdeprecated_include_pathrd   r   r   r   rY      s0    	

zContext.find_pxd_filec                 C   s   |  |d|S )Nz.pyx)r\   )r+   rU   rc   r   r   r   rv     s    zContext.find_pyx_filec                 C   s(   | j |d|dd}|s$t|d|  |S )Nr   T)includez'%s' not found)r\   r   )r+   filenamerc   r`   r   r   r   find_include_file  s    
zContext.find_include_filec                 C   s2   | j }|r|tj }t|tg }t|||||S r7   )r"   sysr`   tuplerx   r\   )r+   rU   suffixrc   ry   rR   include_dirsr   r   r   r\      s    
z"Context.search_include_directoriesc                 C   s
   t |S r7   )r   find_root_package_dir)r+   	file_pathr   r   r   r   *  s    zContext.find_root_package_dirc                 C   s   t |t|S r7   )r   check_package_dirr}   )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 | |D ]L\}}|dkr| ||}n|dkrb| 	||}nqb|rbt||rb dS qbdS )Nr   rh   cimportry   r   )
rC   r`   existsr   Zmodification_timeZfile_newer_thanreplace_suffixread_dependency_filerY   r\   )	r+   source_pathZoutput_pathZc_timerc   Zpxd_pathkindrd   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   rd   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 r7   )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 |  v r|  d dqS ) r   )stripr[   )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 r7   )r    get)r+   rd   r   r   r   rX   X  s    zContext.lookup_submodulec                 C   s*   |  |}|s&t|d | d}|| j|< |S )N)parent_modulecontext)rX   r   r    )r+   rd   rH   r   r   r   rZ   \  s    

zContext.find_submodulec              
   C   s  t |tstd|j}| j|_tj}zt||}ddl	m
} t|||j|| d}	||	||}
| jjrzddlm} W n ty   tdY n0 || W d    n1 s0    Y  W n0 ty } z| ||W Y d }~n
d }~0 0 tj|krt |
S )N$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   RuntimeErrorrz   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   rw   full_module_namesource_filenamer   r   r   streer   er   r   r   parsee  s2    


,"zContext.parsec                 C   s   |j d }|j d }|j d }d}d }}tj|jddddP}	t|	dD ]4\}}
|t|
7 }||krJ||t|
  d } qqJW d    n1 s0    Y  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   rz   	enumeratelenr   )r+   rG   rN   msgpositionr9   r   columnidxr   datar   r   r   r     s     


$
zContext._report_decode_errorc           
      C   sx   t j|\}}t j|\}}d|v r,|S |g}| |rft j|\}}	||krVqf||	 |}q2|  d|S )NrP   )rC   r`   r[   ra   r   rt   reverserp   )
r+   r`   r'   r   rz   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   sz   |j j}t|tstdt  tj|_|jdkr8d}|rv|jrvzt	
|jt|j W n tyn   Y n0 d |_d S )Nr   r   T)compilation_sourcerG   rA   r   r   r   Zclose_listing_filer   c_filer   Zcastrate_filerC   statrz   EnvironmentError)r+   rf   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   rg   rY   rv   r{   r\   r   r   r   r   r   r   rX   rZ   r   r   r   r   r   r   r   r   r   r   ;   s>     

  
Y
!
 

		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`   rp   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 r7   )	CompilationResultrG   rz   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 u r>| }t }tj| }|p`|	| |}t
| |jr|dtj| }||s| }n|}t||}	t|	||} t| |}
|jd u r0tj|
jd d }tj|r0tj|ddd&}d	|d
v rd|_W d    n1 s&0    Y  | dksD|sT||||
}n||||
}|||
 ||| \}}||||
 |
S )Nr   r?   rP   r   z.htmlr   zUTF-8)r9   z<!-- Generated by Cythond   TrS   )r   r@   rC   r`   ra   configure_language_defaultscreate_contextrD   rn   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   )rJ   r'   r   r   r@   Z
source_extr   abs_pathre   rG   rL   Zhtml_filenameZ	html_filerK   rf   Zenddatar   r   r   rF     s<    



&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 r7   )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|v r*d|vr*|d |d< n|dsD|d|d< d|v rdd|vrd|d |d< |d du 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dictr5   r#   difference_updater   rp   
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-     sL    


zCompilationOptions.__init__c                 C   s&   |dkr"| j dd u r"d| j d< d S )NpyZbindingT)r%   r   )r+   Zsource_extensionr   r   r   r   N  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   S  s    

z!CompilationOptions.create_contextc                    s  i }| j  D ]\}}|dv r"qq|dv r.qq|dv r:qq|dv rFqq|dv rRqq|dv r^qq|dv rjqq|dv rvqq|d	v rqq|d
v r|||< q|dv r|||< q|dv r|||< q|dv r|||< q|dgkr| jrtdq|dgkr| jrtdq|||< q 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         zNCompilationOptions.get_fingerprint.<locals>.to_fingerprint.<locals>.<listcomp>)rA   r   sorteditemsr   )itemr   r   r   r     s    
z:CompilationOptions.get_fingerprint.<locals>.to_fingerprint)r   r  r   NotImplementedErrorr   )r+   r   r;   r:   r   r   r   get_fingerprintW  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 r7   )r   Zh_fileZi_fileZapi_filer   Zobject_fileZextension_filer   r   r   r   r   r-     s    zCompilationResult.__init__Nr   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 r7   )r   )r+   rJ   rL   r   r   r   r4     s    zCompilationResultSet.addN)r   r   r   r   r   r4   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   dd | D } t  }t }|j}|j}d}t }| D ]x}||vr6|du rR| }t|||}	|||	}
|rr|
r|rt	j
d|  t|||d}||| d}|| q6|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   )rC   r`   rn   )r   rJ   r   r   r   r     r   z$compile_multiple.<locals>.<listcomp>NzCompiling %s
)r   )r#   r  r   r   rC   rD   r   r   r   r|   stderrwriterF   r4   )sourcesr'   	processedresultsr   r   r   r   rJ   Zoutput_filenameZout_of_daterL   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   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| }| D ]}tj||}tj|r|  S |st||}|durtj||
}tj|r|  S tj||	|}tj|r|  S qd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   r   rP   Nr   r-   )rA   r   r   rC   r`   rq   rz   r   r   r[   r}   rp   r   r   )dirsrU   r~   rc   ry   Z	file_descZdotted_filenamer   r   rI   Zmodule_filenameZpackage_filenamerq   r`   package_dirr   r   r   r\     s@    



r\   c                   C   s
   t ddS )Nr   )command_line)mainr   r   r   r   setuptools_mainG  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
 zt||}|jdkrd}W n@ ttfy } z$t jt|d  d}W Y d }~n
d }~0 0 |rt d d S )Nr   r   )parse_command_linezCython version %s
r   
)r|   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  K  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   )NN)N)NN)F)r   )<
__future__r   rC   rer|   r   version_infor  r	  r}   r  r   r   r   r  r   r   ZStringEncodingr	   ZScanningr
   r   r   r   r   r   ZSymtabr   r   r   r   r  r  rV   r   r`   rn   rp   rq   rr   rs   rx   objectr   r   r   rE   rF   rB   r   r   r   r  r  r  Zcached_functionr\   r  r  r   r   r   r   r   <module>   s   4


   
5 
$
7
