Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/__pycache__/Nodes.cpython-35.pyc



Z��Z~��'@s�ddlmZddlZejdedededededed	ed
ededed
edededededededede�ddlZddlZddlZddlm	Z	ddl
mZddlm
Z
mZmZmZddl
mZddl
mZddl
mZddlmZmZddlmZmZmZmZmZmZmZddlmZdd l m!Z!dd!l
m"Z"dd"l
m#Z#dd#l
m$Z$dd$l%m&Z&m'Z'm(Z(d%d&l)m*Z*ej+dd'kre,Z-ne,e.fZ-d(d)�Z/d*d+�Z0dd,d-�Z1d.d/�Z2Gd0d1�d1e3�Z4Gd2d3�d3e3�Z5d4d5�Z6e6Gd6d7�d7e��Z7Gd8d9�d9e7�Z8Gd:d;�d;e�Z9Gd<d=�d=e7�Z:Gd>d?�d?e7�Z;Gd@dA�dAe;�Z<GdBdC�dCe7�Z=GdDdE�dEe=�Z>GdFdG�dGe=�Z?GdHdI�dIe=�Z@GdJdK�dKe=�ZAGdLdM�dMe=�ZBGdNdO�dOe=�ZCGdPdQ�dQe7�ZDGdRdS�dSe7�ZEGdTdU�dUe7�ZFGdVdW�dWeE�ZGGdXdY�dYeE�ZHGdZd[�d[eE�ZIGd\d]�d]eE�ZJGd^d_�d_eE�ZKGd`da�daeE�ZLGdbdc�dceE�ZMGddde�deeE�ZNGdfdg�dge;�ZOGdhdi�die;�ZPGdjdk�dkePe9�ZQGdldm�dme;�ZRGdndo�doe;�ZSGdpdq�dqe;�ZTGdrds�dse;e9�ZUGdtdu�dueU�ZVGdvdw�dwe7�ZWGdxdy�dye7�ZXGdzd{�d{eU�ZYGd|d}�d}eU�ZZGd~d�deY�Z[Gd�d��d�e[�Z\Gd�d��d�e\�Z]Gd�d��d�e\�Z^Gd�d��d�eY�Z_Gd�d��d�e;�Z`Gd�d��d�e;e9�ZaGd�d��d�ea�ZbGd�d��d�ea�ZcGd�d��d�e;�ZdGd�d��d�e;�ZeGd�d��d�e;�ZfGd�d��d�e;�ZgGd�d��d�e;�ZhGd�d��d�eh�ZiGd�d��d�eh�ZjGd�d��d�eh�ZkGd�d��d�eh�ZlGd�d��d�e;�ZmGd�d��d�e;�ZnGd�d��d�e;�ZoGd�d��d�e;�ZpGd�d��d�e:�ZqGd�d��d�e;�ZrGd�d��d�e;�ZsGd�d��d�e;�ZtGd�d��d�e;�ZuGd�d��d�e;�ZvGd�d��d�e;�ZwGd�d��d�e;�ZxGd�d��d�e7�ZyGd�d��d�e;�ZzGd�d��d�e;�Z{Gd�d��d�e�Z|Gd�d��d�e|e;�Z}Gd�d��d�e7�Z~Gd�d��d�e7�Zd�d��Z�Gd�d��d�e|e;�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e|e;�Z�Gd�d��d�e;�Z�Gd�d��d�eh�Z�Gd�d��d�e;�Z�Gd�d��d�e7�Z�Gd�d��d�e;�Z�Gd�d��d�e��Z�Gd�d��d�e��Z�Gd�d��d�e;�Z�Gd�d��d�e��Z�d�d��Z�d�d�d��d�d�d��d�e�iZ�d�d�d�d�fd�d�d�d�fiZ�Gd�d��d�e;�Z�Gd�d��d�e;�Z�Gd�d��d�e;�Z�Gd�d��d�e7�Z�Gd�d��d�e;e��Z�Gd�d��d�e��Z�Gd�d�de��Z�Gdd�de;�Z�e#j�r(dZ�ndZ�ej�dd�Z�ej�dd�Z�ej�dd	�Z�ej�d
d	�Z�ej�dd	�Z�ej�dd	�Z�ej�d
d	�Z�ej�dd	�Z�eddddde�g�Z�dS(�)�absolute_importN�sys�os�copy�Builtin�error�warning�Naming�
PyrexTypes�py_object_type�ModuleScope�
LocalScope�ClosureScope�StructOrUnionScope�PyClassScope�
CppClassScope�UtilityCode�
EncodedString�
error_type�
_py_int_types)�chain�)r)rr�
InternalError�CompileError)r	)r
)�	TypeSlots)rr)rr
rrrr�
TemplateScope)r)r)�Future)�Options)�
DebugFlags)�has_np_pythran�pythran_type�is_pythran_buffer�)�
add_metaclass�cCs|dj�|dfS)Nrr)Zget_filenametable_entry)�pos�r&�H/opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/Nodes.py�relative_position)sr(cCs�tjs
|Sdt|�}|dkr3t|�S|j}|dk	rty|j|�Wntk
rsd}YnX|s�t|�}nt|d|�}||_|S)NzFile: %s (starting at line %s)�
)rZembed_pos_in_docstringr(r�encoding�encode�UnicodeEncodeError)r%Z	docstringZpos_liner*�docr&r&r'�embed_position-s 	
	
	r.cCsd}d}d}}|jr�t|jd�xb|jD]W\}}|jsQq9|jdkrud}|s�|}q9|jdkr9d}|}q9W|r�|r�t|jd�|j|�}	|jrl|jrl|j	dkrl|dk	r-|	dk	r-|	j
r-|j|�}
|
r-|
j
r-d}d}	|	tj
tjtjfkr�|j	dkrctjnt}	n%|	dk	r�|jr�t|jd�|	dk	r�|r�|r�|	j
r�t|jd
�t|jd|	dd�}n)|r�t|jd�nt|jd�||	fS)NFzSDicts should no longer be used as type annotations. Use 'cython.int' etc. directly.�type�typeT�ctype�ctypez9Duplicate type declarations found in signature annotation�int�long�floatzWStrings should no longer be used for type declarations. Use 'cython.int' etc. directly.zOPython type declaration in signature annotation does not refer to a Python type�is_argz'Ambiguous types in annotation, ignoringz0Unknown type declaration in annotation, ignoring)r/r0)r1r2)r3r4r5)�is_dict_literalrr%�key_value_pairsZis_string_literal�value�analyse_as_type�is_nameZcython_attribute�name�is_pyobjectZ
infer_typer
�c_long_type�
c_int_type�c_float_typeZ
c_double_typer�CAnalysedBaseTypeNode)�
annotation�env�assigned_value�	base_typeZis_ambiguousZexplicit_pytypeZexplicit_ctyper<r9�arg_typeZ
assigned_typer&r&r'�analyse_type_annotationGsP
				
""!		rGcs��fdd�}|S)Ncs1t|�dkr t|d��r |dd�\}}dd|j|jj�j|jdd�f}|jjj�}|j	|�|jjj�}|jd7_�||�}|jd8_||jjj�kr�|jjj
|�n"|jddd�}|j	|�|S�||�SdS)Nrr"z(                    /* %s -> %s.%s %s */� �z->z<-)�len�
isinstanceZ
call_level�	__class__�__name__r%�buffer�stream�tell�putln�truncate�replace)�args�kwds�node�code�markerZpristine�start�res)�codewriter_class�funcr&r'�fws&%
	

zwrite_func_call.<locals>.fr&)r\r[r]r&)r[r\r'�write_func_callvsr^cs"eZdZ�fdd�Z�S)�VerboseCodeWritercs�ddlm}ddlm}t|�}x<|j�D].\}}t||�r9t||�||<q9Wtt	|�j
||||�S)Nr)�FunctionTyper)�CCodeWriter)�typesr`�Codera�dict�itemsrKr^�superr_�__new__)�clsr<�bases�attrsr`ra�mname�m)rLr&r'rg�szVerboseCodeWriter.__new__)rM�
__module__�__qualname__rgr&r&)rLr'r_�sr_cs=eZdZdZedddg�Z�fdd�Z�S)�CheckAnalyserszCMetaclass to check that type analysis functions return a node.
    �
analyse_types�analyse_expressions�analyse_target_typescs�ddlm}dd�}t|�}xK|j�D]=\}}t||�r5||jkr5|||�||<q5Wtt|�j||||�S)Nr)r`cs��fdd�}|S)Ncs6�||�}|dkr2td�||f�|S)Nz%s %s %s)�print)rT�kwargs�retval)r\r<r&r'�call�sz3CheckAnalysers.__new__.<locals>.check.<locals>.callr&)r<r\rvr&)r\r<r'�check�sz%CheckAnalysers.__new__.<locals>.check)	rbr`rdrerK�methodsrfrorg)rhr<rirjr`rwrkrl)rLr&r'rg�szCheckAnalysers.__new__)rMrmrn�__doc__�setrxrgr&r&)rLr'ro�s
rocCstjrtt�|�S|S)N)rZdebug_trace_code_generationr#r_)rhr&r&r'�_with_metaclass�s	r{c@seZdZdZdZdZdZdZdZdZ	dZ
dZdZdd�Z
dZdZddd�ZdZd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd dddd�Zdddd�ZdS)!�NoderFNcKs||_|jj|�dS)N)r%�__dict__�update)�selfr%�kwr&r&r'�__init__�s	z
Node.__init__Z	OperationcCst|jd|j�dS)Nz%s not allowed without gil)rr%�gil_message)rrCr&r&r'�	gil_error�szNode.gil_errorcCs|j�s|j�dS)N)Zis_cpp�	cpp_error)rrCr&r&r'�	cpp_check�szNode.cpp_checkcCst|jd|j�dS)Nz%s only allowed in c++)rr%�cpp_message)rr&r&r'r��szNode.cpp_errorcCsbtj|�}xL|jD]A}t||�}t|t�rt||dd�|D��qW|S)acClone the node. This is defined as a shallow copy, except for member lists
           amongst the child attributes (from get_child_accessors) which are also
           copied. Lists containing child nodes are thus seen as a way for the node
           to hold multiple children directly; the list is not treated as a separate
           level in the tree.cSsg|]}|�qSr&r&)�.0�xr&r&r'�
<listcomp>�s	z#Node.clone_node.<locals>.<listcomp>)r�child_attrs�getattrrK�list�setattr)r�result�attrnamer9r&r&r'�
clone_node�s!zNode.clone_nodecCsdS)Nr&)rrCr&r&r'�analyse_declarationsszNode.analyse_declarationscCstd|jj��dS)Nz*analyse_expressions not implemented for %s)rrLrM)rrCr&r&r'rqszNode.analyse_expressionscCstd|jj��dS)Nz$generate_code not implemented for %s)rrLrM)rrWr&r&r'�
generate_codeszNode.generate_codecCs#t|t�r|jj|�dS)N)rK�	BlockNode�body�annotate)rrWr&r&r'r�sz
Node.annotatecCs�y|jSWn�tk
r�|j}|js:||_|Sx||jD]q}t||�}|dkrhqDt|t�r�x;|D]}t||j��}q~WqDt||j��}qDW||_|SYnXdS)N)	Z_end_pos�AttributeErrorr%r�r�rKr��max�end_pos)rr%�attr�child�cr&r&r'r�#s"
			
	zNode.end_posr%�dc
	sD�dkrdS�dkr%t��t|��krQd|jjt|�fS�jt|������fdd���fdd�|jj�D�}t|�dkr�d	|jjt|�fSd
|}d|jjt|�f}x8|D]0\}}	|d||�|	|d
�f7}q�W|d|7}|SdS)zYDebug helper method that returns a recursive string representation of this node.
        rz<...nesting level cutoff...>Nz<%s (0x%x) -- already output>csmt|t�r)|j���d��St|t�r_ddj��fdd�|D��St|�SdS)Nrz[%s]z, csg|]}�|���qSr&r&)r��item)�
dump_child�levelr&r'r�Gs	z1Node.dump.<locals>.dump_child.<locals>.<listcomp>)rKr|�dumpr��join�repr)r�r�)�cutoffr��encountered�
filter_out)r�r'r�Cs
'zNode.dump.<locals>.dump_childcs.g|]$\}}|�kr||f�qSr&r&)r��keyr9)r�r&r'r�Ks	zNode.dump.<locals>.<listcomp>z<%s (0x%x)>z  z<%s (0x%x)
z%s  %s: %s
rz%s>)rz�idrLrM�addr}rerJ)
rr�r�r�r�rj�indentrZr�r9r&)r�r�r�r�r'r�8s"	"
(z	Node.dumpz(#)c	Cs�|js
dS|j\}}}|jdddd�}|td|d�|�}|d
}|r�|d	|�|||d	�}|j�d
|d<||||d�7}d|j�||dj|�fS)z[Debug helper method that returns the source code context of this node as a string.
        �r*�ASCIIZerror_handling�ignorerr$rNz             # <<<<<<<<<<<<<<
r"z"%s":%d:%d
%s
���r�)r%�	get_linesr��rstripZget_escaped_descriptionr�)	rZmark_columnrXZsource_desc�line�col�contents�lines�currentr&r&r'�dump_posVs	
"z
Node.dump_pos)r%)rMrmrnr;�is_noneZis_nonecheck�
is_literal�
is_terminator�
is_wrapper�tempsr�Zcf_stateZ
coercion_typer�r��nogil_checkr�r�r�r�r�r�rqr�r�r�r�r�r&r&r&r'r|�s2%r|c@s[eZdZdZdgZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
S)�CompilerDirectivesNodez9
    Sets compiler directives for the children nodes
    r�cCs2|j}|j|_|jj|�||_dS)N)�
directivesr�r�)rrC�oldr&r&r'r�ps	z+CompilerDirectivesNode.analyse_declarationscCs7|j}|j|_|jj|�|_||_|S)N)r�r�rq)rrCr�r&r&r'rqvs
		z*CompilerDirectivesNode.analyse_expressionscCsP|j}|jj}|j|j_|jj||�||_||j_dS)N)r��globalstater��generate_function_definitions)rrCrWZenv_oldZcode_oldr&r&r'r�}s		z4CompilerDirectivesNode.generate_function_definitionscCs;|jj}|j|j_|jj|�||j_dS)N)r�r�r��generate_execution_code)rrWr�r&r&r'r��sz.CompilerDirectivesNode.generate_execution_codecCs;|jj}|j|j_|jj|�||j_dS)N)r�r�r�r�)rrWr�r&r&r'r��szCompilerDirectivesNode.annotateN)
rMrmrnryr�r�rqr�r�r�r&r&r&r'r�gs	r�c@s(eZdZdd�Zdd�ZdS)r�cCsA|j�j}x|D]}|jj|�qW|dd�=dS)N)�global_scopeZundeclared_cached_builtinsr�Zadd_cached_builtin_decl)rrCrW�entries�entryr&r&r'�generate_cached_builtins_decls�s
z(BlockNode.generate_cached_builtins_declscCs(x!|jD]}|j||�q
WdS)N)Zlambda_defsr�)rrCrWrVr&r&r'�generate_lambda_definitions�sz%BlockNode.generate_lambda_definitionsN)rMrmrnr�r�r&r&r&r'r��sr�c@sgeZdZdgZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dS)�StatListNode�statscOst|||�}|S)N)r�)r%rCrTr�rVr&r&r'�create_analysed�szStatListNode.create_analysedcCs%x|jD]}|j|�q
WdS)N)r�r�)rrC�statr&r&r'r��sz!StatListNode.analyse_declarationscs#�fdd�|jD�|_|S)Ncsg|]}|j���qSr&)rq)r�r�)rCr&r'r��s	z4StatListNode.analyse_expressions.<locals>.<listcomp>)r�)rrCr&)rCr'rq�sz StatListNode.analyse_expressionscCs(x!|jD]}|j||�q
WdS)N)r�r�)rrCrWr�r&r&r'r��sz*StatListNode.generate_function_definitionscCs5x.|jD]#}|j|j�|j|�q
WdS)N)r��mark_posr%r�)rrWr�r&r&r'r��sz$StatListNode.generate_execution_codecCs%x|jD]}|j|�q
WdS)N)r�r�)rrWr�r&r&r'r��szStatListNode.annotateN)rMrmrnr��staticmethodr�r�rqr�r�r�r&r&r&r'r��s	r�c@s(eZdZdd�Zdd�ZdS)�StatNodecCsdS)Nr&)rrCrWr&r&r'r��sz&StatNode.generate_function_definitionscCstd|jj��dS)Nz.generate_execution_code not implemented for %s)rrLrM)rrWr&r&r'r��sz StatNode.generate_execution_codeN)rMrmrnr�r�r&r&r&r'r��s
r�c@sIeZdZdgZdd�Zdd�Zdd�Zdd	�Zd
S)�CDefExternNoder�cCs�|j}d|_|jj|�||_|js=|jr�|jj}|jds_d}n(|snd}ntdd�|D��}|j|j|j|�dS)NrZpreliminary_late_includes_cy28Fcss|]}t|t�VqdS)N)rK�CVarDefNode)r�rVr&r&r'�	<genexpr>�sz6CDefExternNode.analyse_declarations.<locals>.<genexpr>)	�in_cincluder�r�Zinclude_fileZverbatim_includer�r��all�add_include_file)rrCZold_cinclude_flagr�Zlater&r&r'r��s			
		z#CDefExternNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rq�sz"CDefExternNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz&CDefExternNode.generate_execution_codecCs|jj|�dS)N)r�r�)rrWr&r&r'r��szCDefExternNode.annotateN)rMrmrnr�r�rqr�r�r&r&r&r'r��s
	r�c@s(eZdZgZdZdd�ZdS)�CDeclaratorNoder�cCsdS)Nr&)rr&r&r'�analyse_templatessz!CDeclaratorNode.analyse_templatesN)rMrmrnr��calling_conventionr�r&r&r&r'r��s
r�c@s4eZdZdgZdZddddd�ZdS)�CNameDeclaratorNode�defaultNrFcCs�|r�|jdkr�|js0|js0|jrCt|jd�n@|jr_t|jd�n$|jddddd�|_t}|j	r�|j
r�|j|j
�}||_||fS)Nr�zMissing argument namezJUse spam() rather than spam(void) to declare a function with no arguments.Zfor_displayr�pyrex)
r<�is_ptr�is_array�	is_bufferrr%�is_void�declaration_coder�is_fused�fused_to_specific�
specializer/)rrErC�nonempty�
visibility�in_pxdr&r&r'�analyses		zCNameDeclaratorNode.analyse)rMrmrnr�r�r�r&r&r&r'r�s	r�c@s:eZdZdgZdd�Zddddd�ZdS)	�CPtrDeclaratorNode�basecCs
|jj�S)N)r�r�)rr&r&r'r�1sz$CPtrDeclaratorNode.analyse_templatesrNFc	CsM|jrt|jd�tj|�}|jj||d|d|d|�S)Nz+Pointer base type cannot be a Python objectr�r�r�)r=rr%r
�
c_ptr_typer�r�)rrErCr�r�r�Zptr_typer&r&r'r�4s	zCPtrDeclaratorNode.analyse)rMrmrnr�r�r�r&r&r&r'r�,s	r�c@s:eZdZdgZdd�Zddddd�ZdS)	�CReferenceDeclaratorNoder�cCs
|jj�S)N)r�r�)rr&r&r'r�@sz*CReferenceDeclaratorNode.analyse_templatesrNFc	CsM|jrt|jd�tj|�}|jj||d|d|d|�S)Nz-Reference base type cannot be a Python objectr�r�r�)r=rr%r
Z
c_ref_typer�r�)rrErCr�r�r�Zref_typer&r&r'r�Cs	z CReferenceDeclaratorNode.analyse)rMrmrnr�r�r�r&r&r&r'r�;s	r�c@s1eZdZddgZddddd�ZdS)�CArrayDeclaratorNoder��	dimensionrNFcs�|jr|j�s|jr�ddlm}t|j|�rO|jj}n|jf}�fdd�|D�}d|kr�|jd�}	t	||	j
d�t}n|j|j
|�}|j
j|�d|d|d|�S|jri|jj��|_|jjjs&t	|jj
d	�|jj�}
|
dk	royt|
�}
Wqotk
reYqoXnd}
|j�s�t	|j
d
|�|jr�t	|j
d�|jr�t	|j
d�tj||
�}|j
j|�d|d|d|�S)
Nr)�	TupleNodecsg|]}|j���qSr&)r:)r��v)rCr&r'r�Ws	z0CArrayDeclaratorNode.analyse.<locals>.<listcomp>zTemplate parameter not a typer�r�r�zArray dimension not integerz%Array element type '%s' is incompletez'Array element cannot be a Python objectz"Array element cannot be a function)�is_cpp_class�is_template_type�is_cfunction�	ExprNodesr�rKr�rT�indexrr%r�specialize_herer�r��analyse_const_expressionr/�is_int�get_constant_c_result_coder3�
ValueError�is_completer=r
Zc_array_type)rrErCr�r�r�r�rT�values�ix�sizeZ
array_typer&)rCr'r�Ps>	%	
		zCArrayDeclaratorNode.analyse)rMrmrnr�r�r&r&r&r'r�Jsr�c@sjeZdZdddgZdZdZdZdZdd�Zddddd	d
�Z	ddd�Z
dS)
�CFuncDeclaratorNoder�rT�exception_valuerNcCs�t|jt�r�ddlm}m}|jj}t||�rO|j}n/t||�rj|g}nt|j	d�dSg|_
xL|D]D}t||�r�|j
jtj
|j��q�t|j	d�q�W|jj|_|j
SdSdS)Nr)r��NameNodez*Template arguments must be a list of names)rKr�r�r�r�r�r�rTrr%�	templates�appendr
�TemplatePlaceholderTyper<)rr�r��
template_nodeZtemplate_nodes�templater&r&r'r��s"	
z%CFuncDeclaratorNode.analyse_templatesFcs:|dkri}|r"|d8}g}x�t�j�D]�\}}	|	j�d|d|dkow�jowd�jk�\}
}|
j}||kr||}
|
j��}|dkr�t|
jd�nK|t	j
k	r|j|�rt�jjd�t|
jd�n|}|
j
r4t�jd	�|dkr[�jr[|jr[�j}|jrvt	j|j�}|jr�t|	jd
�|jt	j|||	j��|	jr��jd7_q8�jr8t�jd�q8Wd}d}�jdkr5�jd
��jd��jd��jd�|jrr�jsP�jrr�jdkrrt�jd�n��jdkr��jr��jdkr�|jdk	r�|dkr�|r��js�ddlm}|�jd|jd|��_�jr��jj���_�jdkr��jj}|j rv|jrv|j!o_|j"jo_|jrvt�jjd��j}n}�jj#|��j���_�jj$�}|dkr�t%d�jj&j'��|j(�jj�s�t�jjd��j}|j!r!t�jd�t	j)||�j*d�jd|d|d�jj+d�j,d�j-d �j.d!�j/d"�j0�	}�jr�|j1r���fd#d$�}||_2n�j3|���jd%}|r|j+}|r||krt�jd&||f�||_+�jj|�d'|d(|�S))Nrr��is_self_argrr�z
Not a typez2Signature does not agree with previous declarationzPrevious declaration herez2Function argument cannot have C name specificationzJUse spam() rather than spam(void) to declare a function with no arguments.z-Non-default argument follows default argument�+Zios�newZ	stdexceptZtypeinfozAException clause not allowed for function returning Python object�extern)�	ConstNoder9r/zNException value must be a Python exception or cdef function with no arguments.z1get_constant_c_result_code not implemented for %sz6Exception value incompatible with function return typez!Function cannot return a function�optional_arg_countr��exception_checkr��nogil�with_gil�is_overridable�is_const_methodr�cs�j|�|�dS)N)�declare_optional_arg_struct)�	func_type�fused_cname)rCrr&r'�declare_opt_arg_structsz;CFuncDeclaratorNode.analyse.<locals>.declare_opt_arg_struct�callspecz2cannot have both '%s' and '%s' calling conventionsr�r�)4�	enumeraterTr��is_c_class_scoper�r<r:rr%r
r�same_asr��cnameZis_unspecified�parent_typer�r�rEr�r�CFuncTypeArgr�r	r
r�r=r�r�rr�r/�is_errorr��return_type�	coerce_tor�rrLrM�assignable_from�	CFuncType�has_varargsr�rr�overridablerr�r�rr)rrrCr��directive_localsr�r�Zfunc_type_args�iZarg_node�name_declaratorr/r<�	type_node�
other_typeZexc_val�	exc_checkrZexc_val_typerrrr�r&)rCrr'r��s�
-	
						



	'"		

	
							
		zCFuncDeclaratorNode.analysec	
Cs"t�}dtj}|j|tj|j�xS|jt|j�|j	d�D].}|j|j
|j|jdddd�qSW|jtj
|jj
�}|dk	r�tj||�}|j�jd|ddd	|d
dd|jd
|�}d|_d|_tj|j�|_dS)a
        Declares the optional argument struct (the struct used to hold the
        values for optional arguments). For fused cdef functions, this is
        deferred as analyse_declarations is called only once (on the fused
        cdef function).
        z%snNZallow_pyobjectTZallow_memoryviewr<�kind�struct�scope�typedef_flagrr%rr)rr	�pyrex_prefix�declare_varr
r?r%rTrJr	r<r/�mangleZopt_arg_prefixr�Zget_fused_cnamer��declare_struct_or_union�defined_in_pxd�usedr��
op_arg_struct)	rrrCrr)Zarg_count_member�argZstruct_cnameZop_args_structr&r&r'rs$	
*,				z/CFuncDeclaratorNode.declare_optional_arg_struct)rMrmrnr�r r	rr�r�r�rr&r&r&r'r�vsvr�c@s.eZdZdgZddddd�ZdS)�CConstDeclaratorNoder�rNFc	CsM|jrt|jd�tj|�}|jj||d|d|d|�S)Nz)Const base type cannot be a Python objectr�r�r�)r=rr%r
�c_const_typer�r�)rrErCr�r�r��constr&r&r'r�:s
		zCConstDeclaratorNode.analyse)rMrmrnr�r�r&r&r&r'r35s	r3c@s�eZdZddddgZdZdZdZdZdZdZ	dZ
dZdZdZ
dZddd	d
�Zdd�Zd
d�Zdd�Zdddd�ZdS)�CArgDeclNoderE�
declaratorr�rBrrNFc
Cs
|rd|j_|_|jdkr�t|jt�r�|jjdkr�|r�|jjr�|jj|dd�}|j	�}n|jj}t
|�|j_d|j_d|j_d}nd}d|j_|jj|d|�}t|jd�r|jj
r|jj
|j_|jr�t|jt�r�t|jt�r�|j}xt|jt�ru|j}qWW|jj|_|j}|jr�|r�|jdr�|jjdkr�|j|�}	|	dk	r�|	}|jj||d|�S|j|jfSdS)NTr��
could_be_nameF�arg_name�annotation_typingr�)rErr/rKr7r�r<�is_basic_c_typer��empty_declaration_coderr6�hasattrr9r��TemplatedTypeNoder�r��array_declaratorrBr��inject_type_from_annotationsr#)
rrCr�rr/r9r8rEr7rFr&r&r'r�`s@$			
	.zCArgDeclNode.analysecCsJ|j}|sdSt||d|j�\}}|dk	rF||_|S)NrD)rBrGr�rE)rrCrBrErFr&r&r'r@�s		z)CArgDeclNode.inject_type_from_annotationscCsi|jdkrb|jrb|jjrM|jj|�|jj|jj��S|j|j�|_|jS)N)�
default_valuer�r��generate_evaluation_coder/�	cast_coder�Zget_argument_default_const)rrWr&r&r'�calculate_default_value_code�s	z)CArgDeclNode.calculate_default_value_codecCs|jr|jj|�dS)N)r�r�)rrWr&r&r'r��s	zCArgDeclNode.annotatecCs�|j}|dks|jr"dS|dkr=|j|�}|j|�|j|�|ri|j�n|j|j�}|jd||f�|jj	r�|j
|j��|j|�|j|�dS)Nz%s = %s;)
r�r�rDrB�make_owned_referencer��	result_asr/rQr=�put_giveref�generate_post_assignment_code�
free_temps)rrW�target�overloaded_assignmentr�r�r&r&r'�generate_assignment_code�s	

$
z%CArgDeclNode.generate_assignment_code)rMrmrnr�r�is_type_arg�
is_generic�kw_only�not_none�or_noner/r#rArB�
is_dynamicr�r@rDr�rLr&r&r&r'r6Bs".	
r6c@seZdZdd�ZdS)�
CBaseTypeNodecCs
|j|�S)N)r�)rrCr&r&r'r:�szCBaseTypeNode.analyse_as_typeN)rMrmrnr:r&r&r&r'rS�srSc@s%eZdZgZddd�ZdS)rAFcCs|jS)N)r/)rrCr8r&r&r'r��szCAnalysedBaseTypeNode.analyseN)rMrmrnr�r�r&r&r&r'rA�srAc@s=eZdZgZdZgZdZdZddd�ZdS)�CSimpleBaseTypeNodeNFcCs�d}|jrFtj|j|j|j�}|st|jd�n�|jdkrh|jrht	}n�|jdkr�|j
r�|jr�|j}qt	}nu|jr4|}xK|jD]@}|j
|�}|dk	r�|jr�|jj}q�d}Pq�W|dkr:|j|j|j�}|r:|j|_n|}|r|jrU|j�}|j|j�}|dk	rvn�|r�|j
r�|jr�|j}nt	}t|j�|_n^|jr�|j|jkr�t|jd|j�tj|j�}nt|jd|j�|jr^|js/|jr?t|jd�tj|�}|j|�n.|tjkr�tj }|j|�d|_|r�|Stj!SdS)Nz&Unrecognised type modifier combination�objectz'%s' is not a type identifierz#can only complexify c numeric typesT)"r;r
Z
simple_c_type�signedZlongnessr<rr%�module_pathrrrr�lookupr�r/r)�find_imported_moduler�r�Zlookup_typerr9r�r�complex�
is_numeric�
is_complexZCComplexTypeZcreate_declaration_utility_coderZcomplex_typeZc_double_complex_typer)rrCr8r/r)r�r�r&r&r'r��sh								
	zCSimpleBaseTypeNode.analyse)	rMrmrnr�r9rWr;rZr�r&r&r&r'rT�s
rTc@s=eZdZdZddgZddd�Zdd�Zd	S)
�MemoryViewSliceTypeNode�
memoryview�base_type_node�axesFcCs�|jj|�}|jr|Sddlm}y|j||j�}WnNtk
r�}z.t|j	|j
�tj�|_
|j
SWYdd}~XnX|j|j|�s�t|_
n5tj||�|_
|j
j|j�|j|�|j
S)Nr)�
MemoryView)r_r�rr�raZget_axes_specsr`rr�positionZmessage_onlyr
Z	ErrorTyper/Z
validate_axesr%rZMemoryViewSliceTypeZvalidate_memslice_dtype�use_memview_utilities)rrCr8rEraZ
axes_specs�er&r&r'r�-s 	
zMemoryViewSliceTypeNode.analysecCs$ddlm}|j|j�dS)Nr)ra)r�ra�use_utility_code�view_utility_code)rrCrar&r&r'rcDsz-MemoryViewSliceTypeNode.use_memview_utilitiesN)rMrmrnr<r�r�rcr&r&r&r'r](sr]c@s(eZdZdgZddd�ZdS)�CNestedBaseTypeNoderENcCs�|jj|�}|tjkr(tjS|jsLt|jd|�tjS|jj|j	�}|sr|j
r�t|jd||j	f�tjS|jS)Nz'%s' is not a valid type scopez '%s.%s' is not a type identifier)rEr�r
rr�rr%r)�lookup_herer<�is_typer/)rrCr8rE�
type_entryr&r&r'r�Qs	zCNestedBaseTypeNode.analyse)rMrmrnr�r�r&r&r&r'rgIs	rgc@s@eZdZddddgZdZdZdddd�ZdS)	r>r_�positional_args�keyword_args�
dtype_nodeNFcCs�|dkr|jj|�}|jr+|S|jr�|j�r�|jrt|jjrtt|jd�t	j
|_qag}xO|jD]D}|j
|�}|dkr�t|jd�t
}|j|�q�W|j|j|�|_nz|jr�ddlm}|j|j||j|j|j�}tjddkrYtdd�|j�D��}t	j||�|_t|�rat|j�rat	jt|j�|j�|_n�t|jd	d
dd�}	t|j�dks�|jjrt|jd�t	j
|_nY|jsd}
n
|jd}
t |jd
|	d|
�|_!|j!j||�d|_|jj"r�|j#r�|jj$|j#�|_|jS)Nz+c++ templates cannot take keyword argumentsz!unknown type in template argumentr)�Bufferrr$cSs+g|]!\}}|jd�|f�qS)r�)r+)r�r<r9r&r&r'r��s	z-TemplatedTypeNode.analyse.<locals>.<listcomp>r<r�rzinvalid array declarationr�r�)%r_r�rr�r�rlr8rr%r
rr/rkr:rr�r=r�rn�analyse_buffer_options�buffer_defaultsr�version_infordreZ
BufferTyperr!ZPythranExprr r�rJr�r?r�r�r�)rrCr8rE�template_typesrr/rn�optionsZempty_declaratorr�r&r&r'r�osZ		$!		
	zTemplatedTypeNode.analyse)rMrmrnr�rmr<r�r&r&r&r'r>_s
	r>c@s+eZdZddgZddd�ZdS)�CComplexBaseTypeNoderEr7FcCs4|jj||�}|jj||�\}}|S)N)rEr�r7)rrCr8r��_r/r&r&r'r��szCComplexBaseTypeNode.analyseN)rMrmrnr�r�r&r&r&r'rt�srtc@s(eZdZdgZddd�ZdS)�CTupleBaseTypeNode�
componentsFcCsxg}xJ|jD]?}|j|�}|jrBt|jd�tS|j|�qW|j|j|�}d|_|j	S)Nz/Tuple types can't (yet) contain Python objects.T)
rwr�r=rr%rrZdeclare_tuple_typer0r/)rrCr8Zcomponent_typesr�r/r�r&r&r'r��s		zCTupleBaseTypeNode.analyseN)rMrmrnr�r�r&r&r&r'rv�s	rvc@s7eZdZdZgZdd�Zddd�ZdS)�
FusedTypeNodea
    Represents a fused type in a ctypedef statement:

        ctypedef cython.fused_type(int, long, long long) integral

    name            str                     name of this fused type
    types           [CSimpleBaseTypeNode]   is the list of types to be fused
    cCs7|j|�}|j|j||j�}d|_dS)NT)r��declare_typedefr<r%r�)rrCr/r�r&r&r'r��sz"FusedTypeNode.analyse_declarationsFcCs�g}xe|jD]Z}|j|�}|s>t|jd�q||kr]t|jd�q|j|�qWtj|d|j�S)Nz
Not a typezType specified multiple timesr<)rbr:rr%rr
�	FusedTyper<)rrCr8rbr$r/r&r&r'r��szFusedTypeNode.analyseN)rMrmrnryr�r�r�r&r&r&r'rx�srxc@s(eZdZdgZddd�ZdS)�CConstTypeNoderEFcCs;|jj||�}|jr.t|jd�tj|�S)Nz)Const base type cannot be a Python object)rEr�r=rr%r
r4)rrCr8r�r&r&r'r��s
		zCConstTypeNode.analyseN)rMrmrnr�r�r&r&r&r'r{�s	r{c@s7eZdZddgZdZdZddd�ZdS)r�rE�declaratorsNc
CsA|jdkri|_|s$|}||_|jrL|jdj�}nd}|dk	r�|jdkr}t|jd�t|j�dkr�t|jdjd�td|�}|j	j
|_
x'|D]}|j|j||j�q�W|j
j|�}|jr<|jr<|js(|jr<t|jd�tSd|_|j}x�|jD]�}t|j�dkr�t|t�r�|j
dr�t|jd	d�|jo�|jdko�|j}|r�d
|_t|t�r|j||d|jd|d
|j�\}	}
n'|j||d|d
|j�\}	}
|
j�s}|jdkr`|
jpf|
js}t|jd|
�|jdkr�|
jr�t|jd�|	j}|	j}|dkr�t|jd�dS|
jr�|jdkr�t|jd�|
j r�d|j
kr d|
_!|j"||
|jd|d|jd
|jd|j#d|j$d|j�|_|jdk	r�t%j%|j�|j_|r9|jj&j'|�d|j_(qX|jr�t|jd�|j)||
|jd|d|d
|jd|j#dd�|_t*j+rXt,|j|j-�|j_-qXWdS)NrrzOnly extern functions allowedrz%Can't multiply declare template typesZ
func_templatezFused types not allowed herezwarn.multiple_declaratorsz�Non-trivial type declarators in shared declaration (e.g. mix of pointers and values). Each pointer declaration should be on its own line.Fr!r�r�z Variable type '%s' is incompletez'Python object cannot be declared externr�zMissing name in declaration.z8C++ references cannot be declared; use a pointer insteadr�Tr�api�	modifiersr z,Decorators can only be followed by functions�is_cdef).r!�
dest_scoper|r�r�rr%rJr�outer_scoper�Zdeclare_typer<rEr�r�r�r�is_module_scoperr�rKr�rr r�r�r��is_memoryviewslicer=rZis_referencer��is_static_method�declare_cfunctionr}r~rr/�create_to_py_utility_code�create_wrapperr,r�
docstringsr.r-)
rrCr�r�Ztemplate_paramrEr�r7Zcreate_extern_wrapperr#r/r<rr&r&r'r�s�			
			
			-!!				!		z CVarDefNode.analyse_declarations)rMrmrnr��
decoratorsr!r�r&r&r&r'r�sr�c@sLeZdZdgZddd�Zdd�Zdd�Zd	d
�ZdS)�CStructOrUnionDefNode�
attributesNc
CsO|j|j|j||j|j|jd|jd|jd|j�|_	dS)Nr�r}�packed)
r.r<r'r*r%rr�r}r�r�)rrCr)r&r&r'�declarezszCStructOrUnionDefNode.declarecCs�d}|jdk	r$t|j�}|j||�|jdk	r�|jrb|jrbd|j_x!|jD]}|j||�qlW|j	dkr�xU|j
D]J}|j}x|jr�|j
}q�W||jjkr�t|jd�q�WdS)Nrrz)Struct cannot contain itself as a member.)r�rr<r�r�r�r�r/r�r��var_entriesr/r�rErr%)rrCr)r�r/r&r&r'r��s 	
z*CStructOrUnionDefNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rq�sz)CStructOrUnionDefNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz-CStructOrUnionDefNode.generate_execution_code)rMrmrnr�r�r�rqr�r&r&r&r'r�ls
	r�c@s^eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�CppClassNodeNcCs�|jdkrd}npdd�|jD�}tdd�|jD��}|r�tdd�|jd|�D��r�t|jd�|j|jd|j|jdgd|jd	|�|_	dS)
NcSs)g|]\}}tj||��qSr&)r
r)r��
template_name�requiredr&r&r'r��s	z(CppClassNode.declare.<locals>.<listcomp>css|]\}}|VqdS)Nr&)r�rur�r&r&r'r��sz'CppClassNode.declare.<locals>.<genexpr>css|]\}}|VqdS)Nr&)r�rur�r&r&r'r��szGRequired template parameters must precede optional template parameters.�base_classesr�r�)
r��sumr�rr%�declare_cpp_classr<rr�r�)rrCrrZnum_optional_templatesr&r&r'r��s		
.zCppClassNode.declarec

s�jdkrd}}n,dd��jD�}dd��jD�}d��jdk	rut�j�d|���fdd�}t|��fdd��jD��}�j�j��j�j|d�j	d|��_
�j
dkr�dSd	�j
_�dk	r�j
j�_g}�fd
d���jdk	r��j
re�jred	�j
_xC�jD]8}t|dd�}|r�|j��|j��qoWxI��j�D]8}	|j|	��jdk	r�d
dj|�|	_q�Wt�jd|��_��_dS)NcSsg|]\}}|�qSr&r&)r�r�rur&r&r'r��s	z5CppClassNode.analyse_declarations.<locals>.<listcomp>cSs)g|]\}}tj||��qSr&)r
r)r�r�r�r&r&r'r��s	r�cs.|js|jrdSt�jd|�dS)NTz&Base class '%s' not a struct or class.)r��	is_structrr%)Z
base_class)rr&r'�base_ok�sz2CppClassNode.analyse_declarations.<locals>.base_okcs%g|]}|j�p���qSr&)r�)r��b)rCr)r&r'r��s	r�rc3s]xV|D]N}t|t�r$|Vqt|t�rx�|jj�D]}|VqFWqWdS)N)rK�CFuncDefNoder�r�r�)r�r�Zsub_attr)�func_attributesr&r'r��s
z:CppClassNode.analyse_declarations.<locals>.func_attributesr�ztemplate <typename %s>z, typename r�)r�r�rr<�filterr�r�r%rr�r�r�r/r�r�r/r�r�r�rr��template_declarationr�r�r))
rrCrrZtemplate_namesr�Zbase_class_typesZ
defined_funcsr�r�r\r&)rCr�r)rr'r��sD
	
(!

z!CppClassNode.analyse_declarationscCs"|jj|jjj�|_|S)N)r�rqr�r/r))rrCr&r&r'rq�sz CppClassNode.analyse_expressionscCs |jj|jjj|�dS)N)r�r�r�r/r))rrCrWr&r&r'r��sz*CppClassNode.generate_function_definitionscCs|jj|�dS)N)r�r�)rrWr&r&r'r��sz$CppClassNode.generate_execution_codecCs|jj|�dS)N)r�r�)rrWr&r&r'r��szCppClassNode.annotate)
rMrmrnr�r�r�rqr�r�r�r&r&r&r'r��s
/r�c@sIeZdZdgZdd�Zdd�Zdd�Zdd	�Zd
S)�CEnumDefNoderec
CsL|j|j|jd|jd|jd|jd|jd|j�|_dS)Nrr*r�r}r�)	Zdeclare_enumr<r%rr*r�r}r�r�)rrCr&r&r'r��s
zCEnumDefNode.declarecCsY|jdk	rU|jr.|jr.d|j_x$|jD]}|j||j�q8WdS)Nr)rer�r�r�r/r�)rrCr�r&r&r'r�s
z!CEnumDefNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rqsz CEnumDefNode.analyse_expressionscCs�|jdks|jr�|j|j�|jjtjdd�}x�|jj	D]�}|j
d||j|j||j�f�|j
|�|j
dtj|j||j|j�f�|j|tj�qPW|jj|�dS)N�public�
manage_refTz%s = PyInt_FromLong(%s); %sz.if (PyDict_SetItemString(%s, "%s", %s) < 0) %s)r�r}r�r%�	funcstate�
allocate_tempr
rr��enum_valuesrQr�error_goto_if_null�
put_gotrefr	Z
moddict_cnamer<�
error_goto�put_decref_clear�release_temp)rrW�tempr�r&r&r'r�s 	
	z$CEnumDefNode.generate_execution_codeN)rMrmrnr�r�r�rqr�r&r&r&r'r��s
	r�c@s%eZdZdgZdd�ZdS)�CEnumDefItemNoder9cCs�|jr]|jj|�|_|jjjs]|jjtj|�|_|jj|�|_|j|j|j|j|j	d|j
d|jd|jd|j
o�|jdk�}|jj|�|jr�|jjj|j�dS)Nrr�r}r�)r9r�r/r�rr
r?Z
declare_constr<r%rr�r}r�r�rr�)rrCZ
enum_entryr�r&r&r'r�)s		z%CEnumDefItemNode.analyse_declarationsN)rMrmrnr�r�r&r&r&r'r�"s	r�c@s@eZdZddgZdd�Zdd�Zdd�Zd	S)
�CTypeDefNoderEr7c
Cs�|jj|�}|jj||d|jd|j�\}}|j}|j}|j|||jd|d|jd|j	�}|j
r�d|_|jr�|jr�d|_dS)Nr�r�rr}Tr)
rEr�r7r�r�r<rryr%r}r�r�r/)rrCr�r#r/r<rr�r&r&r'r�Bs	$				z!CTypeDefNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rqSsz CTypeDefNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r�Vsz$CTypeDefNode.generate_execution_codeN)rMrmrnr�r�rqr�r&r&r&r'r�9sr�c@sBeZdZdZdZdZdZdZdZdZ	gZ
dZdZdZ
dZdZdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Z d%d&�Z!d'd(�Z"dS))�FuncDefNodeNFcCs�d}x�|jD]�}|jr}d}|jra|jj|�|_|jj|j|�|_q�t|jd�d|_q|jr�d}q|rt|jd�qWdS)Nrrz)This argument cannot have a default valuez/Non-default argument following default argument)	rTr�rNrprr/rr%rO)rrCZdefault_seenr2r&r&r'�analyse_default_values}s				z"FuncDefNode.analyse_default_valuescCsF|dkrdS|jds3|j|�dkrB|j|�}|S)Nr:)r�r:rp)rrCrBr&r&r'�analyse_annotation�s
#zFuncDefNode.analyse_annotationcCs9x2|jD]'}|jr
|j||j�|_q
WdS)N)rTrBr�)rrCr2r&r&r'�analyse_annotations�s	zFuncDefNode.analyse_annotationscCs|j}|j}|j|kr@||j}|j|�}nTt|t�r�|jr�|jdr�|j}|j|�}|dkr�|Sn|S|dkr�t	|j
d�nK|tk	r�|j|�r�t	|j
j
d�t	|j
d�n	||_|S)Nr:z
Not a typez2Signature does not agree with previous declarationzPrevious declaration here)r!r/r<r:rKr6rBr�r@rr%rrrE)rrCr2r!Z	orig_typer$r%r&r&r'�align_argument_type�s$		
%		zFuncDefNode.align_argument_typecCsdS)Nrr&)r�lenvr&r&r'�need_gil_acquisition�sz FuncDefNode.need_gil_acquisitionc	Cs�|}x|js|jr'|j}q	W|jratd|jjd|d|d|jj�}n!td|jjd|d|�}|j	|_	|jj
}|jr�|jo�|j
|_||_|j|_|S)Nr<r�Zparent_scopeZ
scope_name)�is_py_class_scoperr��
needs_closurerr�r<rr
rr/r�rr�local_scoper�)rrC�genvr�r/r&r&r'�create_local_scope�s$
				zFuncDefNode.create_local_scopecCs|jj|�dS)N)r�r�)rrCrWr&r&r'�generate_function_body�sz"FuncDefNode.generate_function_bodyc'Cs�ddlm}|jjr,ddlm}|j}|jra|jradtj	tj
f}n	tj
}|j|�|jj
||�|j||�|jjdko�|jjj}|jjdko�|jjj}|p�|}	|	rd|jkr|jdg|_|j�}
|jjd}|jjd	}|sH|rd|jjtjd
d��|j|�|j�|_|j|j_|j |j!�|j"||�|j#d�|
r�|j#|
�|j$||�p�|j%}
|j&r|j&j'|d
|
dd�|j'|d
|
�|}x|j(s>|jrJ|j)}q,W|j*r�|j+|j,j-j.tj	��|j#d�nj|j/r�|jr�|j+|j,j-j.tj	��|j#d�|j+|j,j-j.tj
��|j#d�|j0||�x0|j1D]%}|j2p|j3s|j4|�qWd}|jj5s�|jj6rWd}n|jjrpd|j7}|j#d|jj.tj8�|f�|j9�}|j:|�|j;}|j*p�|j/}x$|j<D]}|j-j6r�d}Pq�Wdd�|j=D�}|jo"|j>o"|p"|}|jo;|j>o;|}|jpN|j>}|s]|rv|j?�d|j_n|jr�|j>r�|j@�|s�|r�|jAs�|jB�|jCr�|jCjD|�nd}|jE|�|r�|jF|�|r|jG�|jH|jjd|�|r.|jI|�|j*rHtJjKdd�}tJjL|j,j-j|�}|s}d|j,j-jM}|j#dtj	|j,j-jN�||j,j-jMtjOf�|j#dtj	�|j#dtj	|j,j-jPd�f�|jQdtR�|j#|jS|j!��|j#d�|jTtj	�|j#d�|j/r|jUr�|j#d ||j,j-jN�tjVf�n)|j#d!||j,j-jN�tjVf�|jr�|j#dtj	|f�n,|j*r|jQ||j,j-�|jW|�|s
|rj|jAs^|jXr2|jjd"}n|jj}|jY||j!d#|jj�d|j_Z|j[||�t\|t]�}x�|j<D]�}|j-j6r�|s�t^|j_�dkr|j2r|j`|�q�|r�|j-jr�t^|j_�dkr�|ja|jbd$|jj�q�Wx_|j1D]T}|j3r+t^|j_�dkr+|j2r+|jcrr|jd|�q+|j`|�q+WxC|j1|j<D]1}|j-jer�|jfjgjhr�|ji||�q�W|jj|�x3|j<D](}|j-jer�|jk|||j!�q�W|r(	|jl�d%|j_|jm||�|j |j!d&d%�|j#d�|j#d'�|jjns�	|jj6r�	tj8}|jo||j�nO|jjp}|r�	|j#dtj8|f�n |jj5s�	|j#d(tj8�|jq|jrkr�|jjns
|js|jt�|ju|jq�x7|jjv�D]&\} }!|jw| |!d$|j�q<
Wt^|�d)k}"|"r�
|jjtx�|j#d*�|j#d+�|j#d,�|j#d-�x|D]}|jy||�q�
W|j#d.�|jjr|jztj8|�tj8}#n|j{�}#|j|�}$|#dk	sE|$r�|jro|j>ro|j#d/�|j?�|j}|jj~�|jr�|j>r�|jl�|j#d�n9t|jj!d0|jj~d)�|j�|jj~|j�|jjp}%|#dkr|%r|%}#|#dk	r5|j#dtj8|#f�n |jj5sU|j#d(tj8�|rh|j�|�|"s�|s�|jjr�|js|j�|ju|jt�x|D]}|jy||�q�W|r�|j�|�|jjrO
|j�|jj�tj8��}&|j#d1|&�|jr"
|j?�|j#d2�|jrB
|jl�|j#d�|ju|j�x�|j1D]�}|jhsi
|j2r�
qi
|j-jr�
|j�|jbd$|j�qi
|j-j6ri
|j3s�
t^|j_�dkri
|jcr�
|j�|�qi
|j�|�qi
Wx�|j<D]�}|j-j6rT|s:t^|j_�dkr�|j2r�|j�|�q
|j-jr
|s|t^|j_�dkr
|j�|jbd$|j�q
W|j*r�|j�tj	|j,j-�|js|jjp}%|j{�}#|#dkr�|%r�|%}#|jj6r|j�|jj�tj8��|jj�rX|jjd3krX|j#d4tj8tj8f�|sd|r�d%|j_Z|jAs�|jj6r�|j�tj8d#|jj�n|j�dd#|jj�|js�|j��|s�|jr|j>r|jl�d%|j_|jj5s |j#d5tj8�|j#d�|
rD|j#d6|
�|j�|j�|j��|j&rz|j&j
||�|j�|�dS)7Nr)rn)raz%s->%s�
__getbuffer__�__releasebuffer__Z
cython_unused�profile�	linetraceZProfilez	Profile.cr��with_pymethdef�
proto_onlyT�;z = NULLz = z%s%s;cSsg|]}|jr|�qSr&)r0)r�r�r&r&r'r�3s	z=FuncDefNode.generate_function_definitions.<locals>.<listcomp>�acquire_gilZtp_newrgz
%s->tp_newz%s = (%s)%s(%s, %s, NULL);zif (unlikely(!%s)) {z%s = %s;�Py_Nonez} else {�}z*%s = (%s) __Pyx_CyFunction_GetClosure(%s);z
%s = (%s) %s;z
 (wrapper)r�have_gilF�tracez/* function exit code */z!__Pyx_pretend_to_initialize(&%s);rz0{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;�__Pyx_PyThreadState_declare�__Pyx_PyThreadState_assignz5__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);z5__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}�{z&Unraisable exception in function '%s'.z	if (%s) {zOPyErr_SetString(PyExc_TypeError, "Memoryview return value is not initialized");�__hash__z5if (unlikely(%s == -1) && !PyErr_Occurred()) %s = -2;z
return %s;z#endif /*!(%s)*/)�r�rnrr�rar��is_closure_scopeZis_passthroughr	�cur_scope_cname�outer_scope_cnameZmangle_closure_cnamesr�r�r�r�r<r)rr~�get_preprocessor_guardr�r�rer�load_cached�enter_cfunc_scope�	new_label�return_from_error_cleanup_labelrr��	gil_ownedr�r%r�rQ�needs_assignment_synthesis�pymethdef_required�py_func�generate_function_headerr�r�r��put�scope_classr/r��needs_outer_scope�generate_argument_declarationsr��
in_closurer6�put_var_declarationr�r=Zmemslice_entry_init�retval_cname�insertion_point�generate_keyword_listr��arg_entriesZbuffer_entries�has_with_gil_block�put_ensure_gil�declare_gilstate�is_generator�put_trace_declarations�code_object�calculate_result_code�put_trace_frame_init�getbuffer_check�put_declare_refcount_context�put_setup_refcount_context�getbuffer_initrZConstructorSlotZget_slot_function�
typeptr_cnamer<�empty_tuplerC�
put_increfrr�r��
is_cyfunction�
self_cnamerGr��put_trace_call�	can_trace�generate_argument_parsing_coderKr�rJ�cf_assignments�put_var_incref�put_incref_memoryviewslicer�xdecref_cleanupZput_var_xincrefr�Z
buffer_auxZbuflocal_nd_varr0Z
put_init_vars�generate_argument_type_testsZput_acquire_arg_buffer�put_release_ensured_gilr�r��put_init_to_py_nonerA�error_label�labels_used�put_goto�return_label�	put_label�all_managed_temps�put_xdecref�restore_exception_utility_codeZput_release_buffer_codeZput_init_entry�error_value�caller_will_check_exceptions�put_add_traceback�qualified_namerZput_unraisable�getbuffer_error_cleanup�getbuffer_normal_cleanup�unlikelyZerror_condition�put_xdecref_memoryviewsliceZput_var_xdecref�put_var_decref�
put_decref�put_xgiveref�as_pyobject�
is_special�put_trace_return�put_finish_refcount_context�put_temp_declarations�exit_cfunc_scope�generate_wrapper_functions)'rrCrWrnrar�r�Zis_getbuffer_slotZis_releasebuffer_slotZis_buffer_slot�preprocessor_guardr�r�r��cenvr��init�tempvardecl_coder�Zhave_object_argsr2Zused_buffer_entriesZacquire_gil_for_var_decls_onlyZacquire_gil_for_refnanny_onlyZuse_refnannyr�Ztp_slotZslot_func_cnameZ
trace_namer�lhs�valrr/Zbuffers_present�err_valr&Zdefault_retval�condr&r&r'r��sD			
	


			
			

	
	
	

	
!



			

						
		%' (	



	











	
	
	%					



	z)FuncDefNode.generate_function_definitionscCs�|jjrt|jd�n@|jj�r_|jjpD|jjr_t|jd|j�|j|j|j|j�}|j	r�|j	|_	|S)NzInvalid use of 'void'z Argument type '%s' is incomplete)
r/r�rr%r�r�r�Zdeclare_argr<rB)rrCr2r�r&r&r'�declare_argumentxs)	zFuncDefNode.declare_argumentc	Cs�|jj�r�|jjtjdd��|jj}d|jj}|j	d|||j
|j|jjow|jj
|j|j�f�nt|jd�dS)NZArgTypeTestzFunctionArguments.cz((PyObject *)%s)z:if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s", %s))) %szICannot test type of extern C class without type object name specification)r/Ztypeobj_is_availabler�rerr�r�r�rrQ�accept_noner<�is_builtin_typeZ
require_exactr�r%r)rr2rWr��arg_coder&r&r'�generate_arg_type_test�s	z"FuncDefNode.generate_arg_type_testcCs�|jjrd|jj}n|jj}|jd|�|jdtdt|j��|j|j|j	�f�|jd�dS)Nz
%s.memviewz,if (unlikely(((PyObject *)%s) == Py_None)) {zMPyErr_Format(PyExc_TypeError, "Argument '%%.%ds' must not be None", "%s"); %s��r�)
r/r�r�rrQr�rJr<r�r%)rr2rWrr&r&r'�generate_arg_none_check�s	z#FuncDefNode.generate_arg_none_checkcCsdS)Nr&)rrWr&r&r'r�sz&FuncDefNode.generate_wrapper_functionscCs>|j|j�x'|jD]}|js|j|�qWdS)N)r�r%rTrRrL)rrWr2r&r&r'r��s	z#FuncDefNode.generate_execution_codecCsX|jjd}y|jjjjdj}Wnttfk
rMd}YnX||fS)Nr�obj)r�r�r/rEr)r�r��KeyError)r�	py_buffer�obj_typer&r&r'�_get_py_buffer_info�szFuncDefNode._get_py_buffer_infocCsW|j�\}}|j}|jd|�|jd�|jd�|jd�dS)Nzif (%s == NULL) {zZPyErr_SetString(PyExc_BufferError, "PyObject_GetBuffer: view==NULL argument is obsolete");z
return -1;r�)r&rrQ)rrWr$ru�viewr&r&r'r��s	

zFuncDefNode.getbuffer_checkcCsg|j�\}}|j}|rR|jrR|jd||�|jd|�n|jd|�dS)Nz%s->objz%s->obj = NULL;)r&rr=r�rGrQ)rrWr$r%r'r&r&r'r��s	zFuncDefNode.getbuffer_initcCs�|j�\}}|j}|rp|jrp|jd|�|jd|�|jd||�|jd�n|jd|�dS)Nzif (%s->obj != NULL) {z%s->objr�zPy_CLEAR(%s->obj);)r&rr=rQr�r�)rrWr$r%r'r&r&r'r�s	z#FuncDefNode.getbuffer_error_cleanupcCsq|j�\}}|j}|rm|jrm|jd|�|jd|�|jd||�|jd�dS)Nzif (%s->obj == Py_None) {z%s->objr�)r&rr=rQr�r�)rrWr$r%r'r&r&r'r�s	z$FuncDefNode.getbuffer_normal_cleanupcCs�|jjsdS|jj}tjj|�}|s8dS|dkr^|jjjd�r^dS|dkr}|jjjr}dS|j	�S)NZ__long__�__int__r�r�)r�r�)
r�r
r<r�method_name_to_slot�getr)rhr�preprocessor_guard_code)rr<�slotr&r&r'r��s"z"FuncDefNode.get_preprocessor_guard)#rMrmrnr�r�r�r�r��is_generator_bodyZis_async_defr~�has_fused_arguments�star_arg�starstar_argr�r�r�r�r�r�r�r�r�r�rrr!rr�r&r�r�rrr�r&r&r&r'r�ZsB��


			r�csleZdZddddgZdZdZdZdZdZdZ	dZ
dZdd�Ze
d	d
��Zdd�Zd
d�Zdd�Zdddd�Zdd�Zdd�Zdd�Zdd�Zddd�Zddddd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Z�fd+d,�Zd-d.�Zd/d0�Z d1d2�Z!�S)3r�rEr7r��py_func_statFNcCs
|jjS)N)r�r<)rr&r&r'�unqualified_name	szCFuncDefNode.unqualified_namecCs|jr|jjSdS)N)r�r�)rr&r&r'r�	szCFuncDefNode.code_objectcCs�|j|_|jdkr$i|_|jj|jd�|jdk	r�|jj|�}|dkr�t|jjd�t	j
}n|jj|�}d|jko�|j
d�|_t|jt�r|jj||dd|jdk	d|jd|j�\}}n7|jj||dd|jdk	d|j�\}}|js`t|jd�||_|j|_|j}xt|d	�s�|j}q�W||_|j|_|jj}|jd
ks�|jr�|r�t|jjd�|jdkr |jd
kr t|jjd�xt |j|j�D]�\}}|j!||�|j|_|j"|_"|j#|_#|j$|j|j|�|jj%r�d|_&|jj'r�d|j(kr�t|jdd�|jj's�|jj)r6|jj*r	t|jd�q6d|j(kr6t|jdd�q6W|j$|j+|j|�|j"}	|j#}
|j,|_,|j|_|j-|	||jd|
d|jd|jd|jdk	d|j(d|j�|_.|j/|j._0|j+|_+|j+j1r|jd
krt|jd�|j+j2r.|j+j3|jd�|jr}|j4r}|jr}t5|j�dkst|jdjj6r}d|_|j7|�|j8|�dS)N�localsz
Not a typer�r�r"r!r�z*Suite attached to non-function declarationrTr�zBFunction with optional arguments may not be declared public or apirrz/Only extern functions can throw C++ exceptions.T�inlinez$Buffer unpacking not optimized away.rzUBuffer may not be acquired without the GIL. Consider using memoryview slices instead.rr}�definingr~r zFunction cannot return an arrayzused as a return valuerF)9r�is_c_class_methodr!r~r��directive_returnsr:rr%r
rrEr�rhr�rKr7r�r�r�r�r/r r
r=r�Zcfunc_declaratorrTr	r}r
r�zipr�r<r�_validate_type_visibilityr�r.r�r~�is_pythran_exprrrrr�r��
inline_in_pxdZinline_func_in_pxdr�r�Zcheck_nullary_constructorr�rJr=�declare_cpdef_wrapperr�)rrCrEr#r/r7Z
opt_arg_count�
formal_arg�type_argr<rr&r&r'r�	s�	"		.			
	"		
		)	
z!CFuncDefNode.analyse_declarationscCs�|jr�|jr"t|jd�|jj}|jd|j�}|jr�ddlm	}t
|jd||jdd��g}|djj|�ng}t
d	|jd|jjd
|jddddd
|jd|d|dd�	|_|j|j_|jj|�d|jj_t|jd|jg�|_tj|j_|jj|j_d|j_|jj_|j|j|<|jjr�|js�tjr�t|jd|j�|_t|jd|j|j g�|_ dS)Nz&static cpdef methods not yet supportedr�r)r��	decoratorr<r�rr%rTr/r0r-r�r�r�Tr�r�)!r r�rr%r�r<�call_self_noder�r�r��
DecoratorNoder?rp�DefNoderTr-r�r�r
r�r1r
rr/�as_variabler0r��is_final_cmethodr�lookup_module_cpdef�OverrideCheckNode�overrider�)rrCr<Zpy_func_bodyr�r�r&r&r'r<z	s>			'		
z"CFuncDefNode.declare_cpdef_wrappercCsq|jdkp|j}t|dd�}|rm|rm|jrm|jdkp]|jp]|jsmt|d�dS)zo
        Ensure that types used in cdef functions are public or api, or
        defined in a C header.
        r�r�Nrz:Function declared public or api may not have private types)r�r)r�r}r�r�r�r)rr/r%rCZ
public_or_apir�r&r&r'r9�	sz&CFuncDefNode._validate_type_visibilityrc
s�ddlm��jj}|rB|dt|��jj�}dd�|D�}|r��j�jd�jj	�}|}t
j}n��jjr��jj
jj}�j�jd|j	�}	||	_�j�jd|	d�jj	�}d}n^�jjd	jj}
�j�jd|
j	�}|
|_�j�jd|d�jj	�}|pet
j}�j�jd
|d��fdd�|D�d
|�}td�jdtjd|�S)Nr)r�cSsg|]}|j�qSr&)r<)r�r2r&r&r'r��	s	z/CFuncDefNode.call_self_node.<locals>.<listcomp>r<r"�	attributeTr�functionrTcs(g|]}�j�jd|��qS)r<)r�r%)r��n)r�rr&r'r��	s	Zwrapper_callr%rr9)r�r�r/rTrJr	r�r%r�r<rrEr�r)rZ
AttributeNode�SimpleCallNode�ReturnStatNoder
r)
rZomit_optional_argsr�rT�	arg_names�cfuncZcall_arg_namesZ
skip_dispatchZclass_entryZ
class_noderjr>Zc_callr&)r�rr'r@�	s4 	$		$		zCFuncDefNode.call_self_nodecCsDx=|jjD]/}|js,t|jd�|j||�q
WdS)NzMissing argument name)r/rTr<rr%r)rrCr2r&r&r'�declare_arguments�	s	zCFuncDefNode.declare_argumentscCs
|jjS)N)r/r)rr�r&r&r'r��	sz!CFuncDefNode.need_gil_acquisitioncCs|j}|j}|jr{|r{|jjr>t|jd�x:|jjD],}|jjrK|j	rKt|jd�qKWdS)Nz9Function with Python return type cannot be declared nogilz8Function declared nogil has Python locals or temporaries)
r/rrrr=rr%r�r�Zin_with_gil_block)rrCr/rr�r&r&r'r��	s			zCFuncDefNode.nogil_checkcCs�|j|j_|jdk	r6|jj|�|_nA|jdk	r]|jj|�|_n|j|�|j|�|j|j�|_|S)N)	r�r�r1rqr�r�r�r�r�)rrCr&r&r'rq�	s

z CFuncDefNode.analyse_expressionscCsdS)NFr&)rrCrWr&r&r'r��	sz'CFuncDefNode.needs_assignment_synthesisrcCs?|j}g}|j}xi|jdt|j�|j�D]D}	|	j�}
|j|	j�}|jssd|
}
|j	|
�q<W|r�|j
r�tjjt
j�}|jr�|j	|�n|j	d|�|jr�|r�|j	|jjt
j��|jr|j	d�|s"dg}|dkr:|jj}|j|dj|��}
|jjdkr|d|kr|d}nd}d}|j|jj�}|jj|
d	|�}|j}|jr�|r�|jjd
j|j�|j|j�|r!|jjd
jd|||f�|jd|||f�dS)
NzCYTHON_UNUSED %sz...�voidz, �privatez::zstatic r��dll_linkageZmodule_declarationsz%s%s%s; /* proto*/z%s%s%s {) r�r/rTrJr	r�rXr<�cf_usedrr r
r?r	�skip_dispatch_cnamerGr1�optional_args_cnamerr��
func_cnameZfunction_header_coder�r�Zbuild_function_modifiersZfunc_modifiersrr6r�r��partsrQ)rrWr��
with_opt_args�
with_dispatchrr)Z	arg_declsr/r2Zarg_declr�Zdispatch_arg�entityZ
storage_classrRr~�headerZneeds_protor&r&r'r��	sN		*	
			
				z%CFuncDefNode.generate_function_headercCs�|j}xs|jD]h}|jr|j|j�}|jsF|jr|j|�}|jd|j	j
|j�|f�qWdS)Nz%s = %s;)r�rTr�rXr<rGrSrDrQr/r�r)rrCrWr)r2r�r�r&r&r'r�
s			z+CFuncDefNode.generate_argument_declarationscCsdS)Nr&)rrWr&r&r'r�%
sz"CFuncDefNode.generate_keyword_listc
sud}d}|j}|jjr2�jdtj�x�|jD]���jr?|j�j	�}|j
sr|jr��jdtjtj|f��j
}xt|d�s�|j}q�W�jd�jtj|jj|j	�f�|d7}|d7}q?Wx!t|�D]}�jd�qW�jd���fdd	�}	x'|jD]�|	|j�j	��qQWdS)
Nrz	if (%s) {zif (%s->%sn > %s) {r<z%s = %s->%s;rr�csN|jrJ�jrJ�jd|j|jf��j|��j|�dS)Nz%s = %s;)r�r�rQr�original_cnamer��put_var_giveref)r�)r2rWr&r'�put_into_closureB
s
zECFuncDefNode.generate_argument_parsing_code.<locals>.put_into_closure)r�r/r	rQr	rUrTr�rXr<rGrSr+r7r=r�rZ
opt_arg_cname�rangerh)
rrCrWr"r0r)r�r7rur^r&)r2rWr'r�(
s4				
	

z+CFuncDefNode.generate_argument_parsing_codecCsdS)Nr&)rrWr&r&r'�!generate_argument_conversion_codeK
sz.CFuncDefNode.generate_argument_conversion_codecCs]xV|jjD]H}|jr/|j||�q
|jjr
|jr
|j||�q
WdS)N)r/rT�needs_type_testrr=rr!)rrWr2r&r&r'r�N
s
	z)CFuncDefNode.generate_argument_type_testscs3tt|�j|�|jr/|jj|�dS)N)rfr�r�r1)rrW)rLr&r'r�W
s	z$CFuncDefNode.generate_execution_codecCs!|jjrdS|jjjSdS)N�0)rr=r�r/r�)rr&r&r'r\
szCFuncDefNode.error_valuecCs
|jjjS)N)r�r/r
)rr&r&r'rc
sz)CFuncDefNode.caller_will_check_exceptionsc	Cs�d}|j}|j}xo|jdk	r�|d7}|j}d|jjtj|f|_|j�|j|dd|jjd|jj	d|j�|j
js�|jd�|jj
}dd	�|dt|�|jj	�D�}|jjr|jtj�n|jr|jd
�|jj	r=|jtj�n|j	rS|jd�|jd|jjd
j|�f�|jd�qWdS)Nrrz%s%swrap_%srYrXrzreturn cSsg|]}|j�qSr&)r)r�r2r&r&r'r�y
s	z;CFuncDefNode.generate_wrapper_functions.<locals>.<listcomp>rb�NULLz%s(%s);z, r�)r�r/Z
prev_entryrVr	r+rQr�r
r	rr�r�rTrJrrTrUr�)rrW�kr�rrTZarglistr&r&r'rf
s6		
	
	

-	
	
&z'CFuncDefNode.generate_wrapper_functions)"rMrmrnr�r;r�r!r7rGr�rr1r2�propertyr�r�r<r9r@rOr�r�rqr�r�r�r�r�r`r�r�rrrr&r&)rLr'r��s<]$-
#	r�c@s.eZdZgZdZdZdd�ZdS)�
PyArgDeclNodeFcCs|jj||�dS)N)r�r�)rrCrWr&r&r'r��
sz+PyArgDeclNode.generate_function_definitionsN)rMrmrnr�rrMr�r&r&r&r'rf�
srfc@seZdZdgZdS)rAr?N)rMrmrnr�r&r&r&r'rA�
srAc@s�eZdZddddddgZdZdZdZd	Zd
Zd
Z	dZ
dZdZd
Z
d
ZdZdZdZdZdZdZdZdZdZdZdd
�Zdddddddd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"d d!�Z#d"d#�Z$dd$d%�Z%d&d'�Z&d(d)�Z'd*d+�Z(d
d,d-�Z)d.d/�Z*d0d1�Z+d2d3�Z,d4d5�Z-dS)6rBrTr/r0r�r��return_type_annotationFNrbrTcKs�tj|||�d}}}xJ|jD]?}|jrW|d7}|jsW|d7}|js+|d7}q+W||_||_||_dS)Nrr)r�r�rTrOr��num_kwonly_args�num_required_kw_args�num_required_args)rr%rUrdZrk�rr2r&r&r'r��
s	
	
			zDefNode.__init__cCsG|jrt|jjd�|jr8t|jjd�|pAd%\}}|dkrg}	xf|jD][}
|
j|dd�\}}|	jtjd|j	ddd|
j
d	td
|
j��qfWtjdtd|	d
dddd|ddddd|�}
t
|jd	|
�}n�|dkr.|j}|j}
t|j�t|
j�ks^|
jr~t|jd�t|jd�x�tt|j|
j��D]r\}\}
}|
j|ddd|dko�|j�\}}|dks�|tjkr�|j|
_||
_q�W|dkr\|
jdk	r\ddlm}||jd|
jd	|
j�}t|jdt|jd|j	dd�d|jd
dd|
jd|d|
jd|
j�}t|jd|p�gdt |jd	|
j�d|d|j!d|j"d|
j#d	|
d|
jd|
jd d!d"dd#t$|d#i�d$|�
S)&Nz'cdef function cannot have star argumentz+cdef function cannot have starstar argumentFr�rr<rrBr/r%rrTrr�r
rrr
zwrong number of argumentszprevious declaration hererr)rr9r�r~rEr7r�r-r r�rQr}r!r7)NF)%r/rr%r0rTr�rr
rr<rBrrr�r)r/rJrrr8rr#r�r�rrr�r�r
rrr�rAr�r-r
r�)rrNr)r �returnsZ
except_valr~r�r
Z
cfunc_argsr=r#r/Z
cfunc_typer"r>rr7r&r&r'�as_cfunction�
sz						'.
								zDefNode.as_cfunctioncCs'|jr
dS|js|jr#dSdS)z�Determines if the function's signature is compatible with a
        cdef function.  This can be used before calling
        .as_cfunction() to see if that will be successful.
        FT)r�r/r0)rr&r&r'�is_cdef_func_compatibles
	zDefNode.is_cdef_func_compatiblecCs�|jr_xS|jD]H}|j}|jr|j|jdkO_|j|jdkO_qW|jr�|jd�r�d|_|jr�|jd�r�d|_|jdkr�|jr�d|_|j|�|jdkr�|j	|�n
|j
|�|j|�|jj
j�|_|jtkr|jr|jdr|jjrt|j|�\}}|r|jr||_|j|�t|jd|d	|jjd
|jd|jd|jd
|j�|_|jj|�dS)N�classmethodr�Frgrz<lambda>r:rJr<rTr/r0r)r�r?r;�is_classmethodr<�is_staticmethodrhr��analyse_argument_types�declare_lambda_function�declare_pyfunction�analyse_signaturer��	signaturerrrgr�r
rGr=r��DefNodeWrapperr%rTr/r0�
py_wrapperr�)rrCr?r\rurr&r&r'r�(sB						


	
				zDefNode.analyse_declarationscCs}|jd|_|jd}|j}d|_x|jD]�}t|d�rWd}n�|jj|�}t|�r�|jr�t	j
||jg�}|jj||�\}}|j
|_
||_|jr�d|_|j||�|r|jrt|jd�|jj�|_d|_d|_d|_d|_|jjsa|jjsa|jjr�|jrvd|_q5|jr�d|_q5|jj s�|jj!s�|jjs�|jjr�|j"r�|j"j#dkr�d|_q�||_q5d|_q9d|_|jrt|jd	�|jr9t|jd
�q9W||_t|�rpdd�t$|j�D�|_%n	g|_%dS)
Nr3�allow_none_for_extension_argsr<Tz9Python function argument cannot have C name specificationrrFz.Only Python type arguments can have 'not None'z-Only Python type arguments can have 'or None'cSs(g|]\}}|jjr|�qSr&)r/Zis_numpy_buffer)r�r"�ar&r&r'r��s	z2DefNode.analyse_argument_types.<locals>.<listcomp>)&r�r!r�rTr=rEr�rr:r
rzZ
org_bufferr7r<r/r�r.r�rrr%Zas_argument_type�hdr_type�needs_conversionrarNr=r�r�rQrrP�is_extension_typerr��constant_resultr�np_args_idx)rrCryZf2sr2r#rEr/r&r&r'rrUsZ
										$						"zDefNode.analyse_argument_typescCsB|jjrV|jr%t|jd�t|j�dkoJ|jpI|j|j_	n2|j
dr�|jps|jr�|jjtj
krt|j�dkr�tj|j_q�t|j�dkr�|jdjdkr�|jdjr�tj|j_n�|jjtjkr�t|j�dkr=tj|j_nKt|j�dkr�|jdjdkr�|jdjr�tj|j_|jj}|j�}|tjkr�|dkr�t|j�dkr�|jr�tj
}|j_d|_d}|jrP|jrPd}d|_tj|�|j_}d|_d|_d|_|jsb|jr�|jr�|jr�|jjdd�=x+t t!|t|j���D]}|j|}d|_"|j#|�r+|jr+|jrd|_$t%j&|_'|_(nd|_#|j)|_'|_(d|_*nT|j+|�|_'|j(j,|j'�s|j'j-rv|j(j-rvd|_.n	d|_*|j*r�t/j0|j1|_2q�t/j3|j1|_2q�W|t|j�kr�|j4�dS|t|j�kr>|js|j4�x;|jD]0}|j"r
|j(j5s1|j(j6r
d|_.q
WdS)Nz8special functions of cdef classes cannot have decoratorsrZalways_allow_keywordsrr"T�*)7r�r
r�rr%rJrTr/r0Ztrivial_signaturer�rvrZpyfunction_signatureZpyfunction_noargsr�rOZpyfunction_oneargZpymethod_signatureZ	unaryfuncZibinaryfunc�num_fixed_args�self_in_starargrqrrZfixed_arg_format�has_generic_argsrpr.Zdecorator_indirectionr�r_�minrNrrMrZ	type_typer{r/rr|Zfixed_arg_typerr=rar	Z
arg_prefixr<�	hdr_cnameZ
var_prefix�
bad_signaturer}r)rrC�sigZnfixedr"r2r&r&r'ru�sz	1!''					%
						
	
!zDefNode.analyse_signaturecCs�|jj}d|j�}|jr/|d7}|j}|jd�r_|jd�r_d}nd}t|jd||jt	|j
�|f�dS)Nz%dz or more�__zSpecial method�Methodz>%s %s has wrong number of arguments (%d declared, %s expected))r�rvr�r�r<�
startswith�endswithrr%rJrT)rr�Zexpected_strr<�descr&r&r'r��s	
		zDefNode.bad_signaturecCsn|j}|j|�}|rw|jrD|jjrDt|jd�|jjrw|j	rw|j
rwt|jdd�|j||jd|j
�}||_
|j|j�}tj|||j
_tjrat|j|j�|_tj|||_|jrj|jtjks>|js>|jdkrJ|jdrJd|_qjtj|||_n	d|_dS)Nz:Only final types can have final Python (def/cpdef) methodsz'Overriding cdef method with def method.�Zallow_redefine�__getattr__Zfast_getattr)r<rhrDr�
is_final_typerr%r/r�Zis_builtin_cmethodr�rrtr��next_id�scope_prefixr	Z
pyfunc_prefix�pyfunc_cnamerr�r.r-Zfuncdoc_prefix�	doc_cnamer
rZ	invisibler��wrapperbase_cnameZwrapperbase_prefix)rrCr<r��prefixr&r&r'rt�s(	 			zDefNode.declare_pyfunctioncCs=|j|j|j�}d|_||_|j|j_dS)N)rs�lambda_namer%r-r�rr�)rrCr�r&r&r'rss		zDefNode.declare_lambda_functioncCs�x�|jD]�}|js)t|jd�|jrn|j|j|j|j�|_|jjr�d|j_	n|j
||�|_d|j_d|j_|j
|j_
q
W|j||j�|j||j�dS)NzMissing argument namerbr)rTr<rr%r|r,r/r�r=rrr6r0r�declare_python_argr/r0)rrCr2r&r&r'rOs		!zDefNode.declare_argumentscCsw|rs|jddkr%tj}nt}|j|j||j�}d|_d|_d|_	d|_
||_dS)N�infer_typesFrrb)r�r
Zunspecified_typerr,r<r%r6r0rr�r�)rrCr2r/r�r&r&r'r�s				zDefNode.declare_python_argcCs�|j|j_|j|�|j|�|jrJ|j||j�|_|j|�r�|jr�x3|jddd�D]}|jj	|�|_qzW|j
j|�|S)Nrr�)r�r�r�r�rgr�r�r�r?rqrx�prepare_argument_coercion)rrCr?r&r&r'rq+s

	zDefNode.analyse_expressionscCs�|jr
dS|js"|jjr&dS|jr3dS|jjrCdS|jjrSdS|jse|jr�|dkr|j	j
dS|jj
dS|jp�|j
S)NTFZbinding)rq�specialized_cpdefsr�Zis_fused_specialized�no_assignment_synthesisr
Zis_anonymousr�rr�r�r�r�r�)rrCrWr&r&r'r�9s		z"DefNode.needs_assignment_synthesiscCs
|jjjS)N)r�rvr)rr&r&r'rKszDefNode.error_valuecCs
|jjjS)N)r�rvr
)rr&r&r'rNsz$DefNode.caller_will_check_exceptionscCsg|jr"|jj|j�|�|jrP|jj|j_|jj||�tj|||�dS)N)�defaults_getterr�r��py_wrapper_requiredr�rVrxr�)rrCrWr&r&r'r�Qs		z%DefNode.generate_function_definitionscCs�|r)|jr%|jj||d�dSg}|jjjrkdtj}|js^d|}|j	|�dd�}x$|j
D]}|j	||��q�W|jr�|j	||j��|jr�|j	||j��|r�dj
|�}nd}|jj|jj�}	|jd}
|j�}|r>|
j|�|
jd	|	|f�|rh|
jd
�|jd|	|f�dS)NTzPyObject *%szCYTHON_UNUSED cSsP|j}|jr|j}n	|j}|jj|�}|jsLd|}|S)NzCYTHON_UNUSED )r�r�r\rr/r�rS)r2r�rZdeclr&r&r'�
arg_decl_codejs				
z7DefNode.generate_function_header.<locals>.arg_decl_codez, rPZdeclszstatic %s(%s); /* proto */z#endifzstatic %s(%s) {)r�rxr�r�rv�
has_dummy_argr	r�r�rrTr/r0r�rr�r�r�r�rQ)rrWr�r��
arg_code_list�self_argr�r2r�dcZ
decls_coderr&r&r'r�]s>		

	

		


z DefNode.generate_function_headercCsdS)Nr&)rrCrWr&r&r'r��sz&DefNode.generate_argument_declarationscCsdS)Nr&)rrWr&r&r'r��szDefNode.generate_keyword_listcsg�fdd�}x|jD]}||j�qWx-|j|jfD]}|rF||j�qFWdS)NcsD|jr@�jd|j|jf��j|��j|�dS)Nz%s = %s;)r�rQrr\r�r])r�)rWr&r'r^�s	
z@DefNode.generate_argument_parsing_code.<locals>.put_into_closure)rTr�r/r0)rrCrWr^r2r&)rWr'r��sz&DefNode.generate_argument_parsing_codecCsdS)Nr&)rrWr&r&r'r��sz$DefNode.generate_argument_type_tests).rMrmrnr�rqrpr�Zreqd_kw_flags_cnamer�r�r�rgr�r�r�Z
py_cfunc_node�requires_classobj�defaults_structr-�
fused_py_funcr�rxr�rVr�r�rmrnr�rrrur�rtrsrOr�rqr�rrr�r�r�r�r�r�r&r&r&r'rB�
sVB-@M

.
rBc@sKeZdZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zddd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�ZdS)5rwNcOs\tj|||�|jj|_|jj|_|jj|_|jj|_d|_dS)N)r�r�rJrhrirjr�rv)rrTrtr&r&r'r��szDefNodeWrapper.__init__cCsn|jj}|j}|j|j�}tj|||_tj|||_	|j
|_
|jj|_dS)N)rJr�r<r�r�r	Z
pywrap_prefixrVZpymethdef_prefix�pymethdef_cnamervr)rrCZtarget_entryr<r�r&r&r'r��s	z#DefNodeWrapper.analyse_declarationscCs�xZ|jD]O}|jjs1|jj|�sYq
|jr
|jjr
|jj|�s
q
W|jr�|jjjr�|jj}d|_	x3|j
D](}|jr�|jj
r�d|j_q�WdS)NrT)rTr/r=�create_from_py_utility_coder{r�r0r�rSr�r�r6rr;Z
cf_maybe_null)rrCr2r�Zassr&r&r'r��s	z(DefNodeWrapper.prepare_argument_coercioncCsOt|j�}|dksG|dkrK|jdjsG|jdjrKdSdS)Nrr)rJrTrrM)rZargcountr&r&r'�signature_has_nongeneric_args�sz,DefNodeWrapper.signature_has_nongeneric_argscCs
|jjS)N)rvr�)rr&r&r'�signature_has_generic_args�sz)DefNodeWrapper.signature_has_generic_argscCs*g}|jjr"|jtj�xt|jD]i}|jr�|jjp\|jj	p\|jj
r�|j|jj|jj
��q,|j|jj
�q,W|jr�|j|jjj
�|jr�|j|jjj
�dj|�}|jjs|jdtj�|jd|jjj|f�dS)Nz, z%s = z%s(%s);)rvr�rr	r�rTr{r/r�r�r\rCr�rr/r0r�rr�r�r�rQrJr�)rrWrTr2r&r&r'r��s$
"			z%DefNodeWrapper.generate_function_bodycCs�|jj}|j|j�|jd�|jd�|jj�}|rX|j|�|j|�|j�|_|jj	||�p�|jj
}|j||�|j||�|j
�}|jjr�d}nd}|jjs|jd|jjtj�|f�|j�|jd|j�|j||�|j|�|j|�|j|j�|j|j�|jd�|jd�|j|jkr$|j|j�|j |j�x-|jj!�D]\}}	|j"||	�q�W|j#�}
|
dk	r$|jdtj|
f�|j |j�x3|j$D](}|j%r>|j&jr>|j'|�q>W|j(�|jjs�|jdtj�|jd	�|j)�|r�|jd
|�dS)Nr�z/* Python wrapper */z = 0z%s%s;z%s (wrapper)z/* function exit code */z%s = %s;z
return %s;r�z#endif /*!(%s)*/)*rJr�r�r%rQr�r�r�r�r�r�r�r�r�rr=r�r�r	r�r�r�r<r�r�r�rr�r�r�r�r�r�r�r�rr�r6r/r	rr)rrCrWr�rr�rZretval_initrr/rr�r&r&r'r��sb



		







z,DefNodeWrapper.generate_function_definitionsrc
Cs�g}|j}|js!|jrNdtj}|jsAd|}|j|�x_|jD]T}|jsX|jsy|j	r�|jd|j
�qX|j|jj|j
��qXW|j
j}|jr�|j�tjgkr�|jd�|jjr|jdkr|jd�|jr9|jdtjtjf�dj|�}d}	|jdkru|jjrud}	d
}|jj|j�}
d|	|
|f}|jd|�|r�|j
jr�|j
jj||d
d�dStj r�|j!r�|j
jr�|jj"r�|js|j#r�|j!}|j$r=|j%�}|jd|j&|j'�f�|jr�|jd�|jd|j#�|jd�|s�|j
jr�|j(d|j)�|j*|j
jddd
�|jd|�dS)NzPyObject *%szCYTHON_UNUSED zCYTHON_UNUSED PyObject *unused�__ipow__zPyObject *%s, PyObject *%sz, r�r�r�Fzstatic %s%s(%s)z
%s; /*proto*/r�Tzstatic char %s[] = %s;z#if CYTHON_COMPILING_IN_CPYTHONzstruct wrapperbase %s;z#endifzstatic PyMethodDef %s = r�Z
allow_skipz%s {)r�r�)+rvr�r�r	r�rrTrNrrMr�r{r�rJr�r
Zmethod_flagsrZ
method_noargsr)rr<r��
args_cname�
kwds_cnamer�rrVrQr�r�rr�r-Zis_property_scoper�Z
is_unicodeZas_utf8_stringr�Zas_c_string_literalr�r�Zput_pymethoddef)
rrWr�r�r�r�rr2r�Zmfr�r[Zdocstrr&r&r'r�2
sr	
	

	"

	

			

z'DefNodeWrapper.generate_function_headercCs{xJ|jD]?}|jr
|jr9|jd|j�q
|j|j�q
Wx'|jD]}|jrW|j|�qWWdS)NzPyObject *%s = 0;)	rTrNr|rQr�r�r�r�r6)rrCrWr2r�r&r&r'r�}
s			z-DefNodeWrapper.generate_argument_declarationsc	Cs�|j�}|j}|jd�}|jdk}|jdk	pT|jdk	pT|}x/|jD]$}|jjsa|jj	|�saqaW|j
�s�|r�t|jd�|j
|�n2|j�s�|j|�n|j|j||�||_|j|�r�|j|�s!|j|�|j|�|r�|j|j|�|jr�|jjjru|j|jj�n|j|jj�|j|jjj�|j�|jd|j��|j|�r�|j|�dS)NZargument_unpacking_donerz.This method cannot have * or keyword argumentsz
return %s;)�new_error_labelr�r�rhr/r0rTr/r=r�r�rr%r`r��generate_stararg_copy_code�'generate_tuple_and_keyword_parsing_code�
label_usedr�r��generate_arg_decrefr�r��put_var_xdecref_clear�put_var_decref_clearrrJrrrQr)	rrCrW�old_error_label�our_error_label�	end_labelZhas_kwonly_argsZhas_star_or_kw_argsr2r&r&r'r��
sB		

	
z-DefNodeWrapper.generate_argument_parsing_codecCs|r|j|j�dS)N)r�r�)rr2rWr&r&r'�generate_arg_xdecref�
sz#DefNodeWrapper.generate_arg_xdecrefcCs|r|j|j�dS)N)r�r�)rr2rWr&r&r'r��
sz"DefNodeWrapper.generate_arg_decrefcCs�|jsl|jjtjdd��|jdtj�|jd|j	tj|j
�f�|jd�|jr�|js�|jjj
r�dtj}q�dtj}ndtjtjf}|jjtjd	d��|jd
|tj|j	t|j�|j
�f�|jrk|jjj
rktdd�|jjjD��r�|jd
|�|jd|jjjtj|jjj|j
�f�|j|jjj�|jd�|jd|jjjf�|jd�d|jj_nt|jd|jjjtjtjf�|jd|jjj|j
�f�d|jj_|j|jjj�|jrI|jjrI|jd|jjjtj|jjjf�|jr|jjj
r|jd�|j|jjjt�|jd|j
��|jd�n|jd|j
��|j|jjj�|jtjt�|jtj�|jd|jjjtjf�|jjtj dd�}|jd||tj|f�|jdtj|f�|jdt�|jd�|jd|jjj|f�|jd�|jj!|�d|jj_nN|jr�|jtjt�|jd|jjjtjf�d|jj_dS) N�RaiseArgTupleInvalidzFunctionArguments.cz)if (unlikely(PyTuple_GET_SIZE(%s) > 0)) {zK__Pyx_RaiseArgtupleInvalid("%s", 1, 0, 0, PyTuple_GET_SIZE(%s)); return %s;r�zunlikely(%s)z%sz-unlikely(%s) && unlikely(PyDict_Size(%s) > 0)ZKeywordStringCheckzHif (%s && unlikely(!__Pyx_CheckKeywordStrings(%s, "%s", %d))) return %s;css|]}|jjVqdS)N)rVZ
allow_null)r��refr&r&r'r��
sz<DefNodeWrapper.generate_stararg_copy_code.<locals>.<genexpr>z	if (%s) {z3%s = PyDict_Copy(%s); if (unlikely(!%s)) return %s;z} else {z
%s = NULL;rz,%s = (%s) ? PyDict_Copy(%s) : PyDict_New(); zif (unlikely(!%s)) return %s;rz=%s = PyTuple_New(PyTuple_GET_SIZE(%s)+1); if (unlikely(!%s)) r�z
return %s;zPyTuple_SET_ITEM(%s, 0, %s);r�Fz-for (%s=0; %s < PyTuple_GET_SIZE(%s); %s++) {z*PyObject* item = PyTuple_GET_ITEM(%s, %s);r�z!PyTuple_SET_ITEM(%s, %s+1, item);z%s = %s;)"r/r�rerr�rQr	r�r�r<rr0r�rSr��boolr�Z
cf_referencesrr�r�r�rJrq�put_xdecref_clearrr�r�rGr�r�r
�c_py_ssize_t_typer�)rrWZkwarg_checkr�r&r&r'r��
s�				
		"	

			
			
	
		z)DefNodeWrapper.generate_stararg_copy_codecs��jd�}g}g}g}x�|D]�}|jr(|jr}|jr�|jr�|jrm|j|�q�|j|�q(|jr�|j|�q(|jr(|jr(|j|�q(W||}	|j|j}
t	|�dkr|djs|djr|
d8}
t	|�}|j
o*|
|k}t|	�}
|jsK|j
r[|j
|���jd�t|�t|	�}�jdtjdj�fdd�|D��f�|j|���jd	|jdkr�d
p�dtjf�|j|
|||
||��|jr'|
dks3|
|kr�|
|krR|j
rRd}nd
}�jdtj||
f��j|�|jr`||
kr�|j
r��jdtj|f��j|��jd�xLt|	�D]w\}}|js��j|j�}�jjtjdd���jd|j|f��j�j |j!��Pq�Wn��jd�|
|kr�x�t|�D])\}}�jd|tj|f�q�Wnn�jdtj�|j
r��jd�t"t|��ddd!�}xw|D]o\}}||
dkrX||ddkrC�jd��jd|d��jd|tj|f�qW|
dkr��jd��jd��jd�|j
r�|
rx/t#|
dd"d#�D]}�jd|�q�W�j|�n�jd��j|��jd��jd�x1t|�D]#\}}|j$|d|��q>W�jd��j%|�r��j|��j&|��jjtjdd���jd |j||
|tjf��j�j |j!��dS)$NZargtuple_errorrrr�z static PyObject **%s[] = {%s,0};�,cs&g|]}d�j|j��qS)z&%s)�intern_identifierr<)r�r2)rWr&r'r�9s	zJDefNodeWrapper.generate_tuple_and_keyword_parsing_code.<locals>.<listcomp>z
if (%s(%s)) {�likelyrz!=�<z(} else if (PyTuple_GET_SIZE(%s) %s %d) {z'} else if (PyTuple_GET_SIZE(%s) > %d) {z} else {�RaiseKeywordRequiredzFunctionArguments.cz&__Pyx_RaiseKeywordRequired("%s", %s); z&values[%d] = PyTuple_GET_ITEM(%s, %d);zswitch (PyTuple_GET_SIZE(%s)) {zdefault:zCYTHON_FALLTHROUGH;z
case %2d: z	case  0: zbreak;z	case %2d:z	default: r�z
values[%d]r�zD__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, PyTuple_GET_SIZE(%s)); r�r�r�)'r�rNr�rrMrOrrjrirJr/r�r0�generate_stararg_init_coderQ�tupler	�pykwdlist_cnamer��#generate_argument_values_setup_coder��generate_keyword_unpacking_coder�r�rr�r<r�rerr�r�r�r%r�r_�generate_arg_assignmentr�r�)rrTZ
success_labelrW�argtuple_error_labelrkZrequired_kw_only_argsZoptional_kw_only_argsr2Zkw_only_args�min_positional_args�max_positional_args�has_fixed_positional_count�has_kw_only_args�all_argsZcomparer"�pystring_cnameZ
reversed_argsr&)rWr'r�s�
				
,


	
	
!		
		

			
$	

!


	






			z6DefNodeWrapper.generate_tuple_and_keyword_parsing_codecCs3|jjrV|jr0tj|jtj|�}|j}|jd|j|f�n�|jj	r|j
r||jd|�|j|jj||jj|j|��|j
r/|jd�|jd|jj|j
|�f�|jjr|j|jjdd�|jd�nt|jd|j�dS)Nz%s = %s;z	if (%s) {z} else {r�Tr�z2Cannot convert Python object argument to type '%s')r/r=rNr
Ztypecastrr�rQr�from_py_functionr��from_py_call_coder%rDr�r�r)rr2r�rWr�r&r&r'r��s(				
		z&DefNodeWrapper.generate_arg_assignmentcCs�|jr`d|jj_|jd|jjj|jjj|j�f�|j|jjj�|jr�d|jj_|jdtj	|f�|jd|jjjtj	|tj	f�|jd|jjj�|jr�|j
|jjjt�|j�|jd|j��|jd�|j|jjj�|jd�|j
d	|jjjtjf�|jtjt�|jd�dS)
Nrz0%s = PyDict_New(); if (unlikely(!%s)) return %s;z if (PyTuple_GET_SIZE(%s) > %d) {z4%s = PyTuple_GetSlice(%s, %d, PyTuple_GET_SIZE(%s));zif (unlikely(!%s)) {z
return %s;r�z} else {z	%s = %s; )r0r�r�rQrrr�r/r	r�r�rrr�r�r�)rr�rWr&r&r'r��s4						


#z)DefNodeWrapper.generate_stararg_init_codecCs�t|�}|jd|djd|�f�|jjrk|jd|jjtj|jjtjf�xat|�D]S\}}|j	rx|j
jrx|j|�}|jd||j
j
|�f�qxWdS)NzPyObject* values[%d] = {%s};r�rbz+%s *%s = __Pyx_CyFunction_Defaults(%s, %s);zvalues[%d] = %s;)rJrQr�rJr�r	Zdynamic_args_cnamer�rr�r/r=rDr)rrTrWZmax_argsr"r2rAr&r&r'r��s		z2DefNodeWrapper.generate_argument_values_setup_codec
	Cs�|jd�|jdtj�|jd�|jrD|jd�x]t|dd!d"�D]E}|jd|d�|jd|tj|f�|jd�q[W|jd	�|js�|jd
�|j|�|jd�|jdtj�|js
|d
kr�d#}	x)t	|�D]\}}
|
j
s|}	qW|	|krR|d}	|d
krk|jd�x7t	|d|	d��D]\}}
|d
kr�||kr�|d
kr�|jd�|jr�||kr�|jd�n|jd|�|j|
j�}|
j
rh|
j
r q�|jd�|jdtj|f�|jd|�|jd�q�|jd|tj|f�||kr/|d
kr�|jd�|j|�q�|jd�|jjtjdd��|jd|j||||f�|j|j|j��|jd�q�|
j
r�|jd�|jjtjdd��|jd|j|f�|j|j|j��|jd�q�W|d
kr�|jd�|r�|j||�|jd�|d
kr�d}n/|jr|jd||f�d}nd}|jjtjdd��|jd tjtj|jrn|jjjpqd||j|j|j�f�|jd�dS)$NzPy_ssize_t kw_args;z1const Py_ssize_t pos_args = PyTuple_GET_SIZE(%s);zswitch (pos_args) {zdefault:rz
case %2d: z&values[%d] = PyTuple_GET_ITEM(%s, %d);zCYTHON_FALLTHROUGH;zcase  0: break;z	default: r�zkw_args = PyDict_Size(%s);rz	case %2d:zif (kw_args > 0) {z2PyObject* value = __Pyx_PyDict_GetItemStr(%s, %s);z-if (value) { values[%d] = value; kw_args--; }zKif (likely((values[%d] = __Pyx_PyDict_GetItemStr(%s, %s)) != 0)) kw_args--;zelse zelse {r�zFunctionArguments.cz2__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, %d); r�z&__Pyx_RaiseKeywordRequired("%s", %s); zif (unlikely(kw_args > 0)) {rbzAconst Py_ssize_t used_pos_args = (pos_args < %d) ? pos_args : %d;Z
used_pos_argsZpos_argsZ
ParseKeywordszOif (unlikely(__Pyx_ParseOptionalKeywords(%s, %s, %s, values, %s, "%s") < 0)) %sr�r�r�)rQr	r�r/r_r�r�r�rjrr�r�r<rOr�rerr�r�r%�,generate_optional_kwonly_args_unpacking_coder�r0r�r)
rr�r�r�r�r�r�rWr"Zlast_required_argr2r�Z
pos_arg_countr&r&r'r��s�

	
	
	


	


'
		
		

				
		

						z.DefNodeWrapper.generate_keyword_unpacking_codecCsJg}d}xMt|�D]?\}}|js|jr<q|sH|}|j|j�qW|rFt|�dkr�|jd|jr�dp�dt|�f�|jd�|jd||t|�f�n|jd�|jd|�|jd	tj	tj
f�|jd
�t|�dkr9|jd�|jd�dS)
Nrz'if (kw_args > 0 && %s(kw_args <= %d)) {r�r�zPy_ssize_t index;z6for (index = %d; index < %d && kw_args > 0; index++) {zif (kw_args == 1) {zconst Py_ssize_t index = %d;z:PyObject* value = __Pyx_PyDict_GetItemStr(%s, *%s[index]);z0if (value) { values[index] = value; kw_args--; }r�r�)rrOr�rr<rJrQr0r	r�r�)rr�rWZ
optional_argsZfirst_optional_argr"r2r&r&r'r�hs0	
	
	

z;DefNodeWrapper.generate_optional_kwonly_args_unpacking_codecCs1x*|jD]}|jr
|j||�q
WdS)N)rTr|�generate_arg_conversion)rrWr2r&r&r'r`�s	z0DefNodeWrapper.generate_argument_conversion_codecCs�|j}|j}|jro|jr;|jd|j�n|jd|j�|j||�|jd�nh|jr�|j||�nL|j|�r�|jd|j	j
|jf�nt|jd||f�dS)Nz	if (%s) {z
assert(%s); {r�z%s = %s;z+Cannot convert 1 argument from '%s' to '%s')
r{r/r=r�rQr��%generate_arg_conversion_from_pyobject�#generate_arg_conversion_to_pyobjectrr�rrr%)rr2rW�old_type�new_typer&r&r'r��s					#z&DefNodeWrapper.generate_arg_conversioncCsX|j}|jr@|j|j|j|jj|j|��nt|jd|�dS)Nz2Cannot convert Python object argument to type '%s')	r/r�rQr�r�r�rr%r)rr2rWr�r&r&r'r��s			
z4DefNodeWrapper.generate_arg_conversion_from_pyobjectcCs~|j}|j}|rf|jd|jj||j|j|jj|j�f�|j|j�nt	|jd|�dS)Nz%s = %s(%s); %sz5Cannot convert argument of type '%s' to Python object)
r{Zto_py_functionrQr�rr�r�r%Zput_var_gotrefr)rr2rWr�r\r&r&r'r��s				 z2DefNodeWrapper.generate_arg_conversion_to_pyobjectcCsrxk|jD]`}|jr,|j||�q
|jr
|jjsZ|jjsZ|jjr
|j||�q
WdS)N)	rTrarrr/r=r�r�r!)rrWr2r&r&r'r��s	z+DefNodeWrapper.generate_argument_type_testscCs
|jjS)N)rvr)rr&r&r'r�szDefNodeWrapper.error_value)rMrmrnZdefnoderJr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r`r�r�r�r�rr&r&r&r'rw�s6AK.U�y

rwcs�eZdZdZdZdZdZdZdZe	j
dgZ
�fdd�Z�fdd�Zd	d
�Z
�fdd�Z�S)
�GeneratorDefNodeTF�	Generator�gbodycs9t|dgdd�|d<tt|�j||�dS)Nr�r�Tr�)r�rfr�r�)rr%rt)rLr&r'r��szGeneratorDefNode.__init__cs9tt|�j|�|j|j_|jj|�dS)N)rfr�r�r�r�)rrC)rLr&r'r��sz%GeneratorDefNode.analyse_declarationscCs,|jjj}|j|j�}|j|j�}|j|j�}|jd�|jd|j||j	r|j	j
|�ndtj||||j
d|j�f�|jtjt�|jrd}|jd|tjf�|j|t�|j|�|j�|jd�|jd�dS)	Nr�zk__pyx_CoroutineObject *gen = __Pyx_%s_New((__pyx_coroutine_body_t) %s, %s, (PyObject *) %s, %s, %s, %s); %src�genz
gen->classobjz&%s = __Pyx_CyFunction_GetClassObj(%s);zreturn (PyObject *) gen;r�)r�r�rVr�r<�qualname�module_namerQ�
gen_type_namer�r�r	r�r�r%r
rr�r�r�rGr)rrCrWZ
body_cnamer<r�r�Zclassobj_cnamer&r&r'r��s(
	!		


z'GeneratorDefNode.generate_function_bodycsb|jtj|jd��|jj|dd�tt|�j||�|jj||�dS)NzCoroutine.c�protoT)	rerr�r�r�r�rfr�r�)rrCrW)rLr&r'r��sz.GeneratorDefNode.generate_function_definitions)rMrmrnr��is_coroutine�is_iterable_coroutine�is_asyncgenr�r�rBr�r�r�r�r�r&r&)rLr'r��sr�c@seZdZdZdZdS)�AsyncDefNode�	CoroutineTN)rMrmrnr�r�r&r&r&r'r�sr�c@seZdZdZdZdS)�IterableAsyncDefNodeZIterableCoroutineTN)rMrmrnr�r�r&r&r&r'r�	sr�c@seZdZdZdZdS)�AsyncGenNodeZAsyncGenTN)rMrmrnr�r�r&r&r&r'r�sr�csyeZdZdZdZdZdZdddd�fdd�Zdd�Zdd	�Z	dd
d�Z
dd
�Z�S)�GeneratorBodyDefNodeTFNcsGtt|�jd|d|d|d|dddgdddd�dS)	Nr%r�r<�is_async_gen_bodyr-rTr/r0)rfr�r�)rr%r<r�r�)rLr&r'r�szGeneratorBodyDefNode.__init__cCs~|j|j�}|jd�}tj||}|jdt|jd|dd�}||_t|j	�|_
||_dS)N�	generatorrr�rQ)r�r�r	Zgenbody_prefixr,rr%rVrr<rr�)rrCr�r<rr�r&r&r'�declare_generator_body!s	z+GeneratorBodyDefNode.declare_generator_bodycCs|j|�|j|�dS)N)rrr�)rrCr&r&r'r�+s
z)GeneratorBodyDefNode.analyse_declarationscCsTd|jjtjtjtjf}|r?|jd|�n|jd|�dS)Nz]static PyObject *%s(__pyx_CoroutineObject *%s, CYTHON_UNUSED PyThreadState *%s, PyObject *%s)z%s; /* proto */z%s /* generator body */
{)r�rVr	�generator_cnameZlocal_tstate_cname�sent_value_cnamerQ)rrWr�r[r&r&r'r�/s	
z-GeneratorBodyDefNode.generate_function_headercCs<|j}|jj||�|j|�|j�|_|j|j�|j||�|j	d�|j
|�|j�}|j	dtj
�|j�}|j�|j|jjp�|jj�|jjd}|jjd}|s�|r7|j�d|j_|jr$|jj|�nd}|j|�|jj|jjj�|j�}	|jd�}
|j|
�|j |
�|j	d|j!tj"|j��|j#r`|j$dk	r`|j$}|t%j&kr�d}n@|t%j'kr�d	}n(|t%j(krd
}nt)d|��|j	dtj
||j!tj
|j�f�|j*tj
�|j+||�|jjjj,r�|j	d
|jjj-tj.�|jjj/dtj0�f�|j	dtj.�|s�|r�d|j_|j|j�|j	d�|j	d�|j#rs|jj1rs|j2rS|jj3t4j5dd��|j	d|j2rkdnd�|j6|j7�rp|jj1s�|j8|j9�|j |j7�|j#r�|j$dk	r�|j:tj
t;�t<j=|j>�j?j@kr-|jj3t4j5dd��|j	dtA|j2��x-|jjB�D]\}
}|jC|
|�q=W|jD|jj�|j |j9�|j#r�|jEtj
�n|j:tj
t;�|j	dtj0�|j	dtj0�|j	dtj0�|s�|r|jFtj
d|jjG�|jH�|j	dtj
�|j	d�|jI|j�|s[|r�|	jJ|jj|jd|jjG�|	j	dtj0�|	j	d |
�x.|jKD]#\}}|	j	d!||f�q�W|	j	d"�|s�|r
|	jFd#d|jjG�|	jH�|	j	d$�|	j	d�|jL�dS)%Nr�zPyObject *%s = NULL;r�r�TZ	first_runz%sz
PyList_New(0)zPySet_New(NULL)zPyDict_New()z)invalid type of inlined comprehension: %sz%s = %s; %sz%s = %s;z%s->closurezCYTHON_MAYBE_UNUSED_VAR(%s);Fz/* function exit code */�StopAsyncIterationzCoroutine.czPyErr_SetNone(%s);Z__Pyx_PyExc_StopAsyncIterationZPyExc_StopIterationZpep479z*__Pyx_Generator_Replace_StopIteration(%d);z+__Pyx_Coroutine_ResetAndClearException(%s);z%s->resume_label = -1;z%__Pyx_Coroutine_clear((PyObject*)%s);rz
return %s;r�zswitch (%s->resume_label) {zcase 0: goto %s;zcase %d: goto %s;z2default: /* CPython raises the right error here */r�zreturn NULL;)Mr�r�r�r�r�r�r�r%r�rQr�r�r	r�r�r�r�r<rr�r�r�r�r�r�r�r�Zinit_closure_tempsr�r/r)Z	use_labelr�r�r��
is_inlined�inlined_comprehension_typerZ	list_type�set_type�	dict_typerr�r�r�r�r�rCr�r�r�rerr�r�r�r�r�r�rr�generator_stopr��contextZfuture_directivesr�r�r�rrrr�rrr�Zyield_labelsr)rrCrWr�Zclosure_init_coderr�r�r�Zresume_codeZfirst_run_label�target_typeZ	comp_initrr/r"�labelr&r&r'r�:s�	




!


					
			

				

	



z2GeneratorBodyDefNode.generate_function_definitions)rMrmrnr-r�r�r�r�r�r�r�r�r&r&)rLr'r�s
r�c@s7eZdZdgZdZdd�Zdd�ZdS)rFr�Nc	s|j�_�jjr!d}nd}ddlm��j�jt��_	�j
�jd�j	d��fdd��j|d�D��}|jjs�|jj
r�t�jdt�jd	|�t�jd
d�g��_nt�jd
|��_�jj|��_�S)Nrr)r�rIrTcs+g|]!}�j�jd|j��qS)r<)r�r%r<)r�r2)r�rr&r'r��s	z9OverrideCheckNode.analyse_expressions.<locals>.<listcomp>r��exprr9)r�rTr�r�r�r��RawCNameExprNoder%r�	func_noderKrr��
is_returncoder��ExprStatNoderLr�rq)rrCZ	first_argZ	call_noder&)r�rr'rq�s"	!z%OverrideCheckNode.analyse_expressionscCs�|j|jjj�}|jjr4dtj}nd|jdj}|j	d�|j	dtj
�|j	d�|jjr�|j	d�n|j	d|�|jjt
dd	�}|jj|�|jjtjd
d��|j||j�}|j	d||||f�|j|�d
|}d||jjjf}|j	d||f�|jj|�|j	d�|j|tj
�|jj|�|j	d�dS)Nz((PyObject *)%s)rz /* Check if called by wrapper */zif (unlikely(%s)) ;z#/* Check if overridden in Python */zelse {z5else if (unlikely(Py_TYPE(%s)->tp_dictoffset != 0)) {r�TZPyObjectGetAttrStrzObjectHandling.cz*%s = __Pyx_PyObject_GetAttrStr(%s, %s); %szPyCFunction_Check(%s)z1(PyCFunction_GET_FUNCTION(%s) != (PyCFunction)%s)zif (!%s || %s) {r�)r�r�r�r<r�r	�module_cnamerTrrQrTr�r�rr��	set_cnamer�rerr�r�r%r�rVr�r�r�r
r�)rrWZinterned_attr_cnamer�Zfunc_node_temp�errZis_builtin_function_or_methodZ
is_overriddenr&r&r'r��s6

		


z)OverrideCheckNode.generate_execution_code)rMrmrnr�r�rqr�r&r&r&r'rF�s
	rFc@seZdZdS)�ClassDefNodeN)rMrmrnr&r&r&r'r�	sr�c	@s�eZdZddddddddd	g	Zd
Zd
ZdZd
Zd
Zd
d
ddd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd
S)�PyClassDefNoder�rd�	metaclass�mkwri�class_resultrJ�
class_cellr�NFc	Cstj||�||_||_||_||_||_ddlm}	|jr�t	j
r�t|j|j�}|	j
|d|�}
nd}
|}|rpd}d|_|jr^|jrFxwtt|j��ddd�D]S\}}
|
jjdkr�|jdk	r t|
jd�|
j|_|j|=q�W||_qp|jdk	spt�n|	j|�|_|s�|js�|js�|jr�|jdkr|r�|jr�|j}nd}|r�|jjr�|jjr�n!|	j|d|d	|j�|_d}nd}|	j|d
|d|
d|jd	|jd|j�|_|	j|d
|d	|jd|jd|
d|jd|jd
|d|�|_ nE|	j!|dg�|_|	j"|d
|d	|d|jd|
�|_ |	j#|d
|�|_$|	j%|j�|_&dS)Nr)r�r9FTr�z2keyword argument 'metaclass' passed multiple timesrrir<r-rdZcalculate_metaclass�allow_py2_metaclassr8r�)'r�r�r<r-r�r�rir�r�rr�r.r%Z
StringNode�is_py3_style_classr7r8r�rr�r9r�rr�AssertionError�	ProxyNodeZis_sequence_constructorrTZPyClassMetaclassNodeZPyClassNamespaceNoderdZPy3ClassNode�classobj�DictNodeZ	ClassNoder�rJZClassCellInjectorNoder)rr%r<rir-r�r�rlZforce_py3_semanticsr�Zdoc_noderr"r�ZmkdictZneeds_metaclass_calculationr&r&r'r�%sl								/!
	!zPyClassDefNode.__init__cCs�|jr t|jjd�dSddlm}t|jddddd|jd	|jjpr|j	|jd
g�d|j
d|jd
dd|j�S)zO
        Return this node as if it were declared as an extension class
        z7Python3 style class could not be represented as C classNr)r�r�rQr��
class_namerirTr�r�r�Fr-)
rrrr%r�r��
CClassDefNoder<rir�r�r�r-)rr�r&r&r'�	as_cclassos		$		zPyClassDefNode.as_cclasscCsK|}x|js|jr'|j}q	Wtd|jd|�}|_|S)Nr<r�)r�rr�rr<r))rrCr�rr&r&r'�create_scope�s

zPyClassDefNode.create_scopecCs�|j}|jrmddlm}x?|jddd�D]'}||jd|jd|g�}q9Wd|_||_|jj|�|jj	|�|j
|�}|j|_|jjj
|_|jj|�dS)Nr)rKrIrTr�)rr�r�rKr%r?rr�rJ�analyse_target_declarationrr�r�r�class_obj_cnamer�)rrCrrKr?rr&r&r'r��s 						z#PyClassDefNode.analyse_declarationscCs�|jr|jj|�|_|jr<|jj|�|_|jrZ|jj|�|_|jj|�|_|jj|�|_|j}|jj|�|_|jj	||j
�|jj|�|_|S)N)rirqr�rrdrr)r�rJ�analyse_target_expressionrr)rrCrr&r&r'rq�s				z"PyClassDefNode.analyse_expressionscCs-|j|j|�|jj|j|�dS)N)r�r)r�r�)rrCrWr&r&r'r��sz,PyClassDefNode.generate_function_definitionscCs�|j|j�|jj|�|j}|jrB|jj|�|jr[|jj|�|jrt|jj|�|j	j|�|j	j
�|_|_|j
j|�|jj|�|jj|�|j
j||jj
��|j
j|�|jj
�|_|_|jj|j|�|j	j|�|j	j|�|jrq|jj|�|jj|�|jr�|jj|�|jj|�|jr�|jj|�|jj|�|jj�dS)N)r�r%Z
pyclass_stackrr)rirBrr�rdr��namespace_cnamerrr�r�rZgenerate_injection_code�generate_disposal_coderrJrLrI�pop)rrWrr&r&r'r��s>								z&PyClassDefNode.generate_execution_code)rMrmrnr�r�rrr�rr�rrr�rqr�r�r&r&r&r'r�
sIr�c@s�eZdZdgZdZdZdZdZdZdZ	dZ
dZdd�Zdd�Z
dd	�Zd
d�Zdd
�Zdd�Zeddd��Zdd�ZdS)r
r�NFcCsbt|d�s[ddlm}|jrR|j|j|g|jdd�|_n	d|_|jS)N�_buffer_defaultsr)rnZ
need_completeF)r=r�rn�buffer_defaults_nodero�buffer_defaults_posr)rrCrnr&r&r'rp�s		zCClassDefNode.buffer_defaultscCs�|jrL|jdkrL|jjd�}|j||j�}|sRdSn|}|jd|jd|jddddd|jd	dd
|jd|jd|jd
|j	d|j
d|j|�d|j�
|_
dS)Nr�.r<r%r5r�implementingr�rE�objstruct_cname�
typeobj_cnamer�r*r}rp�shadow)r�r��splitrYr%�declare_c_classr	�objstruct_name�typeobj_namer*r}rprr�)rrCrW�
home_scoper&r&r'r��s(									zCClassDefNode.declarecs��jr#�jr#t�jd��jrCt�jd�j�d�_�jr�d�_x,�j	D]!}|j
�jkrh|�_qhW�jdkr�t�jd�j��_d�j_
�j�j��jjr��jjd}|j��}|tjtjtjfkr4�j|j��j}|dkrZt|jd�j�n�|tjkrrd}n�|jr�|jo�|jr�t|jd|�n�|j�s�t|jd|j
�jf�ny|jr|jjr|j rt|jd|�jf�n;|jrE|j
d,krEt|jd|j
�n	|�_�jj!d
d�dkr�|tjkr�t"�jdd��j#dk	}|r��jr��jjr��jj$j%��fdd��dS�jr(�j&dkr(�jj'd�}�j(|�j�}|s.dSn�}�j&dkr��jdkr��jt)j*kr��j+dd�dkr�t"�jd�jd�|j,d�jd�jd|o��j-d|o��j-d�jd�jd�jd�j.d�j&d �j/d!�j0d"�j1��d#�j2�
�_3�j2rI�j3|j�j�_4|�k	r��j&dkr��j5�j�j3�j��j3jj�_}	|	dk	r��j|	_�j6r�t7j8r�t9�j�j6�|	_6|rr�j#j:|	��jj;d$�}
|
rT|
j<rT|	j=rT|	j>rT�jj?d%�|
_@�jjAd$|
j6|
j��j-rid|	_=n	d|	_>tB�jj�dkrd|s��j-r�t�jjdjd&�x=�jjdd�D]%}|j��r�t|jd'�q�Wd�j3j_Cdd(lDmE}|jF�jd)|jG�jd*�j��j|jH�jd+g�g��_InN�jr��jjJp��jjC�j3j_Cd�_Ind�j3j_Cd�_I�jK�j3�x�j3jj$D]
}
|
�q�WdS)-NzTObject struct name specification required for C class defined in 'extern from' blockz:Decorators not allowed on cdef classes (used on type '%s')rrz+First base of '%s' is not an extension typez'%s' is not an extension typez*Base class '%s' of type '%s' is incompletez%Base class '%s' of type '%s' is finalr��str�byteszGinheritance from PyVarObject types like '%s' is not currently supported�freelistzIfreelists cannot be used on subtypes, only the base class can manage themcs
�j��S)N)r�r&)rCrr&r'�<lambda>Isz4CClassDefNode.analyse_declarations.<locals>.<lambda>rrZ__builtin__�zcpython.z %s already a builtin Cython typer<r%r5rr�rErrr�r*r}rprr}Z__dict__getterz'Only declare first base in declaration.z+Only one extension type base class allowed.)r�rTr9r8)r�r r!)Lr�rrr%r�r	rEr��moduleZcimported_modulesr<rr�Zhas_extern_class�add_imported_modulerirTr:r
r?r>r@rXZ
sign_and_namer/rr}rrr�r)r�r�r*rr�Zdefered_declarationsrr�rrYrZ
builtin_typesrrr�rr*r}rprr�rC�add_imported_entryr-rr�r.r�rhZis_variableZdefinedZimplementedZmangle_internalZgetter_cname�declare_propertyrJ�
early_initr�r�r�ZIdentifierStringNoder�type_init_argsZis_externalZallocate_vtable_names)rrCr%r�rEZbase_class_scopeZhas_bodyrWrr)Z
dict_entryZ
other_baser�Zthunkr&)rCrr'r�s�				
	
			*"											#			$	!	z"CClassDefNode.analyse_declarationscCsJ|jr-|jjj}|jj|�|_|jrF|jj|�|S)N)r�r�r/r)rqr*)rrCr)r&r&r'rq�s		z!CClassDefNode.analyse_expressionscCs6|jr2|j|j|�|jj|j|�dS)N)r�r�r)r�)rrCrWr&r&r'r��s	z+CClassDefNode.generate_function_definitionscCs�|j|j�|jr)|jj|�|jjjs�|jr�|jj|�d|jj	�}d|}|j
jtj
d�}|jd||jj	�f�|j|j||j��|j|�|jd||f�|jd�|jd||f�|j|j|j��|jd�|j
j|�|j|tj
�|j|�|jd	|jjj|f�|j|tj
�|jj|�|jj|�|j|j|d�dS)
NzPyTuple_GET_ITEM(%s, 1)z(((PyTypeObject*)PyTuple_GET_ITEM(%s, 0))Tz0%s = PyType_Type.tp_new(&PyType_Type, %s, NULL);z*if (((PyTypeObject*) %s)->tp_base != %s) {zPPyErr_Format(PyExc_TypeError, "best base '%s' must be equal to first base '%s'",zB             ((PyTypeObject*) %s)->tp_base->tp_name, %s->tp_name);r�z%s.tp_bases = %s;)r�r%r�r�r�r/r)r*rBr�r�r�r
rrQr�r�r�r�r�rGrr�rrI�generate_type_ready_code)rrWriZ
first_baseZ
trial_typer&r&r'r��s8		
	
	
	

 z%CClassDefNode.generate_execution_codecCs�|j}|j}|j}|s%dS|jdkr�x!tjD]}|j||�q>W|r�|jjt	j
dd��d}nd}|jd|||j|j
�f�|jd|�tj|d�}tj|d�}	|d	krw|	d	krw|jrd
}
d}nd}
d
}|jjt	j
|d��|jd||f�|jd||
f�|jd�xZ|jjjD]I}|jd-k}
|jr�tjr�|jr�|
r�tjj|j�}|r�|j�nd}|r�|j|�|jd�|jd�|jd||j|jd|j
�f�|jd�|jd|j�|jd|j|jf�|jd|j�|jd�|jd�|jd�|r�|jd�q�W|jrg|jjt	j
dd��|jd||j|j|j
�f�|rg|jjt	j
d d��|jd!||j|j
�f�|jjr�|jjd"r�|jd#tj |j!||j|j
�f�|j"s�|j#d$�nd}|rF|jt$kr6d%|}|j%r|j&}n
d&|j&}|jd'||||j'f�nt(|j
d(�|j"s^|j#d)�ndr�|jjt	j
d*d��|jd+||j|j
�f�|jr�|jd,|j)|jf�dS).NrZPyType_ReadyzExtensionTypes.cZ__Pyx_PyType_Readyzif (%s(&%s) < 0) %sz%s.tp_print = 0;Ztp_getattroZ
tp_dictoffsetrbZ#__Pyx_PyObject_GenericGetAttrNoDictZPyObject_GenericGetAttrNoDictZ__Pyx_PyObject_GenericGetAttrZPyObject_GenericGetAttrzObjectHandling.cz�if ((CYTHON_USE_TYPE_SLOTS && CYTHON_USE_PYTYPE_LOOKUP) && likely(!%s.tp_dictoffset && %s.tp_getattro == PyObject_GenericGetAttr)) {z%s.tp_getattro = %s;r�r�r�z#if CYTHON_COMPILING_IN_CPYTHONr�zEPyObject *wrapper = PyObject_GetAttrString((PyObject *)&%s, "%s"); %s�wrapperz/if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {z0%s = *((PyWrapperDescrObject *)wrapper)->d_base;z%s.doc = %s;z0((PyWrapperDescrObject *)wrapper)->d_base = &%s;z#endifZ	SetVTablezImportExport.cz+if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %sZMergeVTablesz#if (__Pyx_MergeVtables(&%s) < 0) %s�internalz=if (PyObject_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s�__weakref__z%s.tp_weaklistoffsetz	struct %sz#if (%s == 0) %s = offsetof(%s, %s);z)__weakref__ slot must be of type 'object'Z__reduce_cython__ZSetupReducez.if (__Pyx_setup_reduce((PyObject*)&%s) < 0) %sz	%s = &%s;)r�r�)*r/rr)r�rZ
slot_tableZgenerate_dynamic_init_coder�rerr�rQr�r%Zget_slot_code_by_namer�Zpyfunc_entriesr<r
rr�r�r)r*r+r�r�Zvtable_cnameZ
vtabptr_cnameZis_internalr�r	r�r	Zis_closure_class_scoperhrr*rrrr�)r�rWZheap_type_basesr/rr)r,Z	readyfuncZgetattr_slot_funcZdictoffset_slot_funcZpy_cfuncZutility_funcr\r�rZ
weakref_entryZtp_weaklistoffsetZ	objstructr&r&r'r+�s�									






				
	
				z&CClassDefNode.generate_type_ready_codecCs6|jr|jj|�|jr2|jj|�dS)N)r*r�r�)rrWr&r&r'r�Ps		zCClassDefNode.annotate)rMrmrnr�rrr*r}rrr�rrpr�r�rqr�r�r�r+r�r&r&r&r'r
�s$	�#�r
c@sUeZdZdgZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�PropertyNoder�cCsM|j|j|j|j�|_|j|jj_|jj|jj�dS)N)	r(r<r-r%r�r�r)r�r�)rrCr&r&r'r�as!z!PropertyNode.analyse_declarationscCs|jj|�|_|S)N)r�rq)rrCr&r&r'rqfsz PropertyNode.analyse_expressionscCs|jj||�dS)N)r�r�)rrCrWr&r&r'r�jsz*PropertyNode.generate_function_definitionscCsdS)Nr&)rrWr&r&r'r�msz$PropertyNode.generate_execution_codecCs|jj|�dS)N)r�r�)rrWr&r&r'r�pszPropertyNode.annotateN)	rMrmrnr�r�rqr�r�r�r&r&r&r'r/Ws	r/c@s:eZdZgZdd�Zdd�Zdd�ZdS)�
GlobalNodecCs+x$|jD]}|j||j�q
WdS)N)�namesZdeclare_globalr%)rrCr<r&r&r'r�{szGlobalNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rqszGlobalNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz"GlobalNode.generate_execution_codeN)rMrmrnr�r�rqr�r&r&r&r'r0tsr0c@s:eZdZgZdd�Zdd�Zdd�ZdS)�NonlocalNodecCs+x$|jD]}|j||j�q
WdS)N)r1Zdeclare_nonlocalr%)rrCr<r&r&r'r��sz!NonlocalNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rq�sz NonlocalNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz$NonlocalNode.generate_execution_codeN)rMrmrnr�r�rqr�r&r&r&r'r2�sr2c@sgeZdZdgZdd�Zdd�Zdd�ZdZd	d
�Zdd�Z	d
d�Z
dS)r�r�c
Cs@ddlm}|j}t||j�r�|jj�}|dkr<|j�\}}t|�rtt	|j
d�xd|jD]Y\}}|j|�}	|	dkr�t	|j
d�q~|j
|j|	|j
dd�q~Wt|_nUt|dd�dk	r<|jr!|j|�t|_n|js3|jr<t|_dS)	Nr)r�r�z!Variable names must be specified.zUnknown typerTrB)r�r�r�rKZGeneralCallNoderI�as_cython_attribute�explicit_args_kwdsrJrr%r8r:r,r9�PassStatNoderLr�r;Zdeclare_from_annotation�is_attribute�is_subscript)
rrCr�r�r\rTrU�varr$r/r&r&r'r��s(	#	
z!ExprStatNode.analyse_declarationscCs1d|j_|jj|�|_d|j_|S)NF)r��result_is_usedrq)rrCr&r&r'rq�sz ExprStatNode.analyse_expressionscCs)|jjjr%|jjr%|j�dS)N)r�r/r=�is_tempr�)rrCr&r&r'r��szExprStatNode.nogil_checkzDiscarding owned Python objectcCs�|j|j�d|j_|jj|�|jjr�|jj�r�|jj�}|jjjspd|}|j	d|�|jj
|�|jj|�dS)NFz
(void)(%s)z%s;)r�r%r�r9rBr:r�r/r�rQrrI)rrWr�r&r&r'r��s
z$ExprStatNode.generate_execution_codecCs|jj||�dS)N)r�r�)rrCrWr&r&r'r��sz*ExprStatNode.generate_function_definitionscCs|jj|�dS)N)r�r�)rrWr&r&r'r��szExprStatNode.annotateN)rMrmrnr�r�rqr�r�r�r�r�r&r&r&r'r��s	r�c@s(eZdZdd�Zdd�ZdS)�AssignmentNodecCs`|j|�}t|t�r\t|t�r\|jjjr\|jj�r\t|j	d�|S)Nz9Storing unsafe C derivative of temporary Python reference)
rprKr;�ParallelAssignmentNode�rhsr/r�Zis_ephemeralrr%)rrCrVr&r&r'rq�s
z"AssignmentNode.analyse_expressionscCs.|j|j�|j|�|j|�dS)N)r�r%�generate_rhs_evaluation_coderL)rrWr&r&r'r��s
z&AssignmentNode.generate_execution_codeN)rMrmrnrqr�r&r&r&r'r;�s	r;c@s�eZdZddgZdZdZdZdd�Zddd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
ddd�Zdd�Zdd�ZdS)�SingleAssignmentNoderr=Fc	Cs-ddlm}t|j|j�r|jjj�}|r|jj�\}}|dkrHt|�dkr�t	|dj
d�dS|dk	r�|jd�}|dks�d|kr�t	|j
d�dS|d}nd	}|d
j|�}|dkrt	|d
j
d�dS|j
}	|dkr�t|	|j�rX|	j|	j
fg}
n?t|	|j�r�dd
�|	jD�}
nt	|	j
d�dSx3|
D]+\}}|j|||ddd|�q�Wt|�dkr�|d|_qEd|_qd|_t|	|j�s&t	|	j
d�|j|	j||j
dd	�n�|dkr�d|_t|�d
ks{|dkr�t	|jj
d�dSg}
xa|jD]V\}}|j|�}|dkr�t	|j
d�q�|
j|j||j
f�q�Wt|
�t|j�krdSt|j
|j�sCt	|j
j
d�|j
j}t|�}|j|||d|jj
�x�|
D]"\}}}|j|||�q�Wnb|dkrd|_|r�t	|jjj
d�t|jj
d|j
jd|�}|j|�|jrdS|j
j|�dS)Nr)r�r��typedefr"zInvalid positional argument.r�zInvalid keyword argument.rQrzUnknown typecSs"g|]}|j|jf�qSr&)r<r%)r�r8r&r&r'r� s	z=SingleAssignmentNode.analyse_declarations.<locals>.<listcomp>zInvalid declarationrTzInvalid declaration.r(�unionz.Struct or union members must be given by name.F�
fused_typez*fused_type does not take keyword argumentsr<rb)r�r@)r(rA)r�r�rKr=ZCallNoderIr3r4rJrr%�compile_time_valuer:rr�r<r�rTr,�declaration_onlyryr8rr9rr.rxr�r
)rrCr��	func_namerTrUZkwdictr�r/r�varsr8r%�members�memberr$r<r)Z	fusednoder&r&r'r�s�
	#	"	 	
	z)SingleAssignmentNode.analyse_declarationsrcCspddlm}|jj|�|_|j|�}|r>|S|jj|�|_|jj|�|j|�}|r||St	|j|j
�r�|jj|j�|jj|j�|_nf|jj
jr%t	|j|j�s%|j|jjd|jdddd�|_|jj|�|_|jj
jr�|j|jd|jj
|jj
g�}|r�|j}d|_|j
j|_|j
j|_|jdkr�|jdkr�|jtjd	d
��q|jj|jj
|�}n|jj|jj
|�}|s6|js6|jrH|jrH|j
jrH|j|�}n|j
jrc|j|�}||_|S)Nr)r�r�rY�stop�=TrZCppExceptionConversionzCppSupport.cpp) r�r�r=rp�
unroll_rhsrrr�gil_assignment_check�
unroll_lhsrKZMemoryViewIndexNodeZanalyse_broadcast_operationZ$analyse_as_memview_scalar_assignmentr/r��SliceIndexNoder%r�Zlookup_operator_for_types�is_overloaded_assignmentr
r�rerr�rr6r;r�r=�coerce_to_temp�coerce_to_simple)rrC�use_tempr�Zunrolled_assignment�opr=r&r&r'rpWsH-*			z"SingleAssignmentNode.analyse_typescCsyddlm}m}|}d}}}	}
|jjrI|jj}n�|jjsa|jjr8x6t||j	�r�|j
p�|jr�|j}}qdWt||j	�r|j}|j
}|r�|j
tj|�}|j}|r|j
tj|�}n}|jjri|jjri|j|jdt|jj�dt|jjt�rZ|jjn|j�}nt|jd�dSd}	|	r�|	j
tj|�}	dd�}y-||d�||d�||	d�}Wq5tk
r�t|jd	�dSYq5Xq<|jjr1|jj}t|t�s5dSq<dSndS||krft|jd
||f�dSg}
|j|�}|g}|r�|jr�|j|�}|j|�|r�|jr�|j|�}|j|�|	r|	jr|j|	�}	|j|	�xat|�D]S}|j|jdt|�d|dtj�}|	dk	r�|	j�r�|j|	j}|j|jdt|�d|�}n$|j|jdd
d|	d|�}|dk	r:|j�r|j�r|j|j}|j|jdt|�d|�}n$|j|jddd|d|�}|
j|j|jd|d|j |���qW|
||
fS)Nr)r��	UtilNodesr9r~z*C array iteration requires known end indexcSs3|dkr|S|j�r#|jStd��dS)NzNot a constant.)�has_constant_resultr~r�)rVZ
none_valuer&r&r'�	get_const�s
z.SingleAssignmentNode.unroll.<locals>.get_constrz5C array assignment currently requires known endpointsz=Assignment to/from slice of wrong length, expected %s, got %sr/�operatorr�Zoperand1Zoperand2rr�r�)!r�r�rTr/�	is_ctupler�r�r�rKrNrYrIr�rr
r�ZIntNoder%r rZconstant_value_not_setrr�Z
LetRefNoder�rr_rUr~ZMulNodeZAddNodeZ	IndexNoderp)rrVZtarget_sizerCr�rTr�Z
start_nodeZ	stop_nodeZ	step_node�
check_nodeZ
slice_sizerVre�refsr�Zix_nodeZ
step_valueZindex_valuer&r&r'�unroll�s�(			!-
	


-'$'2zSingleAssignmentNode.unrollc
Cs�ddlm}g}xHt||�D]7\}}	|jt|jd|d|	d|j��q&Wtd|jd|�j|�}
|r�t	d|jd||
g�}
x-|ddd�D]}|j
||
�}
q�W|
S)	Nr)rTrr=�firstr%r�r�)r�rTr8rr?r%r\r<rqr�ZLetNode)rrZrY�lhs_listZrhs_listrCrT�assignmentsrr=rVr�r&r&r'�unroll_assignments�s/!z'SingleAssignmentNode.unroll_assignmentscCs�ddlm}t|j|j�s)dStdd�|jjD��rLdS|j|jt	|jj�|�}|szdS|\}}}|j
|||jj||�S)Nr)r�css|]}|jVqdS)N)Z
is_starred)r�r2r&r&r'r��sz2SingleAssignmentNode.unroll_rhs.<locals>.<genexpr>)r�r�rKrr��anyrTr[r=rJr_)rrCr��unrolledrYrZr=r&r&r'rK�s$zSingleAssignmentNode.unroll_rhscCs�|jjjrdSddlm}t|j|j�s<dS|j|jt	|jj
�|�}|sjdS|\}}}|j||||jj
|�S)Nr)r�)rr/rXr�r�rKr=r�r[rJrTr_)rrCr�rarYrZrr&r&r'rM�s$zSingleAssignmentNode.unroll_lhscCs|jj|�dS)N)r=rB)rrWr&r&r'r>sz1SingleAssignmentNode.generate_rhs_evaluation_codec	CsW|jr=|jj|j|d|jd|jd|j�n|jj|j|�dS)NrKr
r�)rOrrLr=r
r�)rrWrKr&r&r'rLs				
z-SingleAssignmentNode.generate_assignment_codecCs|jj||�dS)N)r=r�)rrCrWr&r&r'r�sz2SingleAssignmentNode.generate_function_definitionscCs$|jj|�|jj|�dS)N)rr�r=)rrWr&r&r'r�szSingleAssignmentNode.annotateN)rMrmrnr�r\rOrDr�rpr[r_rKrMr>rLr�r�r&r&r&r'r?�sT0^
r?c@s�eZdZddddgZdZdZdZdd�Zdd	d
�Zdd�Z	d
dd�Z
dd�Zdd�ZdS)�CascadedAssignmentNoder]r=�coerced_values�
cloned_valuesNcCs%x|jD]}|j|�q
WdS)N)r]r
)rrCrr&r&r'r�5sz+CascadedAssignmentNode.analyse_declarationsrcCs�ddlm}m}t�}xTt|j�D]C\}}|j|�}|j|<|j|�|j|j	�q/W|j
j|�}t|�dkrt
t|��jr�|jd||j
g�}	|	s|j|j�|�}n|j|j�|�}|jrD|jrD|s2|js2|j	jrD|j|�}n|j|�}|jrh||�n||_
g|_i}
g|_x�|jD]�}|j	jo�|jd||j
g�}|jj|�|j	|
kr�|j	|j	kr�||j
�}|s|j|j	|�}|jj|�||
|j	<q�Wg|_x?|jD]4}|
j|j	|j
�}|jj||��qOW|S)Nr)�	CloneNoderrJ)r�rerrzrr]rrrLr�r/r=rprJ�next�iterr�Zlookup_operatorrrr;r�r6r=rPrQr:rc�assignment_overloadsrrdr*)rrCrRrerZ	lhs_typesr"rr=rSrcZ
overloadedr&r&r'rp9sF	
		'!	z$CascadedAssignmentNode.analyse_typescCs|jj|�dS)N)r=rB)rrWr&r&r'r>jsz3CascadedAssignmentNode.generate_rhs_evaluation_codeFcCs�x|jD]}|j|�q
WxOt|j|j|j�D]2\}}}|j|�|j||d|�q=Wx+|jD] }|j|�|j|�q}W|j	j|�|j	j|�dS)NrK)
rcrBr8r]rdrhrLrrIr=)rrWrKr=rZoverload�	rhs_valuer&r&r'rLms+

z/CascadedAssignmentNode.generate_assignment_codecCs|jj||�dS)N)r=r�)rrCrWr&r&r'r�|sz4CascadedAssignmentNode.generate_function_definitionscCsux|jD]}|j|�q
Wx=t|j|j�D]&\}}|j|�|j|�q7W|jj|�dS)N)rcr�r8r]rdr=)rrWr=rr&r&r'r�s"
zCascadedAssignmentNode.annotate)
rMrmrnr�rdrcrhr�rpr>rLr�r�r&r&r&r'rb"s1rbc@sUeZdZdgZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
r<r�cCs%x|jD]}|j|�q
WdS)N)r�r�)rrCr�r&r&r'r��sz+ParallelAssignmentNode.analyse_declarationscs#�fdd�|jD�|_|S)Ncs%g|]}|j�dd��qS)rRr)rp)r�r�)rCr&r'r��s	z>ParallelAssignmentNode.analyse_expressions.<locals>.<listcomp>)r�)rrCr&)rCr'rq�sz*ParallelAssignmentNode.analyse_expressionscCsV|j|j�x|jD]}|j|�qWx|jD]}|j|�q;WdS)N)r�r%r�r>rL)rrWr�r&r&r'r��s
z.ParallelAssignmentNode.generate_execution_codecCs(x!|jD]}|j||�q
WdS)N)r�r�)rrCrWr�r&r&r'r��sz4ParallelAssignmentNode.generate_function_definitionscCs%x|jD]}|j|�q
WdS)N)r�r�)rrWr�r&r&r'r��szParallelAssignmentNode.annotateN)	rMrmrnr�r�rqr�r�r�r&r&r&r'r<�s	r<c@sXeZdZddgZdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�InPlaceAssignmentNoderr=cCs|jj|�dS)N)rr
)rrCr&r&r'r��sz*InPlaceAssignmentNode.analyse_declarationscCs�|jj|�|_|jj|�|_|jjsB|jjrc|jj|jj|�|_n9|jjjr�|j	dkr�|jjt
j|�|_|S)Nz+-)r=rprrr�is_memview_index�is_buffer_accessrr/�	is_stringrWr
r�)rrCr&r&r'rp�s!z#InPlaceAssignmentNode.analyse_typescCsn|j|j�|j|j}}|j|�|j|�|j}|dkr[d}n|dkrwt|jd�|js�|j	r�|j
jr�t|jd�|dkr�|j
jr�|j
jdr�t|jd�|j|||�nB|jrt|jd	�n&|jd
|j�||j�f�|j|�|j|�|j|�|j|�dS)Nz//�/z**zNo C inplace power operatorzAIn-place operators not allowed on object buffers in this release.�%�	cdivisionz;In-place non-c divide operators not allowed on int buffers.z4Inplace operators not supported on memoryview slicesz
%s %s= %s;)rnro)r�r%rr=rBZ generate_subexpr_evaluation_coderWrrlrkr/r=r�r�r�Zgenerate_buffer_setitem_codeZis_memview_slicerQr�Zgenerate_subexpr_disposal_codeZfree_subexpr_tempsrrI)rrWrr=Zc_opr&r&r'r��s,

		)	&


z-InPlaceAssignmentNode.generate_execution_codecCs$|jj|�|jj|�dS)N)rr�r=)rrWr&r&r'r��szInPlaceAssignmentNode.annotatecCs2ddlm}|j|j|j|j|j�S)Nr)r�)r�r�Z
binop_noder%rWrr=)rr�r&r&r'�create_binop_node�sz'InPlaceAssignmentNode.create_binop_nodeN)	rMrmrnr�r�rpr�r�rqr&r&r&r'rj�srjc@s[eZdZddgZdd�ZejZdZdd�Z	dd	�Z
d
d�ZdS)
�
PrintStatNode�	arg_tuplerOcCs�|jr-|jj|�}|j|�|_|jj|�}|j|�|_|jt�t|jj�dkr�|jr�|jt	�|S)Nr)
rOrq�coerce_to_pyobjectrsre�printing_utility_coderJrT�append_newline�printing_one_utility_code)rrCrOrsr&r&r'rqs	
!
z!PrintStatNode.analyse_expressionszPython print statementcCsZ|j|j�|jr;|jj|�|jj�}nd}t|jj�dkr�|jr�|jjd}|j|�|j	d||j�|j
|j�f�|j|�|j|�ne|jj|�|j	d||jj�|j|j
|j�f�|jj|�|jj|�|jrV|jj|�|jj|�dS)Nrbrrz"if (__Pyx_PrintOne(%s, %s) < 0) %sz#if (__Pyx_Print(%s, %s, %d) < 0) %s)
r�r%rOrB�	py_resultrJrsrTrvrQr�rrI)rrWZ
stream_resultr2r&r&r'r�s6	!
	
	z%PrintStatNode.generate_execution_codecCs3|jr|jj||�|jj||�dS)N)rOr�rs)rrCrWr&r&r'r�6s	z+PrintStatNode.generate_function_definitionscCs-|jr|jj|�|jj|�dS)N)rOr�rs)rrWr&r&r'r�;s	zPrintStatNode.annotateN)rMrmrnr�rqr|r�r�r�r�r�r�r&r&r&r'rr�s	!rrc@sLeZdZdgZdd�ZejZdZdd�Z	dd�Z
d	S)
�ExecStatNoderTcCs_xHt|j�D]7\}}|j|�}|j|�}||j|<qW|jtj�|S)N)rrTrqrtrerZpyexec_utility_code)rrCr"r2r&r&r'rqHsz ExecStatNode.analyse_expressionszPython exec statementcCs"|j|j�g}x1|jD]&}|j|�|j|j��q Wt|ddgddt|���}|jj	t
jdd�}|jd|f|�x+|jD] }|j
|�|j|�q�W|j|j||j��|j|�|j|t�|jj|�dS)Nrbr$r�Tz%s = __Pyx_PyExec3(%s, %s, %s);)r�r%rTrBrrxr�rJr�r�r
rrQrrIr�r�r�r�)rrWrTr2Ztemp_resultr&r&r'r�Ss 
*

z$ExecStatNode.generate_execution_codecCs%x|jD]}|j|�q
WdS)N)rTr�)rrWr2r&r&r'r�eszExecStatNode.annotateN)rMrmrnr�rqr|r�r�r�r�r�r&r&r&r'ryAs		ryc@saeZdZdgZdZdd�Zdd�Zdd�Zd	Zd
d�Z	dd
�Z
dS)�DelStatNoderTFcCs%x|jD]}|j|�q
WdS)N)rTr
)rrCr2r&r&r'r�rsz DelStatNode.analyse_declarationscCsx�t|j�D]�\}}|j|d�}|j|<|jjsZ|jr�|jjr�|jr�|jjr�t	|j
d�q|jjr�|jjj
r�|j|�q|jj
r�t	|j
d�q|jr�|jjtjkr�qt	|j
d�qW|S)NzDeletion of global C variablezDeletion of non-heap C++ objectz&Deletion of non-Python, non-C++ object)rrTrr/r=r;r�r�Z
is_cglobalrr%r�rEr�r�r7r�r�bytearray_type)rrCr"r2r&r&r'rqvs!zDelStatNode.analyse_expressionscCs.x'|jD]}|jjr
|j�q
WdS)N)rTr/r=r�)rrCr2r&r&r'r��szDelStatNode.nogil_checkzDeleting Python objectcCs�|j|j�x�|jD]�}|jjsV|jjsV|jro|jjtj	kro|j
|d|j�q|jjr|jj
jr|j|�|jd|j��|j|�qWdS)N�ignore_nonexistingz
delete %s;)r�r%rTr/r=r�r7r�rr{Zgenerate_deletion_coder|r�rEr�rBrQr�r)rrWr2r&r&r'r��s
z#DelStatNode.generate_execution_codecCs%x|jD]}|j|�q
WdS)N)rTr�)rrWr2r&r&r'r��szDelStatNode.annotateN)rMrmrnr�r|r�rqr�r�r�r�r&r&r&r'rzjs	rzc@s.eZdZgZdd�Zdd�ZdS)r5cCs|S)Nr&)rrCr&r&r'rq�sz PassStatNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz$PassStatNode.generate_execution_codeN)rMrmrnr�rqr�r&r&r&r'r5�sr5cs(eZdZdZ�fdd�Z�S)�IndirectionNodez�
    This adds an indirection so that the node can be shared and a subtree can
    be removed at any time by clearing self.stats.
    cs'tt|�j|djd|�dS)Nrr�)rfr}r�r%)rr�)rLr&r'r��szIndirectionNode.__init__)rMrmrnryr�r&r&)rLr'r}�sr}c@s4eZdZgZdZdd�Zdd�ZdS)�
BreakStatNodeTcCs|S)Nr&)rrCr&r&r'rq�sz!BreakStatNode.analyse_expressionscCs@|j|j�|js,t|jd�n|j|j�dS)Nzbreak statement not inside loop)r�r%�break_labelrr�)rrWr&r&r'r��s	z%BreakStatNode.generate_execution_codeN)rMrmrnr�r�rqr�r&r&r&r'r~�sr~c@s4eZdZgZdZdd�Zdd�ZdS)�ContinueStatNodeTcCs|S)Nr&)rrCr&r&r'rq�sz$ContinueStatNode.analyse_expressionscCsA|jst|jd�dS|j|j�|j|j�dS)Nz"continue statement not inside loop)�continue_labelrr%r�r�)rrWr&r&r'r��s
	z(ContinueStatNode.generate_execution_codeN)rMrmrnr�r�rqr�r&r&r&r'r��sr�c@seZdZdgZdZdZdZdZdd�Zdd�Z	dZ
d	d
�Zdd�Zd
d�Z
dd�ZdS)rLr9TFcCs�|j}||_|s,t|jd�|S|jr�|jrNt|jd�|jj|�|_|jsu|jr�t|jjd�q�|jj|j|�|_n.|jr�|j	r�|jr�t|jd�|S)Nz!Return not inside a function bodyz$Return with value in async generatorz"Return with value in void functionzReturn value required)
rrr%r9�in_async_genrpr�r�rr=)rrCrr&r&r'rq�s"				


z"ReturnStatNode.analyse_expressionscCs|jjr|j�dS)N)rr=r�)rrCr&r&r'r��szReturnStatNode.nogil_checkzReturning Python objectc
CsK|j|j�|jsdS|j}|jjr]|jtj|j�|r]|jr]d}|rn|j	|�|jj
r�ddlm}|j
dtjd|jd|jd|d|d|j�n�|jr|jjtjd	d
��|jdtj|j�f�|j|�n3|j|�|jdtj|j|j�f�|j|�|j|�n�|jjr�|jr�|jr�|jjtjd
d
��|jd�|jdtj�q|jtj|j�n"|jjr|j||jj �x-|j!j"�D]\}}|j#||�qW|j$|j%�dS)Nr)raZ	lhs_cnameZlhs_typeZlhs_posr=rWr�ZReturnWithStopIterationzCoroutine.cz-%s = NULL; __Pyx_ReturnWithStopIteration(%s);z%s = %s;r�z/PyErr_SetNone(__Pyx_PyExc_StopAsyncIteration); z
%s = NULL;)&r�r%rr9r=r�r	r�r�rBr�r�raZput_acquire_memoryviewsliceZin_nogil_context�in_generatorr�rerr�rQrxrrErFrHrIr�r�r�r��
put_returnrAr�Ztemps_holding_referencer�r�r�)rrWr9rarr/r&r&r'r�sZ		
				
			
	
			
z&ReturnStatNode.generate_execution_codecCs4|jr|jd�|jdtj|f�dS)Nz%#pragma omp critical(__pyx_returning)z%s = %s;)�in_parallel�putln_openmprQr	r�)rrWr9r&r&r'r�;s	
zReturnStatNode.put_returncCs&|jdk	r"|jj||�dS)N)r9r�)rrCrWr&r&r'r�@sz,ReturnStatNode.generate_function_definitionscCs|jr|jj|�dS)N)r9r�)rrWr&r&r'r�Ds	zReturnStatNode.annotateN)rMrmrnr�r�r�r�r�rqr�r�r�r�r�r�r&r&r&r'rL�s	9rLc@sgeZdZddddgZdZdd�ZejZdZ	d	d
�Z
dd�Zd
d�ZdS)�
RaiseStatNode�exc_type�	exc_value�exc_tb�causeTcCsp|jr-|jj|�}|j|�|_|jrZ|jj|�}|j|�|_|jr�|jj|�}|j|�|_|jr�|jj|�}|j|�|_d|_|jrl|jrl|jrl|j}ddlm}t	||j
�r3|jp&|jdk	o&|jjr3|j
}|jrl|jjrl|j|_|jdkrld|_|S)Nr)r��MemoryError)r�rprtr�r�r��builtin_exc_namer�r�rKrKrTrsrIr;r��
is_builtinr<)rrCr�r�r�r��excr�r&r&r'rqTs0						%		z!RaiseStatNode.analyse_expressionszRaising exceptioncCs�|j|j�|jdkr@|jd|j|j��dS|jr�|jj|�|jj�}|jjr�|j	j
|jj�nd}|jr�|jj|�|jj�}nd}|j
r�|j
j|�|j
j�}nd}|jr |jj|�|jj�}nd}|j	jt�|jd||||f�xF|j|j|j
|jfD]&}|rr|j|�|j|�qrW|j|j|j��dS)Nr�zPyErr_NoMemory(); %srbz__Pyx_Raise(%s, %s, %s, %s);)r�r%r�rQr�r�rBrxr;r�Zuse_entry_utility_coder�r�r�r�re�raise_utility_coderrI)rrWZ	type_codeZ
value_codeZtb_codeZ
cause_coder"r&r&r'r�rsF				%
z%RaiseStatNode.generate_execution_codecCs�|jdk	r"|jj||�|jdk	rD|jj||�|jdk	rf|jj||�|jdk	r�|jj||�dS)N)r�r�r�r�r�)rrCrWr&r&r'r��sz+RaiseStatNode.generate_function_definitionscCsh|jr|jj|�|jr2|jj|�|jrK|jj|�|jrd|jj|�dS)N)r�r�r�r�r�)rrWr&r&r'r��s				zRaiseStatNode.annotateN)
rMrmrnr�r�rqr|r�r�r�r�r�r�r&r&r&r'r�Is	*
r�c@sCeZdZgZdZdd�ZejZdZ	dd�Z
dS)�ReraiseStatNodeTcCs|S)Nr&)rrCr&r&r'rq�sz#ReraiseStatNode.analyse_expressionszRaising exceptioncCs|j|j�|jj}|r�|jjt�|j|d�|j|d�|j|d�|j	dt
|��x|D]}|jd|�q�W|j	�|j	|j|j��n9|jjt
jdd��|j	d|j|j��dS)	Nrrr"z&__Pyx_ErrRestoreWithState(%s, %s, %s);z%s = 0; ZReRaiseExceptionzExceptions.cz__Pyx_ReraiseException(); %s)r�r%r��exc_varsr�rerrGrrQr�r�r�rr�)rrWrFZvarnamer&r&r'r��s

	z'ReraiseStatNode.generate_execution_codeN)rMrmrnr�r�rqr|r�r�r�r�r&r&r&r'r��s	r�c@s[eZdZddgZdd�ZejZdZdd�Z	dd	�Z
d
d�ZdS)
�AssertStatNoderr9cCs�|jj|�|_|jr�|jj|�}|jtjksO|jjr�ddlm	}||j
d|gdd�}|j|dd�j|�|_n|j|�|_|S)Nr)r�rTZslowTZ
skip_children)rZanalyse_boolean_expressionr9rpr/rZ
tuple_typerr�r�r%rt)rrCr9r�r&r&r'rq�s	$z"AssertStatNode.analyse_expressionszRaising exceptioncCs|jd�|jd�|j|j�|jj|�|jd|jj��|jr�|jj|�|jd|jj��|jj|�|jj	|�n
|jd�|j|j
|j��|jd�|jj|�|jj	|�|jd�|jd�dS)Nz!#ifndef CYTHON_WITHOUT_ASSERTIONSz!if (unlikely(!Py_OptimizeFlag)) {zif (unlikely(!%s)) {z*PyErr_SetObject(PyExc_AssertionError, %s);z$PyErr_SetNone(PyExc_AssertionError);r�z#endif)rQr�r%rrBr�r9rxrrIr�)rrWr&r&r'r��s.

	z&AssertStatNode.generate_execution_codecCs9|jj||�|jdk	r5|jj||�dS)N)rr�r9)rrCrWr&r&r'r�sz,AssertStatNode.generate_function_definitionscCs-|jj|�|jr)|jj|�dS)N)rr�r9)rrWr&r&r'r�s	zAssertStatNode.annotateN)rMrmrnr�rqr|r�r�r�r�r�r�r&r&r&r'r��s
	r�c@sgeZdZddgZdd�Zdd�Zdd�Zd	d
d�Zdd
�Zdd�Z	dS)�
IfStatNode�
if_clauses�else_clausecCs>x|jD]}|j|�q
W|jr:|jj|�dS)N)r�r�r�)rrC�	if_clauser&r&r'r�s	zIfStatNode.analyse_declarationscsA�fdd�|jD�|_|jr=|jj��|_|S)Ncsg|]}|j���qSr&)rq)r�r�)rCr&r'r�s	z2IfStatNode.analyse_expressions.<locals>.<listcomp>)r�r�rq)rrCr&)rCr'rqs	zIfStatNode.analyse_expressionscCs|j|j�|j�}t|j�}|jrW|j|jd|jdd�n
|d8}xLt|j�D];\}}|j||j�|j	||d||k�qqW|jr�|j|jj�|j
d�|jj	|�|j
d�|j|�dS)Nr�inverseT�is_lastz
/*else*/ {r�r�)r�r%r�rJr�r��_set_branch_hintrr�r�rQr�)rrWr��lastr"r�r&r&r'r� s	#
 	

z"IfStatNode.generate_execution_codeFcCs�|js
dSt|t�s'|jr+dSt|jdttf�r�t|j�dkr�ttt	t
tf}x.|jdd�D]}t||�s�dSq�W|r�dnd|_dS)Nrr�rr�r�)
r�rKr�r�r�r�rJr�r;rzr0r2�branch_hint)r�clauseZstatements_noder�Znon_branch_nodesrVr&r&r'r�3s	zIfStatNode._set_branch_hintcCsJx!|jD]}|j||�q
W|jdk	rF|jj||�dS)N)r�r�r�)rrCrWr�r&r&r'r�Bsz(IfStatNode.generate_function_definitionscCs>x|jD]}|j|�q
W|jr:|jj|�dS)N)r�r�r�)rrWr�r&r&r'r�Hs	zIfStatNode.annotateN)
rMrmrnr�r�rqr�r�r�r�r&r&r&r'r�sr�c@s^eZdZddgZdZdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�IfClauseNode�	conditionr�NcCs|jj|�dS)N)r�r�)rrCr&r&r'r�Xsz!IfClauseNode.analyse_declarationscCs.|jj|�|_|jj|�|_|S)N)r��analyse_temp_boolean_expressionr�rq)rrCr&r&r'rq[sz IfClauseNode.analyse_expressionscCs�|jj|�|j|j�|jj�}|jrKd|j|f}|jd|�|jj|�|jj|�|j	j
|�|j|jdd�|p�|j	js�|j|�|jd�dS)Nz%s(%s)z	if (%s) {r�Fr�)
r�rBr�r%r�r�rQrrIr�r�r�r�)rrWr�r�r�r&r&r'r�`s	
z$IfClauseNode.generate_execution_codecCs*|jj||�|jj||�dS)N)r�r�r�)rrCrWr&r&r'r�osz*IfClauseNode.generate_function_definitionscCs$|jj|�|jj|�dS)N)r�r�r�)rrWr&r&r'r�sszIfClauseNode.annotate)
rMrmrnr�r�r�rqr�r�r�r&r&r&r'r�Osr�c@s@eZdZddgZdd�Zdd�Zdd�Zd	S)
�SwitchCaseNode�
conditionsr�cCsxE|jD]:}|j|j�|j|�|jd|j��q
W|jj|�|j|jdd�|jd�dS)Nzcase %s:r�Fzbreak;)r�r�r%rBrQr�r�r�)rrWrr&r&r'r��s
z&SwitchCaseNode.generate_execution_codecCs;x!|jD]}|j||�q
W|jj||�dS)N)r�r�r�)rrCrWrr&r&r'r��sz,SwitchCaseNode.generate_function_definitionscCs5x|jD]}|j|�q
W|jj|�dS)N)r�r�r�)rrWrr&r&r'r��szSwitchCaseNode.annotateN)rMrmrnr�r�r�r�r&r&r&r'r�xs	r�c@sCeZdZdddgZdd�Zdd�Zdd	�Zd
S)�SwitchStatNode�test�casesr�cCs�|jj|�|j|j�|jd|jj��x|jD]}|j|�qDW|jdk	r�|jd�|jj|�|jd�n
|jd�|jd�dS)Nz
switch (%s) {zdefault:zbreak;zdefault: break;r�)	r�rBr�r%rQr�r�r�r�)rrW�caser&r&r'r��s

z&SwitchStatNode.generate_execution_codecCs]|jj||�x!|jD]}|j||�qW|jdk	rY|jj||�dS)N)r�r�r�r�)rrCrWr�r&r&r'r��s
z,SwitchStatNode.generate_function_definitionscCsT|jj|�x|jD]}|j|�qW|jdk	rP|jj|�dS)N)r�r�r�r�)rrWr�r&r&r'r��s
zSwitchStatNode.annotateN)rMrmrnr�r�r�r�r&r&r&r'r��sr�c@seZdZdS)�LoopNodeN)rMrmrnr&r&r&r'r��sr�c@s[eZdZdddgZdd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�
WhileStatNoder�r�r�cCs-|jj|�|jr)|jj|�dS)N)r�r�r�)rrCr&r&r'r��s	z"WhileStatNode.analyse_declarationscCsU|jr|jj|�|_|jj|�|_|jrQ|jj|�|_|S)N)r�r�r�rqr�)rrCr&r&r'rq�s		z!WhileStatNode.analyse_expressionscCs|j|j�|j�}|jd�|jr||jj|�|jj|�|jd|jj��|jj|�|j	j
|�|j|j�|jd�|j
}|j|�|jr|j|jj�|jd�|jj
|�|jd�|j|�dS)Nzwhile (1) {zif (!%s) break;r�z
/*else*/ {)r�r%�new_loop_labelsrQr�rBrr�rIr�r�r�r�r�set_loop_labelsr�)rrW�old_loop_labelsrr&r&r'r��s*	
	
	

z%WhileStatNode.generate_execution_codecCsU|jr|jj||�|jj||�|jdk	rQ|jj||�dS)N)r�r�r�r�)rrCrWr&r&r'r��s
	z+WhileStatNode.generate_function_definitionscCsF|jr|jj|�|jj|�|jrB|jj|�dS)N)r�r�r�r�)rrWr&r&r'r��s
		zWhileStatNode.annotateN)	rMrmrnr�r�rqr�r�r�r&r&r&r'r��sr�c
@s�eZdZddddddddd	d
g
ZdZZdZZdZZ	dd
�Z
dd�Zdd�Zdd�Z
dS)�DictIterationNextNode�dict_obj�
expected_size�
pos_index_var�coerced_key_var�coerced_value_var�coerced_tuple_var�
key_target�value_target�tuple_target�is_dict_flagNcCsPtj||jd|d|d|d|d|d|d|dd	d
tj�	dS)Nr�r�r�r�r�r�r�r:Tr/)r|r�r%r
�c_bint_type)rr�r�r�r�r�r�r�r&r&r'r�	szDictIterationNextNode.__init__cCs�ddlm}|jj|�|_|jj|�|_|jrX|jj|�|_|jr�|jj|�|_|j|jj	t
j�|_|jj
|jj|�|_|jr|jj|�|_|j|jj	dt
j�|_|jj
|jj|�|_|jri|jj|�|_|j|jj	t
j�|_|jj
|jj|�|_|jj|�|_|S)Nr)r�r/)r�r�r�rpr�r�r�rr�TempNoder%r
r�key_refrr/r�r��	value_refr�r��	tuple_refr�r�)rrCr�r&r&r'rqs&			!	z)DictIterationNextNode.analyse_expressionscCs|jj||�dS)N)r�r�)rrCrWr&r&r'r�,sz3DictIterationNextNode.generate_function_definitionsc	
Cs'|jjtjdd��|jj|�g}g}x�|j|j|jf|j	|j
|jf|j|j
|jfgD]d\}}}|dkr�d}n3|j|||f�|j|�d|j�}|j|�q~W|jjtjd�}|jd||jj�|jj�|jj�|d|d|d	|jj�f�|jd
|�|j|jd||j��|jj|�x*|D]"\}}}|j|j��q�Wx$|D]\}}}|j|�q�Wx4|D],\}}}|j||�|j |�q�WdS)NZ	dict_iterz
Optimize.crcz&%sFz7%s = __Pyx_dict_iter_next(%s, %s, &%s, %s, %s, %s, %s);rrr"zif (unlikely(%s == 0)) break;z%s == -1)!r�rerr�r�rBr�r�r�r�r�r�r�r�r�r�allocater�r�r�r
r?rQrxr�r�r��
error_goto_ifr%r�r�rL�release)	rrWr^Ztemp_addressesr8r�rJ�addr�result_tempr&r&r'r�/sB(	
	 z-DictIterationNextNode.generate_execution_code)rMrmrnr�r�r�r�r�r�r�r�rqr�r�r&r&r&r'r��s		


r�c@sbeZdZddddddgZdZZdd	�Zd
d�Zdd
�Zdd�Z	dS)�SetIterationNextNode�set_objr�r�r�r��is_set_flagNcCsDtj||jd|d|d|d|d|dddtj�dS)	Nr�r�r�r�r�r:Tr/)r|r�r%r
r�)rr�r�r�r�r�r&r&r'r�aszSetIterationNextNode.__init__cCs�ddlm}|jj|�|_|jj|�|_|jj|�|_|jj|�|_|j|jj	dt
j�|_|jj
|jj|�|_|jj|�|_|S)Nr)r�r/)r�r�r�rpr�r�r�rrr�r%r
rr�rr/r�r�)rrCr�r&r&r'rqls!z(SetIterationNextNode.analyse_expressionscCs|jj||�dS)N)r�r�)rrCrWr&r&r'r�wsz2SetIterationNextNode.generate_function_definitionscCs2|jjtjdd��|jj|�|j}|j|�|jj	t
jd�}|jd||jj
�|jj�|jj�|j�|jj�f�|jd|�|j|jd||j��|jj|�|j|j��|jj|�|jj|j|�|j|�dS)NZset_iterz
Optimize.cFz/%s = __Pyx_set_iter_next(%s, %s, &%s, &%s, %s);zif (unlikely(%s == 0)) break;z%s == -1)r�rerr�r�rBr�r�r�r�r
r?rQrxr�r�r�r�r�r%r�r�r�r�rLr�)rrWr�r�r&r&r'r�zs&	
		 z,SetIterationNextNode.generate_execution_code)
rMrmrnr�r�r�r�rqr�r�r&r&r&r'r�Ys	
r�cKsGd|kr6|djr&t||�St||�Sn
t||�SdS)N�iterator)�is_async�AsyncForStatNode�
ForInStatNode�ForFromStatNode)r%r�r&r&r'�ForStatNode�s


r�c@syeZdZdddddgZdZdZdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�ZdS)�_ForInStatNoderJr�r�r�r�NFcCstd��dS)Nz!must be implemented by subclasses)�NotImplementedError)rr&r&r'�_create_item_node�sz _ForInStatNode._create_item_nodecCsG|jj|�|jj|�|jr9|jj|�|j�dS)N)rJr
r�r�r�r�)rrCr&r&r'r��s
	z#_ForInStatNode.analyse_declarationscCs�|jj|�|_|jj|�|_|j�|jj|�|_|jr�|jjjsq|jjj	r�|jjj
|jj�r�n|jj|jj|�|_|jj|�|_|j
r�|j
j|�|_
|S)N)rJrrr�rqr�r�r�r/r�r�rrr�r�)rrCr&r&r'rq�s

	z"_ForInStatNode.analyse_expressionscCs�|j|j�|j�}|jj|�|jd�|jj|�|jj|j|�|j	j
|�|j|j�|j|j�|jd�|j
}|j|�|jri|j}|jd�|_|jd�|jj
|�|jd�|j|j�r\|j|�|j|j�|j|j�|jj|�|j|�|j|�|j|j�|j|�r�|j|�|jj|�|jj|�dS)Nz
for (;;) {r�Zouter_continuez
/*else*/ {)r�r%r�r�rBrQr�rJrLr�r�r�r�rr�r�r�r�r�rrI)rrWr�rZorig_continue_labelr&r&r'r��s<

	
		





z&_ForInStatNode.generate_execution_codecCs_|jj||�|jj||�|jj||�|jdk	r[|jj||�dS)N)rJr�r�r�r�)rrCrWr&r&r'r��s
z,_ForInStatNode.generate_function_definitionscCs]|jj|�|jj|�|jj|�|jrI|jj|�|jj|�dS)N)rJr�r�r�r�r�)rrWr&r&r'r��s	z_ForInStatNode.annotate)rMrmrnr�r�r�r�r�rqr�r�r�r&r&r&r'r��s
(r�c@s"eZdZdZdd�ZdS)r�FcCs&ddlm}||j�|_dS)Nr)�NextNode)r�r�r�r�)rr�r&r&r'r�szForInStatNode._create_item_nodeN)rMrmrnr�r�r&r&r&r'r�sr�c@s.eZdZdZdd�Zdd�ZdS)r�TcKsYd|kst�ddlm}|j|djdd�|d<tj|||�dS)Nr�r)r�r�r2)rr�r�ZAwaitIterNextExprNoder%r�r�)rr%r�r�r&r&r'r�s zAsyncForStatNode.__init__cCs,ddlm}|j|j�|j_dS)Nr)r�)r�r�Z
AsyncNextNoder�r�r2)rr�r&r&r'r�sz"AsyncForStatNode._create_item_nodeN)rMrmrnr�r�r�r&r&r&r'r�
sr�c	@s�eZdZddddddgZdZdZdZdZd	Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd!dd"dd#dd$iZdd�Zdd �ZdS)%r�rJ�bound1�bound2�stepr�r�FNz&For-loop using object bounds or targetcCs=x6|j|j|jfD]}|jjr|j�qWdS)N)rJr�r�r/r=r�)rrCr�r&r&r'r�9szForFromStatNode.nogil_checkcCs=|jj|�|jj|�|jr9|jj|�dS)N)rJr
r�r�r�)rrCr&r&r'r�>s	z$ForFromStatNode.analyse_declarationscCsddlm}|jj|�|_|jj|�|_|jj|�|_|jdk	r�t|j|j	�r�t
|jjdd�|jj|�|_|j|�|jj
}|jp�|js�t|jjd�|jj|�|_|jr|jj|�|_|S)Nr)r�zdProbable infinite loop in for-from-by statement. Consider switching the directions of the relations.r"z>for-from loop variable must be c numeric type or Python object)r�r�rJrrr�rpr�r�rKZUnaryMinusNoderr%�set_up_loopr/r=r[rr�rqr�)rrCr�r�r&r&r'rqDs"
	z#ForFromStatNode.analyse_expressionscCs&ddlm}|jj}|jr.|}n�|jrJt|jjd�|jr\t	j
nt	j}|jjjs�t	j
||jj�}|jjjs�t	j
||jj�}|jdk	r�|jjjr�t	j
||jj�}|jj||�|_|jj||�|_|jjs;|jj|�|_|jdk	r�|jj||�|_|jjs�|jj|�|_|js�|jr�d|_t|j|j�r�t|jd��|j|_d|_nEd|_|j|j||�}||_|j|�j|�|_dS)Nr)r�zWInteger loops over enum values are fragile. Please cast to a safe integer type instead.FzEBuffer or memoryview slicing/indexing not allowed as for-loop target.T)r�r�rJr/r[�is_enumrr%r=r
r>r?r��widest_numeric_typer�r�rr�rP�is_py_targetrKZBufferIndexNoder�loopvar_node�py_loopvar_noder�rert)rrCr�r�Z	loop_typeZc_loopvar_noder&r&r'r�Ys@						zForFromStatNode.set_up_loopcCs�|j|j�|j�}|j}|jj|�|jj|�|j|j\}}|j	dk	r�|j	j|�|j	j
�}d|d|f}nd}ddlm}t
|j|j�r�|jj|�t
|j|j�r|jj|�|jjjrtjn	|jj}|rL|jrL|jj|d�}	n|jj
�}	|jr�|jr�|jddkr�|jd|	|jj
�|||	|j|jj
�||	|f
�n>|jd	|	|jj
�||	|j|jj
�|	|f�|j}
|
dkr<|r<|j|jj||	�}
|
dk	rh|
j|�|jj|
|�|j j!|�|j"|j#�|r|jr|jj$j%r�|j&|jjd�}|j|�|j'|jj$j(�}|jj$j)j*r|j+j,t-j.d
d��d}
n2|j+j,t-j.d
d��d|jj$j)j/}
|jd|j
�|
||j0|j
�|jj�f�|j1|j
��n	|j}|j2|jj||jj$j)�}|	|_3|j4|�|jj$j%r|j5|j
�|j�|j6|�|jd�|rZ|jrZ|jj|�|jj|j|�|rz|jrz|jj7|	�|j8}|j9|�|j:r�|jd�|j:j!|�|jd�|j"|�|jj;|�|jj<|�|jj;|�|jj<|�t
|j|j�r5|jj6|�t
|j|j�rZ|jj6|�|j	dk	r�|j	j;|�|j	j<|�dS)Nz%s=%sr�1r)r�F�>z-for (%s = %s%s + %s; %s %s %s + %s; ) { %s%s;z!for (%s = %s%s; %s %s %s; %s%s) {ZGetModuleGlobalNamezObjectHandling.cz__Pyx_GetModuleGlobalName(%s)ZGetNameInClassz__Pyx_GetNameInClass(%s, %%s)z%s = %s; %sr�z
/*else*/ {)=r�r%r��
from_ranger�rBr��relation_tableZ	relation1r�r�r�r�rKr�r�r�r�rJr/r�r
r>r�r�r�r�rVZ	relation2rQr�rLr�r�r�r�r�Zis_pyglobalZ
PyTempNoder�r<r)r�r�rerr�rr�r�ZCoerceFromPyTypeNodeZ	temp_code�generate_result_coder
r�r�rr�r�rrI)rrWr�r��offsetZincopr�r�Zloopvar_typeZloopvar_nameZcoerced_loopvar_nodeZtarget_nodeZinterned_cnameZlookup_funcZfrom_py_noderr&r&r'r�s�	$&			

					#		


	
	


z'ForFromStatNode.generate_execution_codez<=r��++r��+1z>=�--r��-1cCs�|jj||�|jj||�|jj||�|jdk	r[|jj||�|jj||�|jdk	r�|jj||�dS)N)rJr�r�r�r�r�r�)rrCrWr&r&r'r��sz-ForFromStatNode.generate_function_definitionscCsv|jj|�|jj|�|jj|�|jrI|jj|�|jj|�|jrr|jj|�dS)N)rJr�r�r�r�r�r�)rrWr&r&r'r�s		zForFromStatNode.annotate)r�r�)r�r�)r�r�)r�r�)rMrmrnr�r�r�r�r�r�r�r�rqr�r�r�r�r�r&r&r&r'r�s"&s
r�c@sdeZdZdZddddgZdZdZdd�Zd	d
�Zdd�Z	d
d�Z
dS)�WithStatNodea
    Represents a Python with statement.

    Implemented by the WithTransform as follows:

        MGR = EXPR
        EXIT = MGR.__exit__
        VALUE = MGR.__enter__()
        EXC = True
        try:
            try:
                TARGET = VALUE  # optional
                BODY
            except:
                EXC = False
                if not EXIT(*EXCINFO):
                    raise
        finally:
            if EXC:
                EXIT(None, None, None)
            MGR = EXIT = VALUE = None
    �manager�
enter_callrJr�NcCs4|jj|�|jj|�|jj|�dS)N)r�r�r�r�)rrCr&r&r'r�/sz!WithStatNode.analyse_declarationscCsz|jj|�|_|jj|�|_|jraddlm}||jj|jj�|_|j	j
|�|_	|S)Nr)r�)r�rpr�rJr�r�r%r/�target_tempr�rq)rrCr�r&r&r'rq4s	z WithStatNode.analyse_expressionscCs=|jj||�|jj||�|jj||�dS)N)r�r�r�r�)rrCrWr&r&r'r�>sz*WithStatNode.generate_function_definitionscCs6|j|j�|jd�|jj|�|jjtdd�|_|j	j
tjdd��|jd|j|jj
�|jt|jr�dnd��|j|j|j�f�|j|j�|j�}|j}|jj|�|jrW|jj|�|jj|�|jd	|jj�|jj�f�|jj|�n|jj|�|jj|�|jj|�|jj|�||_|jj|�|j |�r|j!�}|j"|�|j#|�|j$|jt�|j"|�|j#|�|jj%|j�|jd
�dS)Nz/*with:*/ {r�FZPyObjectLookupSpecialzObjectHandling.cz-%s = __Pyx_PyObject_LookupSpecial(%s, %s); %sZ	__aexit__�__exit__z%s = %s;r�)&r�r%rQr�rBr�r�rZexit_varr�rerr�rxr�rr�r�r�r�r�r�rJr�r�rEr�rHrrIr�r�r�r�r�r�r�r�)rrWr�Zintermediate_error_labelZstep_over_labelr&r&r'r�CsF
		!		)	



z$WithStatNode.generate_execution_code)rMrmrnryr�r�r�r�rqr�r�r&r&r&r'r�
s
r�c@sXeZdZddgZdZdZdd�Zdd�Zdd	�Zd
d�Z	dS)�WithTargetAssignmentStatNoder=rNcCs|jj|�dS)N)rr
)rrCr&r&r'r��sz1WithTargetAssignmentStatNode.analyse_declarationscCsJ|jj|�|_|jj|�|jjj|jj|�|_|S)N)rrrrL�	with_noder�rr/r=)rrCr&r&r'rq�s!z0WithTargetAssignmentStatNode.analyse_expressionscCs=|jj|�|jj|j|�|jjj|�dS)N)r=rBrrLr�r�r�)rrWr&r&r'r��sz4WithTargetAssignmentStatNode.generate_execution_codecCs$|jj|�|jj|�dS)N)rr�r=)rrWr&r&r'r��sz%WithTargetAssignmentStatNode.annotate)
rMrmrnr�r�r=r�rqr�r�r&r&r&r'r�ts
r�c@speZdZdddgZdZdd�Zdd�ZejZ	d	Z
d
d�Zdd
�Zdd�Z
dS)�TryExceptStatNoder��except_clausesr�FcCsN|jj|�x|jD]}|j|�qW|jrJ|jj|�dS)N)r�r�r�r�)rrC�
except_clauser&r&r'r��s
	z&TryExceptStatNode.analyse_declarationscCs�|jj|�|_d}x\t|j�D]K\}}|j|�}|j|<|rgt|jd�|js+d}q+W||_|jr�|jj|�|_|S)Nrzdefault 'except:' must be lastr)	r�rqrr�rr%�pattern�has_default_clauser�)rrCZdefault_clause_seenr"r�r&r&r'rq�s	
		z%TryExceptStatNode.analyse_expressionszTry-except statementcs��j}�j}�j}�j�}�j}�jd�}�jd�}�jd�}	�jd�}
|r��jd�nd}|r��jd�nd}�jd�}
�fdd	�td
�D���j|j��j	d��j
�}�j	d�|
�_|�_|�_|jj���j|jd
d��j	d��j
j�}�j|�}|r�jjt�|js�|j	d�|j	d�|j	ddjdd	��D���x�D]}|j|�q�W��fdd�}n9dd	��D�}|j	ddj|��dd�}|�_|	�_|jj}|jr��j|jj��j	d�|jj���j	d�|s�|jj}|r�|s�x�D]}�j|t�q�W�j|
��j|�x$|D]\}}�j||�qW�j
j}|�j
_x!|jD]}|j�|�qGW|�j
_|js��j|�x�||f||f||f|
|f|	|fgD]|\}}�j|�r�|r��j|
�r��j|
��j|��j|jd
d�|r&|��j|�q�W�j|�r�|rj�j|
�rj�j|
��j|�|r�|��j|
�r��j|
��j	d�x�D]}�j
j |�q�W|�_|�_|�_|�_dS)NZexception_handledZexcept_errorZ
except_returnZ
try_returnZ	try_breakZtry_continueZtry_endcs%g|]}�jjtd��qS)F)r�r�r)r�ru)rWr&r'r��s	z=TryExceptStatNode.generate_execution_code.<locals>.<listcomp>r$r�z
/*try:*/ {r�Fr�r�r�z__Pyx_ExceptionSave(%s);z, cSsg|]}d|�qS)z&%sr&)r�r8r&r&r'r��s	cs<x�D]}�j|�qW�jddj���dS)Nz__Pyx_ExceptionReset(%s);z, )rrQr�)r<)rW�
exc_save_varsr&r'�restore_saved_exception�s
	zJTryExceptStatNode.generate_execution_code.<locals>.restore_saved_exceptioncSsg|]}d|�qS)z	(void)%s;r&)r�r8r&r&r'r��s	z%s /* mark used */rHcSsdS)Nr&r&r&r&r'r��sz/*else:*/ {)!r�rr�r�r�r�r_r�r%rQr�r�r�r��all_free_managed_tempsr�r�re�reset_exception_utility_coder�r��put_xgotrefr�r�r�rr�r�Zcurrent_exceptr��generate_handling_coder�r�)rrW�old_return_label�old_break_label�old_continue_labelr�r�Zexcept_end_labelZexcept_error_labelZexcept_return_labelZtry_return_labelZtry_break_labelZtry_continue_labelZ
try_end_labelZsave_exc�temps_to_clean_upZ	can_raiser8r�Zmark_vars_usedZnormal_case_terminates�	temp_name�	temp_typeZouter_exceptr�Z
exit_label�	old_labelrr&)rWr�r'r��s�				
				

	
			


	
			






			z)TryExceptStatNode.generate_execution_codecCs]|jj||�x!|jD]}|j||�qW|jdk	rY|jj||�dS)N)r�r�r�r�)rrCrWr�r&r&r'r�/s
z/TryExceptStatNode.generate_function_definitionscCsN|jj|�x|jD]}|j|�qW|jrJ|jj|�dS)N)r�r�r�r�)rrWZexcept_noder&r&r'r�6s
	zTryExceptStatNode.annotateN)rMrmrnr�r�r�rqr|r�r�r�r�r�r�r&r&r&r'r��s	xr�c@speZdZddddgZdZdZdZdd�Zd	d
�Zdd�Z	d
d�Z
dd�ZdS)�ExceptClauseNoder�rJr�r�NFcCs-|jr|jj|�|jj|�dS)N)rJr
r�r�)rrCr&r&r'r�Us	z%ExceptClauseNode.analyse_declarationscCs�|j|_|jrZxBt|j�D]1\}}|j|�}|j|�|j|<q%W|jr�ddlm}|j	|j
�|_|jj||j�|_|j
j|�|_
|S)Nr)r�)r�
function_namer�rrqrtrJr�r�ZExcValueNoder%r�rr�)rrCr"r�r�r&r&r'rqZs		z$ExceptClauseNode.analyse_expressionscs�j|j�|jrtdd�|jD��}|r��fdd�td�D�}�jjtjdd���j	dt
|���jjtjd	d
��d|d}n(f}�jjtjd
d��d}g}x5|jD]*}|j��|j||j
��q�W�jjtjdd�}�j	d|dj|�f�x+|jD] }|j��|j��qfW|r��j	dt
|���j	djdd�|D���x|D]}	�jj|	�q�W�j	d|��jj|�n
�j	d�t|jdd�r�|jdkr�|jdkr��jjtjdd���j	d��j|��j	d�dS�fdd�td�D�}�j|j��jjt�dt
|�}
�j	d|
�j|j�f�x|D]}�j|�qW|jrj|j j!|d�|j j��|jj"|j ��|jdk	r�x0t#||jj$�D]\}}
|
j!|�q�W�j%�j&}}�j'd ��_%�j'd!��_&�jj(}|�j_(|jj)��|�j_(|jj*sQx|D]}�j+|t,�q*W�j|�xv�j%|f�j&|fgD]V\}}�j-|�rp�j.|�x|D]}�j+|t,�q�W�j|�qpW|�_%|�_&x|D]}	�jj|	�q�W�j	d�dS)"Ncss.|]$}|jp%|j�o%|jVqdS)N)r��	is_simpler:)r�r�r&r&r'r�osz:ExceptClauseNode.generate_handling_code.<locals>.<genexpr>cs(g|]}�jjtdd��qS)r�T)r�r�r)r�ru)rWr&r'r�ts	z;ExceptClauseNode.generate_handling_code.<locals>.<listcomp>r$�PyErrFetchRestorezExceptions.cz__Pyx_ErrFetch(&%s, &%s, &%s);ZFastTypeCheckszModuleSetupCode.cz*__Pyx_PyErr_GivenExceptionMatches(%s, %%s)rZPyErrExceptionMatchesz __Pyx_PyErr_ExceptionMatches(%s)r�Fz%s = %s;z || z__Pyx_ErrRestore(%s, %s, %s);rHcSsg|]}d|�qS)z%s = 0;r&)r�r8r&r&r'r��s	z	if (%s) {z
/*except:*/ {r�Tz__Pyx_ErrRestore(0,0,0);r�cs(g|]}�jjtdd��qS)r�T)r�r�r)r�ru)rWr&r'r��s	z
&%s, &%s, &%sz"if (__Pyx_GetException(%s) < 0) %srZexcept_breakZexcept_continue)/r�r%r�r�r_r�rerr�rQr�rBrrxr�r�r
r?r�rrIr�r�r��excinfo_targetrJr�rr��get_exception_utility_coder�r�r�Zset_varrLr8rTrr�r�r�r�r�r�rr�r�)rrWr�Zhas_non_literalsr�Z
exc_test_funcZ	exc_testsr�Z
match_flagr�Zexc_argsr8ZtempvarrVr�r��old_exc_varsr�r�r&)rWr'r�js�		
 
#




	
	"



		
z'ExceptClauseNode.generate_handling_codecCs9|jdk	r"|jj||�|jj||�dS)N)rJr�r�)rrCrWr&r&r'r��sz.ExceptClauseNode.generate_function_definitionscCsW|jr*x|jD]}|j|�qW|jrC|jj|�|jj|�dS)N)r�r�rJr�)rrWr�r&r&r'r��s		zExceptClauseNode.annotate)rMrmrnr�r�r�Zis_except_asr�rqr�r�r�r&r&r&r'r�>sjr�c@s�eZdZdddgZdZdZdZdZdZdZ	e
dd	��Zd
d�Zdd
�Z
ejZdZdd�Zdd�Zdddd�Zdddd�Zdd�Zdd�ZdS)�TryFinallyStatNoder��finally_clause�finally_except_clauserTNFcCst|d|d|�}|S)Nr�r)r)r%rCr�rrVr&r&r'r��sz"TryFinallyStatNode.create_analysedcCsI|jj|�tj|j�|_|jj|�|jj|�dS)N)r�r�r�deepcopyrr)rrCr&r&r'r�sz'TryFinallyStatNode.analyse_declarationscCse|jj|�|_|jj|�|_|jj|�|_|jra|jjra|j|_|S)N)r�rqrrrr��func_return_type)rrCr&r&r'rqsz&TryFinallyStatNode.analyse_expressionszTry-finally statementcs��j�j��j}�j�}�j�}�j}�jsL|�_�j�}�jd��jj	}d�j_	�j
j��|�j_	�jd��j|��jj
�}�j�jj��jd��jg�fdd�}	�jo�j|�}
�jj}�j
jsq�jd�|	�j���jjsd�j|��jd�|
r��j|��jd��js��jd	��jr��j�|rt�fd
d�td�D��}�jjtjtjtj��d
d�}
n
d}}
t�fdd�td�D��}�j�||||
��j�}�jd��jj}|dd��j_�j j��|�j_�jd�|r'�j!�|||
�|rx|D]}�jj"|�q�W|
r�jj"|
��j|�x_t#�j�|�D]H\}}�j|�s[q=�j|��j$�|��j|�q=Wx|D]}�jj"|�q�W�jd��j|��j%}f}x�t&t#||��D]�\}\}}�j|�s
q�||kr"|
r"q��jd|�d}||kr
�jr�t�fdd�td�D��}�j�g|��jjs
�j'r
�jr
t(�jt)�r
�jj�j'd
d�}�jd|t*j+f��j'j,r
�jdt*j+�|	�j��||kr�|r|�jdt*j+|f��j'j,rf�jd|��jj"|�d}�jr��j!�|��jjs��j|��jd�q�W�j|��jd�dS)Nz
/*try:*/ {rr�z/*finally:*/ {cs?|d}tj|�}|�jkr5||d<n|}|S)Nr)rrr)Z_nextrVZ	node_copy)rr&r'�fresh_finally_clause*s

zHTryFinallyStatNode.generate_execution_code.<locals>.fresh_finally_clausez/*normal exit:*/{z/*exception exit:*/{r�cs+g|]!}�jjtjdd��qS)r�F)r�r�r
r?)r�ru)rWr&r'r�Gs	z>TryFinallyStatNode.generate_execution_code.<locals>.<listcomp>r"r�Fcs(g|]}�jjtdd��qS)r�F)r�r�r)r�ru)rWr&r'r�Os	�r�r$z%s: {cs(g|]}�jjtdd��qS)r�F)r�r�r)r�ru)rWr&r'r��s	z%s = %s;z%s = 0;)-r�r%r�Zall_new_labels�get_all_labels�handle_error_caser�rQr�Zin_try_finallyr�r��set_all_labelsr�r�preserve_exceptionr�r�r�r�r��is_try_finally_in_nogilr�r�r_r�r
ZCPtrTyper4Zc_char_type�put_error_catcherr�r�put_error_uncatcherr�r8�put_error_cleanerr�rrrK�GILExitNoder	r�r=)rrWr�Z
old_labelsZ
new_labelsr�Zcatch_labelZwas_in_try_finallyr�rZpreserve_errorZneeds_success_cleanup�exc_lineno_cnames�exc_filename_cnamer�Zfinally_old_labelsrrr�r�r�r"Zret_tempr&)rWrr'r�s�				










	
	
	




"



	(		
		

z*TryFinallyStatNode.generate_execution_codecCs*|jj||�|jj||�dS)N)r�r�r)rrCrWr&r&r'r��sz0TryFinallyStatNode.generate_function_definitionsc	CsI|jjt�|jjt�|jjt�|jrI|jdd�|jd�|jdjdd�|D���x$|D]\}}|j	||�q�W|jd|dd��|jd	|dd�d
�x|D]}|j
|�q�W|r2|jd|dtj|d
tj
|tjf�|jrE|j�dS)Nr�Fr�rHcSsg|]}d|�qS)z%s = 0;r&)r�r8r&r&r'r��s	z8TryFinallyStatNode.put_error_catcher.<locals>.<listcomp>z>if (PY_MAJOR_VERSION >= 3) __Pyx_ExceptionSwap(&%s, &%s, &%s);r$zmif ((PY_MAJOR_VERSION < 3) || unlikely(__Pyx_GetException(&%s, &%s, &%s) < 0)) __Pyx_ErrFetch(&%s, &%s, &%s);r"z%s = %s; %s = %s; %s = %s;rr)r�rerr��swap_exception_utility_coderr�rQr�r�r�r	�lineno_cname�
clineno_cname�filename_cnamer�)	rrWr�r�rrr�r/r8r&r&r'r
�s,	
#		
	

	z$TryFinallyStatNode.put_error_catchercCsM|jjt�|jjt�|jr9|jdd�|jd�x%|dd�D]}|j|�qWW|jd|dd��|jd�x%|dd�D]}|j|�q�W|jd|dd��|jr�|j�|jdj	d	d
�|D���|rI|jdt
j|dt
j|d
t
j
|f�dS)Nr�Fzif (PY_MAJOR_VERSION >= 3) {r$z!__Pyx_ExceptionReset(%s, %s, %s);r�z__Pyx_ErrRestore(%s, %s, %s);rHcSsg|]}d|�qS)z%s = 0;r&)r�r8r&r&r'r��s	z:TryFinallyStatNode.put_error_uncatcher.<locals>.<listcomp>z%s = %s; %s = %s; %s = %s;rr)r�rerr�rr�rQrr�r�r	rrr)rrWr�rrr8r&r&r'r�s(	

	
#	

z&TryFinallyStatNode.put_error_uncatchercCs�|jjt�|jr)|jdd�|jd�x%|dd�D]}|j|�qGW|jd|dd��|jd�x(|dd�D]}|j|t�q�W|jr�|j	�|jdj
dgd�|dd��dS)	Nr�Fzif (PY_MAJOR_VERSION >= 3) {r$z!__Pyx_ExceptionReset(%s, %s, %s);r�rHz%s = 0;)r�rer�rr�rQrr�rr�r�)rrWr�r8r&r&r'r�s	

	
z$TryFinallyStatNode.put_error_cleanercCs$|jj|�|jj|�dS)N)r�r�r)rrWr&r&r'r��szTryFinallyStatNode.annotate)rMrmrnr�rr	rrrr�r�r�r�rqr|r�r�r�r�r�r
rrr�r&r&r&r'r�s$	�!rc@s"eZdZdZdZdZdS)�NogilTryFinallyStatNodezJ
    A try/finally statement that may be used in nogil code sections.
    FN)rMrmrnryrr�r&r&r&r'r�srcsXeZdZdZdd�Zdd�Z�fdd�Zdd	�Zd
d�Z�S)�GILStatNodeNcCsQ||_|j|||�tj||d|dt|d|d|j��dS)Nr�r�state�
state_temp)r�create_state_temp_if_neededrr�rr)rr%rr�r&r&r'r�s		zGILStatNode.__init__cCs}ddlm}|�}|j|�|js3dS|dkrKtj}n	tj}ddlm}|j	||�|_
dS)Nr)�YieldNodeCollector�gil)r�)ZParseTreeTransformsrZ
visitchildrenZyieldsr
Zc_gilstate_typeZc_threadstate_ptr_typer�r�r�r)rr%rr�rZ	collectorr�r�r&r&r'rs	
		z'GILStatNode.create_state_temp_if_neededcs@|jdk|_|jdkr*d|_tt|�j|�S)NrT)rZ_in_with_gil_blockr�rfrr�)rrC)rLr&r'r�#s	z GILStatNode.analyse_declarationscCsS|jtjdd��|j}|jdk|_tj||�}||_|S)NZForceInitThreadszModuleSetupCode.cr)rerr�rrrrq)rrC�	was_nogilrVr&r&r'rq*s		zGILStatNode.analyse_expressionscCs�|j|j�|j�|jrE|jj|�|jj�}nd}|jj}|jdkr�|j	d|�d|j_n|j
d|�d|j_tj||�|jr�|jj
|�||j_|j�dS)Nr�variableTF)r�r%�begin_blockrr�r�r�r�rr�Zput_release_gilrr�r��	end_block)rrWrZold_gil_configr&r&r'r�3s"
		z#GILStatNode.generate_execution_code)	rMrmrnrr�rr�rqr�r&r&)rLr'rs		rc@s:eZdZdZgZdZdd�Zdd�ZdS)rz]
    Used as the 'finally' block in a GILStatNode

    state   string   'gil' or 'nogil'
    NcCs|S)Nr&)rrCr&r&r'rqWszGILExitNode.analyse_expressionscCsQ|jr|jj�}nd}|jdkr@|j|�n
|j|�dS)Nr)rr�rr�Zput_acquire_gil)rrWrr&r&r'r�Zs	z#GILExitNode.generate_execution_code)rMrmrnryr�rrqr�r&r&r&r'rMs
rc@s"eZdZdZdd�ZdS)�
EnsureGILNodezI
    Ensure the GIL in nogil functions for cleanup before returning.
    cCs|jdd�dS)Nr�F)r�)rrWr&r&r'r�ksz%EnsureGILNode.generate_execution_codeN)rMrmrnryr�r&r&r&r'r"fsr"cCsddlm}|jS)Nr)ra)r�rarf)rar&r&r'�cython_view_utility_codeosr#z
cpython.arraycCstjdd�S)N�ArrayAPIzarrayarray.h)rr�r&r&r&r'r#wsr#zcpython.array.arraycCstjdd�S)Nr$zarrayarray.h)rr�r&r&r&r'r#xszcython.viewZasyncioZ__Pyx_patch_asyncioZPatchAsyncIOzCoroutine.c�inspectZ__Pyx_patch_inspectZPatchInspectc@s@eZdZgZdZdd�Zdd�Zdd�ZdS)	�CImportStatNodeFcCs}|jst|jd�dS|j|j|jd|jrAdnd�}d|jkr+dd�|jjd�D�}|d}|jj|�}|}xC|dd�D]1}|j|�}|j	|||j�|}q�W|j
r|j	|j
||j�qS|j|�|j	|||j�n(|j
p:|j}|j	|||j�|jtkry|j
t|j��dS)	Nz$cimport only allowed at module level�relative_levelrrrcSsg|]}t|��qSr&)r)r�r<r&r&r'r��s	z8CImportStatNode.analyse_declarations.<locals>.<listcomp>r�)r�rr%�find_moduler��is_absoluterr�Zfind_submodule�declare_module�as_namer&�utility_code_for_cimportsre)rrC�module_scoper1Ztop_nameZtop_module_scoper<�submodule_scoper&r&r'r��s,	'

	
z$CImportStatNode.analyse_declarationscCs|S)Nr&)rrCr&r&r'rq�sz#CImportStatNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz'CImportStatNode.generate_execution_codeN)rMrmrnr�r)r�rqr�r&r&r&r'r&�s
r&c@sXeZdZgZdZdZdZdd�Zdd�Zdd�Z	dd	�Z
dS)
�FromCImportStatNodeNc
Cs�|jst|jd�dS|jrU|j|jjd�krUt|jd�dS|j|j|jd|j�}|j}|j|�x�|j	D]�\}}}}|dkr�x}t
|jj��D]\}}	|j
||	|�q�Wq�|j|�}	|	r7|r+|j|	|�r+|	j|�d|	_n�|dksO|dkry|j|d	|d
dddd
|�}	n�|dkr�|j|d
|d|�}	nl|jj|d|d
|jdd�}
|
j|kr�|j|p�||
|j�nt|d||f�|	r�|p|}|j
||	|�q�W|jd�sV|jd�r�|tkrv|jt|��xM|j	D]B\}}}}d||f}|tkr�|jt|��q�WdS)Nz$cimport only allowed at module levelrz3relative cimport beyond main package is not allowedr'r�rr(rAr'r)r*rr%�classr�Zrelative_toZabsolute_fallbackFz%Name '%s' not declared in module '%s'Zcpython�cythonz%s.%s)r�rr%r'r�countr(r�r&�imported_namesr�r�rer'rX�declaration_matchesZ
redeclaredr0r.rr��
parent_moduler*r�r,re)
rrCr-r�r%r<r+r'Z
local_namer�r.ruZfqnamer&r&r'r��sL	$!	
"
$	z(FromCImportStatNode.analyse_declarationscCsV|js
dS|j}|dkr2|jsRdSn |js?dS||jkrRdSdS)Nrr0r)rir/r}Zis_struct_or_unionr')rr�r'r/r&r&r'r4�s				z'FromCImportStatNode.declaration_matchescCs|S)Nr&)rrCr&r&r'rq�sz'FromCImportStatNode.analyse_expressionscCsdS)Nr&)rrWr&r&r'r��sz+FromCImportStatNode.generate_execution_code)rMrmrnr�r�r'r3r�r4rqr�r&r&r&r'r/�s.r/c@sCeZdZdgZdZdd�Zdd�Zdd�Zd	S)
�FromImportStatNoder%rcCsixb|jD]W\}}|dkrT|js?t|jd�dSd|_d|_q
|j|�q
WdS)Nr�z%import * only allowed at module levelr)rer�rr%Zhas_import_star�import_starr
)rrCr<rJr&r&r'r�
s		z'FromImportStatNode.analyse_declarationsc	
Cs�ddlm}|jj|�|_|j|jt�|_g|_x�|j	D]�\}}|dkr�xf|j
j	�D]<\}}|jrx|jj
rx|jtjdd��PqxWqP|j|j�}|jrv|jj|krvt|jd�rv|jj|jjjkrqPyI|j|jjjd|jd|jj�}|jj|jkr`wPWntk
ruYnX|j|d�}|jtkr�d}n|jj|j|�}|jj|||f�qPW|S)	Nr)r�r�ZExtTypeTestzObjectHandling.cr�r%r')r�r�r%rqr�r%rr��interned_itemsrer�rir/r}rerr�rXr<r=r�r9r(r�rr�rrr)	rrCr�r<rJrur�r%�coerced_itemr&r&r'rqs8	
	z&FromImportStatNode.analyse_expressionsc	Cs�|j|j�|jj|�|jr[|jdtj|jj�|j|j�f�|j	j
tdd�}|jj
|�|jr�|jjtjdd��x�|jD]�\}}}|jd||jj�|j|�|j||j�f�|j|�|dkr.|j|j|�n*|j|�|j|�|j||�|j|t�q�W|j	j|�|jj|�|jj|�dS)Nzif (%s(%s) < 0) %s;r�T�
ImportFromzImportExport.cz!%s = __Pyx_ImportFrom(%s, %s); %s)r�r%r%rBr7rQr	rxr�r�r�rr�r�r8r�rerr�r�r�r�rLZallocate_temp_resultr�r�r�rrI)rrWZ	item_tempr<rJr9r&r&r'r�8s<			


z*FromImportStatNode.generate_execution_codeN)rMrmrnr�r7r�rqr�r&r&r&r'r6�s
		#r6c@seZdZdZdZdS)�ParallelNodez4
    Base class for cython.parallel constructs.
    N)rMrmrnryr�r&r&r&r'r;Zsr;cs�eZdZdZddgZdZdZdZdZdZ	dZ
ejej
ejfZejejejfZejejejfZdZ�fdd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zddd�Zdd�Z dd�Z!dd�Z"dd�Z#fdd�Z$dd �Z%d!d"�Z&d#d$�Z'd%d&�Z(dd'd(�Z)d)d*�Z*d+d,�Z+d-d.�Z,d/d0�Z-dddd1d2�Z.d3Z/d4Z0d5e/e0fZ1d6d7�Z2d8d9�Z3�S):�ParallelStatNodea
    Base class for 'with cython.parallel.parallel():' and 'for i in prange():'.

    assignments     { Entry(var) : (var.pos, inplace_operator_or_None) }
                    assignments to variables in this parallel section

    parent          parent ParallelStatNode or None
    is_parallel     indicates whether this node is OpenMP parallel
                    (true for #pragma omp parallel for and
                              #pragma omp parallel)

    is_parallel is true for:

        #pragma omp parallel
        #pragma omp parallel for

    sections, but NOT for

        #pragma omp for

    We need this to determine the sharing attributes.

    privatization_insertion_point   a code insertion point used to make temps
                                    private (esp. the "nsteps" temp)

    args         tuple          the arguments passed to the parallel construct
    kwargs       DictNode       the keyword arguments passed to the parallel
                                construct (replaced by its compile time value)
    r��num_threadsNFrcsStt|�j||�|jd�p+i|_t�|_i|_g|_dS)Nr^)	rfr<r�r*r^rz�seen_closure_vars�privates�assigned_nodes)rr%rt)rLr&r'r��s
	zParallelStatNode.__init__cCs�|jj|�d|_|jrZg}t�}x�|jjD]�}|jj|krpt|j	d|jj�|j
|jj�|jjdkr�|jjs�|j|_q>|jr�|jjdkr�|jjs�|j|_
q>|j|�q>W||j_y|jj|�|_Wqctk
rV}zt|jj	d�WYdd}~XqcXn	i|_xS|jj�D]B\}}||jkr�t|j	d|�qst|||�qsWdS)Nz$Duplicate keyword argument found: %sr=�	chunksizez=Only compile-time values may be supplied as keyword argumentszInvalid keyword argument: %s)r�r�r=rtrzr8r�r9rr%r�r��	is_prangerArrC�	Exceptionre�valid_keyword_argumentsr�)rrC�pairs�seenZdictitemrdr�rr&r&r'r��s4			)	z%ParallelStatNode.analyse_declarationscCs[|jr|jj|�|_|jr<|jj|�|_|jj|�|_|j|�|jdk	rW|jr�|jjdk	r�|jjr�t|jd�nl|jr�|jjr�t|jd�nC|jj	j
r|jjr|jj|�dkrt|jd�|jj
�s3|jj	jrW|jjtj|�j|�|_|S)Nz-num_threads already declared in outer sectionz;num_threads must be declared in the parent parallel sectionrz.argument to num_threads must be greater than 0)r=rqrAr��analyse_sharing_attributes�parentrBrr%r/r�r�rCr�r=rr
r?rP)rrCr&r&r'rq�s&		
(	z$ParallelStatNode.analyse_expressionscCs�x�|jj�D]�\}\}}|jrW|jrW||jjkrWt|d�q|jrw|rwt|d�qd}|j||||�qWdS)z�
        Analyse the privates for this block and set them in self.privates.
        This should be called in a post-order fashion during the
        analyse_expressions phase
        z0Cannot assign to private of outer parallel blockz*Reductions not allowed for parallel blocksTN)r^rerB�is_parallelrHr�propagate_var_privatization)rrCr�r%rS�lastprivater&r&r'rG�s"

z+ParallelStatNode.analyse_sharing_attributescCs�||f|j|<|jjr0t|d�dS|jr�|jrd||jjkrd|jj}n	|j}|r�|s|r�|j||||�dS)a

        Propagate the sharing attributes of a variable. If the privatization is
        determined by a parent scope, done propagate further.

        If we are a prange, we propagate our sharing attributes outwards to
        other pranges. If we are a prange in parallel block and the parallel
        block does not determine the variable private, we propagate to the
        parent of the parent. Recursion stops at parallel blocks, as they have
        no concept of lastprivate or reduction.

        So the following cases propagate:

            sum is a reduction for all loops:

                for i in prange(n):
                    for j in prange(n):
                        for k in prange(n):
                            sum += i * j * k

            sum is a reduction for both loops, local_var is private to the
            parallel with block:

                for i in prange(n):
                    with parallel:
                        local_var = ... # private to the parallel
                        for j in prange(n):
                            sum += i * j

        Nested with parallel blocks are disallowed, because they wouldn't
        allow you to propagate lastprivates or reductions:

            #pragma omp parallel for lastprivate(i)
            for i in prange(n):

                sum = 0

                #pragma omp parallel private(j, sum)
                with parallel:

                    #pragma omp parallel
                    with parallel:

                        #pragma omp for lastprivate(j) reduction(+:sum)
                        for j in prange(n):
                            sum += i

                    # sum and j are well-defined here

                # sum and j are undefined here

            # sum and j are undefined here
        z9Memoryview slices can only be shared in parallel sectionsN)	r?r/r�rrBrIrHr^rJ)rr�r%rSrKrHr&r&r'rJ s5
		z,ParallelStatNode.propagate_var_privatizationcCs�|jr|jj||�S|j|jkr5|jS|jj|jd�}|jj|j�|jj|�|jj	||jf�|j
d||jf�||_dS)zo
        Helper function that allocate a temporary for a closure variable that
        is assigned to.
        Tz%s = %s;N)rH�_allocate_closure_temprr>r�r�r/r��modified_entriesrrQ)rrWr�rr&r&r'rLR s	z'ParallelStatNode._allocate_closure_tempcCs�d}x�t|jj��D]�\}\}}|r|sH||kr|jj�}|r|rv|jd�d}|jd|j|jj|�f�qWdS)NTz4/* Initialize private variables to invalid values */Fz%s = %s;)�sortedr?rer/�
invalid_valuerQrrC)rrW�excluder\r�rSrKrOr&r&r'�initialize_privates_to_nanh s(
z+ParallelStatNode.initialize_privates_to_nancCsD|j}|jj}||j_|j|�||j_|j�S)N)�1begin_of_parallel_control_block_point_after_declsr��ownerrBr�)rrWr�r�rSr&r&r'�evaluate_before_blockw s	
z&ParallelStatNode.evaluate_before_blockcCs3|jdk	r/|jd|j||j��dS)zS
        Write self.num_threads if set as the num_threads OpenMP directive
        Nz num_threads(%s))r=r�rT)rrWr&r&r'�put_num_threads� sz ParallelStatNode.put_num_threadscCsIg|_x9t|j�D](}|js1|jr|j||�qWdS)a
        If a variable is in a scope object, we need to allocate a temp and
        assign the value from the temp to the variable in the scope object
        after the parallel section. This kind of copying should be done only
        in the outermost parallel section.
        N)rMrNr^Zfrom_closurer�rL)rrWr�r&r&r'�declare_closure_privates� s	z)ParallelStatNode.declare_closure_privatescCsTxM|jD]B\}}|jd||jf�|jj|j�||_q
WdS)z�
        Release any temps used for variables in scope objects. As this is the
        outermost parallel block, we don't need to delete the cnames from
        self.seen_closure_vars.
        z%s = %s;N)rMrQrr�r�)rrWr�r\r&r&r'�release_closure_privates� sz)ParallelStatNode.release_closure_privatesc
Cs!|jr|j}|jj�|_}gg}}xIt|�D];\}}|js`|jrp|j|�qB|j|�qBW|r�|j	ddj
|��|r�|j	ddj
|��|jrtj
g}	|jr|	j|j�|j	d|j�|j	ddj
|	��dS)z�
        Make any used temporaries private. Before the relevant code block
        code.start_collecting_temps() should have been called.
        z private(%s)z, z firstprivate(%s)z private(%s, %s, %s)z shared(%s)N)rI�privatization_insertion_pointr�Zstop_collecting_tempsr�rNr=r�rr�r��breaking_label_usedr	�parallel_why�error_label_used�extend�parallel_exc�pos_info)
rrWZ
exclude_tempsr�r�r?Z
firstprivatesr�r/Zshared_varsr&r&r'�privatize_temps� s$		
		z ParallelStatNode.privatize_tempscCs�|jr�|jr�|jd�xft|j�D]U\}}|jr[|j|dd�q0|jr0|j||�|jd|�q0WdS)Nz/* Clean up any temporaries */r�Fz
%s = NULL;)	rI�is_nested_prangerQrNr�r�rr=r�)rrWr�r/r&r&r'�
cleanup_temps� s
		zParallelStatNode.cleanup_tempscCsx|j�|_|j�|_|j|_|jdd�|_|j�|j�|_	|j�|_
|j|�dS)a�
        Sets up a block that surrounds the parallel block to determine
        how the parallel section was exited. Any kind of return is
        trapped (break, continue, return, exceptions). This is the idea:

        {
            int why = 0;

            #pragma omp parallel
            {
                return # -> goto new_return_label;
                goto end_parallel;

            new_return_label:
                why = 3;
                goto end_parallel;

            end_parallel:;
                #pragma omp flush(why) # we need to flush for every iteration
            }

            if (why == 3)
                goto old_return_label;
        }
        r<�returnN)r�r�r�r�r�r�r�r r��%begin_of_parallel_control_block_pointrR�"undef_builtin_expect_apple_gcc_bug)rrWr&r&r'�!setup_parallel_control_flow_block� s
z2ParallelStatNode.setup_parallel_control_flow_blockcCs|j�|_dS)a�
        Each OpenMP thread in a parallel section that contains a with gil block
        must have the thread-state initialized. The call to
        PyGILState_Release() then deallocates our threadstate. If we wouldn't
        do this, each with gil block would allocate and deallocate one, thereby
        losing exception information before it can be saved before leaving the
        parallel section.
        N)r��begin_of_parallel_block)rrWr&r&r'�begin_parallel_block� s	z%ParallelStatNode.begin_parallel_blockcCs�|jr�|j}|}|jd�|jdd�|jd�|jd�|jd�|jd�|jd�|jd�|j�|jd�|j|�|j�|jd	�|jd
�|jd�dS)a�
        To ensure all OpenMP threads have thread states, we ensure the GIL
        in each thread (which creates a thread state if it doesn't exist),
        after which we release the GIL.
        On exit, reacquire the GIL and release the thread state.

        If compiled without OpenMP support (at the C level), then we still have
        to acquire the GIL to decref any object temporaries.
        z#ifdef _OPENMPr�TZPy_BEGIN_ALLOW_THREADSz#endif /* _OPENMP */ZPy_END_ALLOW_THREADSz#elsez{
z#ifndef _OPENMPz}
N)r[rfrQr�Zput_saferar�)rrWZ
begin_codeZend_coder&r&r'�end_parallel_block� s$
		












z#ParallelStatNode.end_parallel_blockc	Cs�|j�}|j�}d|_d|_d|_g|_|j�}xA|D]9}|j|�rO|jpy||jk|_d|_qOW|jr�|j|�x�t	|�D]�\}}|j|�s�q�||jk}|j
|�|o�|s8||jkrd|_|j|�|j
dtj|df�|jra|jra|ra|j|�q�|j|�q�W|jr�|jr�|jr�|j
|�|j|�|j
|�|r�|jr�|jdtj�dS)a�
        Trap any kind of return inside a parallel construct. 'should_flush'
        indicates whether the variable should be flushed, which is needed by
        prange to skip the loop. It also indicates whether we need to register
        a continue (we need this for parallel blocks, but not for prange
        loops, as it is a direct jump there).

        It uses the same mechanism as try/finally:
            1 continue
            2 break
            3 return
            4 error
        FTz%s = %d;rz#pragma omp flush(%s)N)r��any_label_usedrYr[�parallel_private_tempsrr�r�r�rr�r��fetch_parallel_exceptionrQr	rZrB�save_parallel_varsr�)	rrW�should_flushZsave_lastprivates_labelZdont_return_labelZ
all_labelsr�r"Zis_continue_labelr&r&r'�trap_parallel_exit!sF				
	
	

	
	


z#ParallelStatNode.trap_parallel_exitc
Cs=d|j}|jd|�tjd7_|j�|j}d}x�t|jj��D]�\}\}}|s\|jj	r�q\|jj
�}d|}	|j}
|d7}|jj�}|r�d|jj
|�}nd}|jd||	|f�|jd	|	|
f�|jj|	|
f�q\W|j�d
S)a�
        The following shenanigans are instated when we break, return or
        propagate errors from a prange. In this case we cannot rely on
        lastprivate() to do its job, as no iterations may have executed yet
        in the last thread, leaving the values undefined. It is most likely
        that the breaking thread has well-defined values of the lastprivate
        variables, so we keep those values.
        z__pyx_parallel_lastprivates%dz#pragma omp critical(%s)rrz__pyx_parallel_temp%dz = r�z%s %s%s;z%s = %s;N)�critical_section_counterr�r<r rcrNr?rer/r=r<rrOrCrQrjrr!)
rrW�section_namer�Z
temp_countr�rSrKZ	type_decl�
temp_cname�
private_cnamerOrr&r&r'rl]!s*	

	(
	
z#ParallelStatNode.save_parallel_varscCs�|j�|jdd�|jdtj�|jdtj�|jd|j�tt|j	|j
��}d|j_|jdt
|��|jtj�|jd�|j�|j�dS)	a^
        As each OpenMP thread may raise an exception, we need to fetch that
        exception from the threadstate and save it for after the parallel
        section where it can be re-raised in the master thread.

        Although it would seem that __pyx_filename, __pyx_lineno and
        __pyx_clineno are only assigned to under exception conditions (i.e.,
        when we have the GIL), and thus should be allowed to be shared without
        any race condition, they are in fact subject to the same race
        conditions that they were previously when they were global variables
        and functions were allowed to release the GIL:

            thread A                thread B
                acquire
                set lineno
                release
                                        acquire
                                        set lineno
                                        release
                acquire
                fetch exception
                release
                                        skip the fetch

                deallocate threadstate  deallocate threadstate
        r�Tz#pragma omp flush(%s)z
if (!%s) {z'__Pyx_ErrFetchWithState(&%s, &%s, &%s);z%s = %s; %s = %s; %s = %s;r�N)r r�r�r	�parallel_exc_typerQr]rr8�parallel_pos_infor^r�Zuses_error_indicatorr�r�r�r!)rrWr^r&r&r'rk�!s

z)ParallelStatNode.fetch_parallel_exceptioncCs�|j�|jdd�|jtj�|jd|j�tt|j	|j
��}|jdt|��|j�|j
�dS)zRe-raise a parallel exceptionr�Tz&__Pyx_ErrRestoreWithState(%s, %s, %s);z%s = %s; %s = %s; %s = %s;N)r r�rGr	rsrQr]rr8r^rtr�r�r!)rrWr^r&r&r'�restore_parallel_exception�!s

z+ParallelStatNode.restore_parallel_exceptioncCs$|j|j|j|jf�dS)z�
        Restore all old labels. Call this before the 'else' clause to for
        loops and always before ending the parallel control flow block.
        N)r
r�r�r�)rrWr&r&r'�restore_labels�!szParallelStatNode.restore_labelsc	Cs�|j}|jr||jd|j�|jd|j�|jdtj�|jd�|jdtj�|jd�|r�|j}n	|j	}|r�|jdtj�|jdtj�|jdtj�x.|j
D]#\}}|jd	||f�q�W|jd
tj�|rA|jd�|j|j
�|rd|jd�|j|j�|r�|jd
�|j|j�|jr�|jjt�|jd�|j|�|j|j�|jd�|jd�|j�|j|�dS)a�
        This ends the parallel control flow block and based on how the parallel
        section was exited, takes the corresponding action. The break_ and
        continue_ parameters indicate whether these should be propagated
        outwards:

            for i in prange(...):
                with cython.parallel.parallel():
                    continue

        Here break should be trapped in the parallel block, and propagated to
        the for loop.
        z*const char *%s = NULL; int %s = 0, %s = 0;z,PyObject *%s = NULL, *%s = NULL, *%s = NULL;z	if (%s) {zg/* This may have been overridden by a continue, break or return in another thread. Prefer the error. */z%s = 4;r�zint %s;z%s = 0;z%s = %s;z
switch (%s) {z    case 1: z    case 2: z    case 3: z    case 4:N)rcr[rQrtr]r	rsrZrirYrjr�r�r�rr�r�rerrur�r!�"redef_builtin_expect_apple_gcc_bug)	rrW�break_�	continue_�return_r�rirqrrr&r&r'�end_parallel_control_flow_block�!sP		
	


	



z0ParallelStatNode.end_parallel_control_flow_blockz((defined(__APPLE__) || defined(__OSX__))zQ(defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))))z
(%s && %s)cCs|js|j|j�dS)z]
        A bug on OS X Lion disallows __builtin_expect macros. This code avoids them
        N)rHZundef_builtin_expect�redef_condition)rrWr&r&r'rd"s	z3ParallelStatNode.undef_builtin_expect_apple_gcc_bugcCs|js|j|j�dS)N)rHZredef_builtin_expectr|)rrWr&r&r'rw "s	z3ParallelStatNode.redef_builtin_expect_apple_gcc_bug)4rMrmrnryr�r�rBr`r[r=rAr	rsZparallel_exc_valueZparallel_exc_tbr]Zparallel_filenameZparallel_linenoZparallel_clinenortrrrr^ror�r�rqrGrJrLrQrTrUrVrWr_rarergrhrnrlrkrurvr{Zbuggy_platform_macro_conditionZhave_expect_conditionr|rdrwr&r&)rLr'r<bsZ&G
%C+.
	Hr<csCeZdZdZdgZdZ�fdd�Zdd�Z�S)�ParallelWithBlockNodezI
    This node represents a 'with cython.parallel.parallel():' block
    r=Ncs3tt|�j|�|jr/t|jd�dS)Nz=cython.parallel.parallel() does not take positional arguments)rfr}r�rTrr%)rrC)rLr&r'r�."s	z*ParallelWithBlockNode.analyse_declarationscCs�|j|�|j|�|jd�|jd�|jrsdd�|jD�}|jddjt|���|j�|_|j	|�|jd�|jd�|j
�|j|�|j|�|j
j�|jj|�|j|�|j|�|j|�|j�|j|j�}|j|j�}|j|j�}|j|�|j|d	|d
|d|�|j|�dS)Nz#ifdef _OPENMPz#pragma omp parallel cSs%g|]}|jjs|j�qSr&)r/r=r)r�rdr&r&r'r�<"s	zAParallelWithBlockNode.generate_execution_code.<locals>.<listcomp>zprivate(%s)z, r�z#endif /* _OPENMP */rxryrz)rVrerQr�r?r�rNr�rXrUr rgrQr��start_collecting_tempsr�r�rnr_rhr!r�r�rr�rvr{rW)rrWr?ryrxrzr&r&r'r�4"s8



	 











z-ParallelWithBlockNode.generate_execution_code)rMrmrnryrDr=r�r�r&r&)rLr'r}%"s
	r}cs�eZdZdZddddddgZdZZZZdZ	Z
Zd	ZdZ
dZd
dddgZ�fdd
�Z�fdd�Z�fdd�Zdd�Zdd�Zdd�Z�S)�ParallelRangeNodez�
    This node represents a 'for i in cython.parallel.prange():' construct.

    target       NameNode       the target iteration variable
    else_clause  Node or None   the else clause of this loop
    r�rJr�rTr=rANT�schedulercs,tt|�j||�t|�|_dS)N)rfrr�r5r�)rr%rU)rLr&r'r�q"szParallelRangeNode.__init__cs;tt|�j|�|jj|�|jdk	rE|jj|�|jsdt|j�dkrxt|j	d�dSt|j�dkr�|j\|_
nHt|j�dkr�|j\|_|_
n|j\|_|_
|_t
|jd�r|jjd�|_|jdkr7t|j	d|jf�dS)
Nr$z0Invalid number of positional arguments to prangerr"�decode�ascii�static�dynamic�guided�runtimez'Invalid schedule argument to prange: %s)Nr�r�r�r�)rfrr�rJr
r�rTrJrr%rIrYr�r=r�r�)rrC)rLr&r'r�v"s z&ParallelRangeNode.analyse_declarationscsS|j}|jrd|_|jdkr>t|jd�|S|jj|�|_|jjjs�|jjjs�t|jjd|jj�tj	|_
n4|jj|_
|j
js�t|jjddd�d|_
|j|j|jf}x�t||j
�D]�\}}|dk	r|j|�|jjsMt|jd
|�q|jsu|j|�}t|||�tj|j
|j�|_
qW|jdk	r�|jj|�|_t|jd�r�|jjdf|j|jj<tt|�j|�}|jr�|js)t|jjd�nk|jd
krNt|jjd�nF|jjj r�|jjr�|jj!|�dkr�t|jjd�|jj"tj#|�j|�|_|jr�||_|j$o�|j$j%|_&|j&rO|}x"|j$r|j$j%r|j$}q�W|jj'|j�|j(j'|j(�|j)j*|j)�|S)NTz/prange() can only be used as part of a for loopzMust be of numeric type, not %sz1Unsigned index type not allowed before OpenMP 3.0r�r"rYrIr�z%s argument must be numericr�z$Must provide schedule with chunksizer�z,Chunksize not valid for the schedule runtimerzChunksize must not be negative)rYrIr�)+rrJrr%rrr/r[r=r
r��
index_typerVrr1rYrIr�r8rpr�rPr�r�r�rqr=r^r�rfrrAr�r�rCrr?rHrBr`r~r?r@r\)rrCr�start_stop_steprVr<rH)rLr&r'rq�"sv				
			

				
z%ParallelRangeNode.analyse_expressionscCstd}|j|j|j|jf}xIt||�D]8\}}|dk	r4|jjr4t|jd|�q4WdS)NrYrIr�rJz6%s may not be a Python object as we don't have the GIL)rYrIr�rJ)	rYrIr�rJr8r/r=rr%)rrCr1Znodesr<rVr&r&r'r��"szParallelRangeNode.nogil_checkcCsm|j|�|jjj}d|d|jjj�i}|j|j|jf}d}xvt	||j
|�D]_\}}}|dkr�|}	n1|jr�|j�}	n|j
|�|j�}	|	||<qnW|jj|jd�|d<|jj|jd�|d<|jd	|�|j|�|j�|_|jd
|�|jd|�|j�|j||�|j�|j|�|jr�|jr�|jdtj�|j�|jd
�|jj|�|j�|j |�xD||j!|j"fD],}
|
dk	r|
j#|�|
j$|�qW|jj%|d�|jj%|d�|j&|�dS)a,
        Generate code in the following steps

            1)  copy any closure variables determined thread-private
                into temporaries

            2)  allocate temps for start, stop and step

            3)  generate a loop that calculates the total number of steps,
                which then computes the target iteration variable for every step:

                    for i in prange(start, stop, step):
                        ...

                becomes

                    nsteps = (stop - start) / step;
                    i = start;

                    #pragma omp parallel for lastprivate(i)
                    for (temp = 0; temp < nsteps; temp++) {
                        i = start + step * temp;
                        ...
                    }

                Note that accumulation of 'i' would have a data dependency
                between iterations.

                Also, you can't do this

                    for (i = start; i < stop; i += step)
                        ...

                as the '<' operator should become '>' for descending loops.
                'for i from x < i < y:' does not suffer from this problem
                as the relational operator is known at compile time!

            4) release our temps and write back any private closure variables
        rJr�rbr�NFr"Znstepszif (%(step)s == 0) abort();zS%(nsteps)s = (%(stop)s - %(start)s + %(step)s - %(step)s/abs(%(step)s)) / %(step)s;zif (%(nsteps)s > 0)zif (%s < 2)z
/* else */)rbrbr�)'rVrJr�rr/r<rYrIr�r8r1r�r�rBr�r�r�r�rQrer�Zcontrol_flow_var_code_pointr �
generate_loopr!rvr�rYr�r	rZr�r{rAr=rrIr�rW)rrWZtarget_index_cname�fmt_dictr��defaultsrVr<r�r�r�r&r&r'r��"sP(
%		




		




z)ParallelRangeNode.generate_execution_codec
CsR|jr|jd�n
|jd�|jsZ|jd�|j�|_|jj}n�|jd�|j�|_|j}|jd�|jd�|j�|j|�|jr�|jd�n
|jd�|jd�xt	|j
j��D]�\}\}}|rk|dkrk||jj
krk|jjrNt|jd�q�|jd	||jf�q�||jj
kr�|jd
|j�|jd|j�q�|jjs�|r�d}nd
}|jd||jf�q�W|jr5|jrd|j||j�}nd}|jd|j|f�|j|�|jd�|jd�|jd|�|j�|j�}	|j�|jd|�|j|d|jj
�|jr�|jj�|jj|�|j|dd�|j|�|jr|	jdtj �|j!�|j!�|jrN|j"|�|j!�dS)Nz#if 0z#ifdef _OPENMPz#pragma omp forz#pragma omp parallelr�z#endif /* _OPENMP */z+*-&^|z#Python objects cannot be reductionsz reduction(%s:%s)z firstprivate(%s)z lastprivate(%s)rKrQz %s(%s)z, %sz schedule(%s%s)z,for (%(i)s = 0; %(i)s < %(nsteps)s; %(i)s++)z=%(target)s = (%(target_type)s)(%(start)s + %(step)s * %(i)s);rPrmTzif (%s < 2))#r`rQrIr�r�rXrHr rgrNr?rerJr�r/r=rr%rr�rArTrUrQr�r~r�r�rnr_rYr	rZr!rh)
rrWr�Zreduction_codepointr�rSrKrQrAZguard_around_body_codepointr&r&r'r�c#sv	
	

	



	

($			




	

	

	
zParallelRangeNode.generate_loop)rMrmrnryr�r�rJr�rTrYrIr�rBrr�rDr�r�rqr�r�r�r&r&)rLr'r["s	Yurc@s[eZdZdZdgZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
S)�CnameDecoratorNodea?
    This node is for the cname decorator in CythonUtilityCode:

        @cname('the_cname')
        cdef func(...):
            ...

    In case of a cdef class the cname specifies the objstruct_cname.

    node        the node to which the cname decorator is applied
    cname       the cname the node should get
    rVcCs�|jj|�|j}t|t�r8|jjd}t|t�|_t|tt	f�}|j
}|jr�|j|_|j|_d|_
|jr�d|jkr�|j|j�|_n|r�|j|_|j_n�|j}|j|_|jd|j_tj|j|j_|jd|j_|jj|jj_|jj|j_|jd|_xY|jj�D]H\}}|jr�|j|j�|_|jry|j|j�|_qyWdS)NrTr�_objZ_typeru)rVr�rKr�r�r�r��is_functionr�r�r�rrVr0r�r-r/r)rr	Ztypeobj_prefixrr�rrCr�r�re)rrCrVZis_struct_or_enumrdr)r<r�r&r&r'r��#s8							z'CnameDecoratorNode.analyse_declarationscCs0d|kr|jd�d}d|j|fS)Nrrz%s_%sr�)rr)rrr&r&r'r-�#szCnameDecoratorNode.manglecCs|jj|�|_|S)N)rVrq)rrCr&r&r'rq$sz&CnameDecoratorNode.analyse_expressionscCs�|jr�|jr�|jd}t|jt�rP|jj|dddd�nYddlm}|jj	}|j
}|j|_
|j||j
�|dd�||_
|jj||�d	S)
z=Ensure a prototype for every @cname method in the right placeZutility_code_protor�Fr�Tr)�
ModuleNodeZ
definitionN)r�rr�rKrVrBr�r�r�r�rrVZgenerate_cfunction_declarationr�r�)rrCrWZh_coder�r�rr&r&r'r�$s 
				z0CnameDecoratorNode.generate_function_definitionscCs|jj|�dS)N)rVr�)rrWr&r&r'r�$sz*CnameDecoratorNode.generate_execution_codeN)
rMrmrnryr�r�r-rqr�r�r&r&r&r'r��#s	&r�aC
/* Test for GCC > 2.95 */
#if defined(__GNUC__)     && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))
  #define likely(x)   __builtin_expect(!!(x), 1)
  #define unlikely(x) __builtin_expect(!!(x), 0)
#else /* !__GNUC__ or GCC < 2.95 */
  #define likely(x)   (x)
  #define unlikely(x) (x)
#endif /* __GNUC__ */
z1
#define likely(x)   (x)
#define unlikely(x) (x)
ZPrintz
Printing.cZPrintOner�zExceptions.cZRaiseExceptionZGetExceptionZ
SwapExceptionZSaveResetExceptionZAddTracebackr�zR
static PyObject *__Pyx_GetExceptionTuple(PyThreadState *__pyx_tstate); /*proto*/
�impla1
static PyObject *__Pyx_GetExceptionTuple(CYTHON_UNUSED PyThreadState *__pyx_tstate) {
    PyObject *type = NULL, *value = NULL, *tb = NULL;
    if (__Pyx_GetException(&type, &value, &tb) == 0) {
        PyObject* exc_info = PyTuple_New(3);
        if (exc_info) {
            Py_INCREF(type);
            Py_INCREF(value);
            Py_INCREF(tb);
            PyTuple_SET_ITEM(exc_info, 0, type);
            PyTuple_SET_ITEM(exc_info, 1, value);
            PyTuple_SET_ITEM(exc_info, 2, tb);
            return exc_info;
        }
    }
    return NULL;
}
�requires)��
__future__rr1r�rUrrr�	itertoolsrr�rZErrorsrrrrr	r
rrrZSymtabrr
rrrrrrcrZStringEncodingrrrrZPythranrr r!ZUtilsr#rqr3rr4r(r.rGr^r/r_ror{r|r�r�r�r�r�r�r�r�r�r�r�r3r6rSrArTr]rgr>rtrvrxr{r�r�r�r�r�r�r�r�rfrArBrwr�r�r�r�r�rFr�r�r
r/r0r2r�r;r?rbr<rjrrryrzr5r}r~r�rLr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr"r#r,Zutility_code_for_importsr&r/r6r;r<r}rr�Zgcc_branch_hintsZbranch_prediction_macrosr�rurwrr�r�rr�Ztraceback_utility_codeZ get_exception_tuple_utility_coder&r&r&r'�<module>sH
$"4	/
�*
%#,�
s	^!Q'
j-X1!����������-8�E���@�1f/GC)7
rh=C))<\<
a
�g!���	H	+O\���6�kc