U
    e%3                  	   @   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 ddlmZ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+Z,d dl-m.Z. dd Z*G dd deeZ/ed9ddZ0d:ddZ1d;d d!Z2d"d# Z3ed$d% Z4i Z5e4d&Z6d<d'd(Z7d)d* Z8e1ej9:e# d+ddd,d,dddf	d-d.Z;de;_<e=d/j>Z?d0d1 Z@e=d2ZAd3d4 ZBd5d6 ZCG d7d8 d8eDZEdS )=    )absolute_importN)Distribution	Extension)	build_ext   )Context)default_optionsCompilationOptionsget_directive_defaults)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   7/tmp/pip-unpacked-wheel-7k3cqui1/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&   )ExtensionFileLoaderc                 C   s2   t jj| t| |d}t j|}|j| |S )N)loader)	importlibutilspec_from_file_locationr)   module_from_specr*   exec_module)r'   pathspecmoduler   r   r   r&   0   s    c                       s0   e Zd Z fddZdd Z fddZ  ZS )UnboundSymbolsc                    s   t t| jd d t | _d S )N)context)superr   __init__setunbound)self	__class__r   r   r6   8   s    zUnboundSymbols.__init__c                 C   s"   |   |js| j|j |S r   )Zcurrent_envlookupr'   r8   addr9   noder   r   r   visit_NameNode;   s    zUnboundSymbols.visit_NameNodec                    s   t t| | | jS r   )r5   r3   __call__r8   r>   r:   r   r   rA   ?   s    zUnboundSymbols.__call__)__name__
__module____qualname__r6   r@   rA   __classcell__r   r   r:   r   r3   7   s   r3   c                 C   s   t | } |d kr$tg t ttd}ddlm} td| }t	|dD ]$}|d krTqF||}t
||rF qlqFzdd l}W n tk
r   dd l}Y nX tt |tt| S )Noptionsr   )AnalyseDeclarationsTransformz(tree fragment)Zpyxr   )r   r   r
   r	   r   Compiler.ParseTreeTransformsrH   r   r   Zcreate_pipeliner   builtinsImportError__builtin__tupler3   r7   dir)coder4   rH   treephaserJ   r   r   r   unbound_symbolsD   s$    

rR   c                 C   s"   t | }|tkrdS t| |S d S )Nlong)typeint	safe_type)argr4   py_typer   r   r   unsafe_typeY   s    rY   c                 C   s   t | }|ttttfkr|jS |tkr*dS |tkr6dS |tkrBdS dt	j
krpt| t	j
d jrpd| jj| jf S |jD ]N}|jdk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])rL   rJ   objectF)Zneed_pxdz%s.%s)rT   listrM   dictstrrB   complexfloatboolsysmodulesr   ZndarrayZdtyper'   ndim__mro__rC   find_moduler<   Zis_type)rW   r4   rX   Z	base_typer2   entryr   r   r   rV   a   s(    

rV   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_extensiony   s    
ro   c                 C   s   t t| t ttdS )NrF   )r   r]   r
   r	   r   )cython_include_dirsr   r   r   _create_context   s
    rq   ).c                 C   s   |D ]~}|| kr|d ks |d krJt  jjj}|d kr<|j}|d krJ|j}||kr`|| | |< q||krv|| | |< qtd|  qd S )NzCouldn't find %r)inspectcurrentframef_backf_locals	f_globalsprint)kwdsrR   localsglobalssymbolZcalling_framer   r   r   _populate_unbound   s    r}   c                 C   s.   | |t jt j|tjf}tt|d	 S )Nzutf-8)
rc   version_info
executableCython__version__hashlibsha1r   encode	hexdigest)	orig_codearg_sigslanguage_levelkeyr   r   r   _inline_key   s    r   inlineFc
           ,   	      sf  d krdd |r t t|nt |r0t|ni }|	d krHd|krHd}	|	d k	rX|	|d< d }t| }|d k	rt||| t }t fdd|D }t	| ||	}t| ||f}|d k	rdd |D }|| S | }t
| } t| \} }t| } |d krt jjj}|d kr*t jjj}z"t|  t|< }t||| W n$ tk
rp   |sltd Y nX g }t D ](\}}|tkr|d	|  |= qt}t fd
d|D }|d krt	|||	}d| }|tjkrtj| }n4d }tjd kr(t }|dt_tj |}tj!||tj }tj"|s`t#| |sttj$|sg }g }g }t%&d}|D ]b\}} |'|}!|!r|d|!( d   |!( d dkrdd l)}"||"*  |d qt+| \}#}$d!dd |D }%dd!||#|%|$d }&| D ]\}'}(|&,|'|(}&q2tj!||d })t-|)d}*z|*/|& W 5 |*.  X t0||)g|pd |pd |pd d}+|d krt }t1|+g|pdg||d|_2tj3|)|_4||_5|6  tj7dkr0tj8dkr0t9tj | t:||}W 5 Q R X n
t:||}|j;t|||f< fdd|D }|j;| S )Nc                 S   s   dS )Nr\   r   r    r   r   r   r"      r#   zcython_inline.<locals>.<lambda>r   Z3strc                    s   g | ]\}}| |fqS r   r   ).0rW   value)ctxget_typer   r   
<listcomp>   s     z!cython_inline.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   r   rW   r   r   r   r      s     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   ry   r   r   r      s     Z_cython_inline_ z([.\w]+)[.]z
cimport %sr   r[   )ZNPY_NO_DEPRECATED_APIZNPY_1_7_API_VERSIONz, c                 S   s   g | ]}d | qS )z%s %sr   )r   ar   r   r   r      s     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_argsdefine_macrosrr   )Zinclude_pathZcompiler_directivesquietwin32)r      c                    s   g | ]} | qS r   r   r   )ry   r   r   r   *  s     )<rq   rM   _cython_inline_default_contextr^   _cython_inline_cachegetr}   sorteditemsr   r   r   strip_common_indentrs   rt   ru   rv   rw   rR   AssertionErrorrx   r]   cython_moduleappendrc   rd   cython_inlineso_extro   Zget_ext_filenameosr0   abspathjoinexistsmakedirsisfilerecompilematchgroupsr[   Zget_includeextract_func_codereplaceopenclosewriter   r   
extensionsdirname
build_temp	build_librunplatformr~   Zadd_dll_directoryr&   Z__invoke),rO   r   lib_dirrp   Zcython_compiler_directivesforcer   rz   r{   r   ry   Zkey_hashZ_unbound_symbolsargsr   Zinvokearg_listr   literalsr   r'   rW   	arg_namesmodule_namer2   rn   r(   cflagsr   Zc_include_dirsZ	qualifiedrT   _mr[   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 k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   )rO   Z
min_indentlinesliner   indentixr   r   r   r   5  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   )rO   r2   functioncurrentr   r   r   r   r   r   J  s    


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_bodyZ  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   rs   	getsource_body)r9   fr   r   r   r6   f  s    z RuntimeCompiledFunction.__init__c                 O   s\   t j| jf||}tr8t| jf| jj| jjd|S t| jf| jj| jjd|S d S )N)rz   r{   )rs   getcallargsr   IS_PY3r   r   __globals__Zfunc_globals)r9   r   ry   allr   r   r   rA   j  s     z RuntimeCompiledFunction.__call__N)rB   rC   rD   r6   rA   r   r   r   r   r   d  s   r   )N)N)N)NN)F
__future__r   r   rs   r   r   rc   distutils.corer   r   Zdistutils.command.build_extr   r   ZCompiler.Mainr   ZCompiler.Optionsr   r	   r
   ZCompiler.Visitorr   r   rI   r   ZCompiler.TreeFragmentr   ZCompiler.StringEncodingr   ZDependenciesr   r   r   ZCompilerr   ZUtilsr   Zcythonr   r~   r   r   r%   r&   importlib.utilr+   Zimportlib.machineryr)   r3   rR   rY   rV   ro   rq   r   r   r}   r   r0   r   r   r   r   searchr   r   r   r   r   r\   r   r   r   r   r   <module>   st   






     
 


