a
    b                     @   s  d dl mZmZ d dlZ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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 d d	lmZ d dlZzd d
lmZ W n ey   d d
lmZ Y n0 zd dl Z e jZ!dZ"W n ey   eZ!dZ"Y n0 zd dl#Z#ej$Z%W n eyH   ej&Z%Y n0 zd dl'Z'W n   dZ'Y n0 ddlm(Z( ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3 e)e	j4j5Z6e)e-Z7e)e,Z8ej9d  dk re: Z;e;du re< Z;dd Z=ndd Z=e>Z?dTddZ@dd ZAdUddZBe)dd ZCdd ZDdd  ZEeF ZGeF ZHeF ZIe>eJeJeJeHeHeHeHeJeHeHeJeHeGeId!ZKejLejMejMd"d#d$ ZNG d%d& d&eFZOejLejMejMejMejMejMejMejMejMejMd'	dVd)d*ZPeQd+ejRZSeQd,ejRZTd-d. ZUe)d/d0 ZVd1d2 ZWe)d3d4 ZXe)d5d6 ZYe)d7d8 ZZe)d9d: Z[G d;d< d<eFZ\da]dWd>d?Z^d@dA Z_dXdBdCZ`dYdDdEZae	jbcdFre	jbdF ZddGdH ZendIdH ZeeedZdKdLZfdMdN ZgdOdP Zhd[dRdSZidS )\    )absolute_importprint_functionN   )__version__)iglob)open)relpath)	Extension)	strtobool)Iterablez.gz )Utils)cached_functioncached_methodpath_existssafe_makedirscopy_file_to_dir_if_neweris_package_dirreplace_suffix)ContextCompilationOptionsdefault_options   c                 C   s   t | ts| tS | S N)
isinstancebytesencode_fs_encodingfilename r    i/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Build/Dependencies.pyencode_filename_in_py2;   s    

r"   c                 C   s   | S r   r    r   r    r    r!   r"   @   s    c                    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 r    )
startswith_relpath).0pathbaser    r!   
<listcomp>J   s   z"_make_relative.<locals>.<listcomp>)osgetcwdr'   sep)
file_pathsr)   r    r(   r!   _make_relativeE   s    
r/   c                 c   s  d| v rVt d| }|rV| \}}}|dD ] }t|| | D ]
}|V  qDq0d S d| v rt }| dd\}}	|rt|d }ndg}|D ]b}
tt|
|	D ]}||vr|| |V  qtt|
dd|	 D ]}||vr|| |V  qqnt| D ]
}|V  qd S )	N{z(.*){([^}]+)}(.*),z**/   /r   *)	rematchgroupssplitextended_iglobsetr   	join_pathadd)patternmbeforeswitchafterZcaser'   seenfirstrestrootr    r    r!   r9   N   s2    


r9   expected non-empty iteratorc                 c   s(   d}| D ]}d}|V  q|r$t |d S )NTF)
ValueError)it	error_msgemptyvaluer    r    r!   nonemptyl   s    rL   c                 C   s   t j| }dt||f d}t|}t|d4}|d}|r\|	| |d}qBW d    n1 sp0    Y  |
 S )Nz%d:%sUTF-8rbi  )r+   r'   normpathlenr   hashlibmd5r   readupdate	hexdigest)r   r'   prefixr>   fdatar    r    r!   	file_hashu   s    


*rY   c              	   C   s   t d u rtdzt jjdd}W n ty>   t j }Y n0 | j|d  | j|d  | j|d  | j	|d  | j
|d  | j|d	  | j|d
  d| _dD ](}z| j| W q ty   Y q0 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	TypeErrorr[   extendr\   r]   r^   r_   r`   ra   languageremoverG   )extZpythran_extZ
bad_optionr    r    r!   update_pythran_extension   s&    rk   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#   ]r2   r1    c                    s,   |   } | d dv r$ | dd  S | S d S )Nr   z'"r2   r#   strip)literal)literalsr    r!   unquote   s    zparse_list.<locals>.unquotec                    s   g | ]}|  r |qS r    ro   )r&   item)rs   r    r!   r*          zparse_list.<locals>.<listcomp>)rP   strip_string_literalsr8   )s	delimiterr    )rr   rs   r!   
parse_list   s    $ry   )namesourcesr^   r_   ra   r`   runtime_library_dirsr[   extra_objectsr\   r]   export_symbolsdependsrh   
np_pythran)startendc                 c   sb   t | trNd}| d|}|dkr4| |d  V  d S | || V  |d }qn| D ]
}|V  qRd S )Nr   
r#   r2   )r   
basestringfind)sourcer   r   liner    r    r!   	line_iter   s    
r   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )DistutilsInfoNc                    s:  i | _ |d urt|D ]܉     s(q d dkr8 q dd    t fdddD d }|d urdd  t|d  d	D \}}}t|d } d
r|d u rq|t	t
fv rt|}|dkrdd |D }|tu rt|}|| j |< qn@|d ur6tD ]0}|dv rqt||d }|r|| j |< qd S )Nr   #r2   c                 3   s   | ]}  |r|V  qd S r   )r$   )r&   kr   r    r!   	<genexpr>   ru   z)DistutilsInfo.__init__.<locals>.<genexpr>)z
distutils:cython:c                 S   s   g | ]}|  qS r    ro   r&   rw   r    r    r!   r*      ru   z*DistutilsInfo.__init__.<locals>.<listcomp>=r   r^   c                 S   s,   g | ]$}d |v r t |d dn|dfqS )r   r2   N)tupler8   )r&   macror    r    r!   r*      s   )rz   r{   r   )valuesr   lstripnextrP   	partitiondistutils_settingsgetr$   listtransitive_listry   bool_orr
   getattr)selfr   exnkindkey_rK   typer    r   r!   __init__   s<    &

zDistutilsInfo.__init__c                 C   s   |d u r| S |j  D ]\}}t| }|tu rD|| j vrD|| j |< q|tu r|| j v r| j | d d  }|D ]}||vrl|| ql|}|| j |< q|tu r| j |d|B | j |< q| S )NF)r   itemsr   transitive_strr   appendr   r   )r   otherr   rK   r   allvr    r    r!   merge  s"    
zDistutilsInfo.mergec                 C   s   |d u r| S t  }| j D ]x\}}t| }|ttfv rzg }|D ]2}||v rT|| }t|trh||7 }q@|| q@|}n||v r|| }||j|< q|S r   )r   r   r   r   r   r   r   r   )r   aliasesresolvedr   rK   r   Znew_value_listr   r    r    r!   subs  s$    

zDistutilsInfo.subsc                 C   sJ   | j  D ]:\}}t| }|ttfv r8t||t| }t||| q
d S r   )r   r   r   r   r   r   setattr)r   	extensionr   rK   r   r    r    r!   apply,  s
    zDistutilsInfo.apply)NN)__name__
__module____qualname__r   r   r   r   r    r    r    r!   r      s   
 r   )	r   qsingle_qdouble_q	hash_markr   r   counter	quote_len__Pyx_Lc                 C   s  g }i }d}d }}d}d } }	}
t | }d}d}||k rH| d|}|	|k r\| d|}	|
|k rp| d|}
t|	|
}|dkrt|	|
}|dkr|dkr|| |d  qq4|r| |d  d	kr
d
}||kr| ||  d	kr|d7 }q|d
 dkr
|d7 }q4| | |kr|dks\||d
 kr|| |d    krX| |d
  krn nh|d7 }d||f }| || | ||< | |||  }|| || || ||7 }d}|}n|d7 }q4d|krl||k s|dkrl|| ||d   | d|}|d7 }d||f }|dkr2d}n|}| |d | ||< || |dkrbq| }}q4||d kr| | | |d    kr| |d
  krn nd}nd}d}| | }|| ||  |}||7 }q4d||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   Fr#   Nr   '"r2   \r   z%s%s_r   r   Tr   )rP   r   minmaxr   join)coderV   Znew_coderr   r   r   r   Zin_quoter   r   r   Zcode_len
quote_typer   r   labelZ
full_quoter   Zend_or_noner    r    r!   rv   4  s    














>
rv   z(?:^\s*from +([0-9a-zA-Z_.]+) +cimport)|(?:^\s*cimport +([0-9a-zA-Z_.]+(?: *, *[0-9a-zA-Z_.]+)*))|(?:^\s*cdef +extern +from +['\"]([^'\"]+)['\"])|(?:^\s*include +['\"]([^'\"]+)['\"])zC(?:^\s+\(([0-9a-zA-Z_., ]*)\)[#\n])|(?:^\s+([0-9a-zA-Z_., ]*)[#\n])c                 C   s   t tj| tt|S r   )normalize_existing0r+   r'   dirnamer   r:   )	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   r;   r   rO   )base_dirr   
normalizedZneeded_baserelr'   r    r    r!   r     s    

r   c                 C   s6   t |}g }| D ] }t||}|d ur|| q|S r   )r   resolve_dependr   )r   r[   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#   >)r;   r   r+   r'   rO   )r   r[   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   abspathstrr   packagebasename)r   r   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 d    n1 s,0    Y  t|}t|\}}|dddd}g }g }g }t|D ]}| \ }	}
} r|	  t
j|| d}|r| \}}|p|}| fdd|d	D  qt|	r|d
d |	d	D  qt|
r|	||
  qt|	||  qt||||fS )Nignore)Zerror_handlingz\
rn   	)posc                 3   s   | ]}d   | V  qdS )z{0}.{1}N)formatrp   r   Zcimport_fromr    r!   r     s   z%parse_dependencies.<locals>.<genexpr>r1   c                 s   s   | ]}|  V  qd S r   ro   )r&   xr    r    r!   r     ru   )r   Zopen_source_filerS   r   rv   replacedependency_regexfinditerr7   r   dependency_after_from_regexsearchr   rg   r8   )source_filenamefhr   distutils_inforr   cimportsincludesexternsr>   Zcimport_listZexternincludeZm_after_from	multilineZone_lineZ
subimportsr    r   r!   parse_dependencies  s2    &

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" 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'   rO   r   )r   r   r    r    r!   r   
  s    z!DependencyTree.parse_dependenciesc                 C   s   t  }| |d D ]~}ttj||}t|s@| j|d }|r|dtjj	 |v r`tj
|}|| || | q| jstd||f  q|S )Nr2   r   z*Unable to locate '%s' referenced from '%s')r:   r   r;   r+   r'   r   r   r   Zfind_include_filer-   rO   r<   rT   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   r:   r   cimports_externs_incdirsrT   r   r<   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   3  s    zDependencyTree.cimportsc                 C   s   t |S r   )r   r   r    r    r!   r   6  s    zDependencyTree.packagec                 C   s   t |S r   )r   r   r    r    r!   r   9  s    z#DependencyTree.fully_qualified_nameNc                 C   s   |d dk}|r|st d|d ur|d}|r<|d t| |}|r|d sz|  W n tyv   Y d S 0 |d qJd|| }| j|d }|r|S |rd S | j|d S )Nr   r   zNew relative imports.)	NotImplementedErrorr8   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   |dd  dkr8t |d d d r8|d d d g}ng }| |D ]<}|d d dksF|dkrdqF| ||}|d urF|| qFt|S )N.pyxz.pxd   zcython.cython)r   r   r  r   r   )r   r   Zpxd_listr   Zpxd_filer    r    r!   cimported_filesT  s    $zDependencyTree.cimported_filesc                 C   s.   t |g}|| | || | |S r   )r:   rT   r  r   )r   r   r   r    r    r!   immediate_dependenciesd  s    
z%DependencyTree.immediate_dependenciesc                 C   s   |  || jtjS r   )transitive_merger  r:   unionr   r    r    r!   all_dependenciesk  s    zDependencyTree.all_dependenciesc                 C   s   t j|S r   )r+   r'   getmtimer   r    r    r!   	timestampn  s    zDependencyTree.timestampc                 C   s   |  ||fS r   )r  r   r    r    r!   extract_timestampr  s    z DependencyTree.extract_timestampc                    s   t  fdd |D S )Nc                    s   g | ]}  |qS r    )r  )r&   rW   r   r    r!   r*   v  ru   z4DependencyTree.newest_dependency.<locals>.<listcomp>)r   r
  r   r    r  r!   newest_dependencyu  s    z DependencyTree.newest_dependencyc              	   C   s   zt td}|t|d t| |D ],}tj	
|d dvr4|t|d q4|t|jt|ddt|ddfd || d | W S  ty   Y dS 0 dS )a;  
        Return a fingerprint of a cython file that is about to be cythonized.

        Fingerprints are looked up in future compilations. If the fingerprint
        is found, the cythonization can be skipped. The fingerprint must
        incorporate everything that has an influence on the generated code.
        rM   r2   ).c.cppz.hZpy_limited_apiFr   N)rQ   rR   r   r   rT   rY   sortedr
  r+   r'   r   r   rh   r   Zget_fingerprintrU   IOError)r   r   r   Zcompilation_optionsr>   r   r    r    r!   transitive_fingerprintx  s"    


z%DependencyTree.transitive_fingerprintc           
      C   s   |  |d }|j}| |\}}}t }|rjt||}d|v r^tt|d ||d< nt||d< |rt|	dg }t||D ]}	|	|vr|
|	 q||d< |S )Nr   r   r[   )r   r   r   r+   r,   r/   r   r:   r	  r   r   )
r   r   infokwdsr   r   r   Zbasedirr[   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   sR   z| j ||f }W n$ ty6   i  }| j ||f< Y n0 | ||||i | jd S r   )r   KeyErrortransitive_merge_helperr  )r   nodeextractr   rB   r    r    r!   r    s    zDependencyTree.transitive_mergec              	   C   s   ||v r|| d fS ||}||v r,||fS zt |||< d }||D ]L}	| |	|||||\}
}|d ur|d ur|| || k rn|}|||
}qF||krd }|d u r|||< ||fW ||= S ||= 0 d S r   )rP   r  )r   r  r  r   rB   stackoutgoingdeps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!   r     s2   






r   Fc                 C   s0   t d u r,| d u r tdgtt} t| |da t S )Nr   r   )	_dep_treer   r   r   r   )ctxr   r    r    r!   create_dependency_tree  s
    r$  c                 C   sj   d|v r>| dg dg }t|d |}tt|| j |d< | j}|f i |}t||d d}||fS )Nr   r[   r   rz   )	distutilsmodule_name)r   r   r  r:   r   	__class__dict)templater  r[   r   trj   metadatar    r    r!   default_create_extension  s    r,  c           "   
      s  |d urt d| |d u r"g }| d u r2g i fS t| tsFt| tsL| g} tdd | D }t }t||d}	t }
t|ts|g}|D ]}|
t	t
jjt| qg }i }dtjv rtjd j}tjd j}nt}G dd dt}|jjpt}| D ]}t|tr&| t|g }d	}d }|}nt|||frd
d |jD }|rv|d  t|dkrt d|j|f  n|| q|}|j}t|d}d }n$tdttt||jf }t|tt t d  D ]
}t
j||
v rq|	!|}d	|v r||v r qn
|r |}t"#| ||vrz|	$|||j%}W n" t&yj   |rdY qւ Y n0 |d ur|j%' D ]\}}||vr|||< q||d< |g fdd|jD  }d|v r|d D ]"}t(|}||vr|| q||d< |rd|vr||d< |)dd}|||\}} |p@t*|dd|_+|j+rTt,| || | ||< ||jvrt
j-|d |j.dkrdnd }!z|j/|! W n  t0y   t d|  Y n0 |j1d| |2| qq||fS )NzWarning: passing language={0!r} to cythonize() is deprecated. Instead, put "# distutils: language={0}" in your .pyx or .pxd file(s)c                 S   s   g | ]}t |tr|jqS r    )r   r	   rz   r&   r>   r    r    r!   r*     ru   z)create_extension_list.<locals>.<listcomp>r!  
setuptoolszsetuptools.extensionc                   @   s   e Zd ZdS )z3create_extension_list.<locals>.Extension_setuptoolsN)r   r   r   r    r    r    r!   Extension_setuptools  ru   r/  r4   c                 S   s$   g | ]}t j|d  dv r|qS )r2   ).pyr  r+   r'   r   r   r    r    r!   r*     s   r   r2   zWarning: Multiple cython sources found for extension '%s': %s
See http://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html for sharing declarations among Cython files.)r   zUpattern is not of type str nor subclass of Extension (%s) but of type %s and class %sz'%s' doesn't match any filesrz   c                    s   g | ]}| kr|qS r    r    r-  Zfilepatternr    r!   r*   J  ru   r{   rh   r   Frb   r  r  z@Warning: Cython source file not found in sources list, adding %s)3r   r   r   r   r   r:   r$  r   rT   mapr+   r'   r   r9   sysmodulesZ
_Extensionr	   optionscreate_extensionr,  r   r{   rP   rz   r   r   reprr   r'  rf   rL   r  r   r   Z$raise_error_if_module_name_forbiddenr   r   	Exceptionr   r"   r   r   r   rk   r   rh   ri   rG   insertr<   )"patternsexcluder#  r   r   rh   exclude_failuresZexplicit_modulesrB   r  Z
to_excluder=   module_listmodule_metadataZExtension_distutilsr/  r7  r)  rz   r)   Zext_languageZcython_sourcesmsgfiler&  r  r   rK   r{   r   r   r>   r+  Ztarget_filer    r2  r!   create_extension_list  s    















$rB  c           4         sV  |du rg }d|vrdg|d< d|v r2t |d  |dd}	tdu rLd}
ntf i |}
d|
_d|
_tf i |}tf i |}d|_| }|}t| ||||||d\} }t||d}t	|d	d t
 f fd
d	}tt}g }| D ]} r|jD ]}|| qdd |jD }t|dkr2|j}nd}g }|jD ]}t
j|\}}|dv r|jrv|d }|
}n&|jdkr|d }|}n|d }|} rt
j|rtd t
j |}t
j|}t| |	r||}t
j|\}}|t
js|t
j7 }g } |D ]N}!|! |s@|! dt
jj rV| !t
j"|!| n| !t
j#|! qt
j|d d }"|"d| d 7 }"t$|d d}#|#%|" W d   n1 s0    Y  t
j&|rt
j'|}$nd}$|$|(|k r|(|| }%}d}&n |)|\}%}d||*|v  }&|sB|$|%k r|sv|sv||krft+d|  nt+d||f  |s|j,r|-|||}'nd}'|!|&|||'||| |.|j|f	 |!| || !| n|!|  r@|| q@||_q|j,r t
j&|j,s t
/|j, |0  t|}(dtt1|( })t2|(D ]0}*|)3|*d |(}+||* dd |+f ||*< qH|(dkrd}|rFzddl4},|,j5|t6d}-W n$ t7t8fy   t+d  d}Y nz0 zP|-j9t:|dd!}.|-;  |.< sz|..d" W n |,j=y   Y n0 qW n t>y<   |-?   Y n0 |-  |s`|D ]}/t@|/  qP|r,tA }0|B D ]z\}}1t
j&|s|0C|1 nVt
jD|d#k rttE|d$d%d&}2z*|2Ftd'd'kr|0C|1 W |2;  n
|2;  0 qt|0r,|0D ]}3| G|3 qt+d(d)tHd*d |0D   |j,rHtI|j,t	|d+d, tJjKL  | S )-am  
    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 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 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.
    Nr   r   Zcommon_utility_include_dirdepfileT)r<  r#  r   r=  rh   r   r!  	build_dirc                    sL   t j| }t j| r|} ||rHt t jt| |}t|| d S r   )	r+   r'   r   r   r$   r;   r   r%   copy_once_if_newer)filepathrE   Zfilepath_absZmod_dirrD  r    r!   copy_to_build_dir  s    
z$cythonize.<locals>.copy_to_build_dirc                 S   s$   g | ]}t j|d  dv r|qS )r2   r  r0  r1  )r&   r   r    r    r!   r*     s   zcythonize.<locals>.<listcomp>r2   rI  r  rb   r  z1build_dir has no effect for absolute source pathsz: \
  z \
  r   z.depwr#   r   r   z Compiling %s because it changed.z&Compiling %s because it depends on %s.z[{0:%d}/{1}] )Zinitializerz3multiprocessing required for parallel cythonization)	chunksizei    rz	iso8859-1)encodingz#error zFailed compilations: %sz, c                 S   s   g | ]
}|j qS r    )rz   )r&   r   r    r    r!   r*   y  s   Z
cache_sizei  @)Mr   r   rc   r   Zcplusr   Zcreate_contextrB  r$  r   r+   r,   collectionsdefaultdictr   r   r{   rP   rz   r'   r   rh   r   warningswarnr   r   safe_makedirs_oncer
  r8   endswithr-   r$   r   r   r   r   writeexistsr  r  r  r  r   cacher  r   makedirssortr   ranger   multiprocessingZPool_init_multiprocessing_helperImportErrorOSErrorZ	map_asynccythonize_one_helperclosereadyTimeoutErrorKeyboardInterrupt	terminatecythonize_oner:   r   rT   getsizeio_openrS   ri   r  cleanup_cacher4  stdoutflush)4r>  r<  Znthreadsr   r   forcerh   r=  r6  rC  Zpythran_optionsZ	c_optionsZcpp_optionsr#  r?  r  rH  Zmodules_by_cfileZ
to_compiler>   depZ
cy_sourcesfull_module_nameZnew_sourcesr   r)   rj   c_filer   dependenciesZsrc_base_dirr   pathsfnameZdeplineoutfileZc_timestampZdep_timestamppriorityfingerprintNZprogress_fmtiprogressr[  poolresultargsZfailed_modulesr5  rW   r   r    rG  r!   	cythonizeq  s:   E










*








r{  ZXML_RESULTSc                    s    fdd}|S )Nc                     s
  t   }d}zz |   W n   d}Y n0 W t   | }t| d }d| }d| }|r`d}nd}ttjt|d d	}|d
 t	   |
  nlt   | }t| d }d| }d| }|rd}nd}ttjt|d d	}|d
 t	   |
  0 d S )NTFr   z
cythonize.r2   r   Zfailurez.xmlrJ  aQ  
                    <?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>
                )timer   r   r+   r'   r   compile_result_dirrU  rp   localsr`  )rz  r*  successr   rz   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    Tc	              
      s  ddl m}	m}
 ddlm}m} |rtj|j	s>t
|j	 t|j	dtj||f }|t }|d }tj|stj|r|std|| f  tj|r"t|d  tt|dL}tt|d}t|| W d    n1 s0    Y  W d    n1 s0    Y  npt|d  tj|}tt|6}| D ]}||tj|| qTW d    n1 s0    Y  d S |std	|| f  |d u rt|
}||_||_d
}z"|	| ||d  jd
krd}W n| t |fy> } z0t!j"#d|  d}d
d l$}|%  W Y d }~n8d }~0  t&yl   |rT d
d l$}|%  d}Y n0 |r|r|d | ntj|rt'| n|rt(t)d  fdddD }t*|dkrL|}tt|dR}tt|d d}t|| W d    n1 s 0    Y  W d    n1 s@0    Y  n^|}tt|d dt+0}|D ]}|#|tj| qnW d    n1 s0    Y  t,|d | d S )Nr   )compile_singler   )CompileError
PyrexErrorz%s-%sz.zipz%sFound compiled %s in cacherN   wbz%sCythonizing %sr   )rm  r2   z%s
c                    s   g | ]}t  |d qS r   )r   )r&   attrry  r    r!   r*     s   z!cythonize_one.<locals>.<listcomp>)rn  Zh_fileZapi_fileZi_filez.tmprJ  )-Compiler.Mainr  r   ZCompiler.Errorsr  r  r+   r'   rV  rW  r   r;   r   gzip_extr   utime
contextlibclosing	gzip_openr   shutilcopyfileobjr   zipfileZipFilenamelistr  r   r   Zoutput_fileembedded_metadataZ
num_errorsEnvironmentErrorr4  stderrrU  	traceback	print_excr9  ri   r   filterrP   zipfile_compression_moderename)Zpyx_filern  rt  r   r6  Zraise_on_failurer  rm  rw  r  r   r  r  Zfingerprint_file_baseZgz_fingerprint_fileZzip_fingerprint_filegrW   r   zartifactZany_failureser  Z	artifactsZfingerprint_filezipr    r  r!   re    s    
L:


N
8re  c                 C   s4   dd l }z
t|  W S  ty.   |   Y n0 d S r   )r  re  r9  r  )r>   r  r    r    r!   r_    s    
r_  c                  C   s   dd l } |  | j| j d S r   )signalSIGINTSIG_IGN)r  r    r    r!   r\     s    r\  333333?c              	   C   s  z`t jdddtj| gt jd}| }|dkr^dt|j	 
  d  }||k r^W d S W n ttfyv   Y n0 d}g }t| D ]6}t| |}t|}	||	j7 }||	j|	j|f q||krtt|D ].\}
}}t| ||8 }||| k r qqd S )NZduz-sz-k)ri  r   i   )
subprocessPopenr+   r'   r   PIPEwaitintri  rS   rp   r8   r^  rG   listdirr;   statst_sizer   st_atimereversedr  unlink)rW  Ztarget_sizeZratiopresZ
total_sizer   rA  r'   rw   r|  sizer    r    r!   rh    s,     





rh  )N)rF   )r   )NF)NNNFNF)Nr   NFFNF)NTNNr   )r  )j
__future__r   r   r  r   r   rO  r  rQ   r+   r  r  r5   r4  r|  rQ  globr   ior   rg  os.pathr   r%   distutils.extensionr	   distutils.utilr
   r  collections.abcr   r]  gzipr  r  zlibZIP_DEFLATEDr  
ZIP_STOREDrc   r   r   r   r   r   r   r   r   r  r   r   r   r'   r   r;   rE  rS  version_infogetfilesystemencodingr   getdefaultencodingr"   r   r   r/   r9   rL   rY   rk   ry   objectr   r   r   r   r   r~  Z
Py_ssize_tr   r   rv   compileMr   r   r   r   r   r   r   r   r   r   r"  r$  r,  rB  r{  environr   r}  r  re  r_  r\  rh  r    r    r    r!   <module>   s  


$


	
	

TZ






" Q
  
   
  

   M	