o
    f=                     @   s@  d dl mZ d dlZd dlmZ ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 dd	lmZ d
d Zdd Zdd Zdd Zdd Zdd Zd4ddZdd Zdd Zdd Zd5dd Zd6d"d#Zd$d% Zd&d' Zd(d) Zd7d*d+Zi Zzd d,lmZ W n e y   G d-d. d.e!ZY nw e Z"d/d0 Z#d8d2d3Z$dS )9    )absolute_importN)time   )Errors)
DebugFlags)Options)CompileErrorInternalError
AbortError)Namingc                 C   s   t |   | S N)printdump)t r   T/var/www/html/mig_web/myenv/lib/python3.10/site-packages/Cython/Compiler/Pipeline.pydumptree   s   r   c                 C   s   t  dkr
td| S )Nr   zpipeline break)r   get_errors_countr
   )noder   r   r   abort_on_errors   s   r   c                        fdd}|S )Nc                    sb   | j }| j}|ddf}tjd}t_ j||dd}|t_ j||d|d}| |_||_d|_|S )Nr   r   F)posneed_pxdpxdfull_module_name)	source_descr   r   cimport_from_pyxfind_moduleparsecompilation_sourcescopeis_pxd)compsrcr   r   initial_possaved_cimport_from_pyxr!   treecontextr   r   r      s   
z"parse_stage_factory.<locals>.parser   )r(   r   r   r'   r   parse_stage_factory   s   r)   c                    s    fdd}|S )Nc                    s"    j | dd}|_d|_|S )NTr   )r   r!   r"   )r   r&   r(   module_namer!   r   r   r   *   s   
z&parse_pxd_stage_factory.<locals>.parser   )r(   r!   r+   r   r   r*   r   parse_pxd_stage_factory)   s   r,   c                    s    fdd}|S )Nc                    s   |    | j_S r   )process_implementationr    )module_nodeoptionsresultr   r   generate_pyx_code_stage3   s   z@generate_pyx_code_stage_factory.<locals>.generate_pyx_code_stager   )r0   r1   r2   r   r/   r   generate_pyx_code_stage_factory2   s   r3   c                    r   )Nc                    s,    j  D ]\}\}}| j||dd q| S )Nr   )stage)pxdsitemsmerge_in)r.   namestatlistnoder!   r'   r   r   inject_pxd_code_stage;   s   z<inject_pxd_code_stage_factory.<locals>.inject_pxd_code_stager   )r(   r:   r   r'   r   inject_pxd_code_stage_factory:   s   r;   c                 C   s|   |d u rt  }| j D ]/}||v rq|| |jr1|jr1||j |jjD ]}|| q(q|jr;t	|j|| qd S r   )
setentriesvaluesaddusedutility_code_definitionuse_utility_coderequires	as_moduleuse_utility_code_definitions)r!   targetseenentryrequired_utilityr   r   r   rE   B   s   
rE   c                    s8   i j  fdd | D ]} | qtdS )Nc                    sZ   | }|d u r+d| < t }d| jr t fdd| jD nd |d   }| < |S )Nr   r   c                    s   g | ]} |qS r   r   .0dep)calculate_rankr   r   
<listcomp>]   s    zIsorted_utility_codes_and_deps.<locals>.calculate_rank.<locals>.<listcomp>g:0yE>)lenrC   min)utilcoderankoriginal_orderrM   get_rankranksr   r   rM   W   s    z5sorted_utility_codes_and_deps.<locals>.calculate_rank)key)getsorted	utilcodesrR   r   rU   r   sorted_utility_codes_and_depsS   s   

r]   c                    s6   dd | D  | D ]} fdd|j pdD |_ q	d S )Nc                 S   s   i | ]}||qS r   r   )rK   rR   r   r   r   
<dictcomp>i   s    z"normalize_deps.<locals>.<dictcomp>c                    s   g | ]}  ||qS r   )
setdefaultrJ   depsr   r   rN   k   s    z"normalize_deps.<locals>.<listcomp>r   )rC   r[   r   r`   r   normalize_depsh   s   rb   c                    r   )Nc                    s   |    t j| j | jj}t||d d < t| t }|D ]3}||v r'q || |j	r>|j	D ]}||vr=|
| q2|j jd}|rS| j| |jddd q | S )N)cython_scopeutilityT)r4   merge_scope)prepare_utility_coderE   rc   r!   utility_code_listr]   rb   r<   r?   rC   appendget_treer7   with_compiler_directives)r.   rg   addedrR   rL   r&   r'   r   r   inject_utility_code_stageo   s.   



zDinject_utility_code_stage_factory.<locals>.inject_utility_code_stager   )r(   rl   r   r'   r   !inject_utility_code_stage_factoryn   s   rm   r   c           2         s  |dv sJ ddl m} ddlm}m}m}m} ddlm}m}	m	}
 ddlm
}m} ddlm}m}m} ddlm}m}m} dd	lm}m} dd
lm} ddlm}m} ddlm}m}m} ddlm}m}m} ddlm} ddl m!} ddl"m#}  ddl$m%}!m&}"m'}# ddl$m(}$m)}% ddl$m*}& ddl$m+}'m,}( ddl$m-}) ddl$m.}* ddl/m0}+ ddl1m2},m3}- |dkr|-}.|| }/n|,}.d }/|dkr|| }0nd }0g || || |/| || | j4|| | || |0|| || || |' |! || || |	 |
| || | | |$| || || || || || |+| |.|&| || || || |#| |"| |%| || || |*| |) |(| || | }1 ry fdd|1D }1|1S )N)pyxpyr   r   	PrintTree)WithTransformNormalizeTree	PostParsePxdPostParse)ForwardDeclareTypesInjectGilHandlingAnalyseDeclarationsTransform)AnalyseExpressionsTransformFindInvalidUseOfFusedTypes)CreateClosureClassesMarkClosureVisitorDecoratorTransform)TrackNumpyAttributesInterpretCompilerDirectivesTransformBuiltinMethods)ExpandInplaceOperatorsParallelRangeTransform) CalculateQualifiedNamesTransform)MarkParallelAssignmentsMarkOverflowingArithmetic)AdjustDefByDirectivesAlignFunctionDefinitionsAutoCpdefFunctionDefinitions)RemoveUnreachableCodeGilCheckCoerceCppTemps)ControlFlowAnalysis)AutoTestDictTransform)EmbedSignature)FlattenInListTransformSwitchTransformIterationTransform)EarlyReplaceBuiltinCallsOptimizeBuiltinCalls)InlineDefNodeCalls)ConstantFoldingFinalOptimizePhase)DropRefcountingTransform)ConsolidateOverflowCheck)IntroduceBufferAuxiliaryVars)check_c_declarationscheck_c_declarations_pxdr   ro   c                    s   g | ]	}|j  vr|qS r   )	__class__)rK   sexclude_classesr   r   rN      s    z#create_pipeline.<locals>.<listcomp>)5Visitorrq   ParseTreeTransformsrr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   TypeInferencer   r   r   r   r   r   r   r   FlowControlr   AnalysedTreeTransformsr   AutoDocTransformsr   Optimizer   r   r   r   r   r   r   r   r   r   Bufferr   
ModuleNoder   r   compiler_directives)2r(   moder   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _check_c_declarations_specific_post_parse_align_function_definitionsstagesr   r   r   create_pipeline   s   


	
 !"#$%&'()*+-r   Fc                 C   s   |rdnd}g }g }|j r"ddlm} | }	||	 ||	  |jr@ddlm}
 ddlm	} |

|j| _|| ||g}ng }ttt| gt| ||d|t| t| tg|t||g|S )	Nro   rn      )TreeAssertVisitor)DebugWriterr   )DebugTransformr   )evaluate_tree_assertions	TestUtilsr   rh   create_c_file_validator	gdb_debugDebuggerr   r   r   CythonDebugWriter
output_dirgdb_debug_outputwriterlist	itertoolschainr)   r   r;   rm   r   r3   )r(   r0   r1   ro   r   r   test_supportctest_supportr   test_validatorr   r   debug_transformr   r   r   create_pyx_pipeline   s8   

r   c                 C   s,   ddl m} t| ||gt| d | g S )Nr   )ExtractPxdCoder   )CodeGenerationr   r,   r   )r(   r!   r+   r   r   r   r   create_pxd_pipeline
  s   
r   c                 C   s   t | ||ddS )NT)ro   )r   )r(   r0   r1   r   r   r   create_py_pipeline  s   r   c              	      s   ddl m}m}m}m} ddlm}m} ddlm	  g }t
| | j||||||gd}	ddlm G fddd}
|	D ]}|| t||rQ|d	|
   nq< fd
d}|| |S )Nr   )r   r|   rr   rx   )r   r   StatListNoder   VisitorTransformc                       s   e Zd Zdd Z jZdS )z5create_pyx_as_pxd_pipeline.<locals>.SetInPxdTransformc                 S   s   t |drd|_| | |S )Nin_pxdT)hasattrr   visitchildren)selfr   r   r   r   visit_StatNode,  s   

zDcreate_pyx_as_pxd_pipeline.<locals>.SetInPxdTransform.visit_StatNodeN)__name__
__module____qualname__r   recurse_to_children
visit_Noder   r   r   r   SetInPxdTransform'  s    
r   rO   c                    s\   | j j D ]}|js#d|_|j|jkr#|jdkr#|j t	j
|j|_q | jg d| j fS )Nr   extern)stats)r!   r=   r>   in_cincludedefined_in_pxdr8   cname
visibilitymangler   func_prefixr   )rootrH   r   r   r   fake_pxd9  s   z,create_pyx_as_pxd_pipeline.<locals>.fake_pxd)r   r   r|   rr   rx   r   r   r   Nodesr   r   r0   r   r   rh   
isinstanceinsert)r(   r1   r   r|   rr   rx   r   r   pipelinepyx_pipeliner   r4   r   r   )r   r   r   create_pyx_as_pxd_pipeline  s.   



r   c                 C   s^   |s|sJ |p	|}t | D ]\}}t||r nq|r |d7 }| d| |g | |d  S )z
    Insert a new transform into the pipeline after or before an instance of
    the given class. e.g.

        pipeline = insert_into_pipeline(pipeline, transform,
                                        after=AnalyseDeclarationsTransform)
    r   N)	enumerater   )r   	transformbeforeafterclsir   r   r   r   insert_into_pipelineD  s   
r   )localc                   @   s   e Zd ZdS )_threadlocalN)r   r   r   r   r   r   r   r   a  s    r   c                   C   s    zt jW S  ty   i  Y S w r   )threadlocalcython_pipeline_timingsAttributeErrorr   r   r   r   get_timingsf  s
   r   Tc              
   C   s  ddl m} tjrt  nd }ztj}W n ty#   i  }t_Y nw dd }d }|}zz| D ]z}	|	d u r7q0|s?t	|	|r?q0t
|	dt|	j}
tjrptd|	  zt|
 }W n tyo   td|
 | ||
  }t|
< Y nw t }||	|}t | }z||
 \}}W n ty   d\}}Y nw |t|d	  |d f||
< tjrtd
|  q0W n ty } ztj|dd |}W Y d }~n
d }~ww W ||fS W ||fS  ty } zt dkr |}W Y d }~||fS d }~w ty } z|}W Y d }~||fS d }~ww )Nr   rp   c                 S   s   | |S r   r   )phasedatar   r   r   runv  s   zrun_pipeline.<locals>.runr   zEntering pipeline phase %rz'def %s(phase, data): return phase(data))r   r   i@B z    %.3f secondsF)	use_stackr   )r   rq   r   debug_verbose_pipelineglobalscopyr   r   r   r   getattrtyper   r   _pipeline_entry_pointsKeyErrorexecr   intr   r   report_errorr	   r   r
   )r   source	printtreerq   exec_nstimingsr   errorr   r   
phase_namer   old_tcounterrr   r   r   run_pipelinem  sv   


&r  r   )r   )Fr   )NN)T)%
__future__r   r   r    r   r   r   r   r	   r
   r   r   r   r)   r,   r3   r;   rE   r]   rb   rm   r   r   r   r   r   r   r   	threadingr   r   ImportErrorobjectr   r   r  r   r   r   r   <module>   sB    	

"
Z 
,