Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/Cython/Compiler/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/Cython/Compiler/__pycache__/Nodes.cpython-37.pyc

B

Z��Z~��@sNddlmZddlZejeeeeeeeeeeeeeeeeeeed�ddlZddlZddlZddlm	Z	ddl
mZddlm
Z
mZmZmZddl
mZdd	l
mZdd
l
mZddlmZmZddlmZmZmZmZmZmZmZdd
lmZddl m!Z!ddl
m"Z"ddl
m#Z#ddl
m$Z$ddl%m&Z&m'Z'm(Z(ddl)m*Z*ej+ddk�rVe,Z-ne,e.fZ-dd�Z/dd�Z0d�dd�Z1dd�Z2Gdd�de3�Z4Gd d!�d!e3�Z5d"d#�Z6e6Gd$d%�d%e��Z7Gd&d'�d'e7�Z8Gd(d)�d)e�Z9Gd*d+�d+e7�Z:Gd,d-�d-e7�Z;Gd.d/�d/e;�Z<Gd0d1�d1e7�Z=Gd2d3�d3e=�Z>Gd4d5�d5e=�Z?Gd6d7�d7e=�Z@Gd8d9�d9e=�ZAGd:d;�d;e=�ZBGd<d=�d=e=�ZCGd>d?�d?e7�ZDGd@dA�dAe7�ZEGdBdC�dCe7�ZFGdDdE�dEeE�ZGGdFdG�dGeE�ZHGdHdI�dIeE�ZIGdJdK�dKeE�ZJGdLdM�dMeE�ZKGdNdO�dOeE�ZLGdPdQ�dQeE�ZMGdRdS�dSeE�ZNGdTdU�dUe;�ZOGdVdW�dWe;�ZPGdXdY�dYePe9�ZQGdZd[�d[e;�ZRGd\d]�d]e;�ZSGd^d_�d_e;�ZTGd`da�dae;e9�ZUGdbdc�dceU�ZVGddde�dee7�ZWGdfdg�dge7�ZXGdhdi�dieU�ZYGdjdk�dkeU�ZZGdldm�dmeY�Z[Gdndo�doe[�Z\Gdpdq�dqe\�Z]Gdrds�dse\�Z^Gdtdu�dueY�Z_Gdvdw�dwe;�Z`Gdxdy�dye;e9�ZaGdzd{�d{ea�ZbGd|d}�d}ea�ZcGd~d�de;�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�dDŽ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Մe�dלZ�d�d�dڜZ�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�d�e��Z�Gd�d�d�e;�Z�e#j��r�d�Z�nd�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�e��d�d�Z�ed�d�e�gd��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|d��|dfS)Nrr)Zget_filenametable_entry)�pos�r&�H/opt/alt/python37/lib64/python3.7/site-packages/Cython/Compiler/Nodes.py�relative_position)sr(cCs�tjs
|Sdt|�}|dkr&t|�S|j}|dk	r\y|�|�Wntk
rZd}YnX|sjt|�}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.cCszd}d}d}}|jr|t|jd�xD|jD]:\}}|js:q*|jdkrRd}|sd|}q*|jdkr*d}|}q*W|r||r|t|jd�|�|�}	|jr�|js�|j	dkr�|dk	r�|	dk	r�|	j
s�|�|�}
|
r�|
j
r�d}d}	|	tj
tjtjfkr�|j	dkr�tjnt}	n|	dk	�r|j�rt|jd	�|	dk	�rR|�r@|�s@|	j
�s@t|jd
�t|j|	dd�}n |�rft|jd�nt|jd
�||	fS)NFzSDicts should no longer be used as type annotations. Use 'cython.int' etc. directly.)�typestypeT)�ctypesctypez9Duplicate type declarations found in signature annotation)�int�long�floatr3zWStrings 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)r/�is_argz'Ambiguous types in annotation, ignoringz0Unknown type declaration in annotation, ignoring)�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:r7�arg_typeZ
assigned_typer&r&r'�analyse_type_annotationGsP





rEcs��fdd�}|S)Ncs�t|�dkr�t|d��r�|dd�\}}dd|j|jj�j|jdd�f}|jj��}|�	|�|jj��}|jd7_�||�}|jd8_||jj��kr�|jj�
|�n|�ddd�}|�	|�|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&)rZrYr[r&)rYrZr'�write_func_callvsr\cseZdZ�fdd�Z�ZS)�VerboseCodeWritercsdddlm}ddlm}t|�}x,|��D] \}}t||�r*t||�||<q*Wtt	|��
||||�S)Nr)�FunctionTyper)�CCodeWriter)�typesr^�Coder_�dict�itemsrIr\�superr]�__new__)�clsr:�bases�attrsr^r_�mname�m)rJr&r're�s
zVerboseCodeWriter.__new__)rK�
__module__�__qualname__re�
__classcell__r&r&)rJr'r]�sr]cs.eZdZdZedddg�Z�fdd�Z�ZS)�CheckAnalyserszCMetaclass to check that type analysis functions return a node.
    �
analyse_types�analyse_expressions�analyse_target_typescsjddlm}dd�}t|�}x6|��D]*\}}t||�r&||jkr&|||�||<q&Wtt|��||||�S)Nr)r^cs��fdd�}|S)Ncs(�||�}|dkr$td�||f�|S)Nz%s %s %s)�print)rR�kwargs�retval)rZr:r&r'�call�s
z3CheckAnalysers.__new__.<locals>.check.<locals>.callr&)r:rZrur&)rZr:r'�check�sz%CheckAnalysers.__new__.<locals>.check)	r`r^rbrcrI�methodsrdrnre)rfr:rgrhr^rvrirj)rJr&r're�szCheckAnalysers.__new__)rKrkrl�__doc__�setrwrermr&r&)rJr'rn�s
rncCstjrtt�|�S|S)N)rZdebug_trace_code_generationr#r])rfr&r&r'�_with_metaclass�srzc@s�eZdZdZdZdZdZdZdZdZ	dZ
dZdZdd�Z
dZdZd dd�ZdZd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zd!dd�Zd"dd�ZdS)#�NoderFNcKs||_|j�|�dS)N)r%�__dict__�update)�selfr%�kwr&r&r'�__init__�sz
Node.__init__Z	OperationcCst|jd|j�dS)Nz%s not allowed without gil)rr%�gil_message)r~rAr&r&r'�	gil_error�szNode.gil_errorcCs|��s|��dS)N)Zis_cpp�	cpp_error)r~rAr&r&r'�	cpp_check�szNode.cpp_checkcCst|jd|j�dS)Nz%s only allowed in c++)rr%�cpp_message)r~r&r&r'r��szNode.cpp_errorcCsHt�|�}x8|jD].}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>�sz#Node.clone_node.<locals>.<listcomp>)r�child_attrs�getattrrI�list�setattr)r~�result�attrnamer7r&r&r'�
clone_node�s


zNode.clone_nodecCsdS)Nr&)r~rAr&r&r'�analyse_declarationsszNode.analyse_declarationscCstd|jj��dS)Nz*analyse_expressions not implemented for %s)rrJrK)r~rAr&r&r'rpszNode.analyse_expressionscCstd|jj��dS)Nz$generate_code not implemented for %s)rrJrK)r~rUr&r&r'�
generate_codeszNode.generate_codecCst|t�r|j�|�dS)N)rI�	BlockNode�body�annotate)r~rUr&r&r'r�s
z
Node.annotatec	Cs�y|jStk
r�|j}|js,||_|SxX|jD]N}t||�}|dkrLq4t|t�rtx*|D]}t||���}q\Wq4t||���}q4W||_|SXdS)N)	Z_end_pos�AttributeErrorr%r�r�rIr��max�end_pos)r~r%�attr�child�cr&r&r'r�#s"


zNode.end_pos�r%�dc
	s��dkrdS�dkrt��t|��kr:d|jjt|�fS��t|������fdd���fdd�|j��D�}t|�dkr�d	|jjt|�fSd
|}d|jjt|�f}x,|D]$\}}	|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>csRt|t�r|����d��St|t�rFdd���fdd�|D��St|�SdS)Nrz[%s]z, csg|]}�|���qSr&r&)r��item)�
dump_child�levelr&r'r�Gsz1Node.dump.<locals>.dump_child.<locals>.<listcomp>)rIr{�dumpr��join�repr)r�r�)�cutoffr��encountered�
filter_out)r�r'r�Cs


zNode.dump.<locals>.dump_childcs g|]\}}|�kr||f�qSr&r&)r��keyr7)r�r&r'r�KszNode.dump.<locals>.<listcomp>z<%s (0x%x)>z  z<%s (0x%x)
z%s  %s: %s
rz%s>)ry�idrJrK�addr|rcrH)
r~r�r�r�r�rh�indentrXr�r7r&)r�r�r�r�r'r�8s" z	Node.dump�(#)c	Cs�|js
dS|j\}}}|jddd�}|td|d�|�}|d}|rb|d|�|||d�}|��d	|d<||||d
�7}d|��||d�|�fS)z[Debug helper method that returns the source code context of this node as a string.
        ��ASCII�ignore)r*Zerror_handlingrr$���Nz             # <<<<<<<<<<<<<<
r"z"%s":%d:%d
%s
)r%�	get_linesr��rstripZget_escaped_descriptionr�)	r~Zmark_columnrVZsource_desc�line�col�contents�lines�currentr&r&r'�dump_posVsz
Node.dump_pos)N)rr�r�N)Fr�)rKrkrlr9�is_noneZis_nonecheck�
is_literal�
is_terminator�
is_wrapper�tempsr�Zcf_stateZ
coercion_typer�r��nogil_checkr�r�r�r�r�r�rpr�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�cCs$|j}|j|_|j�|�||_dS)N)�
directivesr�r�)r~rA�oldr&r&r'r�psz+CompilerDirectivesNode.analyse_declarationscCs&|j}|j|_|j�|�|_||_|S)N)r�r�rp)r~rAr�r&r&r'rpvs
z*CompilerDirectivesNode.analyse_expressionscCs8|j}|jj}|j|j_|j�||�||_||j_dS)N)r��globalstater��generate_function_definitions)r~rArUZenv_oldZcode_oldr&r&r'r�}s
z4CompilerDirectivesNode.generate_function_definitionscCs*|jj}|j|j_|j�|�||j_dS)N)r�r�r��generate_execution_code)r~rUr�r&r&r'r��s
z.CompilerDirectivesNode.generate_execution_codecCs*|jj}|j|j_|j�|�||j_dS)N)r�r�r�r�)r~rUr�r&r&r'r��s
zCompilerDirectivesNode.annotateN)
rKrkrlrxr�r�rpr�r�r�r&r&r&r'r�gsr�c@seZdZdd�Zdd�ZdS)r�cCs2|��j}x|D]}|j�|�qW|dd�=dS)N)�global_scopeZundeclared_cached_builtinsr�Zadd_cached_builtin_decl)r~rArU�entries�entryr&r&r'�generate_cached_builtins_decls�s

z(BlockNode.generate_cached_builtins_declscCs x|jD]}|�||�qWdS)N)Zlambda_defsr�)r~rArUrTr&r&r'�generate_lambda_definitions�sz%BlockNode.generate_lambda_definitionsN)rKrkrlr�r�r&r&r&r'r��sr�c@sFeZdZdgZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dS)�StatListNode�statscOst|f|�|�}|S)N)r�)r%rArRrrTr&r&r'�create_analysed�szStatListNode.create_analysedcCsx|jD]}|�|�qWdS)N)r�r�)r~rA�statr&r&r'r��sz!StatListNode.analyse_declarationscs�fdd�|jD�|_|S)Ncsg|]}|����qSr&)rp)r�r�)rAr&r'r��sz4StatListNode.analyse_expressions.<locals>.<listcomp>)r�)r~rAr&)rAr'rp�s
z StatListNode.analyse_expressionscCs x|jD]}|�||�qWdS)N)r�r�)r~rArUr�r&r&r'r��sz*StatListNode.generate_function_definitionscCs*x$|jD]}|�|j�|�|�qWdS)N)r��mark_posr%r�)r~rUr�r&r&r'r��sz$StatListNode.generate_execution_codecCsx|jD]}|�|�qWdS)N)r�r�)r~rUr�r&r&r'r��szStatListNode.annotateN)rKrkrlr��staticmethodr�r�rpr�r�r�r&r&r&r'r��sr�c@seZdZdd�Zdd�ZdS)�StatNodecCsdS)Nr&)r~rArUr&r&r'r��sz&StatNode.generate_function_definitionscCstd|jj��dS)Nz.generate_execution_code not implemented for %s)rrJrK)r~rUr&r&r'r��sz StatNode.generate_execution_codeN)rKrkrlr�r�r&r&r&r'r��s
r�c@s2eZdZdgZdd�Zdd�Zdd�Zdd	�Zd
S)�CDefExternNoder�cCst|j}d|_|j�|�||_|js*|jrp|jj}|jdsBd}n|sLd}ntdd�|D��}|�|j|j|�dS)NrZpreliminary_late_includes_cy28Fcss|]}t|t�VqdS)N)rI�CVarDefNode)r�rTr&r&r'�	<genexpr>�sz6CDefExternNode.analyse_declarations.<locals>.<genexpr>)	�in_cincluder�r�Zinclude_fileZverbatim_includer�r��all�add_include_file)r~rAZold_cinclude_flagr�Zlater&r&r'r��s
z#CDefExternNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rp�sz"CDefExternNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz&CDefExternNode.generate_execution_codecCs|j�|�dS)N)r�r�)r~rUr&r&r'r��szCDefExternNode.annotateN)rKrkrlr�r�rpr�r�r&r&r&r'r��s
r�c@seZdZgZdZdd�ZdS)�CDeclaratorNoder�cCsdS)Nr&)r~r&r&r'�analyse_templatessz!CDeclaratorNode.analyse_templatesN)rKrkrlr��calling_conventionr�r&r&r&r'r��s
r�c@s eZdZdgZdZddd�ZdS)�CNameDeclaratorNode�defaultNrFcCs~|rX|jdkrX|js |js |jr.t|jd�n*|jrBt|jd�n|jdddd�|_t}|j	rp|j
rp|�|j
�}||_||fS)Nr�zMissing argument namezJUse spam() rather than spam(void) to declare a function with no arguments.r)Zfor_display�pyrex)
r:�is_ptr�is_array�	is_bufferrr%�is_void�declaration_coder�is_fused�fused_to_specific�
specializer/)r~rCrA�nonempty�
visibility�in_pxdr&r&r'�analyseszCNameDeclaratorNode.analyse)rNF)rKrkrlr�r�r�r&r&r&r'r�sr�c@s$eZdZdgZdd�Zd	dd�ZdS)
�CPtrDeclaratorNode�basecCs
|j��S)N)r�r�)r~r&r&r'r�1sz$CPtrDeclaratorNode.analyse_templatesrNFcCs2|jrt|jd�t�|�}|jj|||||d�S)Nz+Pointer base type cannot be a Python object)r�r�r�)r;rr%r
�
c_ptr_typer�r�)r~rCrAr�r�r�Zptr_typer&r&r'r�4s
zCPtrDeclaratorNode.analyse)rNF)rKrkrlr�r�r�r&r&r&r'r�,sr�c@s$eZdZdgZdd�Zd	dd�ZdS)
�CReferenceDeclaratorNoder�cCs
|j��S)N)r�r�)r~r&r&r'r�@sz*CReferenceDeclaratorNode.analyse_templatesrNFcCs2|jrt|jd�t�|�}|jj|||||d�S)Nz-Reference base type cannot be a Python object)r�r�r�)r;rr%r
Z
c_ref_typer�r�)r~rCrAr�r�r�Zref_typer&r&r'r�Cs
z CReferenceDeclaratorNode.analyse)rNF)rKrkrlr�r�r�r&r&r&r'r�;sr�c@seZdZddgZddd�ZdS)	�CArrayDeclaratorNoder��	dimensionrNFcsf|jr|��s|jr�ddlm}t|j|�r6|jj}n|jf}�fdd�|D�}d|krx|�d�}	t	||	j
d�t}n|�|j
|�}|j
j|�|||d�S|jr�|j���|_|jjjs�t	|jj
d�|j��}
|
dk	r�yt|
�}
Wntk
r�YnXnd}
|���st	|j
d|�|j�r0t	|j
d	�|j�rDt	|j
d
�t�||
�}|j
j|�|||d�S)Nr)�	TupleNodecsg|]}|����qSr&)r8)r��v)rAr&r'r�Wsz0CArrayDeclaratorNode.analyse.<locals>.<listcomp>zTemplate parameter not a type)r�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�rIr�rR�indexrr%r�specialize_herer�r��analyse_const_expressionr/�is_int�get_constant_c_result_coder1�
ValueError�is_completer;r
Zc_array_type)r~rCrAr�r�r�r�rR�values�ix�sizeZ
array_typer&)rAr'r�Ps>




zCArrayDeclaratorNode.analyse)rNF)rKrkrlr�r�r&r&r&r'r�Jsr�c@sBeZdZdddgZdZdZdZdZdd�Zd
d	d
�Z	ddd�Z
dS)�CFuncDeclaratorNoder�rR�exception_valuerNcCs�t|jt�r�ddlm}m}|jj}t||�r6|j}n"t||�rH|g}nt|j	d�dSg|_
x8|D]0}t||�r�|j
�t�
|j��qdt|j	d�qdW|jj|_|j
SdSdS)Nr)r��NameNodez*Template arguments must be a list of names)rIr�r�r�r�r�r�rRrr%�	templates�appendr
�TemplatePlaceholderTyper:)r~r�r��
template_nodeZtemplate_nodes�templater&r&r'r��s"




z%CFuncDeclaratorNode.analyse_templatesFcs�|dkri}|r|d8}g}�x>t�j�D�].\}}	|	j�||dkoR�joRd�jkd�\}
}|
j}||kr�||}
|
���}|dkr�t|
jd�n4|t	j
k	r�|�|�s�t�jjd�t|
jd�n|}|
j
r�t�jd�|dkr�jr�|jr�j}|j�rt	�|j�}|j�rt|	jd	�|�t	�|||	j��|	j�rH�jd7_q*�jr*t�jd
�q*Wd}d}�jdk�r���d���d
���d���d�|j�rΈj�s��j�rΈjdk�r�t�jd��n.�jdk�r2�j�r2�jdk�r2|jdk	�r2|dk�r2|�s2�j�s2ddlm}|�j|j|d��_�j�r��j����_�jdk�r��jj}|j �s�|j�s�|j!�r�|j"j�s�|j�r�t�jjd��j}nZ�j�#|������_�j�$�}|dk�r�t%d�jj&j'��|�(�jj��s�t�jjd��j}|j!�rt�jd�t	j)||�j*�j||�jj+�j,�j-�j.�j/�j0d�}�j�rv|j1�rj��fdd�}||_2n��3|���jd}|�r�|j+}|�r�||k�r�t�jd||f�||_+�jj|�||d�S)Nrrr�)r��is_self_argz
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)�	ConstNode)r7r/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_methodrcs��|�|�dS)N)�declare_optional_arg_struct)�	func_type�fused_cname)rAr~r&r'�declare_opt_arg_structsz;CFuncDeclaratorNode.analyse.<locals>.declare_opt_arg_struct�callspecz2cannot have both '%s' and '%s' calling conventions)r�r�)4�	enumeraterRr��is_c_class_scoper�r:r8rr%r
r�same_asr��cnameZis_unspecified�parent_typer�r�rCr�r�CFuncTypeArgr�r
rr�r;r�r�r	r�r/�is_errorr��return_type�	coerce_tor�rrJrK�assignable_from�	CFuncType�has_varargsr�rr
�overridablerrr�rr)r~rrAr��directive_localsr�r�Zfunc_type_args�iZarg_node�name_declaratorr/r:�	type_node�
other_type�exc_val�	exc_checkr	Zexc_val_typerrrr�r&)rAr~r'r��s� 






 




zCFuncDeclaratorNode.analysec	Cs�t�}dtj}|�|tj|j�x<|jt|j�|j	d�D]}|j|j
|j|jddd�q>W|�tj
|jj
�}|dk	r�t�||�}|��j|d|d|j|d�}d|_d|_t�|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%snNT)Zallow_pyobjectZallow_memoryview�structr)r:�kind�scope�typedef_flagr%rr)rr	�pyrex_prefix�declare_varr
r=r%rRrHr
r:r/�mangleZopt_arg_prefixr�Zget_fused_cnamer��declare_struct_or_union�defined_in_pxd�usedr��
op_arg_struct)	r~rrArr+Zarg_count_member�argZstruct_cnameZop_args_structr&r&r'rs$
 z/CFuncDeclaratorNode.declare_optional_arg_struct)rNNF)N)rKrkrlr�r!r
rrr�r�rr&r&r&r'r�vs

vr�c@seZdZdgZddd�ZdS)�CConstDeclaratorNoder�rNFcCs2|jrt|jd�t�|�}|jj|||||d�S)Nz)Const base type cannot be a Python object)r�r�r�)r;rr%r
�c_const_typer�r�)r~rCrAr�r�r��constr&r&r'r�:s

zCConstDeclaratorNode.analyse)rNF)rKrkrlr�r�r&r&r&r'r55sr5c@speZdZddddgZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdd	d
�Zdd�Zd
d�Zdd�Zddd�ZdS)�CArgDeclNoderC�
declaratorr�r@rrNFc
Csx|rd|j_|_|jdk�rht|jt�r�|jjdkr�|r�|jjr\|jj|dd�}|�	�}n|jj}t
|�|j_d|j_d|j_d}nd}d|j_|jj||d�}t|jd�r�|jj
r�|jj
|j_|j�rt|jt��rt|jt��r|j}xt|jt��r|j}q�W|jj|_|j}|j�rV|�rV|jd�rV|jjdk�rV|�|�}	|	dk	�rV|	}|jj|||d�S|j|jfSdS)NTr�)�
could_be_nameF�arg_name�annotation_typing)r�)rCrr/rIr9r�r:�is_basic_c_typer��empty_declaration_coderr4�hasattrr;r��TemplatedTypeNoder�r��array_declaratorr@r��inject_type_from_annotationsr$)
r~rAr�rr/r;r:rCr9rDr&r&r'r�`s@


(

zCArgDeclNode.analysecCs4|j}|sdSt|||jd�\}}|dk	r0||_|S)N)rB)r@rEr�rC)r~rAr@rCrDr&r&r'rB�sz)CArgDeclNode.inject_type_from_annotationscCsJ|jdkrD|jrD|jjr6|j�|�|j�|j���S|�|j�|_|jS)N)�
default_valuer�r��generate_evaluation_coder/�	cast_coder�Zget_argument_default_const)r~rUr&r&r'�calculate_default_value_code�s
z)CArgDeclNode.calculate_default_value_codecCs|jr|j�|�dS)N)r�r�)r~rUr&r&r'r��szCArgDeclNode.annotatecCs�|j}|dks|jrdS|dkr*|�|�}|�|�|�|�|rJ|��n
|�|j�}|�d||f�|jj	r~|�
|���|�|�|�|�dS)Nz%s = %s;)
r�r�rFrD�make_owned_referencer��	result_asr/rOr;�put_giveref�generate_post_assignment_code�
free_temps)r~rU�target�overloaded_assignmentr�r�r&r&r'�generate_assignment_code�s



z%CArgDeclNode.generate_assignment_code)rF)NF)rKrkrlr�r�is_type_arg�
is_generic�kw_only�not_none�or_noner/r$rCr@�
is_dynamicr�rBrFr�rNr&r&r&r'r8Bs"
.	
r8c@seZdZdd�ZdS)�
CBaseTypeNodecCs
|�|�S)N)r�)r~rAr&r&r'r8�szCBaseTypeNode.analyse_as_typeN)rKrkrlr8r&r&r&r'rU�srUc@seZdZgZddd�ZdS)r?FcCs|jS)N)r/)r~rAr:r&r&r'r��szCAnalysedBaseTypeNode.analyseN)F)rKrkrlr�r�r&r&r&r'r?�sr?c@s*eZdZgZdZgZdZdZddd�ZdS)�CSimpleBaseTypeNodeNFcCs�d}|jr2t�|j|j|j�}|s.t|jd��nN|jdkrJ|jsJt	}�n6|jdkrp|j
rh|jrh|j}nt	}�n|jr�|}x6|jD],}|�
|�}|dk	r�|jr�|jj}q�d}Pq�W|dkr�|�|j|j�}|r�|j|_n|}|�r�|jr�|��}|�|j�}|dk	�rnx|�r8|j
�r&|j�r&|j}nt	}t|j�|_nH|j�rn|j|jk�r`t|jd|j�t�|j�}nt|jd|j�|j�r�|j�r�|j�r�t|jd�t�|�}|�|�n"|tjk�r�tj }|�|�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_typerr;rr�complex�
is_numeric�
is_complexZCComplexTypeZcreate_declaration_utility_coderZcomplex_typeZc_double_complex_typer)r~rAr:r/r+r�r�r&r&r'r��sh






zCSimpleBaseTypeNode.analyse)F)	rKrkrlr�r;rYr=r\r�r&r&r&r'rV�s
rVc@s*eZdZdZddgZd
dd�Zdd�Zd	S)�MemoryViewSliceTypeNode�
memoryview�base_type_node�axesFc
Cs�|j�|�}|jr|Sddlm}y|�||j�}Wn<tk
rp}zt|j	|j
�t��|_
|j
Sd}~XYnX|�|j|�s�t|_
n&t�||�|_
|j
�|j�|�|�|j
S)Nr)�
MemoryView)rar�rr�rcZget_axes_specsrbrr�positionZmessage_onlyr
Z	ErrorTyper/Z
validate_axesr%rZMemoryViewSliceTypeZvalidate_memslice_dtype�use_memview_utilities)r~rAr:rCrcZ
axes_specs�er&r&r'r�-s 

zMemoryViewSliceTypeNode.analysecCsddlm}|�|j�dS)Nr)rc)r�rc�use_utility_code�view_utility_code)r~rArcr&r&r'reDsz-MemoryViewSliceTypeNode.use_memview_utilitiesN)F)rKrkrlr:r�r�rer&r&r&r'r_(s
r_c@seZdZdgZddd�ZdS)�CNestedBaseTypeNoderCNcCsr|j�|�}|tjkrtjS|js8t|jd|�tjS|j�|j	�}|rP|j
slt|jd||j	f�tjS|jS)Nz'%s' is not a valid type scopez '%s.%s' is not a type identifier)rCr�r
rr�rr%r+�lookup_herer:�is_typer/)r~rAr:rC�
type_entryr&r&r'r�Qs

zCNestedBaseTypeNode.analyse)N)rKrkrlr�r�r&r&r&r'riIsric@s*eZdZddddgZdZdZd	dd�ZdS)
r@ra�positional_args�keyword_args�
dtype_nodeNFcCs�|dkr|j�|�}|jr|S|jr�|��r�|jrP|jjrPt|jd�t	j
|_nPg}x:|jD]0}|�
|�}|dkr�t|jd�t
}|�|�q\W|�|j|�|_�n|j�r4ddlm}|�|j||j|j|j�}tjddkr�tdd�|��D��}t	j|f|�|_t|��r�t|j��r�t	�t|j�|j�|_n~t|jd	dd
�}	t|j�dk�s^|jj�rtt|jd�t	j
|_n>|j�s�d}
n
|jd}
t |j|	|
d�|_!|j!�||�d|_|jj"�r�|j#�r�|j�$|j#�|_|jS)
Nz+c++ templates cannot take keyword argumentsz!unknown type in template argumentr)�Bufferrr$cSsg|]\}}|�d�|f�qS)r�)r+)r�r:r7r&r&r'r��sz-TemplatedTypeNode.analyse.<locals>.<listcomp>r�)r:rzinvalid array declaration)r�r�)%rar�rr�r�rnr6rr%r
rr/rmr8rr�r;r�rp�analyse_buffer_options�buffer_defaultsr�version_inforbrcZ
BufferTyperr!ZPythranExprr r�rHr�rAr�r�r�)r~rAr:rC�template_typesrr/rp�optionsZempty_declaratorr�r&r&r'r�osZ




zTemplatedTypeNode.analyse)FN)rKrkrlr�ror:r�r&r&r&r'r@_s
	r@c@seZdZddgZddd�ZdS)�CComplexBaseTypeNoderCr9FcCs$|j�||�}|j�||�\}}|S)N)rCr�r9)r~rAr:r��_r/r&r&r'r��szCComplexBaseTypeNode.analyseN)F)rKrkrlr�r�r&r&r&r'rv�srvc@seZdZdgZddd�ZdS)�CTupleBaseTypeNode�
componentsFcCsXg}x8|jD].}|�|�}|jr0t|jd�tS|�|�qW|�|j|�}d|_|j	S)Nz/Tuple types can't (yet) contain Python objects.T)
ryr�r;rr%rrZdeclare_tuple_typer2r/)r~rAr:Zcomponent_typesr�r/r�r&r&r'r��s
zCTupleBaseTypeNode.analyseN)F)rKrkrlr�r�r&r&r&r'rx�srxc@s&eZdZdZgZdd�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
    cCs&|�|�}|�|j||j�}d|_dS)NT)r��declare_typedefr:r%r�)r~rAr/r�r&r&r'r��s
z"FusedTypeNode.analyse_declarationsFcCs`g}xJ|jD]@}|�|�}|s,t|jd�q||krBt|jd�q|�|�qWtj||jd�S)Nz
Not a typezType specified multiple times)r:)r`r8rr%rr
�	FusedTyper:)r~rAr:r`r%r/r&r&r'r��s
zFusedTypeNode.analyseN)F)rKrkrlrxr�r�r�r&r&r&r'rz�srzc@seZdZdgZddd�ZdS)�CConstTypeNoderCFcCs*|j�||�}|jr t|jd�t�|�S)Nz)Const base type cannot be a Python object)rCr�r;rr%r
r6)r~rAr:r�r&r&r'r��s
zCConstTypeNode.analyseN)F)rKrkrlr�r�r&r&r&r'r}�sr}c@s&eZdZddgZdZdZddd�ZdS)r�rC�declaratorsNc
Cs|jdkri|_|s|}||_|jr4|jd��}nd}|dk	r�|jdkrVt|jd�t|j�dkrvt|jdjd�td|�}|j	j
|_
x|D]}|�|j||j�q�W|j
�|�}|jr�|js�|js�|jr�t|jd�tSd|_|j}�x|jD�]
}t|j�dk�r0t|t��s0|j
d�r0t|jd	d�|j�oH|jdk�oH|j}|�rVd
|_t|t��r�|j|||j||jd�\}	}
n|j||||jd�\}	}
|
���s�|jdk�r�|
j�s�|
j�s�t|jd
|
�|jdk�r�|
j�r�t|jd�|	j}|	j}|dk�rt|jd�dS|
j�r4|jdk�r4t|jd�|
j �r�d|j
k�rNd|
_!|j"||
|j||j|j|j#|j$|jd�	|_|jdk	�r�t%�%|j�|j_|�r|jj&�'|�d|j_(q�|j�r�t|jd�|j)||
|j|||j|j#dd�|_t*j+r�t,|j|j-�|j_-q�WdS)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.F)r"r�r�)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�T)rr�r��api�	modifiersr!z,Decorators can only be followed by functions)rr�r�r�is_cdef).r"�
dest_scoper~r�r�rr%rHr�outer_scoper�Zdeclare_typer:rCr�r�r�r�is_module_scoperr�rIr�rr!r�r�r��is_memoryviewslicer;rZis_referencer��is_static_method�declare_cfunctionrr�rr/�create_to_py_utility_code�create_wrapperr.r�
docstringsr.r-)
r~rAr�rZtemplate_paramrCr�r9Zcreate_extern_wrapperr$r/r:rr&r&r'r�s�








z CVarDefNode.analyse_declarations)N)rKrkrlr��
decoratorsr"r�r&r&r&r'r�sr�c@s4eZdZdgZddd�Zdd�Zdd�Zd	d
�ZdS)�CStructOrUnionDefNode�
attributesNcCs2|j|j|j||j|j|j|j|j|jd�	|_	dS)N)r�r�packed)
r0r:r*r,r%rr�rr�r�)r~rAr+r&r&r'�declarezszCStructOrUnionDefNode.declarecCs�d}|jdk	rt|j�}|�||�|jdk	r�|jrB|jsBd|j_x|jD]}|�||�qJW|j	dkr�x>|j
D]4}|j}x|jr�|j
}q|W||jjkrpt|jd�qpWdS)Nrrz)Struct cannot contain itself as a member.)r�rr:r�r�r�r�r1r�r��var_entriesr/r�rCrr%)r~rAr+r�r/r&r&r'r��s 




z*CStructOrUnionDefNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rp�sz)CStructOrUnionDefNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz-CStructOrUnionDefNode.generate_execution_code)N)rKrkrlr�r�r�rpr�r&r&r&r'r�ls

r�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�CppClassNodeNc	Cs�|jdkrd}nRdd�|jD�}tdd�|jD��}|rbtdd�|jd|�D��sbt|jd�|j|jd|j|jg|j|d�|_	dS)NcSsg|]\}}t�||��qSr&)r
r)r��
template_name�requiredr&r&r'r��sz(CppClassNode.declare.<locals>.<listcomp>css|]\}}|VqdS)Nr&)r�rwr�r&r&r'r��sz'CppClassNode.declare.<locals>.<genexpr>css|]\}}|VqdS)Nr&)r�rwr�r&r&r'r��szGRequired template parameters must precede optional template parameters.)�base_classesr�r)
r�sumr�rr%�declare_cpp_classr:rr�r�)r~rArtZnum_optional_templatesr&r&r'r��s

"zCppClassNode.declarec
	s��jdkrd}}n dd��jD�}dd��jD�}d��jdk	rRt�j�|d���fdd�}t|��fdd��jD��}�j�j��j�j|�j	|d��_
�j
dkr�dSd	�j
_�dk	rĈj
j�_g}�fd
d���jdk	�rf�j
r�js�d	�j
_x4�jD]*}t|dd�}|�r|���|���q�Wx:��j�D],}	|�|	��jdk	�r6d
d�|�|	_�q6Wt�j|d��_��_dS)NcSsg|]\}}|�qSr&r&)r�r�rwr&r&r'r��sz5CppClassNode.analyse_declarations.<locals>.<listcomp>cSsg|]\}}t�||��qSr&)r
r)r�r�r�r&r&r'r��s)rcs$|js|jrdSt�jd|�dS)NTz&Base class '%s' not a struct or class.)r��	is_structrr%)Z
base_class)r~r&r'�base_ok�sz2CppClassNode.analyse_declarations.<locals>.base_okcsg|]}|��p���qSr&)r�)r��b)rAr+r&r'r��s)r�rrc3sJxD|D]<}t|t�r|Vqt|t�rx�|jj�D]
}|Vq4WqWdS)N)rI�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�)rr�rr:�filterr�r�r%rr�r�r�r/r�r�r1r�r�r�rr��template_declarationr�r�r+)
r~rArtZtemplate_namesr�Zbase_class_typesZ
defined_funcsr�r�rZr&)rAr�r+r~r'r��sD








z!CppClassNode.analyse_declarationscCs|j�|jjj�|_|S)N)r�rpr�r/r+)r~rAr&r&r'rp�sz CppClassNode.analyse_expressionscCs|j�|jjj|�dS)N)r�r�r�r/r+)r~rArUr&r&r'r��sz*CppClassNode.generate_function_definitionscCs|j�|�dS)N)r�r�)r~rUr&r&r'r��sz$CppClassNode.generate_execution_codecCs|j�|�dS)N)r�r�)r~rUr&r&r'r��szCppClassNode.annotate)
rKrkrlr�r�r�rpr�r�r�r&r&r&r'r��s
/r�c@s2eZdZdgZdd�Zdd�Zdd�Zdd	�Zd
S)�CEnumDefNodercc	Cs,|j|j|j|j|j|j|j|jd�|_dS)N)rr,r�rr�)	Zdeclare_enumr:r%rr,r�rr�r�)r~rAr&r&r'r��s
zCEnumDefNode.declarecCs@|jdk	r<|jr|jsd|j_x|jD]}|�||j�q&WdS)Nr)rcr�r�r�r1r�)r~rAr�r&r&r'r�s

z!CEnumDefNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rpsz CEnumDefNode.analyse_expressionsc
Cs�|jdks|jr�|�|j�|jjtjdd�}xj|jj	D]^}|�
d||j|�||j�f�|�
|�|�
dtj|j||�|j�f�|�|tj�q8W|j�|�dS)N�publicT)�
manage_refz%s = PyInt_FromLong(%s); %sz.if (PyDict_SetItemString(%s, "%s", %s) < 0) %s)r�rr�r%�	funcstate�
allocate_tempr
rr��enum_valuesrOr�error_goto_if_null�
put_gotrefr	Z
moddict_cnamer:�
error_goto�put_decref_clear�release_temp)r~rU�tempr�r&r&r'r�s 
z$CEnumDefNode.generate_execution_codeN)rKrkrlr�r�r�rpr�r&r&r&r'r��s
r�c@seZdZdgZdd�ZdS)�CEnumDefItemNoder7c
Cs�|jr>|j�|�|_|jjjs>|j�tj|�|_|j�|�|_|j|j|j|j|j	|j
|j|j|j
ol|jdkd�}|j�|�|jr�|jj�|j�dS)N)rr�rr�)r7r�r/r�rr
r=Z
declare_constr:r%rr�rr�r�rr�)r~rAZ
enum_entryr�r&r&r'r�)s
z%CEnumDefItemNode.analyse_declarationsN)rKrkrlr�r�r&r&r&r'r�"sr�c@s,eZdZddgZdd�Zdd�Zdd�Zd	S)
�CTypeDefNoderCr9cCsr|j�|�}|jj|||j|jd�\}}|j}|j}|j|||j||j|j	d�}|j
r\d|_|jrn|jsnd|_dS)N)r�r�)rr�rTr)
rCr�r9r�r�r:rr{r%rr�r�r1)r~rAr�r$r/r:rr�r&r&r'r�Bsz!CTypeDefNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rpSsz CTypeDefNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r�Vsz$CTypeDefNode.generate_execution_codeN)rKrkrlr�r�rpr�r&r&r&r'r�9sr�c@s�eZdZdZdZdZdZdZdZdZ	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))�FuncDefNodeNFcCszd}xp|jD]f}|jrVd}|jrB|j�|�|_|j�|j|�|_qrt|jd�d|_q|jrbd}q|rt|jd�qWdS)Nrrz)This argument cannot have a default valuez/Non-default argument following default argument)	rRr�rProrr/rr%rQ)r~rAZdefault_seenr4r&r&r'�analyse_default_values}sz"FuncDefNode.analyse_default_valuescCs2|dkrdS|jdr$|�|�dkr.|�|�}|S)Nr<)r�r8ro)r~rAr@r&r&r'�analyse_annotation�s

zFuncDefNode.analyse_annotationcCs*x$|jD]}|jr|�||j�|_qWdS)N)rRr@r�)r~rAr4r&r&r'�analyse_annotations�szFuncDefNode.analyse_annotationscCs�|j}|j}|j|kr,||j}|�|�}n<t|t�rd|jrd|jdrd|j}|�|�}|dkrh|Sn|S|dkr~t	|j
d�n4|tk	r�|�|�s�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:r8rIr8r@r�rBrr%rrrC)r~rAr4r"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_acquisitioncCs�|}x|js|jr|j}qW|jr<t|jj|||jjd�}nt|jj||d�}|j	|_	|jj
}|jrt|jop|j
|_||_|j|_|S)N)r:r��parent_scopeZ
scope_name)r:r�r�)�is_py_class_scoperr��
needs_closurerr�r:rr
rr/r�rr
�local_scoper�)r~rA�genvr�r/r&r&r'�create_local_scope�s$
zFuncDefNode.create_local_scopecCs|j�|�dS)N)r�r�)r~rArUr&r&r'�generate_function_body�sz"FuncDefNode.generate_function_bodyc'Cs�ddlm}|jjr ddlm}|j}|jrD|jsDdtj	tj
f}ntj
}|�|�|j�
||�|�||�|jjdko�|jjj}|jjdko�|jjj}|p�|}	|	r�d|jkr�|jdg|_|��}
|jjd}|jjd	}|s�|r�|j�t�d
d��|�|�|��|_|j|j_|� |j!�|�"||�|�#d�|
�rL|�#|
�|�$||��p^|j%}
|j&�rz|j&j'||
d
d�|j'||
d�|}x|j(�s�|j�r�|j)}�q�W|j*�r�|�+|j,j-�.tj	��|�#d�nP|j/�r$|j�r|�+|j,j-�.tj	��|�#d�|�+|j,j-�.tj
��|�#d�|�0||�x*|j1D] }|j2�s8|j3�s8|�4|��q8Wd}|jj5�s�|jj6�rzd}n|jj�r�d|j7}|�#d|j�.tj8�|f�|�9�}|�:|�|j;}|j*�p�|j/}x |j<D]}|j-j6�r�d
}P�q�Wdd�|j=D�}|j�o|j>�o|�p|}|j�o0|j>�o0|}|j�p@|j>}|�sN|�r`|�?�d
|j_n|j�rx|j>�rx|�@�|�s�|�r�|jA�s�|�B�|jC�r�|jC�D|�nd}|�E|�|�r�|�F|�|�r�|�G�|jH|jj|d�|�r�|�I|�|j*�r�tJ�Kdd�}tJ�L|j,j-j|�}|�s0d|j,j-jM}|�#dtj	|j,j-�N�||j,j-jMtjOf�|�#dtj	�|�#dtj	|j,j-�Pd�f�|�QdtR�|�#|�S|j!��|�#d�|�Ttj	�|�#d�|j/�rT|jU�r�|�#d ||j,j-�N�tjVf�n|�#d!||j,j-�N�tjVf�|j�r2|�#dtj	|f�n"|j*�rT|�Q||j,j-�|�W|�|�s`|�r�|jA�s�|jX�r~|jjd"}n|jj}|jY||j!|jjd#�d
|j_Z|�[||�t\|t]�}xx|j<D]n}|j-j6�r�|�s�t^|j_�dk�r0|j2�s0|�`|�n4|�r�|j-j�r�t^|j_�dk�r�|ja|jb|jjd$��q�WxN|j1D]D}|j3�r>t^|j_�dk�r>|j2�s>|jc�rv|�d|�n
|�`|��q>Wx8|j1|j<D](}|j-je�r�|jfjgjh�r�|�i||��q�W|�j|�x*|j<D] }|j-je�r�|�k|||j!��q�W|�r|�l�d%|j_|�m||�|j |j!d%d&�|�#d�|�#d'�|jjn�s�|jj6�rftj8}|�o||j�n>|jjp}|�r�|�#dtj8|f�n|jj5�s�|�#d(tj8�|jq|jrk�	r�|jjn�s�|�s|jt�|�u|jq�x,|j�v�D]\} }!|jw| |!|jd$��q�Wt^|�d)k}"|"�rn|j�tx�|�#d*�|�#d+�|�#d,�|�#d-�x|D]}|�y||��qNW|�#d.�|jj�r�|�ztj8|�tj8}#n|�{�}#|�|�}$|#dk	�s�|$�	r|j�r�|j>�s�|�#d/�|�?�|�}|jj~�|j�	r,|j>�	s,|�l�|�#d�n*t|jj!d0|jj~d)�|��|jj~|j�|jjp}%|#dk�	rH|%�	rH|%}#|#dk	�	rh|�#dtj8|#f�n|jj5�	s�|�#d(tj8�|�	r�|��|�|"�	s�|�	s�|jj�	r�|�s|j�|�u|jt�x|D]}|�y||��	q�W|�	r�|��|�|jj�
rL|��|j��tj8��}&|�#d1|&�|j�
r(|�?�|�#d2�|j�
rB|�l�|�#d�|�u|j�x�|j1D]z}|jh�
r`|j2�
rx�
q`|j-j�
r�|j�|jb|jd$�n@|j-j6�
r`|j3�
r�t^|j_�dk�
r`|jc�
r�|��|�n
|��|��
q`Wxx|j<D]n}|j-j6�r|�s
t^|j_�dk�rR|j2�sR|��|�n4|j-j�
r�|�r>t^|j_�dk�
r�|j�|jb|jd$��
q�W|j*�rr|��tj	|j,j-�|j�s�|jjp}%|�{�}#|#dk�r�|%�r�|%}#|jj6�r�|��|j��tj8��|jj��r�|jjd3k�r�|�#d4tj8tj8f�|�s�|�r<d%|j_Z|jA�s<|jj6�r(|j�tj8|jjd#�n|j�d|jjd#�|j�sL|���|�sb|j�rr|j>�rr|�l�d%|j_|jj5�s�|�#d5tj8�|�#d�|
�r�|�#d6|
�|��|j�|���|j&�r�|j&�
||�|��|�dS)7Nr)rp)rcz%s->%s�
__getbuffer__�__releasebuffer__Z
cython_unused�profile�	linetraceZProfilez	Profile.cr�T)�with_pymethdef�
proto_only)r��;z = NULLz = z%s%s;cSsg|]}|jr|�qSr&)r2)r�r�r&r&r'r�3sz=FuncDefNode.generate_function_definitions.<locals>.<listcomp>)�acquire_gilZtp_newrez
%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�rprr�rcr��is_closure_scopeZis_passthroughr	�cur_scope_cname�outer_scope_cnameZmangle_closure_cnamesr�r�r�r�r:r+rr��get_preprocessor_guardr�r�rgr�load_cached�enter_cfunc_scope�	new_label�return_from_error_cleanup_labelrr��	gil_ownedr�r%r�rO�needs_assignment_synthesis�pymethdef_required�py_func�generate_function_headerr�r�r��put�scope_classr/r��needs_outer_scope�generate_argument_declarationsr��
in_closurer4�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_tuplerE�
put_increfrr�r��
is_cyfunction�
self_cnamerIr��put_trace_call�	can_trace�generate_argument_parsing_coderIr�rH�cf_assignments�put_var_incref�put_incref_memoryviewslicer�xdecref_cleanupZput_var_xincrefr�Z
buffer_auxZbuflocal_nd_varr2Z
put_init_vars�generate_argument_type_testsZput_acquire_arg_buffer�put_release_ensured_gilr�r��put_init_to_py_nonerC�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)'r~rArUrprcr�r�Zis_getbuffer_slotZis_releasebuffer_slotZis_buffer_slot�preprocessor_guardr�r�r��cenvr��init�tempvardecl_coder�Zhave_object_argsr4Zused_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_definitionscCsh|jjrt|jd�n,|j��sB|jjsB|jjsBt|jd|j�|�|j|j|j�}|j	rd|j	|_	|S)NzInvalid use of 'void'z Argument type '%s' is incomplete)
r/r�rr%r�r�r�Zdeclare_argr:r@)r~rAr4r�r&r&r'�declare_argumentxszFuncDefNode.declare_argumentcCsv|j��rf|j�t�dd��|jj}d|jj}|�	d|||j
|j|jjoR|jj
|�|j�f�nt|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�rgrr�r�r�rrO�accept_noner:�is_builtin_typeZ
require_exactr�r%r)r~r4rUr��arg_coder&r&r'�generate_arg_type_test�s
z"FuncDefNode.generate_arg_type_testcCsd|jjrd|jj}n|jj}|�d|�|�dtdt|j��|j|�|j	�f�|�d�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�rrOr�rHr:r�r%)r~r4rUrr&r&r'�generate_arg_none_check�sz#FuncDefNode.generate_arg_none_checkcCsdS)Nr&)r~rUr&r&r'r�sz&FuncDefNode.generate_wrapper_functionscCs0|�|j�x|jD]}|js|�|�qWdS)N)r�r%rRrTrN)r~rUr4r&r&r'r��sz#FuncDefNode.generate_execution_codec	CsH|jjd}y|jjjjdj}Wnttfk
r>d}YnX||fS)Nr�obj)r�r�r/rCr+r�r��KeyError)r~�	py_buffer�obj_typer&r&r'�_get_py_buffer_info�s
zFuncDefNode._get_py_buffer_infocCsB|��\}}|j}|�d|�|�d�|�d�|�d�dS)Nzif (%s == NULL) {zZPyErr_SetString(PyExc_BufferError, "PyObject_GetBuffer: view==NULL argument is obsolete");z
return -1;r�)r)rrO)r~rUr'rw�viewr&r&r'r��s

zFuncDefNode.getbuffer_checkcCsN|��\}}|j}|r<|jr<|�d||�|�d|�n|�d|�dS)Nz%s->objz%s->obj = NULL;)r)rr;r�rIrO)r~rUr'r(r*r&r&r'r��s
zFuncDefNode.getbuffer_initcCsf|��\}}|j}|rT|jrT|�d|�|�d|�|�d||�|�d�n|�d|�dS)Nzif (%s->obj != NULL) {z%s->objr�zPy_CLEAR(%s->obj);)r)rr;rOr�r�)r~rUr'r(r*r&r&r'r�s
z#FuncDefNode.getbuffer_error_cleanupcCsV|��\}}|j}|rR|jrR|�d|�|�d|�|�d||�|�d�dS)Nzif (%s->obj == Py_None) {z%s->objr�)r)rr;rOr�r�)r~rUr'r(r*r&r&r'r	�s
z$FuncDefNode.getbuffer_normal_cleanupcCs`|jjsdS|jj}tj�|�}|s(dS|dkrB|jj�d�sBdS|dkrX|jjjrXdS|�	�S)NZ__long__�__int__)r�r�)
r�rr:r�method_name_to_slot�getr+rjr�preprocessor_guard_code)r~r:�slotr&r&r'r��sz"FuncDefNode.get_preprocessor_guard)#rKrkrlr�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�rr"r$rr�r)r�r�rr	r�r&r&r&r'r�ZsF/


			r�cs�eZdZddddgZdZdZdZdZdZdZ	dZ
dZdd�Ze
d	d
��Zdd�Zd
d�Zdd�Zd3dd�Zdd�Zdd�Zdd�Zdd�Zd4dd�Zd5dd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Z�fd+d,�Zd-d.�Zd/d0�Z d1d2�Z!�Z"S)6r�rCr9r��py_func_statFNcCs|jjS)N)r�r:)r~r&r&r'�unqualified_name	szCFuncDefNode.unqualified_namecCs|jr|jjSdS)N)r�r�)r~r&r&r'r�	szCFuncDefNode.code_objectcCsN|j|_|jdkri|_|j�|jd�|jdk	r^|j�|�}|dkrjt|jjd�t	j
}n|j�|�}d|jko~|�
d�|_t|jt�r�|jj||d|jdk	|j|jd�\}}n$|jj||d|jdk	|jd�\}}|js�t|jd�||_|j|_|j}xt|d��s|j}�qW||_|j|_|jj}|jd	k�sF|j�rZ|�rZt|jjd
�|jdk�r�|jdk�r�t|jjd
�x�t |j|j�D]�\}}|�!||�|j|_|j"|_"|j#|_#|�$|j|j|�|jj%�r�d|_&|jj'�rd|j(k�rt|jdd�|jj'�s|jj)�r�|jj*�r.t|jd�nd|j(k�r�t|jdd��q�W|�$|j+|j|�|j"}	|j#}
|j,|_,|j|_|j-|	||j|
|j|j|jdk	|j(|jd�	|_.|j/|j._0|j+|_+|j+j1�r�|jdk�r�t|jd�|j+j2�r�|j+�3|jd�|j�r6|j4�s6|j�s6t5|j�dk�s0|jdjj6�s6d|_|�7|�|�8|�dS)N�localsz
Not a typer�r")r�r"r�)r�r�z*Suite attached to non-function declarationrRr�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�r�definingr�r!zFunction cannot return an arrayzused as a return valuerF)9r�is_c_class_methodr"r}r��directive_returnsr8rr%r
rrCr�rjr�rIr9r�r�r�r�r/r!rr?r�Zcfunc_declaratorrRr
rrr�zipr�r:r�_validate_type_visibilityr�r1r�r��is_pythran_exprrrrr�r��
inline_in_pxdZinline_func_in_pxdr�r�Zcheck_nullary_constructorr�rHr;�declare_cpdef_wrapperr�)r~rArCr$r/r9Z
opt_arg_count�
formal_arg�type_argr:rr&r&r'r�	s�






 
z!CFuncDefNode.analyse_declarationscCsH|j�rD|jrt|jd�|jj}|j|jd�}|jrnddlm	}t
|j||jdd�d�g}|dj�|�ng}t
|j|jj|jdd|j||dd	�	|_|j|j_|j�|�d
|jj_t|j|jgd�|_tj|j_|jj|j_d
|j_|jj_|j|j|<|jj�sD|j�rtj�rDt|j|jd�|_t|j|j|j gd�|_ dS)
Nz&static cpdef methods not yet supported)r�r)r�r�)r:)�	decoratorr)	r%r:rRr2r3r-r�r�r�T)r�)r�)!r!r�rr%r�r:�call_self_noder�r�r��
DecoratorNoderBro�DefNoderRr-r�r�rr�r4r
rr/�as_variabler2r��is_final_cmethodr�lookup_module_cpdef�OverrideCheckNode�overrider�)r~rAr:Zpy_func_bodyr�r�r&r&r'r?z	s>




z"CFuncDefNode.declare_cpdef_wrappercCsN|jdkp|j}t|dd�}|rJ|rJ|jrJ|jdksJ|jsJ|jsJt|d�dS)zo
        Ensure that types used in cdef functions are public or api, or
        defined in a C header.
        r�r�N)r�rz:Function declared public or api may not have private types)r�rr�r�r�r)r~r/r%rAZ
public_or_apir�r&r&r'r<�	s
z&CFuncDefNode._validate_type_visibilityrc
s*ddlm��jj}|r0|dt|��jj�}dd�|D�}|rb�j�j�jj	d�}|}t
j}n��jjr��jj
jj}�j�j|j	d�}	||	_�j�j|	�jj	d�}d}n>�jjdjj}
�j�j|
j	d�}|
|_�j�j|�jj	d�}|p�t
j}�j�j|��fd	d�|D�|d
�}t�jtj|d�S)Nr)r�cSsg|]
}|j�qSr&)r:)r�r4r&r&r'r��	sz/CFuncDefNode.call_self_node.<locals>.<listcomp>)r:)r%�	attributeTrcsg|]}�j�j|d��qS))r:)r�r%)r��n)r�r~r&r'r��	s)�functionrRZwrapper_call)r%rr7)r�r�r/rRrHr
r�r%r�r:rrHr�r+rZ
AttributeNode�SimpleCallNode�ReturnStatNoder
r)
r~Zomit_optional_argsr�rR�	arg_names�cfuncZcall_arg_namesZ
skip_dispatchZclass_entryZ
class_noderlrAZc_callr&)r�r~r'rC�	s4zCFuncDefNode.call_self_nodecCs4x.|jjD]"}|js t|jd�|�||�q
WdS)NzMissing argument name)r/rRr:rr%r)r~rAr4r&r&r'�declare_arguments�	szCFuncDefNode.declare_argumentscCs|jjS)N)r/r
)r~r�r&r&r'r��	sz!CFuncDefNode.need_gil_acquisitioncCsZ|j}|j}|jrV|sV|jjr*t|jd�x*|jjD]}|jjr4|j	s4t|jd�q4WdS)Nz9Function with Python return type cannot be declared nogilz8Function declared nogil has Python locals or temporaries)
r/r
rrr;rr%r�r�Zin_with_gil_block)r~rAr/r
r�r&r&r'r��	s
zCFuncDefNode.nogil_checkcCsd|j|j_|jdk	r$|j�|�|_n.|jdk	r>|j�|�|_n|�|�|�|�|�|j�|_|S)N)	r�r�r4rpr�r�r�r�r�)r~rAr&r&r'rp�	s




z CFuncDefNode.analyse_expressionscCsdS)NFr&)r~rArUr&r&r'r��	sz'CFuncDefNode.needs_assignment_synthesisrcCs�|j}g}|j}xN|jdt|j�|j�D]0}	|	��}
|�|	j�}|jsRd|
}
|�	|
�q,W|r�|j
r�tj�t
j�}|jr�|�	|�n|�	d|�|jr�|r�|�	|j�t
j��|jr�|�	d�|s�dg}|dkr�|jj}|�|d�|��}
|jjdk�rd|k�rd}nd}d}|�|jj�}|jj|
|d	�}|j}|j�rj|�r^|jjd
�|j�|�|j�|�r�|jjd
�d|||f�|�d|||f�dS)
NzCYTHON_UNUSED %sz...�voidz, �privatez::zstatic r�)�dll_linkageZmodule_declarationsz%s%s%s; /* proto*/z%s%s%s {) r�r/rRrHr
r�rZr:�cf_usedrr!r
r=r	�skip_dispatch_cnamerJr3�optional_args_cnamer r��
func_cnameZfunction_header_coder�r�Zbuild_function_modifiersZfunc_modifiersrr9r�r��partsrO)r~rUr��
with_opt_args�
with_dispatchrr+Z	arg_declsr/r4Zarg_declr�Zdispatch_arg�entityZ
storage_classrUr��headerZneeds_protor&r&r'r��	sN 


z%CFuncDefNode.generate_function_headercCs^|j}xR|jD]H}|jr|�|j�}|js0|jr|�|�}|�d|j	�
|j�|f�qWdS)Nz%s = %s;)r�rRr�rZr:rJrVrFrOr/r�r)r~rArUr+r4r�r�r&r&r'r�
s
z+CFuncDefNode.generate_argument_declarationscCsdS)Nr&)r~rUr&r&r'r�%
sz"CFuncDefNode.generate_keyword_listc
	sd}d}|j}|jjr��dtj�x�|jD]���jr.|��j	�}|j
sP|jr���dtjtj|f��j
}xt|d�s�|j}qpW��d�jtj|j�|j	�f�|d7}|d7}q.Wxt|�D]}��d�q�W��d���fdd	�}	x|jD]�|	|��j	��q�WdS)
Nrz	if (%s) {zif (%s->%sn > %s) {r:z%s = %s->%s;rr�cs:|jr6�js6��d|j|jf���|���|�dS)Nz%s = %s;)r�r�rOr�original_cnamer��put_var_giveref)r�)r4rUr&r'�put_into_closureB
s
zECFuncDefNode.generate_argument_parsing_code.<locals>.put_into_closure)r�r/r
rOr	rXrRr�rZr:rJrVr-r9r?r�rZ
opt_arg_cname�rangerj)
r~rArUr#r2r+r�r9rwrar&)r4rUr'r�(
s4

z+CFuncDefNode.generate_argument_parsing_codecCsdS)Nr&)r~rUr&r&r'�!generate_argument_conversion_codeK
sz.CFuncDefNode.generate_argument_conversion_codecCsDx>|jjD]2}|jr"|�||�q
|jjr
|js
|�||�q
WdS)N)r/rR�needs_type_testr"r;rr$)r~rUr4r&r&r'r�N
s
z)CFuncDefNode.generate_argument_type_testscs&tt|��|�|jr"|j�|�dS)N)rdr�r�r4)r~rU)rJr&r'r�W
sz$CFuncDefNode.generate_execution_codecCs|jjrdS|jjjSdS)N�0)rr;r�r/r�)r~r&r&r'r\
szCFuncDefNode.error_valuecCs
|jjjS)N)r�r/r)r~r&r&r'rc
sz)CFuncDefNode.caller_will_check_exceptionscCs d}|j}|j}�x|jdk	�r|d7}|j}d|jjtj|f|_|��|j|d|jj|jj	|jd�|j
js||�d�|jj
}dd�|dt|�|jj	�D�}|jjr�|�tj�n|jr�|�d�|jj	r�|�tj�n|j	r�|�d	�|�d
|jjd�|�f�|�d�qWdS)
Nrrz%s%swrap_%s)r\r[rzreturn cSsg|]
}|j�qSr&)r)r�r4r&r&r'r�y
sz;CFuncDefNode.generate_wrapper_functions.<locals>.<listcomp>re�NULLz%s(%s);z, r�)r�r/Z
prev_entryrYr	r-rOr�rr
rr�r�rRrHrrWrXr�)r~rU�kr�rrRZarglistr&r&r'rf
s6

"

z'CFuncDefNode.generate_wrapper_functions)rr)N)rrN)#rKrkrlr�r>r�r"r:rJr�rr4r5�propertyr�r�r?r<rCrRr�r�rpr�r�r�r�r�rcr�r�rrrrmr&r&)rJr'r��s<]$


-
#	r�c@s eZdZgZdZdZdd�ZdS)�
PyArgDeclNodeFcCs|j�||�dS)N)r�r�)r~rArUr&r&r'r��
sz+PyArgDeclNode.generate_function_definitionsN)rKrkrlr�rrOr�r&r&r&r'ri�
sric@seZdZdgZdS)rDrBN)rKrkrlr�r&r&r&r'rD�
srDc@seZdZddddddgZdZdZdZd	Zd
Zd
Z	dZ
dZdZd
Z
d
ZdZdZdZdZdZdZdZdZdZdZdd
�Zd6dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"d d!�Z#d"d#�Z$d7d$d%�Z%d&d'�Z&d(d)�Z'd*d+�Z(d8d,d-�Z)d.d/�Z*d0d1�Z+d2d3�Z,d4d5�Z-dS)9rErRr2r3r�r��return_type_annotationFNrerTcKsltj||f|�d}}}x8|jD].}|jrD|d7}|jsD|d7}|js$|d7}q$W||_||_||_dS)Nrr)r�r�rRrQr��num_kwonly_args�num_required_kw_args�num_required_args)r~r%rSrgZrk�rr4r&r&r'r��
szDefNode.__init__cCs|jrt|jjd�|jr(t|jjd�|p.d\}}|dkr�g}	x@|jD]6}
|
j|dd�\}}|	�tj|j	d|
j
t|
jd��qHWtjt|	dd|dd|d�}
t
|j|
d	�}n�|dkr�|j}|j}
t|j�t|
j�ks�|
jr�t|jd
�t|jd�xhtt|j|
j��D]R\}\}
}|
j|d|dk�o(|jd
�\}}|dk�sH|tjk�r|j|
_||
_�qW|dk�r�|
jdk	�r�ddlm}||j|
j|
jd�}t|jt|j|j	dd�|jd|
j||
j|
jd�}t|j|�p�gt |j|
jd	�||j!|j"|
j#|
|
j|
jddt$|di�|d�S)Nz'cdef function cannot have star argumentz+cdef function cannot have starstar argument)NFr)r�)r:rr@r/r%F)rrRr r�rrr
r)r/zwrong number of argumentszprevious declaration herer)r�r)r	)r7r/)r:r)r�rRr rr�r
rrTr")
r�rCr9r�r-r!r/r
rr�rr"r:)%r2rr%r3rRr�rr
rr:r@rrr�r+r/rHr rr;rr$r�r�r	rr�r�rr
rr�r?r�r-rr�)r~rQr+r!�returnsZ
except_valr�r�rZ
cfunc_argsr@r$r/Z
cfunc_typer#rAr	r9r&r&r'�as_cfunction�
sz 

zDefNode.as_cfunctioncCs|jr
dS|js|jrdSdS)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�r2r3)r~r&r&r'�is_cdef_func_compatibles
zDefNode.is_cdef_func_compatiblec	Cs\|jrJxB|jD]8}|j}|jr|j|jdkO_|j|jdkO_qW|jr`|�d�r`d|_|jrv|�d�rvd|_|jdkr�|jr�d|_|�|�|jdkr�|�	|�n
|�
|�|�|�|jj
��|_|jtk�r|j�r|jd�r|jj�st|j|�\}}|�r|j�r||_|�|�t|j||jj|j|j|j|jd�|_|j�|�dS)	N�classmethodr�Frerz<lambda>r<)rLr:rRr2r3r)r�rBr9�is_classmethodr:�is_staticmethodrjr��analyse_argument_types�declare_lambda_function�declare_pyfunction�analyse_signaturer��	signaturerrrjr�rrEr;r��DefNodeWrapperr%rRr2r3�
py_wrapperr�)r~rArBrZrwrr&r&r'r�(sB




zDefNode.analyse_declarationscCs�|jd|_|jd}|j}d|_�xx|jD�]l}t|d�rBd}nV|j�|�}t|�rl|jrlt	�
||jg�}|j�||�\}}|j
|_
||_|jr�d|_|�||�|r�|jr�t|jd�|j��|_d|_d|_d|_d|_|jjs�|jjs�|jj�rn|j�rd|_nd|j�rd|_nT|jj �s@|jj!�s@|jj�s@|jj�rf|j"�r^|j"j#dk�r^d|_n||_nd|_q,d|_|j�r�t|jd	�|jr,t|jd
�q,W||_t|��r�dd�t$|j�D�|_%ng|_%dS)
Nr6�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'cSsg|]\}}|jjr|�qSr&)r/Zis_numpy_buffer)r�r#�ar&r&r'r��sz2DefNode.analyse_argument_types.<locals>.<listcomp>)&r�r"r�rRr?rCr�rr=r
r|Z
org_bufferr9r:r/r�r1r�rrr%Zas_argument_type�hdr_type�needs_conversionrdrPr;r�r�rSrrR�is_extension_typer r��constant_resultr�np_args_idx)r~rAr|Zf2sr4r$rCr/r&r&r'ruUsZ




zDefNode.analyse_argument_typescCs2|jjr<|jrt|jd�t|j�dko4|jp2|j|j_	n�|j
d�s|j�s|j�s|jjtj
kr�t|j�dkr�tj|j_n4t|j�dkr�|jdjdkr�|jdjs�tj|j_nd|jjtjk�rt|j�dkr�tj|j_n:t|j�dk�r|jdjdk�r|jdj�stj|j_|jj}|��}|tjk�rp|dk�rpt|j�dk�rp|j�rptj
}|j_d|_d}|j�r�|j�r�d}d|_t�|�|j_}d|_d|_d|_|j�s�|j�r�|j�r�|j�r�|jjdd�=x�t t!|t|j���D]�}|j|}d|_"|�#|��rT|j�sT|j�r8d|_$t%j&|_'|_(nd|_#|j)|_'|_(d|_*n>|�+|�|_'|j(�,|j'��s�|j'j-�r�|j(j-�r�d|_.nd|_*|j*�r�t/j0|j1|_2nt/j3|j1|_2�q�W|t|j�k�r�|�4�dS|t|j�k�r.|j�s�|�4�x2|jD](}|j"�r|j(j5�s"|j(j6�rd|_.�qWdS)Nz8special functions of cdef classes cannot have decoratorsrZalways_allow_keywordsrr"T�*)7r�rr�rr%rHrRr2r3Ztrivial_signaturer�ryrZpyfunction_signatureZpyfunction_noargsr�rQZpyfunction_oneargZpymethod_signatureZ	unaryfuncZibinaryfunc�num_fixed_args�self_in_starargrtrrZfixed_arg_format�has_generic_argsrsr1Zdecorator_indirectionr�rb�minrPrrOrZ	type_typer~r/rrZfixed_arg_typerr;rdr	Z
arg_prefixr:�	hdr_cnameZ
var_prefix�
bad_signaturer�r )r~rA�sigZnfixedr#r4r&r&r'rx�sz" 

zDefNode.analyse_signaturecCsj|jj}d|��}|jr"|d7}|j}|�d�rB|�d�rBd}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�ryr�r�r:�
startswith�endswithrr%rHrR)r~r�Zexpected_strr:�descr&r&r'r��szDefNode.bad_signaturecCs|j}|�|�}|rP|jr.|jjs.t|jd�|jjrP|j	sP|j
sPt|jdd�|j||j|j
d�}||_
|�|j�}tj|||j
_tjr�t|j|j�|_tj|||_|jr�|jtjks�|jr�|jdkr�|jdr�d|_q�tj|||_nd|_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:rjrGr�
is_final_typerr%r/r�Zis_builtin_cmethodr�rrwr��next_id�scope_prefixr	Z
pyfunc_prefix�pyfunc_cnamerr�r.r-Zfuncdoc_prefix�	doc_cnamerrZ	invisibler��wrapperbase_cnameZwrapperbase_prefix)r~rAr:r��prefixr&r&r'rw�s(
zDefNode.declare_pyfunctioncCs*|�|j|j�}d|_||_|j|j_dS)N)rv�lambda_namer%r-r�rr�)r~rAr�r&r&r'rvszDefNode.declare_lambda_functioncCs�xv|jD]l}|jst|jd�|jrL|�|j|j|j�|_|jjrZd|j_	n|�
||�|_d|j_d|j_|j
|j_
qW|�||j�|�||j�dS)NzMissing argument namerer)rRr:rr%rr.r/r�r;rrr4r2r�declare_python_argr2r3)r~rAr4r&r&r'rRs
zDefNode.declare_argumentscCsR|rN|jddkrtj}nt}|�|j||j�}d|_d|_d|_	d|_
||_dS)N�infer_typesFrre)r�r
Zunspecified_typerr.r:r%r4r2rr�r�)r~rAr4r/r�r&r&r'r�szDefNode.declare_python_argcCs||j|j_|�|�|�|�|jr4|�||j�|_|�|�sl|jrlx&|jddd�D]}|j�	|�|_qVW|j
�|�|S)Nr�)r�r�r�r�rjr�r�r�rBrpr{�prepare_argument_coercion)r~rArBr&r&r'rp+s


zDefNode.analyse_expressionscCsv|jr
dS|js|jjrdS|jr&dS|jjr2dS|jjr>dS|jsJ|jrj|dkr^|j	j
dS|jj
dS|jpt|j
S)NTFZbinding)rt�specialized_cpdefsr�Zis_fused_specialized�no_assignment_synthesisrZis_anonymousr�rr�r�r�r�r�)r~rArUr&r&r'r�9sz"DefNode.needs_assignment_synthesiscCs
|jjjS)N)r�ryr)r~r&r&r'rKszDefNode.error_valuecCs
|jjjS)N)r�ryr)r~r&r&r'rNsz$DefNode.caller_will_check_exceptionscCsJ|jr|j�|��|�|jr8|jj|j_|j�||�t�|||�dS)N)�defaults_getterr�r��py_wrapper_requiredr�rYr{r�)r~rArUr&r&r'r�Qsz%DefNode.generate_function_definitionscCs|r|jr|j�||d�dSg}|jjjrNdtj}|jsDd|}|�	|�dd�}x|j
D]}|�	||��q^W|jr�|�	||j��|jr�|�	||j��|r�d�
|�}nd}|j�|jj�}	|jd}
|��}|r�|
�|�|
�d	|	|f�|�r|
�d
�|�d|	|f�dS)NTzPyObject *%szCYTHON_UNUSED cSs8|j}|jr|j}n|j}|j�|�}|js4d|}|S)NzCYTHON_UNUSED )r�r�r_rr/r�rV)r4r�rZdeclr&r&r'�
arg_decl_codejsz7DefNode.generate_function_header.<locals>.arg_decl_codez, rSZdeclszstatic %s(%s); /* proto */z#endifzstatic %s(%s) {)r�r{r�r�ry�
has_dummy_argr	r�r�rrRr2r3r�rr�r�r�r�rO)r~rUr�r��
arg_code_list�self_argr�r4r!�dcZ
decls_coderr&r&r'r�]s>






z DefNode.generate_function_headercCsdS)Nr&)r~rArUr&r&r'r��sz&DefNode.generate_argument_declarationscCsdS)Nr&)r~rUr&r&r'r��szDefNode.generate_keyword_listcsN�fdd�}x|jD]}||j�qWx"|j|jfD]}|r4||j�q4WdS)Ncs4|jr0��d|j|jf���|���|�dS)Nz%s = %s;)r�rOrr_r�r`)r�)rUr&r'ra�s
z@DefNode.generate_argument_parsing_code.<locals>.put_into_closure)rRr�r2r3)r~rArUrar4r&)rUr'r��sz&DefNode.generate_argument_parsing_codecCsdS)Nr&)r~rUr&r&r'r��sz$DefNode.generate_argument_type_tests)NNTNNN)N)r).rKrkrlr�rtrsr�Zreqd_kw_flags_cnamer�r�r�rjr�r�r�Z
py_cfunc_node�requires_classobj�defaults_structr-�
fused_py_funcr�r{r�rYr�r�rprqr�rurxr�rwrvrRr�rpr�rrr�r�r�r�r�r�r&r&r&r'rE�
sV
B-@M



.
rEc@s�eZdZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zd5dd�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)6rzNcOsDtj|f|�|�|jj|_|jj|_|jj|_|jj|_d|_dS)N)r�r�rLrkrlrmr�ry)r~rRrsr&r&r'r��s



zDefNodeWrapper.__init__cCsP|jj}|j}|�|j�}tj|||_tj|||_	|j
|_
|jj|_dS)N)rLr�r:r�r�r	Z
pywrap_prefixrYZpymethdef_prefix�pymethdef_cnameryr�)r~rAZtarget_entryr:r�r&r&r'r��sz#DefNodeWrapper.analyse_declarationscCs�x>|jD]4}|jjs"|j�|�s<q|jr|jjs|j�|�sqW|jr�|jjjs�|jj}d|_	x$|j
D]}|jsf|jj
rfd|j_qfWdS)NrT)rRr/r;�create_from_py_utility_coder~r�r3r�rVr�r�r4rr9Z
cf_maybe_null)r~rAr4r�Zassr&r&r'r��sz(DefNodeWrapper.prepare_argument_coercioncCs:t|j�}|dks2|dkr6|jdjs2|jdjr6dSdS)Nrr)rHrRrrO)r~Zargcountr&r&r'�signature_has_nongeneric_args�s
z,DefNodeWrapper.signature_has_nongeneric_argscCs|jjS)N)ryr�)r~r&r&r'�signature_has_generic_args�sz)DefNodeWrapper.signature_has_generic_argscCs�g}|jjr|�tj�xR|jD]H}|jrZ|jjsZ|jj	sZ|jj
sZ|�|j�|jj
��q |�|jj
�q W|jr�|�|jjj
�|jr�|�|jjj
�d�|�}|jjs�|�dtj�|�d|jjj|f�dS)Nz, z%s = z%s(%s);)ryr�rr	r�rRr~r/r�r�r^rEr�rr2r3r�rr�r�r�rOrLr�)r~rUrRr4r&r&r'r��s$
z%DefNodeWrapper.generate_function_bodycCs|jj}|�|j�|�d�|�d�|j��}|r@|�|�|�|�|��|_|j�	||�ph|jj
}|�||�|�||�|�
�}|jjr�d}nd}|jjs�|�d|j�tj�|f�|��|�d|j�|�||�|�|�|�|�|�|j�|�|j�|�d�|�d�|j|jk�r�|�|j�|� |j�x$|j�!�D]\}}	|�"||	��qVW|�#�}
|
dk	�r�|�dtj|
f�|� |j�x,|j$D]"}|j%�r�|j&j�r�|�'|��q�W|�(�|jj�s�|�dtj�|�d	�|�)�|�r|�d
|�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)*/)*rLr�r�r%rOr�r�r�r�r�r�r�r�r�rr;r�r�r	r�r�r�r:r�r�r�rr�r�r�r�r�rrrrr�r4r/rrr)r~rArUr�rr�rZretval_initrr/rr�r&r&r'r��sb













z,DefNodeWrapper.generate_function_definitionsrc
Cs2g}|j}|js|jr8dtj}|js.d|}|�|�xF|jD]<}|js@|jsV|j	rh|�d|j
�q@|�|j�|j
��q@W|j
j}|js�|��tjgkr�|�d�|jjr�|jdkr�|�d�|jr�|�dtjtjf�d�|�}d}	|jdk�r|jj�rd}	d	}|j�|j�}
d
|	|
|f}|�d|�|�r^|j
j�rZ|j
jj||dd
�dStj �r�|j!�r�|j
j�s�|jj"�s�|j�r�|j#�r�|j!}|j$�r�|�%�}|�d|j&|�'�f�|j�r�|�d�|�d|j#�|�d�|�s�|j
j�r |�(d|j)�|j*|j
jdd	d�|�d|�dS)NzPyObject *%szCYTHON_UNUSED zCYTHON_UNUSED PyObject *unused�__ipow__zPyObject *%s, PyObject *%sz, r�)r�r�Fzstatic %s%s(%s)z
%s; /*proto*/T)r�zstatic char %s[] = %s;z#if CYTHON_COMPILING_IN_CPYTHONzstruct wrapperbase %s;z#endifzstatic PyMethodDef %s = r�)Z
allow_skipz%s {)+ryr�r�r	r�rrRrPrrOr�r~r�rLr�rZmethod_flagsrZ
method_noargsr+rr:r��
args_cname�
kwds_cnamer�rrYrOr�r�rr�r-Zis_property_scoper�Z
is_unicodeZas_utf8_stringr�Zas_c_string_literalr�r�Zput_pymethoddef)
r~rUr�r�r�r�r!r4r�Zmfr�r^Zdocstrr&r&r'r�2
sr










z'DefNodeWrapper.generate_function_headercCs^x8|jD].}|jr|jr*|�d|j�q|�|j�qWx|jD]}|jrB|�|�qBWdS)NzPyObject *%s = 0;)	rRrPrrOr�r�r�r�r4)r~rArUr4r�r&r&r'r�}
sz-DefNodeWrapper.generate_argument_declarationsc	Cs`|��}|j}|�d�}|jdk}|jdk	p8|jdk	p8|}x"|jD]}|jjsB|j�	|�sBqBW|�
�s�|rvt|jd�|�
|�n$|��s�|�|�n|�|j||�||_|�|��rF|�|�s�|�|�|�|�|�r|�|j|�|j�r|jjj�r|�|jj�n|�|jj�|�|jjj�|��|�d|���|�|��r\|�|�dS)NZargument_unpacking_donerz.This method cannot have * or keyword argumentsz
return %s;)�new_error_labelr�r�rkr2r3rRr/r;r�r�rr%rcr��generate_stararg_copy_code�'generate_tuple_and_keyword_parsing_code�
label_usedr�r�generate_arg_decrefr�r��put_var_xdecref_clear�put_var_decref_clearrrLrrrOr)	r~rArU�old_error_label�our_error_label�	end_labelZhas_kwonly_argsZhas_star_or_kw_argsr4r&r&r'r��
sB





z-DefNodeWrapper.generate_argument_parsing_codecCs|r|�|j�dS)N)r�r�)r~r4rUr&r&r'�generate_arg_xdecref�
sz#DefNodeWrapper.generate_arg_xdecrefcCs|r|�|j�dS)N)r�r�)r~r4rUr&r&r'r��
sz"DefNodeWrapper.generate_arg_decrefc	Csj|jsP|j�t�dd��|�dtj�|�d|j	tj|�
�f�|�d�|jr~|jsf|jjj
srdtj}q�dtj}ndtjtjf}|j�t�d	d��|�d
|tj|j	t|j�|�
�f�|j�r�|jjj
�r�tdd�|jjjD���rr|�d
|�|�d|jjjtj|jjj|�
�f�|�|jjj�|�d�|�d|jjjf�|�d�d|jj_nT|�d|jjjtjtjf�|�d|jjj|�
�f�d|jj_|�|jjj�|j�r,|jj�s,|�d|jjjtj|jjjf�|j�rH|jjj
�rH|�d�|�|jjjt�|�d|�
��|�d�n|�d|�
��|�|jjj�|�tjt�|�tj�|�d|jjjtjf�|jjtj dd�}|�d||tj|f�|�dtj|f�|�dt�|�d�|�d|jjj|f�|�d�|j�!|�d|jj_n:|j�rf|�tjt�|�d|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)rTZ
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);F)r�z-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;)"r2r�rgrr�rOr	r�r�r:rr3r�rVr��boolr�Z
cf_referencesrr�r�r�rLrt�put_xdecref_clearrr�r�rIr�r�r
�c_py_ssize_t_typer�)r~rUZkwarg_checkr�r&r&r'r��
s�








z)DefNodeWrapper.generate_stararg_copy_codec	s���d�}g}g}g}xj|D]b}|jr|jrV|js~|js~|jrJ|�|�q~|�|�q|jrh|�|�q|js|js|�|�qW||}	|j|j}
t	|�dkr�|djs�|djr�|
d8}
t	|�}|j
o�|
|k}t|	�}
|js�|j
r�|�
|����d�t|�t|	�}��dtjd��fdd�|D��f�|�|����d	|jdk�r\d
�p^dtjf�|�|
|||
||��|j�r�|
dk�s�|
|k�r�|
|k�r�|j
�s�d}nd
}��dtj||
f���|�|j�r�||
k�r|j
�s��dtj|f���|���d�xft|	�D]Z\}}|j�s$��|j�}�j�t�dd����d|j|f����� |j!��P�q$W�nn��d�|
|k�r�x,t|�D] \}}��d|tj|f��q�W�n(��dtj�|j
�r��d�t"t|��ddd�}xd|D]\\}}||
dk�rN||ddk�r<��d���d|d���d|tj|f��q
W|
dk�r���d���d���d�|j
�r�|
�r�x(t#|
ddd�D]}��d|��q�W��|�n��d���|���d���d�x(t|�D]\}}|�$|d|���qW��d���%|��r���|���&|��j�t�d d����d!|j||
|tjf����� |j!��dS)"NZargtuple_errorrrr�z static PyObject **%s[] = {%s,0};�,csg|]}d��|j��qS)z&%s)�intern_identifierr:)r�r4)rUr&r'r�9szJDefNodeWrapper.generate_tuple_and_keyword_parsing_code.<locals>.<listcomp>z
if (%s(%s)) {�likelyr
z!=�<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:r�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�rPr�rrOrQrrmrlrHr2r�r3�generate_stararg_init_coderO�tupler	�pykwdlist_cnamer��#generate_argument_values_setup_coder��generate_keyword_unpacking_coder�r�rr�r:r�rgrr�r�r�r%r�rb�generate_arg_assignmentr�r)r~rRZ
success_labelrU�argtuple_error_labelrmZrequired_kw_only_argsZoptional_kw_only_argsr4Zkw_only_args�min_positional_args�max_positional_args�has_fixed_positional_count�has_kw_only_args�all_args�comparer#�pystring_cnameZ
reversed_argsr&)rUr'r�s�

 








 












z6DefNodeWrapper.generate_tuple_and_keyword_parsing_codecCs�|jjr<|jr t�|jtj|�}|j}|�d|j|f�n�|jj	r�|j
rX|�d|�|�|j�||jj|j|��|j
r�|�d�|�d|jj|�
|�f�|jjr�|j|jjdd�|�d�nt|jd|j�dS)Nz%s = %s;z	if (%s) {z} else {T)r�r�z2Cannot convert Python object argument to type '%s')r/r;rPr
Ztypecastrr�rOr�from_py_functionr��from_py_call_coder%rFr�r�r)r~r4r�rUr�r&r&r'r��s(


z&DefNodeWrapper.generate_arg_assignmentcCs*|jrDd|jj_|�d|jjj|jjj|��f�|�|jjj�|j�r&d|jj_|�dtj	|f�|�d|jjjtj	|tj	f�|�d|jjj�|jr�|�
|jjjt�|��|�d|���|�d�|�|jjj�|�d�|�
d	|jjjtjf�|�tjt�|�d�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; )r3r�r�rOrrr�r2r	r�r�rrr�r�r�)r~r�rUr&r&r'r��s4




z)DefNodeWrapper.generate_stararg_init_codecCs�t|�}|�d|d�d|�f�|jjrN|�d|jjtj|jjtjf�xFt|�D]:\}}|j	rX|j
jrX|�|�}|�d||j
�
|�f�qXWdS)NzPyObject* values[%d] = {%s};r�rez+%s *%s = __Pyx_CyFunction_Defaults(%s, %s);zvalues[%d] = %s;)rHrOr�rLr�r	Zdynamic_args_cnamer�rr�r/r;rFr)r~rRrUZmax_argsr#r4rCr&r&r'r��s

z2DefNodeWrapper.generate_argument_values_setup_codec
Cs�|�d�|�dtj�|�d�|jr4|�d�xJt|ddd�D]6}|�d|d�|�d|tj|f�|�d	�qFW|�d
�|js�|�d�|�|�|�d�|�d
tj�|js�|dk�r�d}	xt	|�D]\}}
|
j
s�|}	q�W|	|k�r|d}	|dk�r|�d��x�t	|d|	d��D�]�\}}
|dk�r�||k�r�|dk�rb|�d	�|j�r�||k�r�|�d�n|�d|�|�|
j�}|
j
�r�|
j
�r��q0|�d�|�dtj|f�|�d|�|�d�n�|�d|tj|f�||k�r||dk�r&|�d�|�|�nT|�d�|j�t�dd��|�d|j||||f�|�|�|j��|�d�nV|
j
�r0|�d�|j�t�dd��|�d|j|f�|�|�|j��|�d��q0W|dk�r�|�d�|�r�|�||�|�d�|dk�rd}n$|j�r8|�d||f�d}nd}|j�t�d d��|�d!tjtj|j�rr|jjj�ptd||j|�|j�f�|�d�dS)"NzPy_ssize_t kw_args;z1const Py_ssize_t pos_args = PyTuple_GET_SIZE(%s);zswitch (pos_args) {zdefault:rr�z
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)) {rezAconst 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)) %s)rOr	r�r2rbr�r�r�rmrr�r�r:rQr�rgrr�r�r%�,generate_optional_kwonly_args_unpacking_coder�r3r�r)
r~r�r�r�r�r�r�rUr#Zlast_required_argr4r�Z
pos_arg_countr&r&r'r��s�









"











z.DefNodeWrapper.generate_keyword_unpacking_codecCs�g}d}x6t|�D]*\}}|jr|js(q|s0|}|�|j�qW|r�t|�dkr�|�d|jrbdpddt|�f�|�d�|�d||t|�f�n|�d�|�d	|�|�d
tj	tj
f�|�d�t|�dkr�|�d�|�d�dS)
Nr�rz'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�)rrQr�rr:rHrOr3r	r�r�)r~r�rUZ
optional_argsZfirst_optional_argr#r4r&r&r'r�hs0



z;DefNodeWrapper.generate_optional_kwonly_args_unpacking_codecCs&x |jD]}|jr|�||�qWdS)N)rRr�generate_arg_conversion)r~rUr4r&r&r'rc�sz0DefNodeWrapper.generate_argument_conversion_codecCs�|j}|j}|jrR|jr*|�d|j�n|�d|j�|�||�|�d�nL|jrf|�||�n8|�|�r�|�d|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�rOr��%generate_arg_conversion_from_pyobject�#generate_arg_conversion_to_pyobjectrr�rrr%)r~r4rU�old_type�new_typer&r&r'r��s
z&DefNodeWrapper.generate_arg_conversioncCs@|j}|jr,|�|�|j|jj|j|��nt|jd|�dS)Nz2Cannot convert Python object argument to type '%s')	r/r�rOr�r�r�rr%r)r~r4rUr�r&r&r'r��s
z4DefNodeWrapper.generate_arg_conversion_from_pyobjectc
Cs\|j}|j}|rH|�d|jj||j|�|jj|j�f�|�|j�nt	|jd|�dS)Nz%s = %s(%s); %sz5Cannot convert argument of type '%s' to Python object)
r~Zto_py_functionrOr�rr�r�r%Zput_var_gotrefr)r~r4rUr�rZr&r&r'r��sz2DefNodeWrapper.generate_arg_conversion_to_pyobjectcCsRxL|jD]B}|jr |�||�q|js|jjs>|jjs>|jjr|�||�qWdS)N)	rRrdr"rr/r;r�r�r$)r~rUr4r&r&r'r��sz+DefNodeWrapper.generate_argument_type_testscCs|jjS)N)ryr)r~r&r&r'r�szDefNodeWrapper.error_value)r)rKrkrlZdefnoderLr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rcr�r�r�r�rr&r&r&r'rz�s8A
K.Uy

rzcs`eZdZdZdZdZdZdZdZe	j
dgZ
�fdd�Z�fdd�Zd	d
�Z
�fdd�Z�ZS)
�GeneratorDefNodeTF�	Generator�gbodycs*t|gdd�|d<tt|�j|f|�dS)NT)r�r�r�)r�rdr�r�)r~r%rs)rJr&r'r��szGeneratorDefNode.__init__cs*tt|��|�|j|j_|j�|�dS)N)rdr�r�r�r�)r~rA)rJr&r'r��s
z%GeneratorDefNode.analyse_declarationscCs�|jjj}|�|j�}|�|j�}|�|j�}|�d�|�d|j||j	rV|j	�
|�ndtj||||�
d|j�f�|�tjt�|jr�d}|�d|tjf�|�|t�|�|�|��|�d�|�d�dS)	Nr�zk__pyx_CoroutineObject *gen = __Pyx_%s_New((__pyx_coroutine_body_t) %s, %s, (PyObject *) %s, %s, %s, %s); %srf�genz
gen->classobjz&%s = __Pyx_CyFunction_GetClassObj(%s);zreturn (PyObject *) gen;r�)r�r�rYr�r:�qualname�module_namerO�
gen_type_namer�r�r	r�r�r%r
rr�r�r�rIr)r~rArUZ
body_cnamer:r�r�Zclassobj_cnamer&r&r'r��s(




z'GeneratorDefNode.generate_function_bodycsH|�t�|jd��|jj|dd�tt|��||�|j�||�dS)NzCoroutine.cT)�proto)	rgrr�r�r�r�rdr�r�)r~rArU)rJr&r'r��sz.GeneratorDefNode.generate_function_definitions)rKrkrlr��is_coroutine�is_iterable_coroutine�is_asyncgenr�r�rEr�r�r�r�r�rmr&r&)rJr'r��sr�c@seZdZdZdZdS)�AsyncDefNode�	CoroutineTN)rKrkrlr�r�r&r&r&r'r�sr�c@seZdZdZdZdS)�IterableAsyncDefNodeZIterableCoroutineTN)rKrkrlr�r�r&r&r&r'r�	sr�c@seZdZdZdZdS)�AsyncGenNodeZAsyncGenTN)rKrkrlr�r�r&r&r&r'r�sr�csPeZdZdZdZdZdZd�fdd�	Zdd�Zdd	�Z	dd
d�Z
dd
�Z�ZS)�GeneratorBodyDefNodeTFNc
s$tt|�j||||dgddd�dS)N)r%r�r:�is_async_gen_bodyr-rRr2r3)rdr�r�)r~r%r:r�r�)rJr&r'r�s
zGeneratorBodyDefNode.__init__cCsV|�|j�}|�d�}tj||}|jdt|j|dd�}||_t|j	�|_
||_dS)N�	generatorrT)rr�)r�r�r	Zgenbody_prefixr.rr%rYrr:rr�)r~rAr�r:rr�r&r&r'�declare_generator_body!s

z+GeneratorBodyDefNode.declare_generator_bodycCs|�|�|�|�dS)N)rur�)r~rAr&r&r'r�+s
z)GeneratorBodyDefNode.analyse_declarationscCs@d|jjtjtjtjf}|r.|�d|�n|�d|�dS)Nz]static PyObject *%s(__pyx_CoroutineObject *%s, CYTHON_UNUSED PyThreadState *%s, PyObject *%s)z%s; /* proto */z%s /* generator body */
{)r�rYr	�generator_cnameZlocal_tstate_cname�sent_value_cnamerO)r~rUr�r^r&r&r'r�/s
z-GeneratorBodyDefNode.generate_function_headerc	Cs�|j}|j�||�|�|�|��|_|�|j�|�||�|�	d�|�
|�|��}|�	dtj
�|��}|��|�|jjp�|jj�|jjd}|jjd}|s�|r�|��d|j_|jr�|j�|�nd}|�|�|j�|jjj�|��}	|�d�}
|�|
�|� |
�|�	d|�!tj"|j��|j#�r�|j$dk	�r�|j$}|t%j&k�r`d}n0|t%j'k�rrd	}n|t%j(k�r�d
}nt)d|��|�	dtj
||�!tj
|j�f�|�*tj
�|�+||�|jjjj,�r|�	d
|jj�-tj.�|jj�/dtj0�f�|�	dtj.�|�s"|�r*d|j_|�|j�|�	d�|�	d�|j#�s�|jj1�s�|j2�rx|j�3t4�5dd��|�	d|j2�r�dnd�|�6|j7��rT|jj1�s�|�8|j9�|� |j7�|j#�r�|j$dk	�r�|�:tj
t;�t<j=|�>�j?j@k�r |j�3t4�5dd��|�	dtA|j2��x$|j�B�D]\}
}|�C|
|��q,W|�D|jj�|� |j9�|j#�rv|�Etj
�n|�:tj
t;�|�	dtj0�|�	dtj0�|�	dtj0�|�s�|�r�|jFtj
|jjGd�|�H�|�	dtj
�|�	d�|�I|j�|�s|�r,|	jJ|jj|j|jjGd�|	�	dtj0�|	�	d |
�x&|jKD]\}}|	�	d!||f��qRW|	�	d"�|�s�|�r�|	jFd#|jjGd�|	�H�|	�	d$�|	�	d�|�L�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);)rz
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�rOr�r�r	r�r�r�r�r:rr�r�r�r�r�r�r�r�Zinit_closure_tempsr�r/r+Z	use_labelrr�r��
is_inlined�inlined_comprehension_typerZ	list_type�set_type�	dict_typerr�r�r�r�r�rEr�r�r�rgrr�r�r�r�r�r�rr�generator_stopr��contextZfuture_directivesr�rrrrrr�rrr�Zyield_labelsr)r~rArUr�Zclosure_init_coderr�r�r�Zresume_codeZfirst_run_label�target_typeZ	comp_initrr/r#�labelr&r&r'r�:s�

















z2GeneratorBodyDefNode.generate_function_definitions)NNNF)F)
rKrkrlr0r�r�r�r�r�r�r�r�rmr&r&)rJr'r�s

r�c@s&eZdZdgZdZdd�Zdd�ZdS)rIr�Ncs�|j�_�jjrd}nd}ddlm����jt��_	�j
�j�j	��fdd��j|d�D�d�}|jjst|jj
r�t�jt�j|d�t�jdd�gd	��_nt�j|d��_�j�|��_�S)
Nrr)r�csg|]}�j�j|jd��qS))r:)r�r%r:)r�r4)r�r~r&r'r��sz9OverrideCheckNode.analyse_expressions.<locals>.<listcomp>)rMrR)�expr)r7)r�)r�rRr�r�r�r��RawCNameExprNoder%r�	func_noderNrr��
is_returncoder��ExprStatNoderOr�rp)r~rAZ	first_argZ	call_noder&)r�r~r'rp�s"z%OverrideCheckNode.analyse_expressionscCsB|�|jjj�}|jjr$dtj}nd|jdj}|�	d�|�	dtj
�|�	d�|jjrl|�	d�n|�	d|�|jjt
dd	�}|j�|�|j�t�d
d��|�||j�}|�	d||||f�|�|�d
|}d||jjjf}|�	d||f�|j�|�|�	d�|�|tj
�|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)) {T)r�ZPyObjectGetAttrStrzObjectHandling.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_cnamerRrrOrWr�r�rr��	set_cnamer�rgrr�r�r%r�rYr�r�r�r
r�)r~rUZinterned_attr_cnamer�Zfunc_node_temp�errZis_builtin_function_or_methodZ
is_overriddenr&r&r'r��s6



z)OverrideCheckNode.generate_execution_code)rKrkrlr�r�rpr�r&r&r&r'rI�s
rIc@seZdZdS)�ClassDefNodeN)rKrkrlr&r&r&r'r	src	@speZdZddddddddd	g	Zd
Zd
ZdZd
Zd
Zddd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd
S)�PyClassDefNoder�rb�	metaclass�mkwrg�class_resultrL�
class_cellr�NFc	Cst�||�||_||_||_||_||_ddlm}	|jr`t	j
r`t|j|j�}|	j
||d�}
nd}
|}|�rd}d|_|jr�|jr�xVtt|j��ddd�D]:\}}
|
jjdkr�|jdk	r�t|
jd�|
j|_|j|=q�W||_n|jdk	s�t�n|	�|�|_|�s |j�s |j�s |j�r�|jdk�r||�rB|j�sB|j}nd}|�sb|jj�rb|jj�sbn|	j|||jd	�|_d}nd}|	j|||
|j|j|jd
�|_|	j|||j|j|
|j|j||d�	|_ n(|	j!|gd�|_|	j"||||j|
d
�|_ |	j#||d�|_$|	�%|j�|_&dS)Nr)r�)r7FTr�rz2keyword argument 'metaclass' passed multiple times)rrg)r:r-rrgr)r:rgrbr-rrZcalculate_metaclass�allow_py2_metaclass)r6)r:rgrbr-)r:)'r�r�r:r-r�r�rgr�r�rr�r.r%Z
StringNode�is_py3_style_classr5r6r�rr�r7rrr�AssertionError�	ProxyNodeZis_sequence_constructorrRZPyClassMetaclassNodeZPyClassNamespaceNoderbZPy3ClassNode�classobj�DictNodeZ	ClassNoder�rLZClassCellInjectorNoder)r~r%r:rgr-r�r�rnZforce_py3_semanticsr�Zdoc_noderr#r�ZmkdictZneeds_metaclass_calculationr&r&r'r�%sl"



zPyClassDefNode.__init__cCs\|jrt|jjd�dSddlm}t|jdd|j|jjpH|j	|jgd�|j
|jd|jd�	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�rT)rRF)r�r��
class_namergr�r�r�r-)
rrrr%r�r��
CClassDefNoder:rgr�r�r�r-)r~r�r&r&r'�	as_cclassoszPyClassDefNode.as_cclasscCs4|}x|js|jr|j}qWt|j|d�}|_|S)N)r:r�)r�rr�rr:r+)r~rAr�rr&r&r'�create_scope�s

zPyClassDefNode.create_scopecCs�|j}|jrLddlm}x,|jddd�D]}||j|j|gd�}q*Wd|_||_|j�|�|j�	|�|�
|�}|j|_|jjj
|_|j�|�dS)Nr)rNr�)rMrR)rr�r�rNr%rBrr�rL�analyse_target_declarationrr�r�r�class_obj_cnamer�)r~rArrNrBrr&r&r'r��s 
z#PyClassDefNode.analyse_declarationscCs�|jr|j�|�|_|jr(|j�|�|_|jr<|j�|�|_|j�|�|_|j�|�|_|j}|j�|�|_|j�	||j
�|j�|�|_|S)N)rgrprrrbrr+r�rL�analyse_target_expressionrr)r~rArr&r&r'rp�sz"PyClassDefNode.analyse_expressionscCs"|�|j|�|j�|j|�dS)N)r�r+r�r�)r~rArUr&r&r'r��sz,PyClassDefNode.generate_function_definitionscCs^|�|j�|j�|�|j}|jr0|j�|�|jrB|j�|�|jrT|j�|�|j	�|�|j	�
�|_|_|j
�|�|j�|�|j�|�|j
�||j�
��|j
�|�|j�
�|_|_|j�|j|�|j	�|�|j	�|�|j�r|j�|�|j�|�|j�r0|j�|�|j�|�|j�rP|j�|�|j�|�|j��dS)N)r�r%Z
pyclass_stackrr+rgrDrrrbr��namespace_cnamerrr�r�rZgenerate_injection_code�generate_disposal_coderrLrNrK�pop)r~rUrr&r&r'r��s>z&PyClassDefNode.generate_execution_code)NNF)rKrkrlr�r�rrrrr�rrr�rpr�r�r&r&r&r'r
s

Irc@sxeZdZdgZdZdZdZdZdZdZ	dZ
dZdd�Zdd�Z
dd	�Zd
d�Zdd
�Zdd�Zeddd��Zdd�ZdS)rr�NFcCsDt|d�s>ddlm}|jr8|j|j|g|jdd�|_nd|_|jS)N�_buffer_defaultsr)rpF)Z
need_complete)r?r�rp�buffer_defaults_noderq�buffer_defaults_posr)r~rArpr&r&r'rr�s
zCClassDefNode.buffer_defaultscCsz|jr4|jdkr4|j�d�}|�||j�}|s8dSn|}|j|j|jdd|jd|j|j|j|j	|j
|�|�|jd�
|_
dS)Nr�.r)
r:r%r8�implementingr�rC�objstruct_cname�
typeobj_cnamer�r,rrr�shadow)r�r��splitr[r%�declare_c_classr
�objstruct_name�typeobj_namer,rrrrr�)r~rArY�
home_scoper&r&r'r��s(zCClassDefNode.declarecs�jr�jst�jd��jr0t�jd�j�d�_�jr�d�_x �j	D]}|j
�jkrJ|�_qJW�jdkr�t�jd�j��_d�j_
���j��jj�r�jjd}|���}|tjtjtjfkr؈�|���j}|dkr�t|jd�j�n�|tjk�rd}n�|j�s0|j�r|j�s0t|jd|�n�|���sTt|jd|j
�jf�n`|j�r�|jj�r�|j �r�t|jd|�jf�n.|j�r�|j
d	k�r�t|jd
|j
�n|�_�j�!dd�dk�r�|tjk�r�t"�jdd��j#dk	}|�r �j�r �jj�s �jj$�%��fd
d��dS�j�rZ�j&dk�rZ�j�'d�}��(|�j�}|�s^dSn�}�j&dk�r��jdk�r��jt)j*k�r��j+dd�dk�r�t"�jd�jd�|j,�j�j|�oˆj-|�oΈj-�j�j�j�j.�j&�j/�j0��1���j2d�
�_3�j2�r�j3|��j�_4|�k	�r@�j&dk�r@��5�j�j3�j��j3jj�_}	|	dk	�rb�j|	_�j6�r�t7j8�r�t9�j�j6�|	_6|�r��j#�:|	��j�;d�}
|
�r�|
j<�r�|	j=�s�|	j>�s�j�?d�|
_@�j�Ad|
j6|
j��j-�r�d|	_=nd|	_>tB�jj�dk�r�|�r�j-�r*t�jjdjd�x2�jjdd�D]}|����r<t|jd��q<Wd�j3j_CddlDmE}|jF�j|jG�j�jd��j|jH�jgd�gd��_In8�j�r҈jjJ�pˆjjC�j3j_Cd�_Ind�j3j_Cd�_I��K�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 final)r��str�byteszGinheritance from PyVarObject types like '%s' is not currently supported�freelistzIfreelists cannot be used on subtypes, only the base class can manage themcs
����S)N)r�r&)rAr~r&r'�<lambda>I�z4CClassDefNode.analyse_declarations.<locals>.<lambda>rrZ__builtin__�zcpython.z %s already a builtin Cython type)
r:r%r8rr�rCrrr�r,rrrrr|Z__dict__getterz'Only declare first base in declaration.z+Only one extension type base class allowed.)r�)r7)r6)rR)Lr�r!rr%r�r
rCr��moduleZcimported_modulesr:rr�Zhas_extern_class�add_imported_modulergrRr8r
r=r<r>rZZ
sign_and_namer/rr�r rr�r+r�r�r-rr�Zdefered_declarationsrr�rr[rZ
builtin_typesrr r�r"r,rrrrr�rF�add_imported_entryr-rr�r.r�rjZis_variable�definedZimplementedZmangle_internalZgetter_cname�declare_propertyrH�
early_initr�r�r�ZIdentifierStringNoder�type_init_argsZis_externalZallocate_vtable_names)r~rAr*r�rCZbase_class_scopeZhas_bodyrYr#r+Z
dict_entryZ
other_baser�Zthunkr&)rAr~r'r�s�




 




z"CClassDefNode.analyse_declarationscCs4|jr|jjj}|j�|�|_|jr0|j�|�|S)N)r�r�r/r+rpr0)r~rAr+r&r&r'rp�s
z!CClassDefNode.analyse_expressionscCs(|jr$|�|j|�|j�|j|�dS)N)r�r�r+r�)r~rArUr&r&r'r��sz+CClassDefNode.generate_function_definitionscCsZ|�|j�|jr|j�|�|jjj�sV|j�rF|j�|�d|j�	�}d|}|j
�tj
d�}|�d||j�	�f�|�|�||j��|�|�|�d||f�|�d�|�d||f�|�|�|j��|�d�|j
�|�|�|tj
�|�|�|�d	|jjj|f�|�|tj
�|j�|�|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/r0rDr�r�r�r
rrOr�r�r�r�r�rIrr�rrK�generate_type_ready_code)r~rUrgZ
first_baseZ
trial_typer&r&r'r��s8



z%CClassDefNode.generate_execution_codec
Cs�|j}|j}|j}|sdS|jdk�r�xtjD]}|�||�q.W|r`|j�t	�
dd��d}nd}|�d|||�|j
�f�|�d|�t�|d�}t�|d�}	|d	k�r|	d	k�r|jr�d
}
d}nd}
d
}|j�t	�
|d��|�d||f�|�d||
f�|�d��x|jjjD�]}|jdk}
|j�r"tj�r"|j�r"|
�s"tj�|j�}|�rl|��nd}|�r�|�|�|�d�|�d�|�d||j|�d|j
�f�|�d�|�d|j�|�d|j|jf�|�d|j�|�d�|�d�|�d�|�r"|�d��q"W|j�r�|j�t	�
dd��|�d||j|�|j
�f�|�r�|j�t	�
dd��|�d ||�|j
�f�|jj�s�|jjd!�s�|�d"tj |j!||�|j
�f�|j"�s�|�#d#�nd}|�r@|jt$k�r4d$|}|j%�r|j&}n
d%|j&}|�d&||||j'f�nt(|j
d'�|j"�sV|�#d(��r�nd�r�|j�t	�
d)d��|�d*||�|j
�f�|j�r�|�d+|j)|jf�dS),NrZPyType_ReadyzExtensionTypes.cZ__Pyx_PyType_Readyzif (%s(&%s) < 0) %sz%s.tp_print = 0;Ztp_getattroZ
tp_dictoffsetreZ#__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/rr+r�rZ
slot_tableZgenerate_dynamic_init_coder�rgrr�rOr�r%Zget_slot_code_by_namer�Zpyfunc_entriesr:rrr�r�r,r-r.r�r�Zvtable_cnameZ
vtabptr_cnameZis_internalr�r	r�r
Zis_closure_class_scoperjrr,rrrr�)r�rUZheap_type_basesr/rr+r/Z	readyfuncZgetattr_slot_funcZdictoffset_slot_funcZpy_cfuncZutility_funcrZr�rZ
weakref_entryZtp_weaklistoffsetZ	objstructr&r&r'r1�s�










z&CClassDefNode.generate_type_ready_codecCs(|jr|j�|�|jr$|j�|�dS)N)r0r�r�)r~rUr&r&r'r�PszCClassDefNode.annotate)F)rKrkrlr�rrr,rr!r"r�rrrr�r�rpr�r�r�r1r�r&r&r&r'r�s(	#rc@s:eZdZdgZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�PropertyNoder�cCs6|�|j|j|j�|_|j|jj_|j�|jj�dS)N)	r.r:r-r%r�r�r+r�r�)r~rAr&r&r'r�asz!PropertyNode.analyse_declarationscCs|j�|�|_|S)N)r�rp)r~rAr&r&r'rpfsz PropertyNode.analyse_expressionscCs|j�||�dS)N)r�r�)r~rArUr&r&r'r�jsz*PropertyNode.generate_function_definitionscCsdS)Nr&)r~rUr&r&r'r�msz$PropertyNode.generate_execution_codecCs|j�|�dS)N)r�r�)r~rUr&r&r'r�pszPropertyNode.annotateN)	rKrkrlr�r�rpr�r�r�r&r&r&r'r5Wsr5c@s(eZdZgZdd�Zdd�Zdd�ZdS)�
GlobalNodecCs"x|jD]}|�||j�qWdS)N)�namesZdeclare_globalr%)r~rAr:r&r&r'r�{szGlobalNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rpszGlobalNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz"GlobalNode.generate_execution_codeN)rKrkrlr�r�rpr�r&r&r&r'r6tsr6c@s(eZdZgZdd�Zdd�Zdd�ZdS)�NonlocalNodecCs"x|jD]}|�||j�qWdS)N)r7Zdeclare_nonlocalr%)r~rAr:r&r&r'r��sz!NonlocalNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rp�sz NonlocalNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz$NonlocalNode.generate_execution_codeN)rKrkrlr�r�rpr�r&r&r&r'r8�sr8c@sFeZdZdgZdd�Zdd�Zdd�ZdZd	d
�Zdd�Z	d
d�Z
dS)r�r�c
Cs�ddlm}|j}t||j�r�|j��}|dkr�|��\}}t|�rPt	|j
d�xH|jD]>\}}|�|�}	|	dkr�t	|j
d�qX|j
|j|	|j
dd�qXWt|_n:t|dd�dk	r�|jr�|�|�t|_n|js�|jr�t|_dS)	Nr)r�r�z!Variable names must be specified.zUnknown typeT)r�r@)r�r�r�rIZGeneralCallNoderM�as_cython_attribute�explicit_args_kwdsrHrr%r6r8r.r7�PassStatNoderJr�r9Zdeclare_from_annotation�is_attribute�is_subscript)
r~rAr�r�rZrRrS�varr%r/r&r&r'r��s(


z!ExprStatNode.analyse_declarationscCs"d|j_|j�|�|_d|j_|S)NF)r��result_is_usedrp)r~rAr&r&r'rp�sz ExprStatNode.analyse_expressionscCs|jjjr|jjr|��dS)N)r�r/r;�is_tempr�)r~rAr&r&r'r��szExprStatNode.nogil_checkzDiscarding owned Python objectcCsx|�|j�d|j_|j�|�|jjs\|j��r\|j��}|jjjsNd|}|�	d|�|j�
|�|j�|�dS)NFz
(void)(%s)z%s;)r�r%r�r?rDr@r�r/r�rOrrK)r~rUr�r&r&r'r��s

z$ExprStatNode.generate_execution_codecCs|j�||�dS)N)r�r�)r~rArUr&r&r'r��sz*ExprStatNode.generate_function_definitionscCs|j�|�dS)N)r�r�)r~rUr&r&r'r��szExprStatNode.annotateN)rKrkrlr�r�rpr�r�r�r�r�r&r&r&r'r��sr�c@seZdZdd�Zdd�ZdS)�AssignmentNodecCsB|�|�}t|t�r>t|t�s>|jjjr>|j��r>t|j	d�|S)Nz9Storing unsafe C derivative of temporary Python reference)
rorIrA�ParallelAssignmentNode�rhsr/r�Zis_ephemeralrr%)r~rArTr&r&r'rp�s

z"AssignmentNode.analyse_expressionscCs$|�|j�|�|�|�|�dS)N)r�r%�generate_rhs_evaluation_coderN)r~rUr&r&r'r��s
z&AssignmentNode.generate_execution_codeN)rKrkrlrpr�r&r&r&r'rA�s	rAc@steZdZddgZdZdZdZdd�Zddd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
ddd�Zdd�Zdd�ZdS)�SingleAssignmentNoderrCFcCsddlm}t|j|j��r|jj��}|�r|j��\}}|dk�r�t|�dkrft	|dj
d�dS|dk	r�|�d�}|dks�d|kr�t	|j
d�dS|d}nd	}|d
�|�}|dkr�t	|d
j
d�dS|j
}	|dk�rvt|	|j�r�|	j|	j
fg}
n0t|	|j��rd
d�|	jD�}
nt	|	j
d�dSx&|
D]\}}|j|||d|d��q2Wt|�dk�rn|d|_nd|_n6d|_t|	|j��s�t	|	j
d�|j|	j||j
d	d��nR|dk�r�d|_t|�d
k�s�|dk�r�t	|jj
d�dSg}
xJ|jD]@\}}|�|�}|dk�r t	|j
d�n|
�|j||j
f��q�Wt|
�t|j�k�rRdSt|j
|j��spt	|j
j
d�|j
j}t|�}|�|||d|jj
�xj|
D]\}}}|�|||��q�WnF|dk�rd|_|�r�t	|jjj
d�t|jj
|j
j|d�}|�|�|j�rdS|j
�|�dS)Nr)r�)r��typedefr"zInvalid positional argument.rFr�zInvalid keyword argument.rTrzUnknown typer�cSsg|]}|j|jf�qSr&)r:r%)r�r>r&r&r'r� sz=SingleAssignmentNode.analyse_declarations.<locals>.<listcomp>zInvalid declarationT)r�r�zInvalid declaration.)r�)r)�unionz.Struct or union members must be given by name.F�
fused_typez*fused_type does not take keyword arguments)r:r`)r�r�rIrCZCallNoderMr9r:rHrr%�compile_time_valuer8rr�r:r�rRr.�declaration_onlyr{r6rr7rr0rzr�r)r~rAr��	func_namerRrSZkwdictr�r/r�varsr>r%�members�memberr%r:r+Z	fusednoder&r&r'r�s�









z)SingleAssignmentNode.analyse_declarationsrcCs�ddlm}|j�|�|_|�|�}|r,|S|j�|�|_|j�|�|�|�}|rX|St	|j|j
�r�|j�|j�|j�|j�|_n@|jj
jr�t	|j|j�s�|j|jj|jddd�|_|j�|�|_|jj
j�rT|�|jd|jj
|jj
g�}|�r@|j}d|_|j
j|_|j
j|_|jdk�rR|jdk�rR|�t�dd��n|j�|jj
|�}n|j�|jj
|�}|�s�|j�s�|j�s�|j�s�|j
j�r�|�|�}n|j
j�r�|�|�}||_|S)	Nr)r�)r�rW�stop�=TrZCppExceptionConversionzCppSupport.cpp) r�r�rCro�
unroll_rhsrrq�gil_assignment_check�
unroll_lhsrIZMemoryViewIndexNodeZanalyse_broadcast_operationZ$analyse_as_memview_scalar_assignmentr/r��SliceIndexNoder%r�Zlookup_operator_for_types�is_overloaded_assignmentrr�rgrr�rr<r9r�r;�coerce_to_temp�coerce_to_simple)r~rA�use_tempr�Zunrolled_assignment�oprCr&r&r'roWsH







z"SingleAssignmentNode.analyse_typesc	Cs6ddlm}m}|}d}}}	}
|jjr8|jj}�nj|jjsJ|jj�r�x&t||j	�rp|j
sp|jsp|j}}qLWt||j	��rt|j}|j
}|r�|�
tj|�}|j}|r�|�
tj|�}nT|jjr�|jjr�|j|jt|jj�t|jjt�r�|jjn|jd�}nt|jd�dSd}	|	�r$|	�
tj|�}	dd�}y"||d�||d�||	d�}Wn"tk
�rpt|jd�dSXn(|jj�r�|jj}t|t��s�dSndSndS||k�r�t|jd	||f�dSg}
|�|�}|g}|�r�|j�s�|�|�}|�|�|�r|j�s|�|�}|�|�|	�r>|	j�s>|�|	�}	|�|	�x�t|�D]�}|j|jt|�|tjd
�}|	dk	�r�|	���r�|j|	j}|j|jt|�|d�}n|j|jd|	|d�}|dk	�r|���r�|���r�|j|j}|j|jt|�|d�}n|j|jd
||d�}|
�|j|j||� |�d���qHW|
||
fS)Nr)r��	UtilNodes)r7r�z*C array iteration requires known end indexcSs&|dkr|S|��r|jStd��dS)NzNot a constant.)�has_constant_resultr�r�)rTZ
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 %s)r7r�r/r�)�operatorZoperand1Zoperand2r)r�r�)!r�r�rZr/�	is_ctupler�r�r�rIrTrWrOr�rr
r�ZIntNoder%r$rZconstant_value_not_setrr�Z
LetRefNoder�rrbr[r�ZMulNodeZAddNodeZ	IndexNodero)r~rTZtarget_sizerAr�rZr�Z
start_nodeZ	stop_nodeZ	step_node�
check_nodeZ
slice_sizer\rc�refsr�Zix_nodeZ
step_valueZindex_valuer&r&r'�unroll�s�"











$zSingleAssignmentNode.unrollc	Cs�ddlm}g}x0t||�D]"\}}	|�t|j||	|jd��qWt|j|d��|�}
|rlt	|j||
gd�}
x"|ddd�D]}|�
||
�}
q|W|
S)Nr)rZ)rrC�first)r%r�r�)r�rZr;rrEr%rbrBrpr�ZLetNode)r~r`r_�lhs_listZrhs_listrArZ�assignmentsrrCrTr�r&r&r'�unroll_assignments�sz'SingleAssignmentNode.unroll_assignmentscCsxddlm}t|j|j�sdStdd�|jjD��r8dS|�|jt	|jj�|�}|sXdS|\}}}|�
|||jj||�S)Nr)r�css|]}|jVqdS)N)Z
is_starred)r�r4r&r&r'r��sz2SingleAssignmentNode.unroll_rhs.<locals>.<genexpr>)r�r�rIrr��anyrRrarCrHre)r~rAr��unrolledr_r`rCr&r&r'rQ�s
zSingleAssignmentNode.unroll_rhscCsl|jjjrdSddlm}t|j|j�s,dS|�|jt	|jj
�|�}|sLdS|\}}}|�||||jj
|�S)Nr)r�)rr/r^r�r�rIrCr�rarHrRre)r~rAr�rgr_r`rr&r&r'rS�s

zSingleAssignmentNode.unroll_lhscCs|j�|�dS)N)rCrD)r~rUr&r&r'rDsz1SingleAssignmentNode.generate_rhs_evaluation_codecCs:|jr&|jj|j||j|j|jd�n|j�|j|�dS)N)rMrr�)rUrrNrCrr�)r~rUrMr&r&r'rNsz-SingleAssignmentNode.generate_assignment_codecCs|j�||�dS)N)rCr�)r~rArUr&r&r'r�sz2SingleAssignmentNode.generate_function_definitionscCs|j�|�|j�|�dS)N)rr�rC)r~rUr&r&r'r�szSingleAssignmentNode.annotateN)r)F)rKrkrlr�rbrUrJr�rorarerQrSrDrNr�r�r&r&r&r'rE�sT
0^

rEc@sXeZdZddddgZdZdZdZdd�Zdd	d
�Zdd�Z	ddd�Z
dd�Zdd�ZdS)�CascadedAssignmentNodercrC�coerced_values�
cloned_valuesNcCsx|jD]}|�|�qWdS)N)rcr)r~rArr&r&r'r�5sz+CascadedAssignmentNode.analyse_declarationsrcCs�ddlm}m}t�}x@t|j�D]2\}}|�|�}|j|<|�|�|�|j	�q"W|j
�|�}t|�dkr�t
t|��jr�|�d||j
g�}	|	s�|�|��|�}n|�|��|�}|js�|js�|s�|js�|j	jr�|�|�}n
|�|�}|jr�||�n||_
g|_i}
g|_x�|jD]|}|j	j�o4|�d||j
g�}|j�|�|j	|
k�r|j	|j	k�r||j
�}|�sz|�|j	|�}|j�|�||
|j	<�qWg|_x0|jD]&}|
�|j	|j
�}|j�||���q�W|S)Nr)�	CloneNoder
rP)r�rkr
ryrrcrqrRr�r/rCrorH�next�iterr�Zlookup_operatorrrr9r�r<r;rVrWr@ri�assignment_overloadsrrjr-)r~rArXrkr
Z	lhs_typesr#rrCrYriZ
overloadedr&r&r'ro9sF


z$CascadedAssignmentNode.analyse_typescCs|j�|�dS)N)rCrD)r~rUr&r&r'rDjsz3CascadedAssignmentNode.generate_rhs_evaluation_codeFcCs�x|jD]}|�|�qWx:t|j|j|j�D]$\}}}|�|�|j|||d�q.Wx"|jD]}|�|�|�|�q^W|j	�|�|j	�|�dS)N)rM)
rirDr;rcrjrnrNrrKrC)r~rUrMrCr�overload�	rhs_valuer&r&r'rNms

z/CascadedAssignmentNode.generate_assignment_codecCs|j�||�dS)N)rCr�)r~rArUr&r&r'r�|sz4CascadedAssignmentNode.generate_function_definitionscCsZx|jD]}|�|�qWx.t|j|j�D]\}}|�|�|�|�q*W|j�|�dS)N)rir�r;rcrjrC)r~rUrCrr&r&r'r�s
zCascadedAssignmentNode.annotate)r)F)
rKrkrlr�rjrirnr�rorDrNr�r�r&r&r&r'rh"s
1
rhc@s:eZdZdgZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
rBr�cCsx|jD]}|�|�qWdS)N)r�r�)r~rAr�r&r&r'r��sz+ParallelAssignmentNode.analyse_declarationscs�fdd�|jD�|_|S)Ncsg|]}|j�dd��qS)r)rX)ro)r�r�)rAr&r'r��sz>ParallelAssignmentNode.analyse_expressions.<locals>.<listcomp>)r�)r~rAr&)rAr'rp�s
z*ParallelAssignmentNode.analyse_expressionscCsD|�|j�x|jD]}|�|�qWx|jD]}|�|�q.WdS)N)r�r%r�rDrN)r~rUr�r&r&r'r��s
z.ParallelAssignmentNode.generate_execution_codecCs x|jD]}|�||�qWdS)N)r�r�)r~rArUr�r&r&r'r��sz4ParallelAssignmentNode.generate_function_definitionscCsx|jD]}|�|�qWdS)N)r�r�)r~rUr�r&r&r'r��szParallelAssignmentNode.annotateN)	rKrkrlr�r�rpr�r�r�r&r&r&r'rB�srBc@s<eZdZddgZdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�InPlaceAssignmentNoderrCcCs|j�|�dS)N)rr)r~rAr&r&r'r��sz*InPlaceAssignmentNode.analyse_declarationscCsl|j�|�|_|j�|�|_|jjs,|jjrB|j�|jj|�|_n&|jjjrh|j	dkrh|j�t
j|�|_|S)Nz+-)rCrorrq�is_memview_index�is_buffer_accessrr/�	is_stringr]r
r�)r~rAr&r&r'ro�sz#InPlaceAssignmentNode.analyse_typescCs
|�|j�|j|j}}|�|�|�|�|j}|dkrBd}n|dkrVt|jd�|jsb|j	r�|j
jrvt|jd�|dkr�|j
jr�|j
jds�t|jd�|�|||�n0|jr�t|jd	�n|�d
|��||��f�|�|�|�|�|�|�|�|�dS)Nz//�/z**zNo C inplace power operatorzAIn-place operators not allowed on object buffers in this release.)ru�%�	cdivisionz;In-place non-c divide operators not allowed on int buffers.z4Inplace operators not supported on memoryview slicesz
%s %s= %s;)r�r%rrCrDZ generate_subexpr_evaluation_coder]rrsrrr/r;r�r�r�Zgenerate_buffer_setitem_codeZis_memview_slicerOr�Zgenerate_subexpr_disposal_codeZfree_subexpr_tempsrrK)r~rUrrCZc_opr&r&r'r��s,




z-InPlaceAssignmentNode.generate_execution_codecCs|j�|�|j�|�dS)N)rr�rC)r~rUr&r&r'r��szInPlaceAssignmentNode.annotatecCs$ddlm}|�|j|j|j|j�S)Nr)r�)r�r�Z
binop_noder%r]rrC)r~r�r&r&r'�create_binop_node�sz'InPlaceAssignmentNode.create_binop_nodeN)	rKrkrlr�r�ror�r�rxr&r&r&r'rq�srqc@s>eZdZddgZdd�ZejZdZdd�Z	dd	�Z
d
d�ZdS)
�
PrintStatNode�	arg_tuplerMcCsd|jr|j�|�}|�|�|_|j�|�}|�|�|_|�t�t|jj�dkr`|jr`|�t	�|S)Nr)
rMrp�coerce_to_pyobjectrzrg�printing_utility_coderHrR�append_newline�printing_one_utility_code)r~rArMrzr&r&r'rps

z!PrintStatNode.analyse_expressionszPython print statementc	Cs�|�|j�|jr*|j�|�|j��}nd}t|jj�dkr�|jr�|jjd}|�|�|�	d||��|�
|j�f�|�|�|�|�nJ|j�|�|�	d||j��|j|�
|j�f�|j�|�|j�|�|jr�|j�|�|j�|�dS)Nrerrz"if (__Pyx_PrintOne(%s, %s) < 0) %sz#if (__Pyx_Print(%s, %s, %d) < 0) %s)
r�r%rMrD�	py_resultrHrzrRr}rOr�rrK)r~rUZ
stream_resultr4r&r&r'r�s6

z%PrintStatNode.generate_execution_codecCs&|jr|j�||�|j�||�dS)N)rMr�rz)r~rArUr&r&r'r�6sz+PrintStatNode.generate_function_definitionscCs"|jr|j�|�|j�|�dS)N)rMr�rz)r~rUr&r&r'r�;szPrintStatNode.annotateN)rKrkrlr�rpr{r�r�r�r�r�r�r&r&r&r'ry�s!ryc@s4eZdZdgZdd�ZejZdZdd�Z	dd�Z
d	S)
�ExecStatNoderRcCsFx4t|j�D]&\}}|�|�}|�|�}||j|<qW|�tj�|S)N)rrRrpr{rgrZpyexec_utility_code)r~rAr#r4r&r&r'rpHs

z ExecStatNode.analyse_expressionszPython exec statementcCs�|�|j�g}x&|jD]}|�|�|�|���qWt|ddgddt|���}|jj	t
jdd�}|�d|f|�x"|jD]}|�
|�|�|�q�W|�|�||j��|�|�|�|t�|j�|�dS)Nrer$T)r�z%s = __Pyx_PyExec3(%s, %s, %s);)r�r%rRrDrrr�rHr�r�r
rrOrrKr�r�r�r�)r~rUrRr4Ztemp_resultr&r&r'r�Ss 
 

z$ExecStatNode.generate_execution_codecCsx|jD]}|�|�qWdS)N)rRr�)r~rUr4r&r&r'r�eszExecStatNode.annotateN)rKrkrlr�rpr{r�r�r�r�r�r&r&r&r'r�Asr�c@sBeZdZdgZdZdd�Zdd�Zdd�Zd	Zd
d�Z	dd
�Z
dS)�DelStatNoderRFcCsx|jD]}|�|�qWdS)N)rRr)r~rAr4r&r&r'r�rsz DelStatNode.analyse_declarationscCs�x�t|j�D]�\}}|�|d�}|j|<|jjs@|jr\|jjr\|jr�|jjr�t	|j
d�q|jjrz|jjj
rz|�|�q|jj
r�t	|j
d�q|jr�|jjtjkr�qt	|j
d�qW|S)NzDeletion of global C variablezDeletion of non-heap C++ objectz&Deletion of non-Python, non-C++ object)rrRrr/r;r9r�r�Z
is_cglobalrr%r�rCr�r�r=r�r�bytearray_type)r~rAr#r4r&r&r'rpvszDelStatNode.analyse_expressionscCs$x|jD]}|jjr|��qWdS)N)rRr/r;r�)r~rAr4r&r&r'r��szDelStatNode.nogil_checkzDeleting Python objectcCs�|�|j�x||jD]r}|jjs<|jjs<|jrN|jjtj	krN|j
||jd�q|jjr|jj
jr|�|�|�d|���|�|�qWdS)N)�ignore_nonexistingz
delete %s;)r�r%rRr/r;r�r=r�rr�Zgenerate_deletion_coder�r�rCr�rDrOr�r)r~rUr4r&r&r'r��s
z#DelStatNode.generate_execution_codecCsx|jD]}|�|�qWdS)N)rRr�)r~rUr4r&r&r'r��szDelStatNode.annotateN)rKrkrlr�r�r�rpr�r�r�r�r&r&r&r'r�jsr�c@s eZdZgZdd�Zdd�ZdS)r;cCs|S)Nr&)r~rAr&r&r'rp�sz PassStatNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz$PassStatNode.generate_execution_codeN)rKrkrlr�rpr�r&r&r&r'r;�sr;cs eZdZdZ�fdd�Z�ZS)�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.
    cstt|�j|dj|d�dS)Nr)r�)rdr�r�r%)r~r�)rJr&r'r��szIndirectionNode.__init__)rKrkrlrxr�rmr&r&)rJr'r��sr�c@s$eZdZgZdZdd�Zdd�ZdS)�
BreakStatNodeTcCs|S)Nr&)r~rAr&r&r'rp�sz!BreakStatNode.analyse_expressionscCs0|�|j�|js t|jd�n|�|j�dS)Nzbreak statement not inside loop)r�r%�break_labelrr�)r~rUr&r&r'r��sz%BreakStatNode.generate_execution_codeN)rKrkrlr�r�rpr�r&r&r&r'r��sr�c@s$eZdZgZdZdd�Zdd�ZdS)�ContinueStatNodeTcCs|S)Nr&)r~rAr&r&r'rp�sz$ContinueStatNode.analyse_expressionscCs2|jst|jd�dS|�|j�|�|j�dS)Nz"continue statement not inside loop)�continue_labelrr%r�r�)r~rUr&r&r'r��s
z(ContinueStatNode.generate_execution_codeN)rKrkrlr�r�rpr�r&r&r&r'r��sr�c@sVeZdZdgZdZdZdZdZdd�Zdd�Z	dZ
d	d
�Zdd�Zd
d�Z
dd�ZdS)rOr7TFcCs�|j}||_|s t|jd�|S|jrv|jr8t|jd�|j�|�|_|jsR|jrbt|jjd�q�|j�|j|�|_n|js�|j	s�|js�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%r7�in_async_genror�r�rr;)r~rArr&r&r'rp�s"z"ReturnStatNode.analyse_expressionscCs|jjr|��dS)N)rr;r�)r~rAr&r&r'r��szReturnStatNode.nogil_checkzReturning Python objectcCs�|�|j�|jsdS|j}|jjrB|�tj|j�|rB|jrBd}|r�|�	|�|jj
r�ddlm}|j
tj|j|j|||jd�nd|jr�|j�t�dd��|�dtj|��f�|�|�n&|�|�|�dtj|�|j�f�|�|�|�|�nv|jj�rZ|j�rH|j�r6|j�t�dd��|�d	�|�d
tj�n|�tj|j�n|jj�rt|�||jj �x$|j!�"�D]\}}|�#||��q�W|�$|j%�dS)Nr)rc)Z	lhs_cnameZlhs_typeZlhs_posrCrUr�ZReturnWithStopIterationzCoroutine.cz-%s = NULL; __Pyx_ReturnWithStopIteration(%s);z%s = %s;r�z/PyErr_SetNone(__Pyx_PyExc_StopAsyncIteration); z
%s = NULL;)&r�r%rr7r;rr	r�r�rDr�r�rcZput_acquire_memoryviewsliceZin_nogil_context�in_generatorr�rgrr�rOrrrGrHrJrKr�r�r�r��
put_returnrCr�Ztemps_holding_referencer�r�r�)r~rUr7rcrr/r&r&r'r�sZ






z&ReturnStatNode.generate_execution_codecCs(|jr|�d�|�dtj|f�dS)Nz%#pragma omp critical(__pyx_returning)z%s = %s;)�in_parallel�putln_openmprOr	r�)r~rUr7r&r&r'r�;s
zReturnStatNode.put_returncCs|jdk	r|j�||�dS)N)r7r�)r~rArUr&r&r'r�@s
z,ReturnStatNode.generate_function_definitionscCs|jr|j�|�dS)N)r7r�)r~rUr&r&r'r�DszReturnStatNode.annotateN)rKrkrlr�r�r�r�r�rpr�r�r�r�r�r�r&r&r&r'rO�s9rOc@sFeZdZddddgZdZdd�ZejZdZ	d	d
�Z
dd�Zd
d�ZdS)�
RaiseStatNode�exc_type�	exc_value�exc_tb�causeTcCs�|jr|j�|�}|�|�|_|jr<|j�|�}|�|�|_|jrZ|j�|�}|�|�|_|jrx|j�|�}|�|�|_d|_|jr�|js�|js�|j}ddlm}t	||j
�r�|js�|jdk	r�|jjs�|j
}|jr�|jjr�|j|_|jdkr�d|_|S)Nr)r��MemoryError)r�ror{r�r�r��builtin_exc_namer�r�rIrNrRrzrMr9r��
is_builtinr:)r~rAr�r�r�r��excr�r&r&r'rpTs0
z!RaiseStatNode.analyse_expressionszRaising exceptioncCsB|�|j�|jdkr0|�d|�|j��dS|jrf|j�|�|j��}|jjrj|j	�
|jj�nd}|jr�|j�|�|j��}nd}|j
r�|j
�|�|j
��}nd}|jr�|j�|�|j��}nd}|j	�t�|�d||||f�x8|j|j|j
|jfD] }|�r|�|�|�|��qW|�|�|j��dS)Nr�zPyErr_NoMemory(); %srez__Pyx_Raise(%s, %s, %s, %s);)r�r%r�rOr�r�rDrr9r�Zuse_entry_utility_coder�r�r�r�rg�raise_utility_coderrK)r~rUZ	type_codeZ
value_codeZtb_codeZ
cause_coder%r&r&r'r�rsF



z%RaiseStatNode.generate_execution_codecCsd|jdk	r|j�||�|jdk	r0|j�||�|jdk	rH|j�||�|jdk	r`|j�||�dS)N)r�r�r�r�r�)r~rArUr&r&r'r��s



z+RaiseStatNode.generate_function_definitionscCsL|jr|j�|�|jr$|j�|�|jr6|j�|�|jrH|j�|�dS)N)r�r�r�r�r�)r~rUr&r&r'r��szRaiseStatNode.annotateN)
rKrkrlr�r�rpr{r�r�r�r�r�r�r&r&r&r'r�Is*
r�c@s.eZdZgZdZdd�ZejZdZ	dd�Z
dS)�ReraiseStatNodeTcCs|S)Nr&)r~rAr&r&r'rp�sz#ReraiseStatNode.analyse_expressionszRaising exceptioncCs�|�|j�|jj}|r�|j�t�|�|d�|�|d�|�|d�|�	dt
|��x|D]}|�d|�qfW|�	�|�	|�|j��n*|j�t
�dd��|�	d|�|j��dS)	Nrrr"z&__Pyx_ErrRestoreWithState(%s, %s, %s);z%s = 0; ZReRaiseExceptionzExceptions.cz__Pyx_ReraiseException(); %s)r�r%r��exc_varsr�rgrrIrrOr�r�r�rr�)r~rUrLZvarnamer&r&r'r��s
z'ReraiseStatNode.generate_execution_codeN)rKrkrlr�r�rpr{r�r�r�r�r&r&r&r'r��sr�c@s>eZdZddgZdd�ZejZdZdd�Z	dd	�Z
d
d�ZdS)
�AssertStatNoderr7cCsz|j�|�|_|jrv|j�|�}|jtjks4|jjsjddlm	}||j
|gdd�}|j|dd��|�|_n|�|�|_|S)Nr)r�T)rRZslow)Z
skip_children)rZanalyse_boolean_expressionr7ror/rZ
tuple_typer r�r�r%r{)r~rAr7r�r&r&r'rp�sz"AssertStatNode.analyse_expressionszRaising exceptioncCs�|�d�|�d�|�|j�|j�|�|�d|j���|jr�|j�|�|�d|j���|j�|�|j�	|�n
|�d�|�|�
|j��|�d�|j�|�|j�	|�|�d�|�d�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)rOr�r%rrDr�r7rrrKr�)r~rUr&r&r'r��s.

z&AssertStatNode.generate_execution_codecCs*|j�||�|jdk	r&|j�||�dS)N)rr�r7)r~rArUr&r&r'r�s
z,AssertStatNode.generate_function_definitionscCs"|j�|�|jr|j�|�dS)N)rr�r7)r~rUr&r&r'r�szAssertStatNode.annotateN)rKrkrlr�rpr{r�r�r�r�r�r�r&r&r&r'r��s
r�c@sFeZdZddgZdd�Zdd�Zdd�Zdd
d�Zdd
�Zdd�Z	dS)�
IfStatNode�
if_clauses�else_clausecCs0x|jD]}|�|�qW|jr,|j�|�dS)N)r�r�r�)r~rA�	if_clauser&r&r'r�szIfStatNode.analyse_declarationscs.�fdd�|jD�|_|jr*|j���|_|S)Ncsg|]}|����qSr&)rp)r�r�)rAr&r'r�sz2IfStatNode.analyse_expressions.<locals>.<listcomp>)r�r�rp)r~rAr&)rAr'rpszIfStatNode.analyse_expressionscCs�|�|j�|��}t|j�}|jr>|j|jd|jdd�n|d8}x8t|j�D]*\}}|�||j�|j	||||kd�qRW|jr�|�|jj�|�
d�|j�	|�|�
d�|�|�dS)Nr�T)�inverser)�is_lastz
/*else*/ {r�)r�r%r�rHr�r��_set_branch_hintrr�r�rOr)r~rUr��lastr#r�r&r&r'r� s


z"IfStatNode.generate_execution_codeFcCs�|js
dSt|t�r|jsdSt|jdttf�r�t|j�dkrtttt	t
tf}x$|jdd�D]}t||�s^dSq^W|r|dnd|_dS)Nr�rr�r
)
r�rIr�r�r�r�rHr�rAr�r6r8�branch_hint)r~�clauseZstatements_noder�Znon_branch_nodesrTr&r&r'r�3s
zIfStatNode._set_branch_hintcCs8x|jD]}|�||�qW|jdk	r4|j�||�dS)N)r�r�r�)r~rArUr�r&r&r'r�Bs
z(IfStatNode.generate_function_definitionscCs0x|jD]}|�|�qW|jr,|j�|�dS)N)r�r�r�)r~rUr�r&r&r'r�HszIfStatNode.annotateN)F)
rKrkrlr�r�rpr�r�r�r�r&r&r&r'r�s
r�c@s@eZdZddgZdZdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�IfClauseNode�	conditionr�NcCs|j�|�dS)N)r�r�)r~rAr&r&r'r�Xsz!IfClauseNode.analyse_declarationscCs |j�|�|_|j�|�|_|S)N)r��analyse_temp_boolean_expressionr�rp)r~rAr&r&r'rp[sz IfClauseNode.analyse_expressionscCs�|j�|�|�|j�|j��}|jr6d|j|f}|�d|�|j�|�|j�|�|j	�
|�|j|jdd�|s�|j	js�|�|�|�d�dS)Nz%s(%s)z	if (%s) {F)r�r�)
r�rDr�r%r�r�rOrrKr�r�r�r�)r~rUr�r�r�r&r&r'r�`s

z$IfClauseNode.generate_execution_codecCs |j�||�|j�||�dS)N)r�r�r�)r~rArUr&r&r'r�osz*IfClauseNode.generate_function_definitionscCs|j�|�|j�|�dS)N)r�r�r�)r~rUr&r&r'r�sszIfClauseNode.annotate)
rKrkrlr�r�r�rpr�r�r�r&r&r&r'r�Osr�c@s,eZdZddgZdd�Zdd�Zdd�Zd	S)
�SwitchCaseNode�
conditionsr�cCsbx6|jD],}|�|j�|�|�|�d|���qW|j�|�|j|jdd�|�d�dS)Nzcase %s:F)r�zbreak;)r�r�r%rDrOr�r�r�)r~rUrr&r&r'r��s
z&SwitchCaseNode.generate_execution_codecCs.x|jD]}|�||�qW|j�||�dS)N)r�r�r�)r~rArUrr&r&r'r��sz,SwitchCaseNode.generate_function_definitionscCs*x|jD]}|�|�qW|j�|�dS)N)r�r�r�)r~rUrr&r&r'r��szSwitchCaseNode.annotateN)rKrkrlr�r�r�r�r&r&r&r'r�xs	r�c@s.eZdZdddgZdd�Zdd�Zdd	�Zd
S)�SwitchStatNode�test�casesr�cCs�|j�|�|�|j�|�d|j���x|jD]}|�|�q4W|jdk	rr|�d�|j�|�|�d�n
|�d�|�d�dS)Nz
switch (%s) {zdefault:zbreak;zdefault: break;r�)	r�rDr�r%rOr�r�r�r�)r~rU�caser&r&r'r��s


z&SwitchStatNode.generate_execution_codecCsF|j�||�x|jD]}|�||�qW|jdk	rB|j�||�dS)N)r�r�r�r�)r~rArUr�r&r&r'r��s

z,SwitchStatNode.generate_function_definitionscCs@|j�|�x|jD]}|�|�qW|jdk	r<|j�|�dS)N)r�r�r�r�)r~rUr�r&r&r'r��s

zSwitchStatNode.annotateN)rKrkrlr�r�r�r�r&r&r&r'r��s
r�c@seZdZdS)�LoopNodeN)rKrkrlr&r&r&r'r��sr�c@s>eZdZdddgZdd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�
WhileStatNoder�r�r�cCs"|j�|�|jr|j�|�dS)N)r�r�r�)r~rAr&r&r'r��sz"WhileStatNode.analyse_declarationscCs:|jr|j�|�|_|j�|�|_|jr6|j�|�|_|S)N)r�r�r�rpr�)r~rAr&r&r'rp�sz!WhileStatNode.analyse_expressionscCs�|�|j�|��}|�d�|jr\|j�|�|j�|�|�d|j���|j�|�|j	�
|�|�|j�|�d�|j
}|�|�|jr�|�|jj�|�d�|j�
|�|�d�|�|�dS)Nzwhile (1) {zif (!%s) break;r�z
/*else*/ {)r�r%�new_loop_labelsrOr�rDrr�rKr�r�rr�r��set_loop_labelsr�)r~rU�old_loop_labelsr�r&r&r'r��s*



z%WhileStatNode.generate_execution_codecCs>|jr|j�||�|j�||�|jdk	r:|j�||�dS)N)r�r�r�r�)r~rArUr&r&r'r��s

z+WhileStatNode.generate_function_definitionscCs4|jr|j�|�|j�|�|jr0|j�|�dS)N)r�r�r�r�)r~rUr&r&r'r��s
zWhileStatNode.annotateN)	rKrkrlr�r�rpr�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_flagNc
Cs(tj||j|||||||dtjd�dS)NT)	r�r�r�r�r�r�r�r@r/)r{r�r%r
�c_bint_type)r~r�r�r�r�r�r�r�r&r&r'r�	szDictIterationNextNode.__init__cCsddlm}|j�|�|_|j�|�|_|jr<|j�|�|_|jrx|j�|�|_|�|jj	t
j�|_|j�
|jj|�|_|jr�|j�|�|_|j|jj	t
jd�|_|j�
|jj|�|_|jr�|j�|�|_|�|jj	t
j�|_|j�
|jj|�|_|j�|�|_|S)Nr)r�)r/)r�r�r�ror�r�r�rq�TempNoder%r
r�key_refrr/r�r��	value_refr�r��	tuple_refr�r�)r~rAr�r&r&r'rps&z)DictIterationNextNode.analyse_expressionscCs|j�||�dS)N)r�r�)r~rArUr&r&r'r�,sz3DictIterationNextNode.generate_function_definitionsc	Cs�|j�t�dd��|j�|�g}g}xz|j|j|jf|j	|j
|jf|j|j
|jfgD]H\}}}|dkrpd}n&|�|||f�|�|�d|��}|�|�qXW|j�tjd�}|�d||j��|j��|j��|d|d|d	|j��f�|�d
|�|�|�d||j��|j�|�x"|D]\}}}|�|����q.Wx|D]\}}}|�|��qRWx*|D]"\}}}|�||�|� |��qrWdS)NZ	dict_iterz
Optimize.crfz&%sFz7%s = __Pyx_dict_iter_next(%s, %s, &%s, %s, %s, %s, %s);rrr"zif (unlikely(%s == 0)) break;z%s == -1)!r�rgrr�r�rDr�r�r�r�r�r�r�r�r�r�allocater�r�r�r
r=rOrr�r�r��
error_goto_ifr%r�r�rN�release)	r~rUrdZtemp_addressesr>r�rL�addr�result_tempr&r&r'r�/sB
z-DictIterationNextNode.generate_execution_code)rKrkrlr�r�r�r�r�r�r�r�rpr�r�r&r&r&r'r��sr�c@sDeZdZddddddgZdZZdd	�Zd
d�Zdd
�Zdd�Z	dS)�SetIterationNextNode�set_objr�r�r�r��is_set_flagNcCs$tj||j|||||dtjd�	dS)NT)r�r�r�r�r�r@r/)r{r�r%r
r�)r~r�r�r�r�r�r&r&r'r�aszSetIterationNextNode.__init__cCs�ddlm}|j�|�|_|j�|�|_|j�|�|_|j�|�|_|j|jj	t
jd�|_|j�
|jj|�|_|j�|�|_|S)Nr)r�)r/)r�r�r�ror�r�r�rqr�r%r
rr�rr/r�r�)r~rAr�r&r&r'rplsz(SetIterationNextNode.analyse_expressionscCs|j�||�dS)N)r�r�)r~rArUr&r&r'r�wsz2SetIterationNextNode.generate_function_definitionsc
Cs�|j�t�dd��|j�|�|j}|�|�|j�	t
jd�}|�d||j�
�|j��|j��|��|j��f�|�d|�|�|�d||j��|j�|�|�|���|j�|�|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�rgrr�r�rDr�r�r�r�r
r=rOrr�r�r�r�r�r%r�r�r�r�rNr�)r~rUr�r�r&r&r'r�zs&
z,SetIterationNextNode.generate_execution_code)
rKrkrlr�r�r�r�rpr�r�r&r&r&r'r�Ys
r�cKs<d|kr,|djrt|f|�St|f|�Snt|f|�SdS)N�iterator)�is_async�AsyncForStatNode�
ForInStatNode�ForFromStatNode)r%rr&r&r'�ForStatNode�s

r�c@sReZdZdddddgZdZdZdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�ZdS)�_ForInStatNoderLr�r�r�r�NFcCstd��dS)Nz!must be implemented by subclasses)�NotImplementedError)r~r&r&r'�_create_item_node�sz _ForInStatNode._create_item_nodecCs6|j�|�|j�|�|jr*|j�|�|��dS)N)rLrr�r�r�r�)r~rAr&r&r'r��s
z#_ForInStatNode.analyse_declarationscCs�|j�|�|_|j�|�|_|��|j�|�|_|js`|jjjsL|jjj	r`|jj�
|jj�r`n|j�|jj|�|_|j�|�|_|j
r�|j
�|�|_
|S)N)rLrqr�rpr�r�r�r/r�r�rrr�r�)r~rAr&r&r'rp�sz"_ForInStatNode.analyse_expressionscCsL|�|j�|��}|j�|�|�d�|j�|�|j�|j|�|j	�
|�|�|j�|�|j�|�d�|j
}|�|�|j�r|j}|�d�|_|�d�|j�
|�|�d�|�|j��r|�|�|�|j�|�|j�|j�|�|�|�|�|�|�|j�|�|��r0|�|�|j�|�|j�|�dS)Nz
for (;;) {r�Zouter_continuez
/*else*/ {)r�r%r�r�rDrOr�rLrNr�r�rr�r�r�r�r�r�r�rrK)r~rUr�r�Zorig_continue_labelr&r&r'r��s<








z&_ForInStatNode.generate_execution_codecCsF|j�||�|j�||�|j�||�|jdk	rB|j�||�dS)N)rLr�r�r�r�)r~rArUr&r&r'r��s

z,_ForInStatNode.generate_function_definitionscCsF|j�|�|j�|�|j�|�|jr6|j�|�|j�|�dS)N)rLr�r�r�r�r�)r~rUr&r&r'r��sz_ForInStatNode.annotate)rKrkrlr�r�r�r�r�rpr�r�r�r&r&r&r'r��s
(r�c@seZdZdZdd�ZdS)r�FcCsddlm}||j�|_dS)Nr)�NextNode)r�r�r�r�)r~r�r&r&r'r�szForInStatNode._create_item_nodeN)rKrkrlr�r�r&r&r&r'r�sr�c@s eZdZdZdd�Zdd�ZdS)r�TcKsDd|kst�ddlm}|j|djdd�|d<tj||f|�dS)Nr�r)r�r�)r4)r	r�r�ZAwaitIterNextExprNoder%r�r�)r~r%rr�r&r&r'r�szAsyncForStatNode.__init__cCs ddlm}|�|j�|j_dS)Nr)r�)r�r�Z
AsyncNextNoder�r�r4)r~r�r&r&r'r�sz"AsyncForStatNode._create_item_nodeN)rKrkrlr�r�r�r&r&r&r'r�
sr�c@sveZdZddddddgZdZdZdZdZd	Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
ddddd�Zdd�Zdd�ZdS)r�rL�bound1�bound2�stepr�r�FNz&For-loop using object bounds or targetcCs.x(|j|j|jfD]}|jjr|��qWdS)N)rLr�r�r/r;r�)r~rAr�r&r&r'r�9szForFromStatNode.nogil_checkcCs.|j�|�|j�|�|jr*|j�|�dS)N)rLrr�r�r�)r~rAr&r&r'r�>sz$ForFromStatNode.analyse_declarationscCs�ddlm}|j�|�|_|j�|�|_|j�|�|_|jdk	rlt|j|j	�r^t
|jjdd�|j�|�|_|�|�|jj
}|js�|js�t|jjd�|j�|�|_|jr�|j�|�|_|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�rLrqr�ror�r�rIZUnaryMinusNoderr%�set_up_loopr/r;r]rr�rpr�)r~rAr�r�r&r&r'rpDs"


z#ForFromStatNode.analyse_expressionscCs|ddlm}|jj}|jr |}n~|jr4t|jjd�|jr@t	j
nt	j}|jjjs`t	�
||jj�}|jjjszt	�
||jj�}|jdk	r�|jjjs�t	�
||jj�}|j�||�|_|j�||�|_|jjs�|j�|�|_|jdk	�r|j�||�|_|jj�s|j�|�|_|j�s|j�rJd|_t|j|j��r:t|jd��|j|_d|_n.d|_|�|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�rLr/r]�is_enumrr%r;r
r<r=r��widest_numeric_typer�r�rr�rV�is_py_targetrIZBufferIndexNoder�loopvar_node�py_loopvar_noder�rkr{)r~rAr�r�Z	loop_typeZc_loopvar_noder&r&r'r�Ys@


zForFromStatNode.set_up_loopc
Cs|�|j�|��}|j}|j�|�|j�|�|j|j\}}|j	dk	rt|j	�|�|j	�
�}d|d|f}nd}ddlm}t
|j|j�r�|j�|�t
|j|j�r�|j�|�|jjjr�tjn|jj}|r�|js�|j�|d�}	n
|j�
�}	|j�rF|j�sF|jddk�rF|�d|	|j�
�|||	|j|j�
�||	|f
�n,|�d	|	|j�
�||	|j|j�
�|	|f�|j}
|
dk�r�|�r�|�|jj||	�}
|
dk	�r�|
�|�|j�|
|�|j �!|�|�"|j#�|�s�|j�r�|jj$j%�r�|�&|jjd�}|�|�|�'|jj$j(�}|jj$j)j*�r@|j+�,t-�.d
d��d}
n$|j+�,t-�.d
d��d|jj$j)j/}
|�d|�
�|
||�0|�
�|jj�f�|�1|�
��n|j}|�2|jj||jj$j)�}|	|_3|�4|�|jj$j%�r�|�5|�
�|j�|�6|�|�d�|�s*|j�r*|j�|�|j�|j|�|�rD|j�sD|j�7|	�|j8}|�9|�|j:�r||�d�|j:�!|�|�d�|�"|�|j�;|�|j�<|�|j�;|�|j�<|�t
|j|j��r�|j�6|�t
|j|j��r�|j�6|�|j	dk	�r|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�rDr��relation_tableZ	relation1r�r�r�r�rIr�r�r�r�rLr/r�r
r<r�r�r�r�rXZ	relation2rOr�rNr�r�rr�r�Zis_pyglobalZ
PyTempNoder�r:r+r�r�rgrr�rr�r�ZCoerceFromPyTypeNodeZ	temp_code�generate_result_coder
r�r�r�r�r�rrK)r~rUr�r��offsetZincopr�r�Zloopvar_typeZloopvar_nameZcoerced_loopvar_nodeZtarget_nodeZinterned_cnameZlookup_funcZfrom_py_noder�r&r&r'r�s�



 










z'ForFromStatNode.generate_execution_code)r�z++)z+1z++)r�z--)z-1z--)z<=r�z>=r�cCsl|j�||�|j�||�|j�||�|jdk	rB|j�||�|j�||�|jdk	rh|j�||�dS)N)rLr�r�r�r�r�r�)r~rArUr&r&r'r��s

z-ForFromStatNode.generate_function_definitionscCsX|j�|�|j�|�|j�|�|jr6|j�|�|j�|�|jrT|j�|�dS)N)rLr�r�r�r�r�r�)r~rUr&r&r'r�szForFromStatNode.annotate)rKrkrlr�r�r�r�r�r�r�r�rpr�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_callrLr�NcCs(|j�|�|j�|�|j�|�dS)N)r�r�r�r�)r~rAr&r&r'r�/sz!WithStatNode.analyse_declarationscCsT|j�|�|_|j�|�|_|jrBddlm}||jj|jj�|_|j	�
|�|_	|S)Nr)r�)r�ror�rLr�r�r%r/�target_tempr�rp)r~rAr�r&r&r'rp4sz WithStatNode.analyse_expressionscCs.|j�||�|j�||�|j�||�dS)N)r�r�r�r�)r~rArUr&r&r'r�>sz*WithStatNode.generate_function_definitionsc
Cs�|�|j�|�d�|j�|�|jjtdd�|_|j	�
t�dd��|�d|j|j�
�|�t|jrjdnd��|�|j|j�f�|�|j�|��}|j}|j�|�|jr�|j�|�|j�|�|�d	|j��|j��f�|j�|�n|j�|�|j�|�|j�|�|j�|�||_|j�|�|� |��r�|�!�}|�"|�|�#|�|�$|jt�|�"|�|�#|�|j�%|j�|�d
�dS)Nz/*with:*/ {F)r�ZPyObjectLookupSpecialzObjectHandling.cz-%s = __Pyx_PyObject_LookupSpecial(%s, %s); %s�	__aexit__�__exit__z%s = %s;r�)&r�r%rOr�rDr�r�rZexit_varr�rgrr�rr�rr�r�r�r�r�r�rLr�r�rGr�rJrrKr�r�r�r�r�rr�r�)r~rUr�Zintermediate_error_labelZstep_over_labelr&r&r'r�CsF




z$WithStatNode.generate_execution_code)rKrkrlrxr�r�r�r�rpr�r�r&r&r&r'r�
s
r�c@s<eZdZddgZdZdZdd�Zdd�Zdd	�Zd
d�Z	dS)�WithTargetAssignmentStatNoderCrNcCs|j�|�dS)N)rr)r~rAr&r&r'r��sz1WithTargetAssignmentStatNode.analyse_declarationscCs4|j�|�|_|j�|�|jj�|jj|�|_|S)N)rrqrR�	with_noder�rr/rC)r~rAr&r&r'rp�sz0WithTargetAssignmentStatNode.analyse_expressionscCs.|j�|�|j�|j|�|jj�|�dS)N)rCrDrrNr�r�r�)r~rUr&r&r'r��sz4WithTargetAssignmentStatNode.generate_execution_codecCs|j�|�|j�|�dS)N)rr�rC)r~rUr&r&r'r��sz%WithTargetAssignmentStatNode.annotate)
rKrkrlr�r�rCr�rpr�r�r&r&r&r'r�ts
r�c@sLeZdZdddgZdZdd�Zdd�ZejZ	d	Z
d
d�Zdd
�Zdd�Z
dS)�TryExceptStatNoder��except_clausesr�FcCs<|j�|�x|jD]}|�|�qW|jr8|j�|�dS)N)r�r�r�r�)r~rA�
except_clauser&r&r'r��s
z&TryExceptStatNode.analyse_declarationscCsv|j�|�|_d}xDt|j�D]6\}}|�|�}|j|<|rJt|jd�|jsd}qW||_|jrr|j�|�|_|S)Nrzdefault 'except:' must be lastr)	r�rprr�rr%�pattern�has_default_clauser�)r~rAZdefault_clause_seenr#r�r&r&r'rp�sz%TryExceptStatNode.analyse_expressionszTry-except statementcs��j}�j}�j}���}�j}��d�}��d�}��d�}	��d�}
|rV��d�nd}|rh��d�nd}��d�}
�fdd	�td
�D����|j���	d���
�}��	d�|
�_|�_|�_|j����j|jd
d���	d��j
��}��|�}|�rv�j�t�|j�s.|�	d�|�	d�|�	dd�dd	��D���x�D]}|�|��qRW��fdd�}n*dd	��D�}|�	dd�|��dd�}|�_|	�_|jj}|j�r���|jj���	d�|j�����	d�|�s�|jj}|�r�|�s*x�D]}��|t��q
W��|
���|�x|D]\}}��||��q:W�j
j}|�j
_x|jD]}|��|��qlW|�j
_|j�s���|�x�||f||f||f|
|f|	|fgD]b\}}��|��r�|�s��|
��s��|
���|��j|jd
d�|�r|���|��q�W��|��rd|�sN��|
��sN��|
���|�|�rd|���|
��rz��|
���	d�x�D]}�j
� |��q�W|�_|�_|�_|�_dS)NZexception_handledZexcept_errorZ
except_returnZ
try_returnZ	try_breakZtry_continueZtry_endcsg|]}�j�td��qS)F)r�r�r)r�rw)rUr&r'r��sz=TryExceptStatNode.generate_execution_code.<locals>.<listcomp>r$r�z
/*try:*/ {F)r�r�r�r�z__Pyx_ExceptionSave(%s);z, cSsg|]}d|�qS)z&%sr&)r�r>r&r&r'r��scs0x�D]}��|�qW��dd����dS)Nz__Pyx_ExceptionReset(%s);z, )rrOr�)r:)rU�
exc_save_varsr&r'�restore_saved_exception�s
zJTryExceptStatNode.generate_execution_code.<locals>.restore_saved_exceptioncSsg|]}d|�qS)z	(void)%s;r&)r�r>r&r&r'r��sz%s /* mark used */rFcSsdS)Nr&r&r&r&r'r��sz/*else:*/ {)!r�r�r�r�r�r�rbr�r%rOr�r�r�r��all_free_managed_tempsr�r�rg�reset_exception_utility_coder�r��put_xgotrefr�r�r�rr�rZcurrent_exceptr��generate_handling_coder�r�)r~rU�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_raiser>r�Zmark_vars_usedZnormal_case_terminates�	temp_name�	temp_typeZouter_exceptr�Z
exit_label�	old_labelrr&)rUr�r'r��s�






















z)TryExceptStatNode.generate_execution_codecCsF|j�||�x|jD]}|�||�qW|jdk	rB|j�||�dS)N)r�r�r�r�)r~rArUr�r&r&r'r�/s

z/TryExceptStatNode.generate_function_definitionscCs<|j�|�x|jD]}|�|�qW|jr8|j�|�dS)N)r�r�r�r�)r~rUZexcept_noder&r&r'r�6s
zTryExceptStatNode.annotateN)rKrkrlr�r�r�rpr{r�r�r�r�r�r�r&r&r&r'r��s
xr�c@sLeZdZddddgZdZdZdZdd�Zd	d
�Zdd�Z	d
d�Z
dd�ZdS)�ExceptClauseNoder�rLr�r�NFcCs"|jr|j�|�|j�|�dS)N)rLrr�r�)r~rAr&r&r'r�Usz%ExceptClauseNode.analyse_declarationscCs�|j|_|jr@x0t|j�D]"\}}|�|�}|�|�|j|<qW|jrrddlm}|�	|j
�|_|j�||j�|_|j
�|�|_
|S)Nr)r�)r�
function_namer�rrpr{rLr�r�ZExcValueNoder%r�rr�)r~rAr#r�r�r&r&r'rpZs
z$ExceptClauseNode.analyse_expressionscs���|j�|j�r�tdd�|jD��}|r��fdd�td�D�}�j�t�dd����	dt
|���j�t�d	d
��d|d}nd
}�j�t�dd��d}g}x*|jD] }|���|�||�
��q�W�jjtjdd�}��	d|d�|�f�x$|jD]}|���|����q
W|�rv��	dt
|����	d�dd�|D���x|D]}	�j�|	��q`W��	d|��j�|�n
��	d�t|jdd��s�|jdk�r�|jdk�r��j�t�dd����	d���|���	d�dS�fdd�td�D�}��|j��j�t�dt
|�}
��	d|
��|j�f�x|D]}��|��qTW|j�r�|j �!|d �|j ���|j�"|j ��|jdk	�r�x&t#||jj$�D]\}}
|
�!|��q�W�j%�j&}}��'d!��_%��'d"��_&�jj(}|�j_(|j�)��|�j_(|jj*�sJx|D]}��+|t,��q*W��|�x^�j%|f�j&|fgD]F\}}��-|��r`��.|�x|D]}��+|t,��q�W��|��q`W|�_%|�_&x|D]}	�j�|	��q�W��	d�dS)#Ncss$|]}|jp|��o|jVqdS)N)r��	is_simpler@)r�r�r&r&r'r�osz:ExceptClauseNode.generate_handling_code.<locals>.<genexpr>csg|]}�jjtdd��qS)T)r�)r�r�r)r�rw)rUr&r'r�tsz;ExceptClauseNode.generate_handling_code.<locals>.<listcomp>r$�PyErrFetchRestorezExceptions.cz__Pyx_ErrFetch(&%s, &%s, &%s);ZFastTypeCheckszModuleSetupCode.cz*__Pyx_PyErr_GivenExceptionMatches(%s, %%s)rr&ZPyErrExceptionMatchesz __Pyx_PyErr_ExceptionMatches(%s)F)r�z%s = %s;z || z__Pyx_ErrRestore(%s, %s, %s);rFcSsg|]}d|�qS)z%s = 0;r&)r�r>r&r&r'r��sz	if (%s) {z
/*except:*/ {r�Tz__Pyx_ErrRestore(0,0,0);r�csg|]}�jjtdd��qS)T)r�)r�r�r)r�rw)rUr&r'r��sz
&%s, &%s, &%sz"if (__Pyx_GetException(%s) < 0) %srZexcept_breakZexcept_continue)/r�r%r�r�rbr�rgrr�rOr�rDrrr�r�r
r=r�rrKr�r�r��excinfo_targetrLr�rr��get_exception_utility_coder�r�r�Zset_varrNr;rRr�r�r�r�r�r�r�rr�r)r~rUr�Zhas_non_literalsr�Z
exc_test_funcZ	exc_testsr�Z
match_flagr�Zexc_argsr>ZtempvarrTr�r��old_exc_varsr�r�r&)rUr'r�js�
















z'ExceptClauseNode.generate_handling_codecCs*|jdk	r|j�||�|j�||�dS)N)rLr�r�)r~rArUr&r&r'r��s
z.ExceptClauseNode.generate_function_definitionscCsB|jr x|jD]}|�|�qW|jr2|j�|�|j�|�dS)N)r�r�rLr�)r~rUr�r&r&r'r��szExceptClauseNode.annotate)rKrkrlr�r�rZis_except_asr�rpr�r�r�r&r&r&r'r�>sjr�c@s�eZdZdddgZdZdZdZdZdZdZ	e
dd	��Zd
d�Zdd
�Z
ejZdZdd�Zdd�Zddd�Zddd�Zdd�Zdd�ZdS)�TryFinallyStatNoder��finally_clause�finally_except_clauserTNFcCst|||d�}|S)N)r�r)r)r%rAr�rrTr&r&r'r��sz"TryFinallyStatNode.create_analysedcCs6|j�|�t�|j�|_|j�|�|j�|�dS)N)r�r�r�deepcopyrr)r~rAr&r&r'r�sz'TryFinallyStatNode.analyse_declarationscCsD|j�|�|_|j�|�|_|j�|�|_|jr@|jjs@|j|_|S)N)r�rprrrr��func_return_type)r~rAr&r&r'rpsz&TryFinallyStatNode.analyse_expressionszTry-finally statementcs����j��j}���}���}�j}�js4|�_���}��d��jj	}d�j_	�j
���|�j_	��d���|��j�
�}���jj���d��jgf�fdd�	}	�joˆ�|�}
�jj}�j
j�s��d�|	�����jj�s��|���d�|
�r̈�|���d��j�s8��d	��j�rH���|�r�t�fd
d�td�D��}�jjt�t�tj��d
d�}
nd}}
t�fdd�td�D��}���||||
����}��d��jj}|dd��j_�j ���|�j_��d�|�rX��!�|||
�|�r<x|D]}�j�"|��q&W|
�rN�j�"|
���|�xLt#���|�D]:\}}��|��s��qh��|���$�|���|��qhWx|D]}�j�"|��q�W��d���|��j%}d}�x�t&t#||��D�]p\}\}}��|��s�q�||k�r$|
�r$�q��d|�d}||k�r؈j�rpt�fdd�td�D��}���g|��jj�s؈j'�r؈j�s�t(�jt)��s؈jj�j'd
d�}��d|t*j+f��j'j,�r؈�dt*j+�|	����||k�rD|�r0��dt*j+|f��j'j,�r ��d|��j�"|�d}�j�rD��!�|��jj�sX��|���d��q�W��|���d�dS)Nz
/*try:*/ {rr�z/*finally:*/ {cs.|d}t�|�}|�jkr&||d<n|}|S)Nr)rrr)Z_nextrTZ	node_copy)r~r&r'�fresh_finally_clause*s


zHTryFinallyStatNode.generate_execution_code.<locals>.fresh_finally_clausez/*normal exit:*/{z/*exception exit:*/{r�csg|]}�jjtjdd��qS)F)r�)r�r�r
r=)r�rw)rUr&r'r�Gsz>TryFinallyStatNode.generate_execution_code.<locals>.<listcomp>r"F)r�csg|]}�jjtdd��qS)F)r�)r�r�r)r�rw)rUr&r'r�Os�r�r$r&z%s: {csg|]}�jjtdd��qS)F)r�)r�r�r)r�rw)rUr&r'r��sz%s = %s;z%s = 0;)-r�r%r�Zall_new_labels�get_all_labels�handle_error_caser�rOr�Zin_try_finallyr�r��set_all_labelsr�r�preserve_exceptionr�r�r�rr��is_try_finally_in_nogilr�r�rbr�r
ZCPtrTyper6Zc_char_type�put_error_catcherr�r�put_error_uncatcherr�r;�put_error_cleanerr�rr	rI�GILExitNoder	r�r;)r~rUr�Z
old_labelsZ
new_labelsr�Zcatch_labelZwas_in_try_finallyr�r
Zpreserve_errorZneeds_success_cleanup�exc_lineno_cnames�exc_filename_cnamer�Zfinally_old_labelsrrr�r�r�r#Zret_tempr&)rUr~r'r�s�

























 








z*TryFinallyStatNode.generate_execution_codecCs |j�||�|j�||�dS)N)r�r�r)r~rArUr&r&r'r��sz0TryFinallyStatNode.generate_function_definitionsc		Cs�|j�t�|j�t�|j�t�|jr6|jdd�|�d�|�d�dd�|D���x|D]\}}|�	||�q`W|�d|dd��|�d	|dd�d
�x|D]}|�
|�q�W|r�|�d|dtj|d
tj
|tjf�|jr�|��dS)NF)r�r�rFcSsg|]}d|�qS)z%s = 0;r&)r�r>r&r&r'r��sz8TryFinallyStatNode.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�rgrr�swap_exception_utility_coderr�rOr�r�r�r	�lineno_cname�
clineno_cname�filename_cnamer�)	r~rUr�r�rrr�r/r>r&r&r'r�s,



z$TryFinallyStatNode.put_error_catcherc	Cs|j�t�|j�t�|jr*|jdd�|�d�x|dd�D]}|�|�qBW|�d|dd��|�d�x|dd�D]}|�|�q�W|�d|dd��|jr�|��|�d�	d	d
�|D���|r�|�dt
j|dt
j|d
t
j
|f�dS)NF)r�zif (PY_MAJOR_VERSION >= 3) {r$z!__Pyx_ExceptionReset(%s, %s, %s);r�z__Pyx_ErrRestore(%s, %s, %s);rFcSsg|]}d|�qS)z%s = 0;r&)r�r>r&r&r'r��sz:TryFinallyStatNode.put_error_uncatcher.<locals>.<listcomp>z%s = %s; %s = %s; %s = %s;rr)r�rgrr�rr�rOrr�r�r	rrr)r~rUr�rrr>r&r&r'r�s(



z&TryFinallyStatNode.put_error_uncatchercCs�|j�t�|jr|jdd�|�d�x|dd�D]}|�|�q6W|�d|dd��|�d�x |dd�D]}|�|t�qvW|jr�|�	�|�d�
dgd�|dd��dS)	NF)r�zif (PY_MAJOR_VERSION >= 3) {r$z!__Pyx_ExceptionReset(%s, %s, %s);r�rFz%s = 0;)r�rgr�rr�rOrr�rr�r�)r~rUr�r>r&r&r'r�s

z$TryFinallyStatNode.put_error_cleanercCs|j�|�|j�|�dS)N)r�r�r)r~rUr&r&r'r��szTryFinallyStatNode.annotate)NN)NN)rKrkrlr�rr
r	rrr�r�r�r�rpr{r�r�r�r�r�rrrr�r&r&r&r'r�s&

!
rc@seZdZdZdZdZdS)�NogilTryFinallyStatNodezJ
    A try/finally statement that may be used in nogil code sections.
    FN)rKrkrlrxrr�r&r&r&r'r�srcs@eZdZdZdd�Zdd�Z�fdd�Zdd	�Zd
d�Z�Z	S)�GILStatNodeNc	Cs6||_|�|||�tj|||t|||jd�d�dS)N)�state�
state_temp)r�r)r�create_state_temp_if_neededrr�rr)r~r%rr�r&r&r'r�szGILStatNode.__init__cCsZddlm}|�}|�|�|js&dS|dkr6tj}ntj}ddlm}|�	||�|_
dS)Nr)�YieldNodeCollector�gil)r�)ZParseTreeTransformsr Z
visitchildrenZyieldsr
Zc_gilstate_typeZc_threadstate_ptr_typer�r�r�r)r~r%rr�r Z	collectorr�r�r&r&r'rs
z'GILStatNode.create_state_temp_if_neededcs,|jdk|_|jdkrd|_tt|��|�S)Nr!T)rZ_in_with_gil_blockr�rdrr�)r~rA)rJr&r'r�#s
z GILStatNode.analyse_declarationscCs:|�t�dd��|j}|jdk|_t�||�}||_|S)NZForceInitThreadszModuleSetupCode.cr)rgrr�rrrrp)r~rA�	was_nogilrTr&r&r'rp*szGILStatNode.analyse_expressionscCs�|�|j�|��|jr2|j�|�|j��}nd}|jj}|jdkr^|j	|d�d|j_n|j
|d�d|j_t�||�|jr�|j�
|�||j_|��dS)Nr!)�variableTF)r�r%�begin_blockrr�r�r�r�rr�Zput_release_gilrr�r��	end_block)r~rUr#Zold_gil_configr&r&r'r�3s"

z#GILStatNode.generate_execution_code)
rKrkrlrr�rr�rpr�rmr&r&)rJr'rs		rc@s(eZdZdZgZdZdd�Zdd�ZdS)rz]
    Used as the 'finally' block in a GILStatNode

    state   string   'gil' or 'nogil'
    NcCs|S)Nr&)r~rAr&r&r'rpWszGILExitNode.analyse_expressionscCs:|jr|j��}nd}|jdkr,|�|�n
|�|�dS)Nr!)rr�rr�Zput_acquire_gil)r~rUr#r&r&r'r�Zs
z#GILExitNode.generate_execution_code)rKrkrlrxr�rrpr�r&r&r&r'rMs
rc@seZdZdZdd�ZdS)�
EnsureGILNodezI
    Ensure the GIL in nogil functions for cleanup before returning.
    cCs|jdd�dS)NF)r�)r�)r~rUr&r&r'r�ksz%EnsureGILNode.generate_execution_codeN)rKrkrlrxr�r&r&r&r'r&fsr&cCsddlm}|jS)Nr)rc)r�rcrh)rcr&r&r'�cython_view_utility_codeosr'cCst�dd�S)N�ArrayAPIzarrayarray.h)rr�r&r&r&r'r'wr(r'cCst�dd�S)Nr(zarrayarray.h)rr�r&r&r&r'r'xr()z
cpython.arrayzcpython.array.arrayzcython.view)Z__Pyx_patch_asyncioZPatchAsyncIOzCoroutine.c)Z__Pyx_patch_inspectZPatchInspectzCoroutine.c)Zasyncio�inspectc@s,eZdZgZdZdd�Zdd�Zdd�ZdS)	�CImportStatNodeFcCs|jst|jd�dS|j|j|j|jr,dndd�}d|jkr�dd�|j�d�D�}|d}|j�|�}|}x2|dd�D]"}|�|�}|�	|||j�|}qzW|j
r�|�	|j
||j�q�|�|�|�	|||j�n|j
p�|j}|�	|||j�|jtk�r|�
t|j��dS)	Nz$cimport only allowed at module levelrr�)�relative_levelrcSsg|]}t|��qSr&)r)r�r:r&r&r'r��sz8CImportStatNode.analyse_declarations.<locals>.<listcomp>r)r�rr%�find_moduler��is_absoluterr�Zfind_submodule�declare_module�as_namer+�utility_code_for_cimportsrg)r~rA�module_scoper7Ztop_nameZtop_module_scoper:�submodule_scoper&r&r'r��s,


z$CImportStatNode.analyse_declarationscCs|S)Nr&)r~rAr&r&r'rp�sz#CImportStatNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz'CImportStatNode.generate_execution_codeN)rKrkrlr�r-r�rpr�r&r&r&r'r*�s
r*c@s<eZdZgZdZdZdZdd�Zdd�Zdd�Z	dd	�Z
dS)
�FromCImportStatNodeNc
Cs|jst|jd�dS|jr>|j|j�d�kr>t|jd�dS|j|j|j|jd�}|j}|�|��x(|j	D�]\}}}}|dkr�x(t
|j���D]\}}	|�
||	|�q�Wqn|�|�}	|	r�|r�|�|	|�s�|	�|�d|	_n�|dks�|dk�r|j||dd	|d
�}	nh|dk�r$|j|||d�}	nL|jj|||jd
d�}
|
j|k�r^|�|�pR||
|j�nt|d||f�|	rn|�p||}|�
||	|�qnW|�d��s�|�d��r|tk�r�|�t|��x>|j	D]4\}}}}d||f}|tk�r�|�t|���q�WdS)Nz$cimport only allowed at module levelrz3relative cimport beyond main package is not allowed)r+r�rr)rGr)r*r+r,r%�class)r%r�F)Zrelative_tor%Zabsolute_fallbackz%Name '%s' not declared in module '%s'Zcpython�cythonz%s.%s)r�rr%r+r�countr,r�r+�imported_namesr�r�rcr,rZ�declaration_matchesZ
redeclaredr2r0r r��
parent_moduler.r�r0rg)
r~rAr1r�r%r:r/r*Z
local_namer�r2rwZfqnamer&r&r'r��sL






z(FromCImportStatNode.analyse_declarationscCs@|js
dS|j}|dkr$|js<dSn|js.dS||jkr<dSdS)Nrr4r)rkr/r�Zis_struct_or_unionr*)r~r�r*r/r&r&r'r8�s
z'FromCImportStatNode.declaration_matchescCs|S)Nr&)r~rAr&r&r'rp�sz'FromCImportStatNode.analyse_expressionscCsdS)Nr&)r~rUr&r&r'r��sz+FromCImportStatNode.generate_execution_code)rKrkrlr�r�r+r7r�r8rpr�r&r&r&r'r3�s.r3c@s.eZdZdgZdZdd�Zdd�Zdd�Zd	S)
�FromImportStatNoder*rcCsNxH|jD]>\}}|dkr<|js.t|jd�dSd|_d|_q|�|�qWdS)Nr�z%import * only allowed at module levelr)rcr�rr%Zhas_import_star�import_starr)r~rAr:rLr&r&r'r�
sz'FromImportStatNode.analyse_declarationsc		Cs\ddlm}|j�|�|_|�|jt�|_g|_�x$|j	D�]\}}|dkr�x8|j
�	�D]*\}}|jsX|jj
rX|�t�dd��PqXWq:|�|j�}|j�r|jj|k�rt|jd��r|jj|jjjkr�q:y0|j|jjj|j|jjd�}|jj|jkr�w:Wntk
�rYnX|�|d�}|jtk�r2d}n|j�|j|�}|j�|||f�q:W|S)Nr)r�r�ZExtTypeTestzObjectHandling.cr�)r%r+)r�r�r*rpr�r%rr��interned_itemsrcr�rkr/r�rgrr�rZr:r?r�r7r,r�rr�rrr)	r~rAr�r:rLrwr�r*�coerced_itemr&r&r'rps8z&FromImportStatNode.analyse_expressionscCs0|�|j�|j�|�|jrB|�dtj|j��|�|j�f�|j	j
tdd�}|j�
|�|jrx|j�t�dd��x�|jD]�\}}}|�d||j��|�|�|�||j�f�|�|�|dkr�|�|j|�n |�|�|�|�|�||�|�|t�q�W|j	�|�|j�|�|j�|�dS)Nzif (%s(%s) < 0) %s;T)r��
ImportFromzImportExport.cz!%s = __Pyx_ImportFrom(%s, %s); %s)r�r%r*rDr;rOr	rr�r�r�rr�r�r<r�rgrr�r�r�r�rNZallocate_temp_resultr�r�r�rrK)r~rUZ	item_tempr:rLr=r&r&r'r�8s<


z*FromImportStatNode.generate_execution_codeN)rKrkrlr�r;r�rpr�r&r&r&r'r:�s
	#r:c@seZdZdZdZdS)�ParallelNodez4
    Base class for cython.parallel constructs.
    N)rKrkrlrxr�r&r&r&r'r?Zsr?csHeZdZdZddgZdZdZdZdZdZ	dZ
ejej
ejfZejejejfZejejejfZdZ�fdd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zd;dd�Zdd�Z dd�Z!dd�Z"dd�Z#d<dd�Z$d d!�Z%d"d#�Z&d$d%�Z'd&d'�Z(d=d(d)�Z)d*d+�Z*d,d-�Z+d.d/�Z,d0d1�Z-d>d2d3�Z.d4Z/d5Z0d6e/e0fZ1d7d8�Z2d9d:�Z3�Z4S)?�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_threadsNFrcs<tt|�j|f|�|�d�p i|_t�|_i|_g|_dS)Nrd)	rdr@r�r-rdry�seen_closure_vars�privates�assigned_nodes)r~r%rs)rJr&r'r��s
zParallelStatNode.__init__c
CsJ|j�|�d|_|jr�g}t�}x�|jjD]~}|jj|krPt|j	d|jj�|�
|jj�|jjdkr||jjs�|j|_q,|jr�|jjdkr�|jjs�|j|_
q,|�|�q,W||j_y|j�|�|_Wn0tk
r�}zt|jj	d�Wdd}~XYnXni|_xB|j��D]4\}}||jk�r4t|j	d|�nt|||��qWdS)Nz$Duplicate keyword argument found: %srA�	chunksizez=Only compile-time values may be supplied as keyword argumentszInvalid keyword argument: %s)r�r�rArsryr6r�r7rr%r�r��	is_prangerErrI�	Exceptionrc�valid_keyword_argumentsr�)r~rA�pairs�seenZdictitemrfrrr&r&r'r��s4

"z%ParallelStatNode.analyse_declarationscCs�|jr|j�|�|_|jr(|j�|�|_|j�|�|_|�|�|jdk	r�|jrr|jjdk	rr|jjsrt|jd�nJ|jr�|jjs�t|jd�n.|jj	j
r�|jjr�|j�|�dkr�t|jd�|j�
�r�|jj	jr�|j�tj|��|�|_|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)rArprEr��analyse_sharing_attributes�parentrFrr%r/r�r�rIrr;rr
r=rV)r~rAr&r&r'rp�s&


z$ParallelStatNode.analyse_expressionscCsnxh|j��D]Z\}\}}|jr<|js<||jjkr<t|d�q|jsR|rRt|d�qd}|�||||�qWdS)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)rdrcrF�is_parallelrLr�propagate_var_privatization)r~rAr�r%rY�lastprivater&r&r'rK�s


z+ParallelStatNode.analyse_sharing_attributescCsl||f|j|<|jjr$t|d�dS|jrh|jsF||jjkrF|jj}n|j}|rh|sX|rh|�||||�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)	rCr/r�rrFrMrLrdrN)r~r�r%rYrOrLr&r&r'rN s5

z,ParallelStatNode.propagate_var_privatizationcCs�|jr|j�||�S|j|jkr&|jS|j�|jd�}|j�|j�|j�|�|j�	||jf�|�
d||jf�||_dS)zo
        Helper function that allocate a temporary for a closure variable that
        is assigned to.
        Tz%s = %s;N)rL�_allocate_closure_temprrBr�r�r/r��modified_entriesrrO)r~rUr�rr&r&r'rPR sz'ParallelStatNode._allocate_closure_tempcCstd}xjt|j���D]X\}\}}|s|r0||kr|j��}|r|rP|�d�d}|�d|j|j�|�f�qWdS)NTz4/* Initialize private variables to invalid values */Fz%s = %s;)�sortedrCrcr/�
invalid_valuerOrrE)r~rU�excluderbr�rYrOrSr&r&r'�initialize_privates_to_nanh s


z+ParallelStatNode.initialize_privates_to_nancCs0|j}|jj}||j_|�|�||j_|��S)N)�1begin_of_parallel_control_block_point_after_declsr��ownerrDr�)r~rUr�r�rWr&r&r'�evaluate_before_blockw s
z&ParallelStatNode.evaluate_before_blockcCs&|jdk	r"|�d|�||j��dS)zS
        Write self.num_threads if set as the num_threads OpenMP directive
        Nz num_threads(%s))rAr�rX)r~rUr&r&r'�put_num_threads� s
z ParallelStatNode.put_num_threadscCs6g|_x*t|j�D]}|js"|jr|�||�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)rQrRrdZfrom_closurer�rP)r~rUr�r&r&r'�declare_closure_privates� sz)ParallelStatNode.declare_closure_privatescCs@x:|jD]0\}}|�d||jf�|j�|j�||_qWdS)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)rQrOrr�r�)r~rUr�r_r&r&r'�release_closure_privates� sz)ParallelStatNode.release_closure_privatesr&c
Cs�|jr�|j}|j��|_}gg}}x6t|�D]*\}}|jsD|jrP|�|�q0|�|�q0W|rv|�	dd�
|��|r�|�	dd�
|��|jr�tj
g}	|jr�|	�|j�|�	d|j�|�	dd�
|	��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)rM�privatization_insertion_pointr�Zstop_collecting_tempsr�rRr;r�rr�r��breaking_label_usedr	�parallel_why�error_label_used�extend�parallel_exc�pos_info)
r~rUZ
exclude_tempsr�r�rCZ
firstprivatesr�r/Zshared_varsr&r&r'�privatize_temps� s$
z ParallelStatNode.privatize_tempscCsh|jrd|jsd|�d�xLt|j�D]>\}}|jr@|j|dd�q"|jr"|�||�|�d|�q"WdS)Nz/* Clean up any temporaries */F)r�z
%s = NULL;)	rM�is_nested_prangerOrRr�r�rr;r)r~rUr�r/r&r&r'�
cleanup_temps� s
zParallelStatNode.cleanup_tempscCsT|��|_|��|_|j|_|jdd�|_|��|��|_	|��|_
|�|�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;
        }
        �return)r:N)r�r�r�r�r�r�r�r$r��%begin_of_parallel_control_block_pointrV�"undef_builtin_expect_apple_gcc_bug)r~rUr&r&r'�!setup_parallel_control_flow_block� s



z2ParallelStatNode.setup_parallel_control_flow_blockcCs|��|_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)r~rUr&r&r'�begin_parallel_block� s	z%ParallelStatNode.begin_parallel_blockcCs�|jr�|j}|}|�d�|jdd�|�d�|�d�|�d�|�d�|�d�|�d�|��|�d�|�|�|��|�d	�|�d
�|�d�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 _OPENMPT)r�ZPy_BEGIN_ALLOW_THREADSz#endif /* _OPENMP */ZPy_END_ALLOW_THREADSz#elsez{
z#ifndef _OPENMPz}
N)r_rjrOr�Zput_saferer�)r~rUZ
begin_codeZend_coder&r&r'�end_parallel_block� s$











z#ParallelStatNode.end_parallel_blockc	Cs^|��}|��}d|_d|_d|_g|_|��}x.|D]&}|�|�r6|jpR||jk|_d|_q6W|jrp|�|�x�t	|�D]�\}}|�|�s�qz||jk}|�
|�|r�|s�||jkr�d|_|�|�|�
dtj|df�|jr�|jr�|s�|�|�qz|�|�qzW|j�rZ|j�r2|j�r2|�
|�|�|�|�
|�|�rZ|j�rZ|�dtj�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_usedr]r_�parallel_private_tempsrr�r�r�rrr��fetch_parallel_exceptionrOr	r^rF�save_parallel_varsr�)	r~rU�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}|�d|�tjd7_|��|j}d}x�t|j���D]�\}\}}|rH|jj	rbqH|j�
�}d|}	|j}
|d7}|j��}|r�d|j�
|�}nd}|�d||	|f�|�d	|	|
f�|j�|	|
f�qHW|��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$rgrRrCrcr/r;r>rrSrErOrnrr%)
r~rU�section_namer�Z
temp_countr�rYrOZ	type_decl�
temp_cname�
private_cnamerSrr&r&r'rp]!s*	


z#ParallelStatNode.save_parallel_varscCs�|��|jdd�|�dtj�|�dtj�|�d|j�tt|j	|j
��}d|j_|�dt
|��|�tj�|�d�|��|��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
        T)r�z#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_typerOrarr;�parallel_pos_inforbr�Zuses_error_indicatorr�r�r�r%)r~rUrbr&r&r'ro�!sz)ParallelStatNode.fetch_parallel_exceptioncCsh|��|jdd�|�tj�|�d|j�tt|j	|j
��}|�dt|��|��|�
�dS)zRe-raise a parallel exceptionT)r�z&__Pyx_ErrRestoreWithState(%s, %s, %s);z%s = %s; %s = %s; %s = %s;N)r$r�rIr	rwrOrarr;rbrxr�r�r%)r~rUrbr&r&r'�restore_parallel_exception�!sz+ParallelStatNode.restore_parallel_exceptioncCs|�|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)rr�r�r�)r~rUr&r&r'�restore_labels�!szParallelStatNode.restore_labelsc	Cs�|j}|jr`|�d|j�|�d|j�|�dtj�|�d�|�dtj�|�d�|rl|j}n|j	}|�rx|�dtj�|�dtj�|�dtj�x$|j
D]\}}|�d	||f�q�W|�d
tj�|r�|�d�|�|j
�|�r|�d�|�|j�|�r0|�d
�|�|j�|j�rd|j�t�|�d�|�|�|�|j�|�d�|�d�|��|�|�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)rgr_rOrxrar	rwr^rmr]rnr�r�r�r�r�r�rgrryr�r%�"redef_builtin_expect_apple_gcc_bug)	r~rU�break_�	continue_�return_r�rmrurvr&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�dS)z]
        A bug on OS X Lion disallows __builtin_expect macros. This code avoids them
        N)rLZundef_builtin_expect�redef_condition)r~rUr&r&r'rh"sz3ParallelStatNode.undef_builtin_expect_apple_gcc_bugcCs|js|�|j�dS)N)rLZredef_builtin_expectr�)r~rUr&r&r'r{ "sz3ParallelStatNode.redef_builtin_expect_apple_gcc_bug)N)r&)F)FFF)5rKrkrlrxr�r�rFrdr_rArEr	rwZparallel_exc_valueZparallel_exc_tbraZparallel_filenameZparallel_linenoZparallel_clinenorxrrrrbrsr�r�rprKrNrPrUrXrYrZr[rcrerirkrlrrrproryrzrZbuggy_platform_macro_conditionZhave_expect_conditionr�rhr{rmr&r&)rJr'r@bsZ&G


%
C+.
	
Hr@cs2eZdZdZdgZdZ�fdd�Zdd�Z�ZS)�ParallelWithBlockNodezI
    This node represents a 'with cython.parallel.parallel():' block
    rANcs&tt|��|�|jr"t|jd�dS)Nz=cython.parallel.parallel() does not take positional arguments)rdr�r�rRrr%)r~rA)rJr&r'r�."sz*ParallelWithBlockNode.analyse_declarationscCs$|�|�|�|�|�d�|�d�|jrVdd�|jD�}|�dd�t|���|��|_|�	|�|�d�|�d�|�
�|�|�|�|�|j
��|j�|�|�|�|�|�|�|�|��|�|j�}|�|j�}|�|j�}|�|�|j||||d	�|�|�dS)
Nz#ifdef _OPENMPz#pragma omp parallel cSsg|]}|jjs|j�qSr&)r/r;r)r�rfr&r&r'r�<"szAParallelWithBlockNode.generate_execution_code.<locals>.<listcomp>zprivate(%s)z, r�z#endif /* _OPENMP */)r|r}r~)rZrirOr�rCr�rRr�r\rYr$rkrUr��start_collecting_tempsr�r�rrrcrlr%r�r�r�r�rzrr[)r~rUrCr}r|r~r&r&r'r�4"s8














z-ParallelWithBlockNode.generate_execution_code)	rKrkrlrxrHrAr�r�rmr&r&)rJr'r�%"s
r�cs�eZdZdZddddddgZdZZZZdZ	Z
Zd	ZdZ
dZd
dddgZ�fdd
�Z�fdd�Z�fdd�Zdd�Zdd�Zdd�Z�ZS)�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�rLr�rRrArENT�schedulercs"tt|�j|f|�t|�|_dS)N)rdr�r�r;r�)r~r%rS)rJr&r'r�q"szParallelRangeNode.__init__cs�tt|��|�|j�|�|jdk	r2|j�|�|jrFt|j�dkrVt|j	d�dSt|j�dkrp|j\|_
n0t|j�dkr�|j\|_|_
n|j\|_|_
|_t
|jd�r�|j�d�|_|jdkr�t|j	d|jf�dS)	Nr$z0Invalid number of positional arguments to prangerr"�decode�ascii)N�staticZdynamicZguided�runtimez'Invalid schedule argument to prange: %s)rdr�r�rLrr�rRrHrr%rOrWr�r?r�r�)r~rA)rJr&r'r�v"s 

z&ParallelRangeNode.analyse_declarationscsp|j}|jrd|_|jdkr,t|jd�|S|j�|�|_|jjjsn|jjjsdt|jjd|jj�tj	|_
n$|jj|_
|j
js�t|jjddd�d|_
|j|j|jf}xtt||j
�D]d\}}|dk	r�|�|�|jjs�t|jd|�q�|j�s|�|�}t|||�t�|j
|j�|_
q�W|jdk	�r8|j�|�|_t|jd	��r\|jjdf|j|jj<tt|��|�}|j�r�|j�s�t|jjd
�nR|jdk�r�t|jjd�n6|jjj �r�|jj�r�|j�!|�d
k�r�t|jjd�|j�"tj#|��|�|_|j�r||_|j$�o|j$j%|_&|j&�rl|}x|j$�r@|j$j%�r@|j$}�q$W|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�)rWrOr�z%s argument must be numericr�z$Must provide schedule with chunksizer�z,Chunksize not valid for the schedule runtimerzChunksize must not be negative)+rrLrr%rqr/r]r;r
r��
index_typerXrr7rWrOr�r;ror�rVr�r�r�rpr?rdr�rdr�rEr�r�rIrr=rLrFrdr}rCrDr`)r~rAr"�start_stop_steprTr:rL)rJr&r'rp�"sv







z%ParallelRangeNode.analyse_expressionscCsTd}|j|j|j|jf}x6t||�D](\}}|dk	r$|jjr$t|jd|�q$WdS)N)rWrOr�rLz6%s may not be a Python object as we don't have the GIL)	rWrOr�rLr;r/r;rr%)r~rAr7Znodesr:rTr&r&r'r��"szParallelRangeNode.nogil_checkcCs�|�|�|jjj}||jj��d�}|j|j|jf}d}xTt	||j
|�D]B\}}}|dkrb|}	n"|jrr|��}	n|�
|�|��}	|	||<qJW|j�|jd�|d<|j�|jd�|d<|�d|�|�|�|��|_|�d|�|�d	|�|��|�||�|��|�|�|j�rb|j�r<|�d
tj�|��|�d�|j�|�|��|� |�x8||j!|j"fD]$}
|
dk	�r~|
�#|�|
�$|��q~W|j�%|d�|j�%|d�|�&|�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
        )rLr�)rerer�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 */)'rZrLr�rr/r>rWrOr�r;r7r�r�rDr�r�r�r�rOrir�Zcontrol_flow_var_code_pointr$�
generate_loopr%rzr�r]r�r	r^r�rrErArrKr�r[)r~rUZtarget_index_cname�fmt_dictr��defaultsrTr:r�r�r�r&r&r'r��"sP(










z)ParallelRangeNode.generate_execution_codec
Cs�|jr|�d�n
|�d�|js@|�d�|��|_|jj}nf|�d�|��|_|j}|�d�|�d�|��|�|�|jr�|�d�n
|�d�|�d�x�t	|j
���D]�\}\}}|�r|dk�r||jj
k�r|jjr�t|jd�n|�d	||jf�q�||jj
k�r<|�d
|j�|�d|j�q�|jjs�|�rPd}nd
}|�d||jf�q�W|j�r�|j�r�d|�||j�}nd}|�d|j|f�|�|�|�d�|�d�|�d|�|��|��}	|��|�d|�|j||jj
d�|j�r|j��|j�|�|j|dd�|�|�|j�rZ|	�dtj �|�!�|�!�|j�r�|�"|�|�!�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)rOrTz %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);)rTT)rqzif (%s < 2))#rdrOrMr�r�r\rLr$rkrRrCrcrLr�r/r;rr%rr�rErXrYrUr�r�r�r�rrrcr]r	r^r%rl)
r~rUr�Zreduction_codepointr�rYrOrTrEZguard_around_body_codepointr&r&r'r�c#sv
















zParallelRangeNode.generate_loop)rKrkrlrxr�r�rLr�rRrWrOr�rFrr�rHr�r�rpr�r�r�rmr&r&)rJr'r�["s
Yur�c@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
    rTcCsD|j�|�|j}t|t�r(|jjd}t|t�|_t|tt	f�}|j
}|jr�|j|_|j|_d|_
|jr�d|jkr�|�|j�|_n�|r�|j|_|j_n�|j}|j|_|jd|j_tj|j|j_|jd|j_|jj|jj_|jj|j_|jd|_xD|j��D]6\}}|j�r$|�|j�|_|j�r|�|j�|_�qWdS)NrTr�_objZ_typerw)rTr�rIr�r�r�r��is_functionr�r�r�rrYr2r�r/r/r+rr	Ztypeobj_prefixrr�rrFr�r�rc)r~rArTZis_struct_or_enumrfr+r:r�r&r&r'r��#s8
z'CnameDecoratorNode.analyse_declarationscCs$d|kr|�d�d}d|j|fS)Nrr�z%s_%s)rr)r~rr&r&r'r/�#szCnameDecoratorNode.manglecCs|j�|�|_|S)N)rTrp)r~rAr&r&r'rp$sz&CnameDecoratorNode.analyse_expressionscCs�|jrt|jrt|jd}t|jt�r6|jj|ddd�n>ddlm}|jj	}|j
}|j|_
|j||�
�|dd�||_
|j�||�dS)	z=Ensure a prototype for every @cname method in the right placeZutility_code_protoFT)r�r�r)�
ModuleNode)Z
definitionN)r�rr�rIrTrEr�r�r�r�rrYZgenerate_cfunction_declarationr�r�)r~rArUZh_coder�r�rr&r&r'r�$s 
z0CnameDecoratorNode.generate_function_definitionscCs|j�|�dS)N)rTr�)r~rUr&r&r'r�$sz*CnameDecoratorNode.generate_execution_codeN)
rKrkrlrxr�r�r/rpr�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.cZPrintOnerzExceptions.cZRaiseExceptionZGetExceptionZ
SwapExceptionZSaveResetExceptionZAddTracebackzR
static PyObject *__Pyx_GetExceptionTuple(PyThreadState *__pyx_tstate); /*proto*/
a1
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;
}
)r��impl�requires)N)��
__future__rr5r�rWrrr�	itertoolsrr�rZErrorsrrrrr	r
rrrZSymtabrr
rrrrrrarZStringEncodingrrrrZPythranrr r!ZUtilsr#rsr1rr2r(r.rEr\r/r]rnrzr{r�r�r�r�r�r�r�r�r�r�r�r5r8rUr?rVr_rir@rvrxrzr}r�r�r�r�r�r�r�r�rirDrErzr�r�r�r�r�rIrrrr5r6r8r�rArErhrBrqryr�r�r;r�r�r�rOr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr&r'r0Zutility_code_for_importsr*r3r:r?r@r�r�r�Zgcc_branch_hintsZbranch_prediction_macrosr�r|r~rr�rrr�Ztraceback_utility_codeZ get_exception_tuple_utility_coder&r&r&r'�<module>sz


$
/
(*
%#,@
s	^!Q'
j-X1!
082EC@2f/GC)7
rh=C))<\<
a
pg!*%	H	+O\H6lc