o
    wif?                     @   sD  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dd Zd7ddZdd Zdd Zd8ddZd9d!d"Zd:d$d%Zd&d' Zd(d) Zd*d+ Zd;d,d-Zzd d.lmZ W n ey   G d/d0 d0ZY nw e Zd1d2 Z i Z!d3d4 Z"d8d5d6Z#dS )<    N)time   )Errors)
DebugFlags)Options)CompileErrorInternalError
AbortError)Namingc                 C   s   t |   | S N)printdump)t r   U/home/ubuntu/sommelier/.venv/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                        fdd}|S )Nc                    s   |    | j_S r   )process_implementationr   )module_nodeoptionsresultr   r   generate_pyx_code_stage1   s   z@generate_pyx_code_stage_factory.<locals>.generate_pyx_code_stager   )r0   r1   r2   r   r/   r   generate_pyx_code_stage_factory0   s   r3   c                    r   )Nc                    s(    j  D ]\}}| j||dd q| S Nr   stage)utility_pxdsvaluesmerge_in)r.   statlistnoder    r&   r   r   inject_utility_pxd_code_stage9   s   zLinject_utility_pxd_code_stage_factory.<locals>.inject_utility_pxd_code_stager   )r'   r;   r   r&   r   %inject_utility_pxd_code_stage_factory7   s   r<   c                    r   )Nc                    s,    j  D ]\}\}}| j||dd q| S r4   )pxdsitemsr9   )r.   namer:   r    r&   r   r   inject_pxd_code_stageB   s   z<inject_pxd_code_stage_factory.<locals>.inject_pxd_code_stager   )r'   r@   r   r&   r   inject_pxd_code_stage_factory@   s   rA   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entriesr8   addusedutility_code_definitionuse_utility_coderequires	as_moduleuse_utility_code_definitions)r    targetseenentryrequired_utilityr   r   r   rJ   I   s   
rJ   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>d   s    zIsorted_utility_codes_and_deps.<locals>.calculate_rank.<locals>.<listcomp>g:0yE>)lenrH   min)utilcoderankoriginal_orderrR   get_rankranksr   r   rR   ^   s    z5sorted_utility_codes_and_deps.<locals>.calculate_rank)key)getsorted	utilcodesrW   r   rZ   r   sorted_utility_codes_and_depsZ   s   

rb   c                    s6   dd | D  | D ]} fdd|j pdD |_ q	d S )Nc                 S   s   i | ]}||qS r   r   )rP   rW   r   r   r   
<dictcomp>p   s    z"normalize_deps.<locals>.<dictcomp>c                    s   g | ]}  ||qS r   )
setdefaultrO   depsr   r   rS   r   s    z"normalize_deps.<locals>.<listcomp>r   )rH   r`   r   re   r   normalize_depso   s   rg   Tc                    r,   )Nc                    s   |    t j| j | j}|j}t||d d < t| t }|D ]J}||v r)q"|| |j	r@|j	D ]}||vr?|
| q4|j jd }r]| j| |jdd | j|jd q"|j jd }rl|j
| q"| S )N)cython_scopeutilityr5   )internalise_c_class_entries)prepare_utility_coderJ   rh   r    utility_code_listrb   rg   rB   rD   rH   appendget_treer9   with_compiler_directivesmerge_scopeget_shared_library_scopecimported_modules)r.   module_scoperl   addedrW   rQ   r%   shared_library_scoper'   rj   r   r   inject_utility_code_stagev   s2   



zDinject_utility_code_stage_factory.<locals>.inject_utility_code_stager   )r'   rj   rw   r   rv   r   !inject_utility_code_stage_factoryu   s   rx   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 rx 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   rz   c                    s   g | ]	}|j  vr|qS r   )	__class__)rP   sexclude_classesr   r   rS      s    z#create_pipeline.<locals>.<listcomp>)5Visitorr|   ParseTreeTransformsr}   r~   r   r   r   r   r   r   r   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   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   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| tg|t||g|S )	Nrz   ry      )TreeAssertVisitor)DebugWriterr   )DebugTransformr   )evaluate_tree_assertions	TestUtilsr   rm   create_c_file_validator	gdb_debugDebuggerr   r   r   CythonDebugWriter
output_dirgdb_debug_outputwriterlist	itertoolschainr(   r   rA   rx   r<   r   r3   )r'   r0   r1   rz   r   r   test_supportctest_supportr   test_validatorr   r   debug_transformr   r   r   create_pyx_pipeline   s:   

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)rz   )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   r}   r   )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_StatNode8  s   

zDcreate_pyx_as_pxd_pipeline.<locals>.SetInPxdTransform.visit_StatNodeN)__name__
__module____qualname__r   recurse_to_children
visit_Noder   r   r   r   SetInPxdTransform3  s    
r   rT   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    rC   r8   in_cincludedefined_in_pxdr?   cname
visibilitymangler
   func_prefixr   )rootrM   r   r   r   fake_pxdE  s   z,create_pyx_as_pxd_pipeline.<locals>.fake_pxd)r   r   r   r}   r   r   r   r   Nodesr   r   r0   r   r   rm   
isinstanceinsert)r'   r1   r   r   r}   r   r   r   pipelinepyx_pipeliner   r6   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_pipelineP  s   
r   )localc                   @   s   e Zd ZdS )_threadlocalN)r   r   r   r   r   r   r   r   k  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_timingsp  s
   r   c                 C   s>   zt |  W S  ty   Y nw dd }|  |_|_|t | < |S )Nc                 S      | |S r   r   phasedatar   r   r   run     z%_make_debug_phase_runner.<locals>.run)_pipeline_entry_pointsKeyErrorr   r   )
phase_namer  r   r   r   _make_debug_phase_runnery  s   
r  c              
   C   s  ddl m} ztj}W n ty   i  }t_Y nw dd }d }|}zzd| D ]_}|d u r-q&|s5t||r5q&t|dt|j}	t	j
rKtd|  t|	}t }
|||}t |
 }
z||	 \}}W n tym   d\}}Y nw |t|
d  |d f||	< t	j
rtd	|
  q&W 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   r{   c                 S   r   r   r   r   r   r   r   r    r  zrun_pipeline.<locals>.runr   zEntering pipeline phase %r)r   r   i@B z    %.3f secondsF)	use_stackr   )r   r|   r   r   r   r   getattrtyper   r   debug_verbose_pipeliner   r  r   r  intr   r   report_errorr   r   r	   )r   source	printtreer|   timingsr  errorr  r  r  r   old_tcounterrr   r   r   run_pipeline  sj   


!r  r   )T)r   )Fr   )NN)$r   r    r   r   r   r   r   r	   r
   r   r   r(   r+   r3   r<   rA   rJ   rb   rg   rx   r   r   r   r   r   r   	threadingr   r   ImportErrorr   r   r  r  r  r   r   r   r   <module>   sD    		
	

$
Z#
,