U
    {hD                     @   s  d dl 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 d dlm	Z	 d dl
mZ ddlmZmZ d dlmZ zd dlZW n   dZY nX dd	lmZ dd
lmZmZmZ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!m"Z"m#Z# eej$j%Z&eeZ'eeZ(dOddZ)dd Z*dPddZ+dd Z,dd Z-e. Z/e. Z0e. Z1e2e3e3e3e0e0e0e0e3e0e0e3e0e/e1dZ4dd Z5G dd dZ6e 7e.e8dej9j:Z;e 7e.e8d ej9j:Z<e 7e.e8d!ej9j:Z=dQe2e2d#d$d%Z>e8d&ej?ej9B Z@e8d'ej?ej9B ZAd(d) ZBed*d+ ZCd,d- ZDed.d/ ZEed0d1 ZFed2d3 ZGed4d5 ZHG d6d7 d7ZIdaJdRd9d:ZKd;d< ZLdSd=d>ZMdTd?d@ZNdAdB ZOejPQdCrejPdC ZRdDdE ZSndFdE ZSeSdUdIdJZTdKdL ZUdMdN ZVdS )V    N)iglob)StringIO)relpath   )CacheFingerprintFlags)Iterable   )Utils)cached_functioncached_methodpath_existssafe_makedirscopy_file_to_dir_if_neweris_package_dirwrite_depfile)Errors)Context)Options)CompilationOptionsdefault_optionsget_directive_defaultsc                    s:    st    d t jjkr( t jj7   fdd| D S )Nc                    s$   g | ]}|  rt| n|qS  )
startswith_relpath).0pathbaser   =/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Build/Dependencies.py
<listcomp>%   s   z"_make_relative.<locals>.<listcomp>)osgetcwdr   sep)Z
file_pathsr   r   r   r    _make_relative    s    
r%   c                 c   s:  d| krVt d| }|rV| \}}}|dD ] }t|| | D ]
}|V  qDq0d S d| ksttjdkr d| kr t }t dtjdkrdnd	 | d
\}}	|rt|tj }ndg}|D ]`}
tt	|
|	D ]}||kr|
| |V  qtt	|
dd|	D ]}||kr|
| |V  qqnt| D ]}|V  q(d S )N{z(.*){([^}]+)}(.*),z**/\z**\z\*\*[%s]z/\\/r    *z**)rematchgroupssplitextended_iglobr"   r$   setr   	join_pathadd)patternmbeforeswitchafterZcaser   seenfirstrestrootr   r   r    r0   )   s2    
$

r0   expected non-empty iteratorc                 c   s(   d}| D ]}d}|V  q|r$t |d S )NTF)
ValueError)it	error_msgemptyvaluer   r   r    nonemptyJ   s    rC   c              	   C   s   t d krtdzt jjdd}W n tk
r@   t j }Y nX | j|d  | j|d  | j|d  | j	|d  | j
|d  | j|d	  | j|d
  d| _dD ]*}z| j| W q tk
r   Y qX qd S )NzBYou first need to install Pythran to use the np_pythran directive.T)pythoninclude_dirsextra_compile_argsextra_link_argsdefine_macrosundef_macroslibrary_dirs	librariesc++)z-fwhole-programz-fvisibility=hidden)pythranRuntimeErrorconfigZmake_extension	TypeErrorrE   extendrF   rG   rH   rI   rJ   rK   languageremover>   )extZpythran_extZ
bad_optionr   r   r    update_pythran_extensionS   s&    rU   c                    sj   t | dkr6| d dkr6| d dkr6| dd } d}nd}t| \}   fd	d
fdd| |D S )a#  
    >>> parse_list("")
    []
    >>> parse_list("a")
    ['a']
    >>> parse_list("a b c")
    ['a', 'b', 'c']
    >>> parse_list("[a, b, c]")
    ['a', 'b', 'c']
    >>> parse_list('a " " b')
    ['a', ' ', 'b']
    >>> parse_list('[a, ",a", "a,", ",", ]')
    ['a', ',a', 'a,', ',']
    r	   r   [r   ]r   r'    c                    s,   |   } | d dkr$ | dd  S | S d S )Nr   z'"r   r   strip)literal)literalsr   r    unquote   s    zparse_list.<locals>.unquotec                    s   g | ]}|  r |qS r   rY   )r   item)r]   r   r    r!      s      zparse_list.<locals>.<listcomp>)lenstrip_string_literalsr/   )s	delimiterr   )r\   r]   r    
parse_listl   s    $rc   )namesourcesrH   rI   rK   rJ   runtime_library_dirsrE   extra_objectsrF   rG   export_symbolsdependsrR   
np_pythranc                 C   s`   | dkrdS | dkrdS dd l }|jdtd |  } | dkrBdS | d	krNdS td
| f d S )NTrueTFalseFr   z1The 'np_python' option requires 'True' or 'False')category)yyesttrueon1)nnoffalseoff0zinvalid truth value %r)warningswarnDeprecationWarninglowerr>   )valrz   r   r   r    _legacy_strtobool   s    r   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )DistutilsInfoNc           	         sR  i | _ |d k	rt|tr"t|n|}|D ]     s<q* d dkrN qN dd    t fdddD d }|d k	r*dd  t|d  d	D \}}}t	|d } 
d
r|d krq*|ttfkrt|}|dkrdd |D }|tkr t|}|| j |< q*n@|d k	rNtD ]0}|dkr.qt||d }|r|| j |< qd S )Nr   #r   c                 3   s   | ]}  |r|V  qd S N)r   )r   kliner   r    	<genexpr>   s     
 z)DistutilsInfo.__init__.<locals>.<genexpr>)z
distutils:cython:c                 S   s   g | ]}|  qS r   rY   r   ra   r   r   r    r!      s     z*DistutilsInfo.__init__.<locals>.<listcomp>=r   rH   c                 S   s,   g | ]$}d |kr t |d dn|dfqS )r   r   N)tupler/   )r   macror   r   r    r!      s   )rd   re   rj   )values
isinstancestrr   lstripnextr_   	partitiondistutils_settingsgetr   listtransitive_listrc   bool_orr   getattr)	selfsourceexnZsource_lineskindkey_rB   typer   r   r    __init__   s@    
& 


zDistutilsInfo.__init__c                 C   s   |d kr| S |j  D ]\}}t| }|tkrD|| j krD|| j |< q|tkr|| j kr| j | d d  }|D ]}||krl|| ql|}|| j |< q|tkr| j |d|B | j |< q| S )NF)r   itemsr   transitive_strr   appendr   r   )r   otherr   rB   r   allvr   r   r    merge   s"    
zDistutilsInfo.mergec                 C   s   |d kr| S t  }| j D ]x\}}t| }|ttfkrzg }|D ]2}||krT|| }t|trh||7 }q@|| q@|}n||kr|| }||j|< q|S r   )r   r   r   r   r   r   r   r   )r   aliasesresolvedr   rB   r   Znew_value_listr   r   r   r    subs   s$    

zDistutilsInfo.subsc                 C   sJ   | j  D ]:\}}t| }|ttfkr8t||t| }t||| q
d S r   )r   r   r   r   r   r   setattr)r   	extensionr   rB   r   r   r   r    apply   s
    zDistutilsInfo.apply)NN)__name__
__module____qualname__r   r   r   r   r   r   r   r    r      s   
!r   z]
    (?P<comment> [#] ) |
    (?P<brace> [{}] ) |
    (?P<fstring> f )? (?P<quote> '+ | "+ )
z`
    (?P<escape> [\\]+ ) (?P<escaped_quote> ['"] ) |
    (?P<fstring> f )? (?P<quote> '+ | "+ )
z
    (?P<braces> [{]+ | [}]+ ) |
    (?P<escape> [\\]+ ) (?P<escaped_quote> ['"] ) |
    (?P<fstring> f )? (?P<quote> '+ | "+ )
__Pyx_Lcodeprefixc                    s   g i dt fdd ttjtjtjd fdddtjtjtjd fd	d
d dfS )z
    Normalizes every string literal to be of the form '__Pyx_Lxxx',
    returning the normalized code and a mapping of labels to
    string literals.
    r   c                    s,    d7     d}| |<  | d S )Nr   r   )r   )r[   label)counterr\   new_coder   r   r    append_new_label%  s    z/strip_string_literals.<locals>.append_new_label)
quote_typestart
is_fstringreturnc                    sB  |}|rt nt}|dkr>||}|d krD |d   d}q>| }|d rt|d d dkr|d | d kr|d8 }q|r|d rt|d d dkrq|d d dkr||d k rԈ ||d   d |d	d
 }}q|d | r|d}||kr" ||  |  |t| 7 }q>q|S )Nr   escaper	   r   Zescaped_quoter   Zbracesr&   T
in_fstringquote)_FIND_FSTRING_TOKEN_FIND_STRING_TOKENendr_   r   r   r   )r   r   r   charpos
find_tokentoken)r   r   r   
parse_coder   r    parse_string,  s8    

$




z+strip_string_literals.<locals>.parse_stringF)r   r   r   c                    s  | }|dkr|}|d kr: | d   d}q|  }}|d r|d }t|dkrv|d t|d  }|rt|dkrt|dkr|t|d 8 }|d d } | |  |||d d } }q|d r2 | |  d	|} ||dkr|nd   |dkr,q|} q|r|d
 r|d
 dkrt| d  }} | |  qq| d } | |  |dd } }q|S )Nr   r      r	      Zfstring)r   comment
brace}r   Tr   )r   r   r_   findr   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r    r   U  sD    



z)strip_string_literals.<locals>.parse_coder*   )F)_FIND_TOKENr   cythonZ
Py_ssize_tZbintjoinr   r   )	r   r   r   r   r\   r   r   r   r   r    r`     s    $)(/r`   a  (?:^ [ \t\f]* from     [ \t\f]+ cython\.cimports\.([\w.]+) [ \t\f]+ c?import ) |(?:^ [ \t\f]* from     [ \t\f]+ ([\w.]+) [ \t\f]+ cimport ) |(?:^ [ \t\f]* c?import [ \t\f]+ cython\.cimports\.([\w.]+) ) |(?:^ [ \t\f]* cimport  [ \t\f]+ ([\w.]+ (?:[ \t\f]* , [ \t\f]* [\w.]+)*) ) |(?:^ [ \t\f]* cdef     [ \t\f]+ extern [ \t\f]+ from [ \t\f]+ ['\"] ([^'\"]+) ['\"] ) |(?:^ [ \t\f]* include  [ \t\f]+ ['\"] ([^'\"]+) ['\"] )zf(?:^ [ \t\f]+ \( ([\w., \t\f]*) \) [ \t\f]* [#\n]) |(?:^ [ \t\f]+    ([\w., \t\f]*)    [ \t\f]* [#\n])c                 C   s   t tj| tt|S r   )normalize_existing0r"   r   dirnamer   r1   )	base_path	rel_pathsr   r   r    normalize_existing  s    r   c                 C   sf   g }d}|D ]P}t j|r(|| qt| |}t|rR|t j| | }q|| q||fS )a  
    Given some base directory ``base_dir`` and a list of path names
    ``rel_paths``, normalize each relative path name ``rel`` by
    replacing it by ``os.path.join(base, rel)`` if that file exists.

    Return a couple ``(normalized, needed_base)`` where ``normalized``
    if the list of normalized file names and ``needed_base`` is
    ``base_dir`` if we actually needed ``base_dir``. If no paths were
    changed (for example, if all paths were already absolute), then
    ``needed_base`` is ``None``.
    N)r"   r   isabsr   r2   r   normpath)base_dirr   
normalizedZneeded_baserelr   r   r   r    r     s    

r   c                 C   s6   t |}g }| D ] }t||}|d k	r|| q|S r   )r   resolve_dependr   )ri   rE   r   dependr   r   r   r    resolve_depends  s    
r   c                 C   sL   | d dkr| d dkrd S |D ]&}t || }t|r tj|  S q d S )Nr   <r   >)r2   r   r"   r   r   )r   rE   dirr   r   r   r    r     s    
r   c                 C   sF   t jt jt| }|| kr>t|r>t|t j|f S dS d S )Nr   )r"   r   r   abspathr   r   packagebasename)filenamer   r   r   r    r     s    r   c                 C   s,   t jt j| d }dt| |f S )Nr   .)r"   r   splitextr   r   r   )r   moduler   r   r    fully_qualified_name  s    r   c              	      s4  t j| dd}| }W 5 Q R X t|}t|\}}|dddd}g }g }g }t|D ]}| \}	 }
}}}|	r|	 |
r|
} r|	  t
j|| d}|r| \}}|p|}| fdd|d	D  q`|r|d
d |d	D  q`|r|	||  q`|	||  q`||||fS )Nignore)Zerror_handlingz\
rX   	)posc                 3   s   | ]}d   | V  qdS )z{}.{}N)formatrZ   r   Zcimport_fromr   r    r     s   z%parse_dependencies.<locals>.<genexpr>r'   c                 s   s   | ]}|  V  qd S r   rY   )r   xr   r   r    r     s     )r
   Zopen_source_filereadr   r`   replacedependency_regexfinditerr.   r   dependency_after_from_regexsearchr   rQ   r/   )source_filenamefhr   distutils_infor\   cimportsincludesexternsr5   Zpycimports_fromZpycimports_listZcimport_listexternincludeZm_after_from	multilineZone_lineZ
subimportsr   r   r    parse_dependencies  s:    

r   c                   @   s   e Zd Zd'ddZdd Zedd Zedd	 Zd
d Zdd Z	dd Z
ed(ddZedd Zedd Zdd Zedd Zdd Zdd Zdd  Zd)d!d"Zd#d$ Zd%d& ZdS )*DependencyTreeFc                 C   s   || _ || _i | _d S r   )contextquiet_transitive_cache)r   r  r  r   r   r    r     s    zDependencyTree.__init__c                 C   s   t |rtj|}t|S r   )r   r"   r   r   r   )r   r   r   r   r    r     s    z!DependencyTree.parse_dependenciesc                 C   s   t  }| |d D ]}ttj||}t|sB| jj||d}|r~dtjj	 |krbtj
|}|| || | q| jstd||f  q|S )Nr   Zsource_file_pathr   z*Unable to locate '%s' referenced from '%s')r1   r   r2   r"   r   r   r   r  Zfind_include_filer$   r   r3   updateincluded_filesr  print)r   r   r   r   include_pathr   r   r    r    s    
zDependencyTree.included_filesc                 C   s   |  |d d \}}}t|}t|}t }| |D ]2}| |\}}}	|| || ||	 q8t||\}}
|
r||
 t|||fS )Nr   )r   r1   r  cimports_externs_incdirsr  r   r3   r   )r   r   r   r   r   incdirsr   Zincluded_cimportsZincluded_externsZincluded_incdirsincdirr   r   r    r	  #  s    


z'DependencyTree.cimports_externs_incdirsc                 C   s   |  |d S Nr   )r	  r   r   r   r   r    r   5  s    zDependencyTree.cimportsc                 C   s   t |S r   )r   r  r   r   r    r   8  s    zDependencyTree.packagec                 C   s   t |S r   )r   r  r   r   r    r   ;  s    z#DependencyTree.fully_qualified_nameNc                 C   s   |d dk}|r|st d|d k	r|d}|r<|d t| |}|r|d sz|  W n tk
rx   Y d S X |d qJd|| }| jj||d}|r|S |rd S | jj||dS )Nr   r   zNew relative imports.r  )	NotImplementedErrorr/   popr   r   
IndexErrorr   r  Zfind_pxd_file)r   r   r   Zis_relativemodule_pathpackage_pathrelativeZpxdr   r   r    find_pxd>  s*    

zDependencyTree.find_pxdc                 C   s   t j|\}}|dkr0t|d r0|d g}ng }| |D ]<}|d d dks>|dkr\q>| ||}|d k	r>|| q>t|S )N.pyx.pyz.pxd   zcython.r   )r"   r   r   r   r   r  r   r   )r   r   Zfilename_rootZfilename_extZpxd_listr   Zpxd_filer   r   r    cimported_filesV  s    zDependencyTree.cimported_filesc                 C   s*   |h}| | | | | | |S r   )r  r  r  )r   r   Zall_depsr   r   r    immediate_dependenciesg  s    z%DependencyTree.immediate_dependenciesc                 C   s   |  || jtjS r   )transitive_merger  r1   unionr  r   r   r    all_dependenciesn  s    zDependencyTree.all_dependenciesc                 C   s   t j|S r   )r"   r   getmtimer  r   r   r    	timestampq  s    zDependencyTree.timestampc                 C   s   |  ||fS r   )r  r  r   r   r    extract_timestampu  s    z DependencyTree.extract_timestampc                    s   t  fdd |D S )Nc                    s   g | ]}  |qS r   )r   )r   rv   r   r   r    r!   y  s     z4DependencyTree.newest_dependency.<locals>.<listcomp>)maxr  r  r   r!  r    newest_dependencyx  s    z DependencyTree.newest_dependencyc           
      C   s   |  |d }|j}| |\}}}t }|rjt||}d|kr^tt|d ||d< nt||d< |rt|	dg }t||D ]}	|	|kr|
|	 q||d< |S )Nr   ri   rE   )r   r   r	  r"   r#   r%   r   r1   r  r   r   )
r   r   infokwdsr   r   r
  basedirrE   incr   r   r    distutils_info0{  s     
zDependencyTree.distutils_info0c                 C   s   |  || jtj||S r   )r  r(  r   r   r   )r   r   r   r   r   r   r    r     s
    zDependencyTree.distutils_infoc                 C   sT   z| j ||f }W n& tk
r8   i  }| j ||f< Y nX | ||||i | jd S r  )r  KeyErrortransitive_merge_helperr  )r   nodeextractr   r9   r   r   r    r    s         zDependencyTree.transitive_mergec              	   C   s   ||kr|| d fS ||}||kr,||fS zt |||< d }||D ]L}	| |	|||||\}
}|d k	r|d k	r|| || k rn|}|||
}qF||krd }|d kr|||< ||fW S ||= X d S r   )r_   r*  )r   r+  r,  r   r9   stackoutgoingdepsZloopr   Zsub_depsZsub_loopr   r   r    r*    s*    z&DependencyTree.transitive_merge_helper)F)N)NN)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     s0   






r   Fc                 C   s6   t d kr2| d kr&tdgt ttd} t| |da t S )Nr   )optionsr  )	_dep_treer   r   r   r   r   )ctxr  r   r   r    create_dependency_tree  s    
r4  c                 C   sx   d|kr>| dg dg }t|d |}tt|| j |d< | j}|f |}t| dr`| j|_t||d d}||fS )Nri   rE   r   py_limited_apird   )	distutilsmodule_name)	r   r   sortedr1   ri   	__class__hasattrr5  dict)templater%  rE   ri   rp   rT   metadatar   r   r    default_create_extension  s    

r>  c           #   
      sh  |d k	rt d| |d kr"g }| d kr2g i fS t| tsFt| tsL| g} ddlm} dtjkr~|tjd j	tjd jf n|f  fdd| D }t
||d}	|j}
t }t|ts|g}|D ]}|ttjjt| qg }i }|jjpt}t }| D ]^}t|tr.|||g }d	}d }|}nt| rd
d |jD }|rz|d t|dkrt d|j|f  n~|
r|j|
kr||t|
|jp|
dtj|jdkrdnd g|jd\}}d|_d |_||  qn||  q|}|j}t |d}d }n$tdt!|t"||j#f }t$|t%t&td D ]}tj||krhqL|	'|}d	|kr||krqLn
|r|}t()| ||krLz|	*|||j+}W n$ t,k
r   |rY qL Y nX |d k	r|j+- D ]\}}||kr|||< q||d< |gfdd|jD  }d|krf|d D ]}||krJ|| qJ||d< |rd|kr||d< |.dd} |||\}}!| pt/|dd|_|
|_|jrt0| || |!||< ||jkrNtj1|d |jdkrdnd }"z|j2|" W n" t3k
r>   t d|  Y nX |j4d| |5| qL q||fS )NzWarning: passing language={0!r} to cythonize() is deprecated. Instead, put "# distutils: language={0}" in your .pyx or .pxd file(s)r   )	Extension
setuptoolszsetuptools.extensionc                    s   h | ]}t | r|jqS r   )r   rd   r   r5   )extension_classesr   r    	<setcomp>  s     
 z(create_extension_list.<locals>.<setcomp>r1  r+   c                 S   s$   g | ]}t j|d  dkr|qS )r   )r  r  r"   r   r   r   r   r   r    r!     s    z)create_extension_list.<locals>.<listcomp>r   zWarning: Multiple cython sources found for extension '%s': %s
See https://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html for sharing declarations among Cython files.r   rL   .cpp.c)rd   re   rR   F)r   zUpattern is not of type str nor subclass of Extension (%s) but of type %s and class %sz'%s' doesn't match any filesrd   c                    s   g | ]}| kr|qS r   r   rA  )filepatternr   r    r!   C  s      re   rR   rj   z@Warning: Cython source file not found in sources list, adding %s)6r  r   r   r   r   distutils.extensionr?  sysmodulesZ
_Extensionr4  shared_utility_qualified_namer1   r   r  mapr"   r   r   r0   r0  create_extensionr>  re   r_   rd   r;  r   r$   rR   rj   r   r   reprr   r9  rP   rC   r8  r   r
   Z$raise_error_if_module_name_forbiddenr   r   	Exceptionr   r  r   rU   r   rS   r>   insertr3   )#patternsexcluder3  r   r  rR   exclude_failuresr?  Zexplicit_modulesr/  rK  Z
to_excluder4   module_listmodule_metadatarM  r9   r<  rd   r   Zext_languageZcython_sourcesr5   r   msgfiler7  r%  r   rB   re   r   rj   r=  Ztarget_filer   )rB  rG  r    create_extension_list  s    





 












$rX  c	           2         s2  |dkrg }d|	krdg|	d< d|	kr2t |	d  |	dd}
tdkrLd}ntf |	}d|_d|_|dkrztjddk}tf |	}tf |	}d|_t	
|}|}	|j}t| ||||||d	\} }t|  t||d
}t|	dd |	jr$|	js$tjs$|	jdkr
dn|	j}t|t|	ddndt f fdd	} fdd}tt}g }| D ]} r||jD ]}|| qldd |jD }t|dkr|j}nd}t|dd}t|dd}|r|}	n|jdkr|}	n|}	g }|jD ]6}tj|\}} | dkrv||jdks|r"dnd }!||!}!|
rL||}"t |!||" t!"|!rftj#|!}#nd}#|#|$|k r|$|| }$}d}%n |%|\}$}d||&|k }%|s|#|$k rf|s|s||krt'dt!(|  nt'dt!(|t!(|f  |s:r:)||||	t*|jp.d ||}&nd}&|+|%||!|&||	| ||j||f
 ||! +| n|r|j|kr||}!t|	|!dd!}'t!,|!st'd"|j d#|! d$ n>|st!"|!sdd%l-m.}( |st'd&|j d$ |(|' n|}! r|| |+|! q||_qZ|/  t|})d'tt0|)|)f j1fd(dt2|dD }|)dkr|d}|rddl3}*|*4 d)krt'd* d}|r>ddl3}*|*j5|t6d+}+zR|+j7t8|dd,},|+9  |,: sz|,d- W n |*j;k
r
   Y nX qW n  t<k
r2   |+=   Y nX |+>  n|D ]}-t?|-  qB|rt@ }.|A D ]r\}!}/tjB|!s|.C|/ nNtjD|!d.k rftE|!d/d0d1}0z"|0Ftd2d2kr|.C|/ W 5 |09  X qf|.r|.D ]}1| G|1 qt'd3d4>tHd5d |.D   r$I  tJjKL  | S )6a  
    Compile a set of source modules into C/C++ files and return a list of distutils
    Extension objects for them.

    :param module_list: As module list, pass either a glob pattern, a list of glob
                        patterns or a list of Extension objects.  The latter
                        allows you to configure the extensions separately
                        through the normal distutils options.
                        You can also pass Extension objects that have
                        glob patterns as their sources. Then, cythonize
                        will resolve the pattern and create a
                        copy of the Extension for every matching file.

    :param exclude: When passing glob patterns as ``module_list``, you can exclude certain
                    module names explicitly by passing them into the ``exclude`` option.

    :param nthreads: The number of concurrent builds for parallel compilation
                     (requires the ``multiprocessing`` module).

    :param aliases: If you want to use compiler directives like ``# distutils: ...`` but
                    can only know at compile time (when running the ``setup.py``) which values
                    to use, you can use aliases and pass a dictionary mapping those aliases
                    to Python strings when calling :func:`cythonize`. As an example, say you
                    want to use the compiler
                    directive ``# distutils: include_dirs = ../static_libs/include/``
                    but this path isn't always fixed and you want to find it when running
                    the ``setup.py``. You can then do ``# distutils: include_dirs = MY_HEADERS``,
                    find the value of ``MY_HEADERS`` in the ``setup.py``, put it in a python
                    variable called ``foo`` as a string, and then call
                    ``cythonize(..., aliases={'MY_HEADERS': foo})``.

    :param quiet: If True, Cython won't print error, warning, or status messages during the
                  compilation.

    :param force: Forces the recompilation of the Cython modules, even if the timestamps
                  don't indicate that a recompilation is necessary.

    :param language: To globally enable C++ mode, you can pass ``language='c++'``. Otherwise, this
                     will be determined at a per-file level based on compiler directives.  This
                     affects only modules found based on file names.  Extension instances passed
                     into :func:`cythonize` will not be changed. It is recommended to rather
                     use the compiler directive ``# distutils: language = c++`` than this option.

    :param exclude_failures: For a broad 'try to compile' mode that ignores compilation
                             failures and simply excludes the failed extensions,
                             pass ``exclude_failures=True``. Note that this only
                             really makes sense for compiling ``.py`` files which can also
                             be used without compilation.

    :param show_all_warnings: By default, not all Cython warnings are printed.
                              Set to true to show all warnings.

    :param annotate: If ``True``, will produce a HTML file for each of the ``.pyx`` or ``.py``
                     files compiled. The HTML file gives an indication
                     of how much Python interaction there is in
                     each of the source code lines, compared to plain C code.
                     It also allows you to see the C/C++ code
                     generated for each line of Cython code. This report is invaluable when
                     optimizing a function for speed,
                     and for determining when to :ref:`release the GIL <nogil>`:
                     in general, a ``nogil`` block may contain only "white" code.
                     See examples in :ref:`determining_where_to_add_types` or
                     :ref:`primes`.


    :param annotate-fullc: If ``True`` will produce a colorized HTML version of
                           the source which includes entire generated C/C++-code.


    :param compiler_directives: Allow to set compiler directives in the ``setup.py`` like this:
                                ``compiler_directives={'embedsignature': True}``.
                                See :ref:`compiler-directives`.

    :param depfile: produce depfiles for the sources if True.
    :param cache: If ``True`` the cache enabled with default path. If the value is a path to a directory,
                  then the directory is used to cache generated ``.c``/``.cpp`` files. By default cache is disabled.
                  See :ref:`cython-cache`.
    Nr  r   Zcommon_utility_include_dirdepfileTZCYTHON_FORCE_REGENrs   )rR  r3  r  rS  rR   r   r1  	build_dirZ
cache_sizec                    sL   t j| }t j| r|} ||rHt t jt| |}t|| d S r   )	r"   r   r   r   r   r2   r   r   copy_once_if_newer)filepathr<   Zfilepath_absZmod_dirrZ  r   r    copy_to_build_dir  s    
z$cythonize.<locals>.copy_to_build_dirc                    s\    s| S t j| r6t j| d } | t jdd } t j | } t j| }t| | S )Nr   )	r"   r   r   
splitdriver/   r$   r   r   safe_makedirs_once)c_filer   r]  r   r    file_in_build_dir  s    z$cythonize.<locals>.file_in_build_dirc                 S   s$   g | ]}t j|d  dkr|qS )r   r  rD  )r   r   r   r   r    r!     s   zcythonize.<locals>.<listcomp>r   rj   Fr5  rL   r  rE  rF  r   r   r	   z Compiling %s because it changed.z&Compiling %s because it depends on %s.c)Zshared_c_file_pathrK  zHWarning: Shared module source file is not a Cython file - not creating 'z' as '')generate_shared_modulezGenerating shared module 'z[{0:%d}/%d] c                    s(   g | ] \}}|d d  |f qS )r   Nr   )r   iZtask)build_progress_indicatorcacher   r    r!   e  s   spawnzBDisabling parallel cythonization for "spawn" process start method.)Zinitializer)	chunksizei    rz	iso8859-1)encodingz#error zFailed compilations: %sz, c                 S   s   g | ]
}|j qS r   )rd   )r   r   r   r   r    r!     s    )Mr   r  rM   r   Zcplusrj   r"   environr   r   Zfrom_optionsrK  rX  fix_windows_unicode_modulesr4  r   rh  Zannotater   r   r#   collectionsdefaultdictr   ri   re   r_   rd   rR   r   r   r  r   r
   Zfile_generated_by_this_cythonr  r  r#  r  r  decode_filenameZtransitive_fingerprintr   r   Zis_cython_generated_fileZSharedModulere  sortr   r   	enumeratemultiprocessingZget_start_methodZPool_init_multiprocessing_helperZ	map_asynccythonize_one_helperclosereadyTimeoutErrorKeyboardInterrupt	terminater   cythonize_oner1   r   existsr  getsizeopenr   rS   r8  Zcleanup_cacherI  stdoutflush)2rT  rR  Znthreadsr   r  forcerR   rS  show_all_warningsr0  rY  Zpythran_optionsZ	c_optionsZcpp_optionsr3  rK  rU  r/  
cache_pathr^  rb  Zmodules_by_cfileZ
to_compiler5   depZ
cy_sourcesfull_module_namerj   r5  Znew_sourcesr   r   rT   ra  ZdependenciesZc_timestampZdep_timestamppriorityfingerprintZmodule_optionsre  Nru  poolresultargsZfailed_modulesrJ  rv   r   r   )rZ  rg  rh  r    	cythonizej  s`   P



 

	






        
   


 


r  c                 C   s^   t jdkrd S t jdkrd S dd }| D ]0}|j r8q(|d|jddd  |j|_q(d S )	Nwin32)r      r	   c                    s<   G  fdddt }||}|r8| fdd|D  |S )Nc                       s   e Zd Z fddZdS )zVfix_windows_unicode_modules.<locals>.make_filtered_list.<locals>.FilteredExportSymbolsc                    s   | kpt | |S r   )r   __contains__)r   r~   ignored_symbolr   r    r    s    zcfix_windows_unicode_modules.<locals>.make_filtered_list.<locals>.FilteredExportSymbols.__contains__N)r   r   r   r  r   r  r   r    FilteredExportSymbols  s   r  c                 3   s   | ]}| kr|V  qd S r   r   )r   rd   r  r   r    r     s      zJfix_windows_unicode_modules.<locals>.make_filtered_list.<locals>.<genexpr>)r   rQ   )r  Zold_entriesr  Zfiltered_listr   r  r    make_filtered_list  s
    z7fix_windows_unicode_modules.<locals>.make_filtered_listZPyInit_r   r   r   )rI  platformversion_inford   isasciirsplitrh   )rT  r  r5   r   r   r    ro    s    


ro  ZXML_RESULTSc                    s    fdd}|S )Nc                     s   t   }d}z"z |   W n   d
}Y nX W 5 t   | }t| d }d| }d| }|rbd}nd}ttjt|d d}|d	 t	   |
  X d S )NTr   z
cythonize.r   r*   Zfailurez.xmlwaQ  
                    <?xml version="1.0" ?>
                    <testsuite name="%(name)s" errors="0" failures="%(failures)s" tests="1" time="%(t)s">
                    <testcase classname="%(name)s" name="cythonize">
                    %(failure_item)s
                    </testcase>
                    </testsuite>
                F)timer   r  r"   r   r   compile_result_dirwriterZ   localsrx  )r  rp   successr   rd   ZfailuresZfailure_itemoutputfuncr   r    with_record  s&    
z#record_results.<locals>.with_recordr   )r  r  r   r  r    record_results  s    r  c                 C   s   | S r   r   r  r   r   r    r    s    Tr*   c              
   C   s  ddl m}m} ddlm}m} |sf|
rN|rN|
||rNt|	 d|  d nt|	 dt	|   |d krvt
|}||_||_tj}|rdt_d}zz$|| |||
|d}|jdkrd	}W n| t|fk
r } z(tjd
|  d	}dd l}|  W 5 d }~X Y n2 tk
r8   |r  dd l}|  d	}Y nX W 5 |rJ|t_X |r||rd|d | ntj|r|t| d S )Nr	   )compile_singler   )CompileError
PyrexErrorzFound compiled z	 in cachezCythonizing r   )r  rh  r  r   z%s
)Compiler.Mainr  r   ZCompiler.Errorsr  r  Zlookup_cacher  r
   rr  r   Zoutput_fileembedded_metadatar   ZLEVELZ
num_errorsOSErrorrI  stderrr  	traceback	print_excrO  r"   r   r~  rS   )Zpyx_filera  r  r  r0  Zraise_on_failurer  r  r  progressrh  r  r   r  r  Zold_warning_levelZany_failuresr  er  r   r   r    r}    sH    
r}  c                 C   s6   dd l }z
t|  W S  tk
r0   |   Y nX d S r  )r  r}  rO  r  )r5   r  r   r   r    rw    s    
rw  c                  C   s   dd l } |  | j| j d S r  )signalSIGINTSIG_IGN)r  r   r   r    rv  %  s    rv  )N)r=   )r   )NF)NNNFNF)Nr   NFNNFF)	NFNTNNFr*   N)Wr   rp  r"   r,   rI  r  globr   ior   os.pathr   r   r   r   collections.abcr   rM   r*   r
   r   r   r   r   r   r   r   ZCompilerr   r  r   r   ZCompiler.Optionsr   r   r   r   r   r2   r[  r`  r%   r0   rC   rU   rc   objectr   r   r   r   r   r   r   r   ZdeclarecompileVERBOSEr   r   r   r   r`   	MULTILINEr   r   r   r   r   r   r   r   r   r   r2  r4  r>  rX  r  ro  rn  r   r  r  r}  rw  rv  r   r   r   r    <module>   s   
$
	!
	Uq








' 4
  
     
  =

              2	