a
    b4                  	   @   s&  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlZddlmZ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mZmZ ddlmZ ddlm Z  d dl!Z"ej#dkZ$e$sdd Z%ndd Z%ej#dk rd dl&Z&dd Z'nd dl(m)Z* dd Z'G dd deeZ+ed:ddZ,d;ddZ-d<ddZ.d d! Z/ed"d# Z0i Z1e0d$Z2d=d%d&Z3d'd( Z4e-ej56e  d)ddd*d*dddf	d+d,Z7de7_8e9d-j:Z;d.d/ Z<e9d0Z=d1d2 Z>zd d3lm?Z? W n e@y   d4d5 Z?Y n0 d6d7 ZAG d8d9 d9eBZCdS )>    )absolute_importN)Distribution	Extension)	build_ext   )Contextdefault_options)CythonTransformEnvTransform)SkipDeclarations)parse_from_strings)_unicode   )strip_string_literals	cythonizecached_function)Pipeline)get_cython_cache_dir)   c                 C   s   t | tr| dS | S d S )Nascii)
isinstancebytesdecode)s r   c/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Build/Inline.py
to_unicode   s    

r   c                 C   s   | S Nr   xr   r   r   <lambda>#       r    )r      c                 C   s   t | |S r   )impload_dynamic)namemodule_pathr   r   r   r$   '   s    r$   c                 C   s&   t | |}t |}|j| |S r   )_importlib_utilspec_from_file_locationmodule_from_specloaderexec_module)r%   r&   specmoduler   r   r   r$   +   s    
c                       s,   e Zd Zdd Zdd Z fddZ  ZS )UnboundSymbolsc                 C   s   t | d  t | _d S r   )r	   __init__setunbound)selfr   r   r   r/   3   s    zUnboundSymbols.__init__c                 C   s"   |   |js| j|j |S r   )Zcurrent_envlookupr%   r1   addr2   noder   r   r   visit_NameNode6   s    zUnboundSymbols.visit_NameNodec                    s   t t| | | jS r   )superr.   __call__r1   r5   	__class__r   r   r9   :   s    zUnboundSymbols.__call__)__name__
__module____qualname__r/   r7   r9   __classcell__r   r   r:   r   r.   2   s   r.   c                 C   s   t | } |d u rtg t}ddlm} td| }t|dD ]$}|d u rJq<||}t||r< qbq<zdd l	}W n t
y   dd l}Y n0 tt |tt| S )Nr   )AnalyseDeclarationsTransformz(tree fragment)Zpyxr   )r   r   r   Compiler.ParseTreeTransformsr@   r   r   Zcreate_pipeliner   builtinsImportError__builtin__tupler.   r0   dir)codecontextr@   treephaserB   r   r   r   unbound_symbols?   s     


rK   c                 C   s"   t | }|tu rdS t| |S d S )Nlong)typeint	safe_type)argrH   py_typer   r   r   unsafe_typeS   s    rR   c                 C   s   t | }|ttttfv r|jS |tu r*dS |tu r6dS |tu rBdS dt	j
v rpt| t	j
d jrpd| jj| jf S |jD ]N}|jdv r dS |j|jdd	}|rv||j}|jrvd
|j|jf   S qvdS d S )Nzdouble complexdoubleZbintnumpyz"numpy.ndarray[numpy.%s_t, ndim=%s])rD   rB   objectF)Zneed_pxdz%s.%s)rM   listrE   dictstrr<   complexfloatboolsysmodulesr   ZndarrayZdtyper%   ndim__mro__r=   find_moduler3   Zis_type)rP   rH   rQ   Z	base_typer-   entryr   r   r   rO   [   s(    

rO   c                  C   s,   t  } |  }| | t| }|  |S r   )r   find_config_filesparse_config_filesr   finalize_options)distconfig_filesbuild_extensionr   r   r   _get_build_extensions   s    
rh   c                 C   s   t t| tS r   )r   rV   r   )cython_include_dirsr   r   r   _create_context~   s    rj   ).c                 C   s   |D ]~}|| vr|d u s |d u rJt  jjj}|d u r<|j}|d u rJ|j}||v r`|| | |< q||v rv|| | |< qtd|  qd S )NzCouldn't find %r)inspectcurrentframef_backf_locals	f_globalsprint)kwdsrK   localsglobalssymbolZcalling_framer   r   r   _populate_unbound   s    rv   c                 C   s.   | |t jt j|tjf}tt|d	 S )Nzutf-8)
r\   version_info
executableCython__version__hashlibsha1r   encode	hexdigest)	orig_codearg_sigslanguage_levelkeyr   r   r   _inline_key   s    r   inlineFc
           +         s  d u rdd |r t t|nt |r0t|ni }|	d u rHd|vrHd}	|	d urX|	|d< t| }|d urt||| t }t fdd|D }t	| ||	}t| ||f}|d urdd |D }|| S | }t
| } t| \} }t| } |d u rt jjj}|d u r&t jjj}z"t|  t|< }t||| W n" tyj   |sftd Y n0 g }t D ](\}}|tu r||d	|  |= q|t}t fd
d|D }t	|||	}d| }|tjv rtj| }nd }tjd u rt }|dt_tj ||tj }tj!|sDt"| |sXtj#|sg }g }t$%d}|D ]X\}}|&|} | rn|d| ' d   | ' d dkrndd l(}!||!)  qnt*| \}"}#d dd |D }$dd ||"|$|#d }%| D ]\}&}'|%+|&|'}%qtj ||d }(t,|(d})z|)-|% W |).  n
|).  0 t/||(g||d}*|d u rt }t0|*g|pdg||d|_1tj2|(|_3||_4|5  t6||}|j7t|||f< fdd|D }|j7| S )Nc                 S   s   dS )NrU   r   r   r   r   r   r       r!   zcython_inline.<locals>.<lambda>r   Z3strc                    s   g | ]\}}| |fqS r   r   ).0rP   value)ctxget_typer   r   
<listcomp>   r!   z!cython_inline.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   r   rP   r   r   r   r      r!   z>Could not parse code as a string (to extract unbound symbols).z
cimport cython as %sc                    s   g | ]}|  |fqS r   r   r   r   r   rr   r   r   r      r!   Z_cython_inline_ z([.\w]+)[.]z
cimport %sr   rT   z, c                 S   s   g | ]}d | qS )z%s %sr   )r   ar   r   r   r      r!   zf
%(module_body)s
%(cimports)s
def __invoke(%(params)s):
%(func_body)s
    return locals()
            
)cimportsmodule_bodyparams	func_bodyz.pyxw)r%   sourcesinclude_dirsextra_compile_argsrk   )Zinclude_pathZcompiler_directivesquietc                    s   g | ]} | qS r   r   r   )rr   r   r   r     r!   )8rj   rE   _cython_inline_default_contextrW   _cython_inline_cachegetrv   sorteditemsr   r   r   strip_common_indentrl   rm   rn   ro   rp   rK   AssertionErrorrq   rV   cython_moduleappendr\   r]   cython_inlineso_extrh   Zget_ext_filenameospathjoinexistsmakedirsisfilerecompilematchgroupsrT   Zget_includeextract_func_codereplaceopenwritecloser   r   
extensionsdirname
build_temp	build_librunr$   Z__invoke)+rG   r   lib_dirri   Zcython_compiler_directivesforcer   rs   rt   r   rr   Z_unbound_symbolsargsr   Zkey_hashZinvokearg_listr   literalsr   r%   rP   	arg_namesmodule_namer-   rg   r&   cflagsZc_include_dirsZ	qualifiedrM   _mrT   r   r   r   Zmodule_coder   r   Zpyx_filefh	extensionr   r   r   r      s    












r   z[^ ]c                 C   s   d }|   }|D ]<}t|}|s"q| }|| dkr8q|d u sH||kr|}qt|D ]>\}}t|}|rV|rV|||d  dkrqV||d  ||< qVd|S )N#r   r   )
splitlines_find_non_spacestart	enumerater   )rG   Z
min_indentlinesliner   indentixr   r   r   r     s"    r   zI^((cdef +(extern|class))|cimport|(from .+ cimport)|(from .+ import +[*]))c                 C   sl   g }g }|}|  dd} | d}|D ],}|dsHt|rD|}n|}|| q&d|dd| fS )N	 r   z    z
    )r   split
startswithmodule_statementr   r   r   )rG   r-   functioncurrentr   r   r   r   r   r   4  s    


r   )getcallargsc                 O   s  i }t | \}}}}|d ur2|t|d  ||< t||D ]\}}	|	||< q<t| D ]2\}}	||v rZ||v r~td| ||||< qZ|d ur|||< n|rtdt| |d u rd}t|t| }
t|D ]8\}}||vr||
kr|||
  ||< qtd| q|S )NzDuplicate argument %sz Unexpected keyword arguments: %sr   zMissing argument: %s)	rl   
getargspeclenziprV   r   	TypeErrorpopr   )funcZ
arg_valuesZ
kwd_valuesallr   varargsrr   defaultsr%   r   Zfirst_defaultr   r   r   r   r   G  s0    


r   c                 C   sB   |  d}| d d dkr.d| |d d   S | |d d  S d S )N:r"   lambdaz	return %sr   )index)sourcer   r   r   r   get_bodyc  s    
r   c                   @   s   e Zd Zdd Zdd ZdS )RuntimeCompiledFunctionc                 C   s   || _ tt|| _d S r   )_fr   rl   	getsource_body)r2   fr   r   r   r/   o  s    z RuntimeCompiledFunction.__init__c                 O   s`   t | jg|R i |}tr<t| jf| jj| jjd|S t| jf| jj| jjd|S d S )N)rs   rt   )r   r   IS_PY3r   r   __globals__Zfunc_globals)r2   r   rr   r   r   r   r   r9   s  s     z RuntimeCompiledFunction.__call__N)r<   r=   r>   r/   r9   r   r   r   r   r   m  s   r   )N)N)N)NN)D
__future__r   r{   rl   r   r   r\   distutils.corer   r   Zdistutils.command.build_extr   ry   ZCompiler.Mainr   r   ZCompiler.Visitorr	   r
   rA   r   ZCompiler.TreeFragmentr   ZCompiler.StringEncodingr   ZDependenciesr   r   r   ZCompilerr   ZUtilsr   Zcythonr   rw   r   r   r#   r$   Zimportlib.utilutilr'   r.   rK   rR   rO   rh   rj   r   r   rv   r   r   r   r   r   r   searchr   r   r   r   r   rC   r   rU   r   r   r   r   r   <module>   sl   








}

