
    AeQ                         d dl mZ d dlZ ej                  dci dedededededed	ed
edededededededede d dlZd dlZd dlZddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZmZ ddlmZmZ ddlmZ ddlmZmZ dd lmZmZmZm Z  dd!l!m"Z"  G d" d#e      Z# G d$ d%e      Z$ G d& d'e      Z%d(Z&d)Z'd*Z( G d+ d,e      Z)d- Z*d. Z+d/ Z,d0 Z-d1 Z. G d2 d3ee#      Z/ G d4 d5ee#      Z0 G d6 d7e      Z1 G d8 d9ee#      Z2 G d: d;ee#      Z3 G d< d=ee#      Z4 G d> d?ee#      Z5 G d@ dAe      Z6 G dB dCe      Z7dD Z8 G dE dFe      Z9 G dG dHe      Z: G dI dJe      Z; G dK dLe      Z< G dM dNee#      Z= G dO dPe      Z> G dQ dRe      Z? G dS dTe      Z@ G dU dVe      ZA G dW dXe      ZB G dY dZee#      ZC G d[ d\e      ZD G d] d^e      ZE G d_ d`e      ZF G da dbe      ZGy)d    )absolute_importN
PyrexTypesNaming	ExprNodesNodesOptions	UtilNodesLetNode
LetRefNodeTreeFragmentEncodedStringerrorwarningcopyhashlibsys_unicode   )r   )r   )r   )r   )r   )Builtin)Errors)VisitorTransformTreeVisitor)CythonTransformEnvTransformScopeTrackingTransform)r
   r   )r   )r   r   )r   r   CompileErrorInternalError)UtilityCodec                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	SkipDeclarationsa)  
    Variable and function declarations can often have a deep tree structure,
    and yet most transformations don't need to descend to this depth.

    Declaration nodes are removed after AnalyseDeclarationsTransform, so there
    is no need to use this for transformations after that point.
    c                     |S N selfnodes     Clib/python3.12/site-packages/Cython/Compiler/ParseTreeTransforms.pyvisit_CTypeDefNodez#SkipDeclarations.visit_CTypeDefNode'           c                     |S r"   r#   r$   s     r'   visit_CVarDefNodez"SkipDeclarations.visit_CVarDefNode*   r)   r*   c                     |S r"   r#   r$   s     r'   visit_CDeclaratorNodez&SkipDeclarations.visit_CDeclaratorNode-   r)   r*   c                     |S r"   r#   r$   s     r'   visit_CBaseTypeNodez$SkipDeclarations.visit_CBaseTypeNode0   r)   r*   c                     |S r"   r#   r$   s     r'   visit_CEnumDefNodez#SkipDeclarations.visit_CEnumDefNode3   r)   r*   c                     |S r"   r#   r$   s     r'   visit_CStructOrUnionDefNodez,SkipDeclarations.visit_CStructOrUnionDefNode6   r)   r*   N)
__name__
__module____qualname____doc__r(   r,   r.   r0   r2   r4   r#   r*   r'   r    r       s%    r*   r    c                   Z     e Zd ZdZ fdZd ZddZd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )NormalizeTreea\  
    This transform fixes up a few things after parsing
    in order to make the parse tree more suitable for
    transforms.

    a) After parsing, blocks with only one statement will
    be represented by that statement, not by a StatListNode.
    When doing transforms this is annoying and inconsistent,
    as one cannot in general remove a statement in a consistent
    way and so on. This transform wraps any single statements
    in a StatListNode containing a single statement.

    b) The PassStatNode is a noop and serves no purpose beyond
    plugging such one-statement blocks; i.e., once parsed a
`    "pass" can just as well be represented using an empty
    StatListNode. This means less special cases to worry about
    in subsequent transforms (one always checks to see if a
    StatListNode has no children to see if the block is empty).
    c                 H    t         t        |   |       d| _        d| _        y NF)superr:   __init__is_in_statlist
is_in_exprr%   context	__class__s     r'   r>   zNormalizeTree.__init__O   s!    mT+G4#r*   c                 \    | j                   }d| _         | j                  |       || _         |S NT)r@   visitchildren)r%   r&   stacktmps      r'   visit_ExprNodezNormalizeTree.visit_ExprNodeT   s,    ??4 "r*   c                     | j                   }|| _         | j                  |       || _         | j                   s.| j                  s"t        j                  |j
                  |g      S |S )Nposstats)r?   rF   r@   r   StatListNoderK   )r%   r&   is_listcontainerrG   s       r'   visit_StatNodezNormalizeTree.visit_StatNode[   sX    &&.4 &""4??%%$((4&AAKr*   c                 D    d| _         | j                  |       d| _         |S NTF)r?   rF   r$   s     r'   visit_StatListNodez NormalizeTree.visit_StatListNodee   s%    "4 #r*   c                 &    | j                  |d      S rE   rO   r$   s     r'   visit_ParallelAssignmentNodez*NormalizeTree.visit_ParallelAssignmentNodek       ""4..r*   c                 &    | j                  |d      S rE   rT   r$   s     r'   r2   z NormalizeTree.visit_CEnumDefNoden   rV   r*   c                 &    | j                  |d      S rE   rT   r$   s     r'   r4   z)NormalizeTree.visit_CStructOrUnionDefNodeq   rV   r*   c                 `    | j                   s!t        j                  |j                  g       S g S )zEliminate PassStatNoderJ   )r?   r   rM   rK   r$   s     r'   visit_PassStatNodez NormalizeTree.visit_PassStatNodet   s'    ""%%$(("==Ir*   c                 r    |j                   j                  r| j                  |      S | j                  |      S )z!Eliminate useless string literals)expris_string_literalrZ   rO   r$   s     r'   visit_ExprStatNodez NormalizeTree.visit_ExprStatNode{   s1    99&&**400&&t,,r*   c                     |S r"   r#   r$   s     r'   r.   z#NormalizeTree.visit_CDeclaratorNode   r)   r*   F)r5   r6   r7   r8   r>   rH   rO   rR   rU   r2   r4   rZ   r^   r.   __classcell__rC   s   @r'   r:   r:   :   s:    ( 
///-r*   r:   c                       e Zd Zy)PostParseErrorN)r5   r6   r7   r#   r*   r'   rd   rd      s    r*   rd   zHCannot assign default value to fields in cdef classes, structs or unionsz0Invalid buffer defaults specification (see docs)z0Special attributes must not have a type declaredc                   d     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )	PostParseaf  
    Basic interpretation of the parse tree, as well as validity
    checking that can be done on a very basic level on the parse
    tree (while still not being a problem with the basic syntax,
    as such).

    Specifically:
    - Default values to cdef assignments are turned into single
    assignments following the declaration (everywhere but in class
    bodies, where they raise a compile error)

    - Interpret some node structures into Python runtime values.
    Some nodes take compile-time arguments (currently:
    TemplatedTypeNode[args] and __cythonbufferdefaults__ = {args}),
    which should be interpreted. This happens in a general way
    and other steps should be taken to ensure validity.

    Type arguments cannot be interpreted in this way.

    - For __cythonbufferdefaults__ the arguments are checked for
    validity.

    TemplatedTypeNode has its directives interpreted:
    Any first positional argument goes into the "dtype" attribute,
    any "ndim" keyword argument goes into the "ndim" attribute and
    so on. Also it is checked that the directive combination is valid.
    - __cythonbufferdefaults__ attributes are parsed and put into the
    type information.

    Note: Currently Parsing.py does a lot of interpretation and
    reorganization that can be refactored into this transform
    if a more pure Abstract Syntax Tree is wanted.
    c                 R    t         t        |   |       d| j                  i| _        y )N__cythonbufferdefaults__)r=   rf   r>   handle_bufferdefaultsspecialattribute_handlersrA   s     r'   r>   zPostParse.__init__   s'    i'0&)C)C*
&r*   c           	      f   t               }|j                  |j                         |j                  s0|j                  s$t        |j                  t        j                        r6t        j                  |j                  j                  |j                        }n5t        j                  |j                  j                  |j                        }t        j                  |j                  |j                  |j                  |j                  |j                   |d       |_        | j                  |       |S )Nr\   value)nameargsstar_argstarstar_argbodydoc)YieldNodeCollectorrF   result_expr	has_yield	has_await
isinstancer   YieldExprNoder   ExprStatNoderK   ReturnStatNodeDefNodero   rp   rq   rr   def_node)r%   r&   	collectorrs   s       r'   visit_LambdaNodezPostParse.visit_LambdaNode   s    &(	 0 01)"5"5DDTDTV_VmVm9n%%  $$4+;+;=D ''  $$D,<,<>DHH499T]]**4	!
 	4 r*   c           
         t               }|j                  |j                         t        j                  |j
                  |j                  d g d d |j                  |j                        |_        | j                  |       |S )N)ro   rt   rp   rq   rr   rs   is_async_def)	ru   rF   loopr   r}   rK   ro   rx   r~   r%   r&   r   s      r'   visit_GeneratorExpressionNodez'PostParse.visit_GeneratorExpressionNode   sf    &(			*HH499$d)<)<> 	4 r*   c                     |j                   s8t               }|j                  |j                         |j                  rd|_         | j                  |       |S rE   )has_local_scoperu   rF   r   rx   r   s      r'   visit_ComprehensionNodez!PostParse.visit_ComprehensionNode   sI    ##*,I##DII.""'+$4 r*   c                     t        |j                  t        j                        st	        |j
                  t              |j                  | j                  _        |j
                  | j                  _	        y r"   )
ry   defaultr   DictNoderd   rK   ERR_BUF_DEFAULTS
scope_nodebuffer_defaults_nodebuffer_defaults_pos)r%   decls     r'   ri   zPostParse.handle_bufferdefaults   sH    $,,	(:(:; +;<</3||,.2hh+r*   c           
         	 | j                  |       |g}g }|j                  D ]  }|}t        |t        j                        r'|j
                  }t        |t        j                        r't        |t        j                        r"|j                  | j                  dv rt        | j                  t        j                        rN| j                  j                  |j                        }|r'||urt        |j                  t                ||       t        |j                  t"              | j                  dk7  }|j%                  t        j&                  |j                  t)        j*                  |j                  |j                        |j                  |             d |_        |j%                  |        ||_        |S # t        $ r%}| j,                  j/                  |       Y d }~y d }~ww xY w)N)cclasspyclassstructmodulero   )lhsrhsfirst)rF   declaratorsry   r   CPtrDeclaratorNodebaseCNameDeclaratorNoder   
scope_typer   CClassDefNoderj   getro   rd   rK   ERR_INVALID_SPECIALATTR_TYPEERR_CDEF_INCLASSappendSingleAssignmentNoder   NameNoderB   nonfatal_error)	r%   r&   rL   newdeclsr   declbasehandlerfirst_assignmentes	            r'   r,   zPostParse.visit_CVarDefNode   s   
	t$FEH(( & 5+C+CD'}}H !5+C+CDh(A(AB''3??.MM)$//5;N;NO*.*H*H*L*LTYY*W#*'+8';.<TXXGc.d(d$+DM$,"0;K"LL+/??h+F(U%?%? ) 2 2488(-- P ( 0 08H&J K ,0(%)&*  (DL 	 LL''*		s   A(G +EG 	G3G..G3c                 t    | j                  |       | j                  ||j                  |j                  g      S r"   )rF   _visit_assignment_noder   r   r$   s     r'   visit_SingleAssignmentNodez$PostParse.visit_SingleAssignmentNode  s0    4 **4$((DHH1EFFr*   c                 x    | j                  |       | j                  ||j                  |j                  gz         S r"   )rF   r   lhs_listr   r$   s     r'   visit_CascadedAssignmentNodez&PostParse.visit_CascadedAssignmentNode  s2    4 **4$((1KLLr*   c                    t        |D cg c]  }|j                  s|j                  rd c}      dk  r|S g }t        ||       g }t	        ||       g }|D ]s  }|dd }|d   }t        |      dk(  r&t        j                  |j                  |d   |      }n"t        j                  |j                  ||      }|j                  |       u t        |      dk(  r|d   }	n$t        j                  |d   j                  |      }	|rB|D 
cg c]  }
|
j                  |
f }}
t        |       |ddd   D ]  \  }}t        ||	      }	 |	S c c}w c c}
w )	zgFlatten parallel assignments into separate single
        assignments or cascaded assignments.
        r      Nr   r   r   )r   r   rL   )sumis_sequence_constructorr]   flatten_parallel_assignmentseliminate_rhs_duplicateslenr   r   rK   CascadedAssignmentNoder   ParallelAssignmentNode
expressionsort_common_subsequencesr
   )r%   r&   	expr_listr\   expr_list_list	temp_refsnodesr   r   assign_nodetempduplicates_and_temps_temp_refs                 r'   r   z PostParse._visit_assignment_node  s}    y Mt00D4J4J  M NPQR K$Y?	 ;' 		I "~HB-C8}!11#''"1+S2 33CGG's4LL		 u:?(K66uQx||USK1:$=)- '+oot%< $=  $=$%9:3DbD9 =8%h<= EM8$=s   !EEc                     |j                   D ]2  }|j                  r| j                  ||       "|j                  |       4 |S r"   )rp   r   _flatten_sequencer   )r%   seqresultargs       r'   r   zPostParse._flatten_sequence@  sA    88 	#C**&&sF3c"		#
 r*   c                 V    | j                  |       | j                  |g       |_        |S r"   )rF   r   rp   r$   s     r'   visit_DelStatNodezPostParse.visit_DelStatNodeH  s)    4 **44	r*   c                    |j                   rt        j                  |j                  t	        j
                  |j                  j                  |j                  j                        gd      }t        j                  |j                  t        j                  |j                  |j                  t        j                  |j                  |g            g      |_
        | j                  |       |S )Nr   T)rp   ignore_nonexistingr   )rs   finally_clause)is_except_asr   DelStatNoderK   r   r   targetro   rM   TryFinallyStatNoders   rF   )r%   r&   
del_targets      r'   visit_ExceptClauseNodez PostParse.visit_ExceptClauseNodeM  s    **((KKOO$++*:*:< =#'	)J
 **//HH#(#5#5)l$,- ./DI 	4 r*   )r5   r6   r7   r8   r>   r   r   r   ri   r,   r   r   r   r   r   r   ra   rb   s   @r'   rf   rf      sI     D
$	7$ZGM&P
r*   rf   c                 
   t               i fd| D ]  }|d   } |        syfdD ]3  }|j                  st        t        |j                              |_        5 | D ]  } |d         |d<    y)zReplace rhs items by LetRefNodes if they appear more than once.
    Creates a sequence of LetRefNodes that set up the required temps
    and appends them to ref_node_sequence.  The input list is modified
    in-place.
    c                     | j                   s| j                  ry | v r'| vr"t        |       }|| <   j                  |       y y j	                  |        | j
                  r| j                  D ]
  } |        y y r"   )
is_literalis_namer   r   addr   rp   )r&   ref_nodeitemfind_duplicatesref_node_sequence	ref_nodes
seen_nodess      r'   r   z1eliminate_rhs_duplicates.<locals>.find_duplicatesi  s    ??dll :9$%d+"*	$!((2 %
 NN4 ++ II *D#D)* ,r*   r   Nc                 z    | v r|    S | j                   r$t        t        | j                              | _        | S r"   )r   listmaprp   )r&   r   substitute_nodess    r'   r   z2eliminate_rhs_duplicates.<locals>.substitute_nodes  s;    9T?"))S!1499=>DIr*   )setr   r   r   rp   )	r   r   r   r   r&   r   r   r   r   s	    `   @@@@r'   r   r   a  s     JI*  $ 	m   ?''S!1499=>DI?
 $ 8	(27	"8r*   c                     fdfd}t        |       D ][  \  }}|d   }|}t        |dz
  dd      D ]  } ||| |   d         s|} ||k7  s:t        ||d      D ]  }| |dz
     | |<    || |<   ] y)a  Sort items/subsequences so that all items and subsequences that
    an item contains appear before the item itself.  This is needed
    because each rhs item must only be evaluated once, so its value
    must be evaluated first and then reused when packing sequences
    that contain it.

    This implies a partial order, and the sort must be stable to
    preserve the original order as much as possible, so we use a
    simple insertion sort (which is very fast for short sequences, the
    normal case in practice).
    c                 d    | D ]*  }||u r y|j                   s |j                  |      s* y yrQ   r   rp   )r   xr   containss      r'   r   z*sort_common_subsequences.<locals>.contains  s:     	Dqy--(499a2H		
 r*   c                 F    |j                   xr  |j                  |       S r"   r   )abr   s     r'   
lower_thanz,sort_common_subsequences.<locals>.lower_than  s    ((@Xaffa-@@r*   r   r   r   N)	enumeraterange)itemsr   rK   r   keynew_posir   s          @r'   r   r     s    A u% 	"	T1gs1ub"% 	A#uQx{+	 c>3, & 1:a&!E'N	"r*   c                     g }| j                   }| j                  }| j                  }|j                  }|D ]$  } ||      }|j                   ||||             & |S )N)rn   constant_result)rK   rC   rn   r   )literalcharsrK   stypesval	sval_typecharcvals           r'   #unpack_string_to_character_literalsr    sc    E
++CE==DI CU3dDABC Lr*   c                    | d   }|j                   st        |t        j                        r&t	        | d d D cg c]  }|j                    c}      s|j                  |        y g }|j                   r|j                  }n|j                  rt        |      }t              }t        |      D cg c]  }g  }}g }	| d d D ]y  }|j                   s4|j                  rt        |j                  d       |j                  |       Dt        |j                        }
t	        |j                  D cg c]  }|j                  sd c}      }|dkD  r*t        |j                  d       |j                  ||g       |
|z
  |kD  r:t        |j                  d||dk7  xr dxs dfz         |j                  ||g       |rt        ||	|j                  |       |
|k  r0t        |j                  d|
|fz         |j                  ||g       Kt        ||j                        D ]  \  }}|j                  |        | |r"|j                  |       |j                  |       t        ||      D ]%  \  }}|s	|j                  |       t!        ||       ' |	D ]/  }|d	   j                   rt!        ||       |j                  |       1 y c c}w c c}w c c}w )
Nr   z4starred assignment target must be in a list or tupler   z,more than 1 starred expression in assignmentz#need more than %d value%s to unpacks z/too many values to unpack (expected %d, got %d)r   )r   ry   r   UnicodeNoder   r   rp   r]   r  r   r   
is_starredr   rK   map_starred_assignmentzipr   )inputoutputr   r   complete_assignmentsrhs_argsrhs_sizer   lhs_targetsstarred_assignmentslhs_sizer\   starred_targetstargetscascades                  r'   r   r     s    )C((JsI<Q<Q,R5":FCC//FGe
""88			6s;8}H$X/!2/K/Sbz %**~~cggUV '',sxx=SXXITqIJQ#''IJMM3s)$'(2#''@A63<"=> ?MM3s)$";0C#&88X7 #''Lx() *MM3s)$!$[#((!; %t$%5%: ##C(*+ K2 :NN3(&9: ' #1:--(&9MM'"	#m G 0 Js   K
.	K,K
>K
c                    t        t        | |            D ]:  \  }\  }}|j                  r|}t        |      |z
  dz
  } n|j	                  |       < t        d      t        t        | | d  ||dz   d              D ]  \  }\  }}|j	                  |        ||   j                  }	||d  }
|r|
d |  }
|
r|
d   j                  }n|	j                  }|j	                  |	t        j                  ||
      g       y )Nr   z6no starred arg found when splitting starred assignmentr   )rK   rp   )
r   r  r  r   r   r   r   rK   r   ListNode)r  r  lhs_argsr  r   r  r\   starredlhs_remainingr   starred_rhsrK   s               r'   r  r     s#    (K(BC V?GT??GMA-1MtV TUU (K,H,4Wq[\,B)D E ?GTt
 g%%F78$K!/M>2!n  jj	""s= ? @r*   c                   6     e Zd ZdZdZdZ fdZd Zd Z xZ	S )PxdPostParsea  
    Basic interpretation/validity checking that should only be
    done on pxd trees.

    A lot of this checking currently happens in the parser; but
    what is listed below happens here.

    - "def" functions are let through only if they fill the
    getbuffer/releasebuffer slots

    - cdef functions are let through only if they are on the
    top level and are declared "inline"
    z>function definition in pxd file must be declared 'cdef inline'z5inline function definition in pxd file cannot be '%s'c                 8    d| _         t        t        |   |      S )Npxd)r   r=   r  __call__r%   r&   rC   s     r'   r  zPxdPostParse.__call__5  s    \41$77r*   c                 \    | j                   }d| _         | j                  |       || _         |S Nr   )r   rF   )r%   r&   olds      r'   visit_CClassDefNodez PxdPostParse.visit_CClassDefNode9  s,    oo"4 r*   c                    | j                   }t        |t        j                        r| j                  dk(  r|j
                  dv rd }t        |t        j                        rwd|j                  v r]| j                  dv rOd|_        |j                  dk7  r| j                  |j                  z  }n+|j                  r| j                  dz  }nd }n| j                   }|r0| j                  j                  t        |j                  |             y |S )Nr   )__getbuffer____releasebuffer__inline)r  r   Tprivateapi)ERR_INLINE_ONLYry   r   r}   r   ro   CFuncDefNode	modifiersinline_in_pxd
visibilityERR_NOGO_WITH_INLINEr)  rB   r   rd   rK   )r%   r&   errs      r'   visit_FuncDefNodezPxdPostParse.visit_FuncDefNode@  s     ""tU]]+80K		CCCdE../T^^+#44%)"??i/33dooECXX33e;CC**LL''txx(EFKr*   )
r5   r6   r7   r8   r*  r/  r  r#  r1  ra   rb   s   @r'   r  r  $  s%     WOR8r*   r  c                   B     e Zd Z fdZd Zd Zej                  Z xZ	S )TrackNumpyAttributesc                 H    t         t        |           t               | _        y r"   )r=   r3  r>   r   numpy_module_namesr%   rC   s    r'   r>   zTrackNumpyAttributes.__init___  s    "D24"%%r*   c                 v    |j                   dk(  r)| j                  j                  |j                  xs d       |S )Nnumpy)module_namer5  r   as_namer$   s     r'   visit_CImportStatNodez*TrackNumpyAttributes.visit_CImportStatNodec  s2    x'##''(@Ar*   c                     | j                  |       |j                  }|j                  r|j                  | j                  v s|j
                  rd|_        |S rE   )rF   objr   ro   r5  is_numpy_attribute)r%   r&   r=  s      r'   visit_AttributeNodez(TrackNumpyAttributes.visit_AttributeNodeh  sF    4 hhKKCHH(?(??CDZDZ&*D#r*   )
r5   r6   r7   r>   r;  r?  r   recurse_to_children
visit_Nodera   rb   s   @r'   r3  r3  ]  s    (
 "55Jr*   r3  c            
           e Zd ZdZej
                  ej                  ej                   ej                  dd       ej                  dd       ej                  dd       ej                  dd      ej                  ej                  d	Z
d ej                  d      iZ eg d	      Zej                  e
        eg d
      Z fdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z% xZ&S ) InterpretCompilerDirectivesaq  
    After parsing, directives can be stored in a number of places:
    - #cython-comments at the top of the file (stored in ModuleNode)
    - Command-line arguments overriding these
    - @cython.directivename decorators
    - with cython.directivename: statements

    This transform is responsible for interpreting these various sources
    and store the directive in two ways:
    - Set the directives attribute of the ModuleNode for global directives.
    - Use a CompilerDirectivesNode to override directives for a subtree.

    (The first one is primarily to not have to modify with the tree
    structure, so that ModuleNode stay on top.)

    The directives are stored in dictionaries from name to value in effect.
    Each such dictionary is always filled in for all possible directives,
    using default values where no value is given by the user.

    The available directives are controlled in Options.py.

    Note that we have to run this prior to analysis, and so some minor
    duplication of functionality has to occur: We manually track cimports
    and which names the "cython" module may have been imported to.
    Tz++z--F)	typeofzoperator.addresszoperator.dereferencezoperator.preincrementzoperator.predecrementzoperator.postincrementzoperator.postdecrementzoperator.typeidaddresszoperator.comma,)declareunionr   typedefsizeofcastpointercompiledNULL
fused_typeparallel)rP  prangethreadidc                 8   t         t        |   |       t               | _        ddi| _        i | _        t        j                  t        j                               }|j                         D ]&  \  }}t        j                  |      |t        |      <   ( || _        y )Nstaticmethod)r=   rC  r>   r   cython_module_namesdirective_namesparallel_directivesr   deepcopyr   get_directive_defaultsr   r   
directives)r%   rB   compilation_directive_defaultsrZ  r   rn   rC   s         r'   r>   z$InterpretCompilerDirectives.__init__  s    )49'B#&5  .?#% ]]7#A#A#CD
8>>@ 	=JC(,e(<Jx}%	=$r*   c           
          t         j                  j                  |d       }|r1||vr-| j                  j	                  t        |d|d|d             y|t         j                  vrt        |d|d       y)NzThe z& compiler directive is not allowed in z scopeFzInvalid directive: 'z'.T)r   directive_scopesr   rB   r   rd   directive_typesr   )r%   rK   	directivescopelegal_scopess        r'   check_directive_scopez1InterpretCompilerDirectives.check_directive_scope  sn    //33ItDE5LL''sHQSX=Z )[ \ 7 77cDEr*   c                    t        |j                        D ]J  }| j                  |j                  |d      r!| j	                  |j                  |d       |j                  |= L |j
                  | _        | j                  j                  |j                         | j                  |_        | j                  |_	        | j                  |       | j                  |_        |S )Nr   )sorteddirective_commentsrb  rK   wrong_scope_errorr`  module_scoperZ  updaterW  rF   rU  )r%   r&   r   s      r'   visit_ModuleNodez,InterpretCompilerDirectives.visit_ModuleNode  s    $112 	1C--dhhXF&&txxh?++C0	1
 !JJt667//#'#;#; 4 #'#;#; r*   c                 t    |t         j                  v xs% || j                  v xs t        j                  |      S r"   )r   r^  special_methodsr   parse_basic_type)r%   ro   s     r'   is_cython_directivez/InterpretCompilerDirectives.is_cython_directive  s;    /// 2,,,2++D1	3r*   c                    |dz   j                  d      }|r|j                  d      }|dk(  rd| j                  d<   nW|dk(  r$| j                  D ]  }d|z  | j                  |<    n.t	        |      dk7  s|d   | j                  vrt        |d	|z         | j                  j                  t        j                  d
d             |S )z
        Checks to see if fullname (e.g. cython.parallel.prange) is a valid
        parallel directive. If it is a star import it also updates the
        parallel_directives.
        .cython.parallel.cython.parallelrP  zcython.parallel.*zcython.parallel.%s   r   zNo such directive: %sInitThreadsModuleSetupCode.c)

startswithsplitrW  valid_parallel_directivesr   r   rg  use_utility_coder   load_cached)r%   	full_namerK   r   r_  ro   s         r'   is_parallel_directivez1InterpretCompilerDirectives.is_parallel_directive  s     c/--.@A!,I..8J((522 :: RD5JT5QD,,T2Ri.A%B-t'E'EEc2Y>?..''7JKM r*   c                 0   |j                   dk(  r+| j                  j                  |j                  xs d       |S |j                   j	                  d      r?|j                   j	                  d      r#t        |j                  |j                   dz          |j                   dk(  r|j                  r3|j                  dk7  r$|j                   | j                  |j                  <   n4| j                  j                  d       |j                   | j                  d<   | j                  j                  t        j                  dd             y |j                  r'|j                   dd  | j                  |j                  <   y | j                  j                  d       y |S )	Ncythoncython.rp  z is not a modulerq  rs  rt     )r9  rU  r   r:  ru  r   rK   rW  rg  rx  r   ry  rV  r$   s     r'   r;  z1InterpretCompilerDirectives.visit_CImportStatNode  s^   y($$(()BC( ' ((4**+>?dhh 0 03E EF#55<<DLLI$==A=M=MD,,T\\:,,00;:>:J:J ,,$68!!22++M;NOQ  595E5Eab5I$$T\\2
  ((,,Y7 r*   c                    |j                   s|j                  dk(  s|j                  j                  d      r|j                  dz   dd  }g }|j                  D ]  \  }}}}||z   }d|z   }	| j	                  |	|j
                        r|	| j                  |xs |<   B| j                  |      r<|| j                  |xs |<   |i| j                  j                  t        |d             |j                  ||||f        |sy ||_        |S )Nr}  r~  ro  r  z0Compiler directive imports must be plain imports)relative_levelr9  ru  imported_namesr{  rK   rW  rm  rV  rB   r   rd   r   )
r%   r&   	submodulenewimprK   ro   r:  kindrz  qualified_names
             r'   visit_FromCImportStatNodez5InterpretCompilerDirectives.visit_FromCImportStatNode  s   ""  I-1A1A1L1LZ1X))D0!"5IF,0,?,? >(T7D%,	!+i!7--ndhhG AOD,,W_=--i8<ED((D9'33N3N5P Q MM3gt"<=>  "(Dr*   c                 2   |j                   j                  j                  dk(  s/|j                   j                  j                  j                  d      r|j                   j                  j                  dz   dd  }g }|j                  D ]  \  }}||z   }d|z   }| j                  ||j                        r|| j                  |j                  <   F| j                  |      r|| j                  |j                  <   q|j                  ||f        |sy ||_        |S )Nr}  r~  ro  r  )r   r9  rn   ru  r   r{  rK   rW  ro   rm  rV  r   )r%   r&   r  r  ro   	name_noderz  r  s           r'   visit_FromImportStatNodez4InterpretCompilerDirectives.visit_FromImportStatNode#  s    KK##))Y6{{&&,,77
C0066=qrBIF#':: 5i%,	!+i!7--ndhhG?MD,,Y^^<--i8;DD((8MM4"345 DJr*   c                    t        |j                  t        j                        r|j                  j                  j
                  }|dz   j                  d      }|dk7  r|s|S |j                  j                  j
                  }|j                  j                  }t        j                  |j                  ||      }| j                  |      }|S | j                  |       |S )Nro  rp  r}  )r9  r:  )ry   r   r   
ImportNoder9  rn   ru  r   ro   r   CImportStatNoderK   r;  rF   )r%   r&   r9  is_parallelr:  s        r'   r   z6InterpretCompilerDirectives.visit_SingleAssignmentNode6  s    dhh	 4 45((..44K&-99:MNKi'((..44KhhmmG((7B3:<D --d3D  t$r*   c                     |j                   | j                  v r	d|_        |S | j                  j	                  |j                         }|||_        |S rE   )ro   rU  is_cython_modulerV  r   cython_attribute)r%   r&   r_  s      r'   visit_NameNodez*InterpretCompilerDirectives.visit_NameNodeJ  sR    99000$(D!
  ,,00;I$(1%r*   c                 ^    | j                  |j                         | j                  |       |S r"   )visitcppclassrF   r$   s     r'   visit_NewExprNodez-InterpretCompilerDirectives.visit_NewExprNodeS  s%    

4==!4 r*   c           	         t        |t        j                        rN| j                  |j                         |j                  j                         }|rt        j                  j                  |      }|r|j                         \  }}g }g }||t        ur|j                  D ]w  }|\  }	}
|d|	j                  }t        j                  j                  |      r/|j                  | j                  ||
gd |j                               g|j                  |       y |sd }n||_        |r|s|s|S |j                  | j                  ||||j                  j                               |S y t        |t        j                   t        j"                  f      r| j                  |       |j                         }|rt        j                  j                  |      }|t$        u rAt        j&                  |j                  d      }| j                  ||gd |j                        gS ||d fgS t)        |j                  d|z        y )Nro  Trm   z5The '%s' directive should be used as a function call.)ry   r   CallNoder  functionas_cython_attributer   r^  r   explicit_args_kwdsdictkey_value_pairsrn   r   try_to_parse_directiverK   AttributeNoder   boolBoolNoderd   )r%   r&   optnamedirectivetyperp   kwdsrZ  r  keyvaluer   rn   sub_optnamer   s                r'   try_to_parse_directivesz3InterpretCompilerDirectives.try_to_parse_directivesX  s    dI../JJt}}%mm779G ' 7 7 ; ;G D !%!8!8!:JD$!#J&(O'M,E(,(<(< AH)1JC5<cii*HK&66::;G * 1 1$2M2Mk\a[bdhjrjvjv2w x / 6 6x @A  /#'D3BD0%d4#--%%d&A&A'4QUW[WdWdWhWh&ij%%  y66	8J8JKLJJt..0G ' 7 7 ; ;G D D(#,,TXXTBC 77#dhhWXX"*$dO,,("Y\c"ce er*   c           	         |dk(  r%| j                   j                  st        |d|z        |dk(  rt        |      dkD  }d}|r|j                  r|j                  d   }t        |j                        dk(  rj|j
                  j                  rT|j
                  j                  dk(  r;t        |j                  t        j                        r|j                  j                  }nd}|rt        |d      d|r|d   |ffS d |ffS t        j                  j                  |      }t        |      dk(  r6t        |d   t        j                        r|t        j                         |   fS |t         u rM|+t        |      dk7  st        |d   t        j                        st        |d	|z        ||d   j                  fS |t"        u rV|+t        |      dk7  st        |d   t        j$                        st        |d
|z        |t#        |d   j                        fS |t&        u rf|;t        |      dk7  s-t        |d   t        j(                  t        j*                  f      st        |d|z        |t'        |d   j                        fS |t,        u r&|t        |      dk7  rt        |d|z        ||d   fS |t.        u rSt        |      dk7  rt        |d|z        |t/        |j                  D 	
cg c]  \  }	}
|	j                  |
f c}
}	      fS |t0        u rN|r't        |j                        dk7  rt        |d|z        ||D cg c]  }t'        |j                         c}fS t3        |      rm|;t        |      dk7  s-t        |d   t        j(                  t        j*                  f      st        |d|z        | ||t'        |d   j                              fS J c c}
}	w c c}w )N
np_pythranz.The %s directive can only be used in C++ mode.	exceptvalr   Tr   checkzYThe exceptval directive takes 0 or 1 positional arguments and the boolean keyword "check"z8The %s directive takes one compile-time boolean argumentz8The %s directive takes one compile-time integer argumentz7The %s directive takes one compile-time string argumentz(The %s directive takes one type argumentz1The %s directive takes no prepositional argumentsz+The %s directive takes no keyword arguments)rB   cpprd   r   r  r   r]   rn   ry   r   r  r   r^  r   NoneNoderY  r  intIntNodestr
StringNoder  typer  r   callable)r%   r  rp   r  rK   	arg_errorr  kwr  r   rn   r   s               r'   r  z2InterpretCompilerDirectives.try_to_parse_directive  s   l"4<<+;+; &VY`&`aa#D	AIE,,))!,,,-200RVV\\W5L"288Y-?-?@HHNNE $I$tv vT$q'U!CDDtU!CDD//33G<t9>ja)2D2DEG::<WEEEd"3t9>DGYM_M_9`$SNQXXZ ZT!W]]++c!3t9>DGYM^M^9_$SNQXXZ ZSa/00c!3t9>Gi22I4I4IJ:L$SMPWWY YSa/00d"3t9>$S>HJ JT!W%%d"4yA~$SG'QS SDtG[G[!\e399e"4!\]]]d"D001Q6$SAGKM M>c#))n>>>m$3t9>Gi22I4I4IJ:L$SMPWWY Y]7CQ4FGHH "]
 ?s   +OO"c                    |s| j                  |      S | j                  }t        |      }|j                  |       ||k(  r| j                  |      S || _        | j                  |      }|| _        t	        |t
        j                        s"t        j                  |j                  |g      }t        j                  |j                  ||      S )Nr   )rK   rs   rZ  )	rA  rZ  r  rh  ry   r   rM   rK   CompilerDirectivesNode)r%   r&   rZ  old_directivesnew_directivesretbodys         r'   visit_with_directivesz1InterpretCompilerDirectives.visit_with_directives  s    ??4((n-j)^+??4(((//$'('5#5#56(('CG++'nF 	Fr*   c                 J    | j                  |d      }| j                  ||      S )Nr  _extract_directivesr  r%   r&   rZ  s      r'   r1  z-InterpretCompilerDirectives.visit_FuncDefNode  '    --dJ?
))$
;;r*   c                    | j                  |d      }|j                         D ]I  \  }}|dk(  r||_        |dvs| j                  j	                  t        |j                  d|z               K | j                  ||      S )Nr  locals)finalrT  zXCdef functions can only take cython.locals(), staticmethod, or final decorators, got %s.)r  r   directive_localsrB   r   rd   rK   r  )r%   r&   rZ  ro   rn   s        r'   r,   z-InterpretCompilerDirectives.visit_CVarDefNode  s    --dJ?
%++- 	JKD%x(-%66++NHHACGH-I J		J ))$
;;r*   c                 J    | j                  |d      }| j                  ||      S r!  r  r  s      r'   r#  z/InterpretCompilerDirectives.visit_CClassDefNode  s'    --dH=
))$
;;r*   c                 J    | j                  |d      }| j                  ||      S )Nr  r  r  s      r'   visit_CppClassNodez.InterpretCompilerDirectives.visit_CppClassNode  r  r*   c                 J    | j                  |d      }| j                  ||      S )Nclassr  r  s      r'   visit_PyClassDefNodez0InterpretCompilerDirectives.visit_PyClassDefNode  s'    --dG<
))$
;;r*   c                    |j                   si S g }g }g }|j                   d d d   D ]  }| j                  |j                        }||D ]  }| j                  |j                  |d   |      rV|\  }	}
| j
                  j                  |	t                     |
k7  r|j                  |       |d   dk(  r|j                  |       |d   dk(  s|dk(  sd} |j                  |        |rW|dk(  s9t        |t        j                  t        j                  t        j                  f      rt        |d   j                  d      |d d d   |d d d   z   |_         i }|D ]_  }|\  }	}
|	|v rO||	   }t        |t              r|j!                  |
       3t        |t"              r|j%                  |
       U|
||	<   [|
||	<   a |S )Nr   r   rT  r   r  z8Cdef functions/classes cannot take arbitrary decorators.)
decoratorsr  	decoratorrb  rK   rZ  r   objectr   ry   r   r+  r   CVarDefNoderd   r  rh  r   extend)r%   r&   
scope_namerZ  realdecsbothdecr  r_  ro   rn   optdict	old_values                r'   r  z/InterpretCompilerDirectives._extract_directives  s   I
??4R4( 	%C!99#--HN)!/ 	.I11$((IaL*U&/e??..tVX>%G&--i8$Q<>9 KK, |x/J'4I%-
	. $	% x/#D5+=+=u?R?RTYTeTe*fg !2lmm"4R4.4":5# 	&I#KD%w#DM	 i.$$U+	40$$U+$)GDM %	& r*   c                     i }| j                  |j                        xs g D ]  }||j                  0| j                  j	                  t        |j                  d             B|\  }}|dv r?t        j                  |j                  ||j                        }| j                  |      c S | j                  |j                  |d      s|||<    |r| j                  |j                  |      S | j                  |      S )Nz6Compiler directive with statements cannot contain 'as')nogilgilstaters   zwith statement)r  managerr   rB   r   rd   rK   r   GILStatNoders   rA  rb  r  )r%   r&   directive_dictr_  ro   rn   s         r'   visit_WithStatNodez.InterpretCompilerDirectives.visit_WithStatNode"  s    55dllCIr 	5I$;;*LL//&txx1ijl #,KD%//$004PTPYPYZ#t4411$((DBRS/4t,	5 --diiHHt$$r*   )'r5   r6   r7   r8   r   
TypeofNodeAmpersandNodeDereferenceNodeinc_dec_constructor
TypeidNodeunop_method_nodesc_binop_constructorbinop_method_nodesr   rk  rh  rw  r>   rb  ri  rm  r{  r;  r  r  r   r  r  r  r  r  r1  r,   r#  r  r  r  r  ra   rb   s   @r'   rC  rC  r  sE   4 &&%33 ) 9 9"?)"?"?d"K"?)"?"?d"K"?)"?"?t"L"?)"?"?t"L"+"6"6 **  	"?)"?"?"D  = >O ,- # % !%	"3
008&(
*X=~F*<
<<<<+\%r*   rC  c                        e Zd ZdZdZdZdZdZej                  e
j                  ej                  dZd Zd Zd Zd Zd	 Zd
 Zd Zd Z fdZ xZS )ParallelRangeTransforma  
    Transform cython.parallel stuff. The parallel_directives come from the
    module node, set there by InterpretCompilerDirectives.

        x = cython.parallel.threadavailable()   -> ParallelThreadAvailableNode
        with nogil, cython.parallel.parallel(): -> ParallelWithBlockNode
            print cython.parallel.threadid()    -> ParallelThreadIdNode
            for i in cython.parallel.prange(...):  -> ParallelRangeNode
                ...
    NF)zcython.parallel.parallelzcython.parallel.threadidzcython.parallel.prangec                 N    |j                   | j                  v xs |j                  S r"   )ro   rW  r  r$   s     r'   node_is_parallel_directivez1ParallelRangeTransform.node_is_parallel_directiveW  s"    yyD444M8M8MMr*   c                    | j                   rdj                  | j                        }nP| j                  | j                  d      }|ddj                  | j                  dd       }|j	                  d      }| j
                  j                  |      }|7| j                   r| j                  d   dk7  st        |j                  d|z         d| _         d| _        |S )z
        Figure out which parallel directive was used and return the associated
        Node class.

        E.g. for a cython.parallel.prange() call we return ParallelRangeNode
        ro  r   r   NrP  zInvalid directive: %sF)	namenode_is_cython_modulejoinparallel_directiverW  rstripdirective_to_noder   r   rK   )r%   r&   r_  clss       r'   get_directive_class_nodez/ParallelRangeTransform.get_directive_class_nodeZ  s     ))!8!89I001H1H1KLI#,#&88D,C,CAB,G#HJI!((-I$$((3; > > $ 7 7 :j H$((3i?@).&"&
r*   c                 b    |j                   r"|j                   | _         | j                  |      S |S )zd
        If any parallel directives were imported, copy them over and visit
        the AST
        )rW  rA  r$   s     r'   ri  z'ParallelRangeTransform.visit_ModuleNodes  s1    
 ##'+'?'?D$??4(( r*   c                 n    | j                  |      r#|j                  g| _        |j                  | _        |S r"   )r  ro   r  r  r  r$   s     r'   r  z%ParallelRangeTransform.visit_NameNode  s1    **40'+yykD#-1-B-BD*r*   c                     | j                  |       | j                  r%| j                  j                  |j                         |S r"   )rF   r  r   	attributer$   s     r'   r?  z*ParallelRangeTransform.visit_AttributeNode  s6    4 ""##**4>>:r*   c                 d   | j                  |j                         | j                  s| j                  |d       |S t	        |t
        j                        r#|j                  j                  }|j                  }n|j                  }i }| j                  |      }|r ||j                  ||      }|S )N)r  )exclude)rp   kwargs)r  r  r  rF   ry   r   GeneralCallNodepositional_argsrp   keyword_argsr  rK   )r%   r&   rp   r  parallel_directive_classs        r'   visit_CallNodez%ParallelRangeTransform.visit_CallNode  s    

4==!&&t];K
 dI556'',,D&&F99DF#'#@#@#F # ,DHH4ODr*   c                    | j                  |j                        }t        |t        j                        ra| j
                  dk(  r t        |j                  j                  d       d| _        | j                  |j                        }d| _        ||_        |S | j                  r=| j                  |      }|sy|t        j                  u rt        |j                  d       y| j                  |j                        |_        |S )z.Rewrite with cython.parallel.parallel() blockszparallel withz*Nested parallel with blocks are disallowedNz%The parallel directive must be called)r  r  ry   r   ParallelWithBlockNoder  r   rK   rs   r  r  )r%   r&   newnoders   r  s        r'   r  z)ParallelRangeTransform.visit_WithStatNode  s    **T\\*gu::;zz_,dll&&BD )DJ::dii(DDJGLN$$'+'D'DT'J$+'5+F+FFdhh GHJJtyy)	r*   c                    | j                  |j                         | j                  |j                         t        |j                  j                  t
        j                        }| j                  }|r|j                  j                  }|j                  |_        |j                  |_        |j                  |_	        |}t        |j                  t        j                        s t        |j                  j                  d       d| _        | j                  |j                         || _        | j                  |j                         |S )z/Rewrite 'for i in cython.parallel.prange(...):'z+Can only iterate over an iteration variablerQ  )r  iteratorr   ry   sequencer   ParallelRangeNoder  rs   else_clauser   r   r   rK   )r%   r&   	in_prangeprevious_stateparallel_range_nodes        r'   visit_ForInStatNodez*ParallelRangeTransform.visit_ForInStatNode  s    

4==!

4;;t}}55$668	 #'--"8"8)-&'+yy$.2.>.>+&Ddkk9+=+=>dkkooCE "DJ

499#


4##$r*   c                 0    |t         t        |   |      S y)zVisit a node that may be NoneN)r=   r  r  r  s     r'   r  zParallelRangeTransform.visit  s!    /<TBB r*   )r5   r6   r7   r8   r  r  in_context_manager_sectionr  r   r  r   ParallelThreadIdNoder  r  r  r  ri  r  r?  r  r  r  r  ra   rb   s   @r'   r  r  6  s    	  !& "' E &+%@%@%.%C%C#(#:#:	N2
0:>C Cr*   r  c                       e Zd Zd Zd Zy)WithTransformc                 L   | j                  |d       |j                  }|j                  }|j                  |j                  |j
                  }}}t        j                  |t        j                  |t        j                  |      t        |rdnd      d      g d      |_        |r&t        j                  ||j                        |_        |/t        j                  |t        j                  |||      |g	      }t        j                   |dt#        d
      D cg c]  }t        j$                  |       c}      }t        j&                  |t        j(                  |t        j*                  |t        j,                  |t        j.                  ||d||rt        j                  |d       nd             t        j0                  |            gd       d d |      }	t        j2                  |t        j4                  |||	gd       t        j6                  |t        j.                  ||dt        j                   |t#        d
      D cg c]  }t        j8                  |       c}      |rt        j                  |d       nd             d      |_        |S c c}w c c}w )Nrs   
__aenter__	__enter__T)r=  r  is_special_lookup)r  rp   is_temp)r   )r   	with_noder   rr  )slowrp   F)	with_stattest_if_runrp   
await_exproperand)	conditionrs   )
if_clausesr  )rs   patternr   excinfo_target)rs   except_clausesr  rp   rl   )rs   r   handle_error_case)rF   rK   is_asyncrs   r   r  r   SimpleCallNoder  	CloneNoder   
enter_callAwaitExprNoder   rM   WithTargetAssignmentStatNode	TupleNoder   ExcValueNodeExceptClauseNode
IfStatNodeIfClauseNodeNotNodeWithExitCallNodeReraiseStatNoder   TryExceptStatNoder{   r  )
r%   r&   rK   r$  rs   r   r  r   r   except_clauses
             r'   r  z WithTransform.visit_WithStatNode  sc   4(hh== $		4;;gf#22)11,,W5'kR"&(  '55ctODO%%6649D #,,St16qC;,-I""3'C; <..e&&&&y'8'8)C)C #t,1%3U]9+B+B3D+Qcg	*i(j #2237
! !" )!
& ,,e--$ - 
 !--)444 $",,E!H"Mq9#5#5c#:"MOIQy66sEW[]^ $
	 IC;@ #Ns   JJ!"c                     |S r"   r#   r$   s     r'   rH   zWithTransform.visit_ExprNode#      r*   N)r5   r6   r7   r  rH   r#   r*   r'   r  r    s    ;zr*   r  c                        e Zd ZdZdZddddj
                  Z fdZd Zd	 Z	e
d
        Ze
d        Ze
d        Z xZS )DecoratorTransforma  
    Transforms method decorators in cdef classes into nested calls or properties.

    Python-style decorator properties are transformed into a PropertyNode
    with up to the three getter, setter and deleter DefNodes.
    The functional style isn't supported yet.
    N__get____set____del__)gettersetterdeleterc                     | j                   g | _         | j                   j                  i        t        t        |   |       | j                   j                          |S r"   )_propertiesr   r=   r7  r#  popr  s     r'   r#  z&DecoratorTransform.visit_CClassDefNode8  sR    #!D# $;DAr*   c                     t        |j                  d   t              rdnd}t        |j                  d|j                  z  |       |S )Nr   r   z4'property %s:' syntax is deprecated, use '@property')ry   rK   r  r   ro   )r%   r&   levels      r'   visit_PropertyNodez%DecoratorTransform.visit_PropertyNode@  s=    S1qPSWS\S\\^cdr*   c           	         | j                   }| j                  |      }|dk7  s|j                  s|S | j                  d   }|j                  d d d   D ]  }|j                  }|j
                  r|j                  dk(  rt        |j                        dkD  r| j                  ||      c S |j                  }t        d      |_        |j                  j                  |       |g}||v r<||   }|j                  |_        |j                  |_        ||j                  _        g c S t        j                   |j                  |      }|j                  |_        t        j"                  |j                  |      |_        |||<   |gc S |j$                  sC|j&                  j                  |v s]| j)                  |j*                        }	|	s||j&                  j                  |j                  k7  r=t-        |j                  d|j&                  j                  d	|j                  d
       t        |j                        dkD  r| j                  ||      c S | j/                  |||	|      c S  |j                  D ]_  }|j                  }
|
j
                  s|xj0                  |
j                  dk(  z  c_        |xj2                  |
j                  dk(  z  c_        a |j                  }d |_        | j5                  |||j                        S )Nr   r   propertyr   r8  r   r   z&Mismatching property names, expected 'z', got ''classmethodrT  )r   r1  r  r?  r  r   ro   r   _reject_decorated_propertyr   removerK   rt   rs   rL   r   PropertyNoderM   is_attributer=  _map_property_attributer  r   _add_to_propertyis_classmethodis_staticmethodchain_decorators)r%   r&   r   
propertiesdecorator_noder  ro   	stat_listprophandler_namefuncdecss               r'   visit_DefNodez DecoratorTransform.visit_DefNodeF  s   __
%%d+!K %%b)
"oodd3 	eN&00I  Y^^z%At'!+::4PPyy))4	&&~6!F	:%%d+D#xxDH#xxDH&/DIIOI))$((>88!..txxyI	#'
4 v''IMM,>,>*,L#;;I<O<OP }}))TYY6n00"+--"4"4diiAB T__-1#>>t^TT#44Z|Ucdd?	eF  	DI&&D||##tyyM'AA#$$		^(CC$		D $$T4;;r*   c                 `    | j                   D ]  }||k7  s	t        |j                  d         | S )Nz=Property methods with additional decorators are not supported)r  r   rK   )r&   rR  decos      r'   rH  z-DecoratorTransform._reject_decorated_property|  s7     OO 	aD~%dhh _`	a r*   c                 
   | |j                      }||_         |j                  j                  |       |j                  j                  }t        |      D ]  \  }}|j                   |k(  s|||<    g S  |j                  |       g S r"   )ro   r  rI  rs   rL   r   r   )rQ  r&   ro   r  rT  rL   r   stats           r'   rM  z#DecoratorTransform._add_to_property  s    $))$	y)		 ' 	GAtyyD a 		
 LL	r*   c                    t        j                  | j                  |      }|ddd   D ]/  }t        j                  |j                  |j                  |g      }1 t        j                  | j                  |      }t        j                  | j                  ||      }t        j                  |g      }|| _        | |gS )af  
        Decorators are applied directly in DefNode and PyClassDefNode to avoid
        reassignments to the function/class name - except for cdef class methods.
        For those, the reassignment is required as methods are originally
        defined in the PyMethodDef struct.

        The IndirectionNode allows DefNode to override the decorator.
        r   Nr   r  rp   r   )	r   r   rK   r%  r  r   r   IndirectionNodedecorator_indirection)r&   r  ro   decorator_resultr  r  reassignments          r'   rP  z#DecoratorTransform.chain_decorators  s     %--dhhTB#DbD) 	)I(77",,&' )	) &&txxd;	11HH "
 ,,l^<%1"l##r*   )r5   r6   r7   r8   r?  r   rL  r#  rC  rX  rT  rH  rM  rP  ra   rb   s   @r'   r7  r7  (  sw     K  
c	 4<l     $ $r*   r7  c                   &    e Zd ZdZd ZeZeZeZeZy)CnameDirectivesTransformz
    Only part of the CythonUtilityCode pipeline. Must be run before
    DecoratorTransform in case this is a decorator for a cdef class.
    It filters out @cname('my_cname') decorators and rewrites them to
    CnameDecoratorNodes.
    c                    t        |dd       s| j                  |      S t        |j                        D ]  \  }}|j                  }t        |t        j                        s.|j                  j                  sE|j                  j                  dk(  s_|j                         \  }}|rt        d      t        |      dk7  rt        d      |d   j                  r |d   j                  t         j"                  k(  st        d      |d   j%                  d       }|j                  |= t'        j(                  |j*                  ||      } n | j                  |      S )	Nr  cnamez/cname decorator does not take keyword argumentsr   z*cname decorator takes exactly one argumentr   z4argument to cname decorator must be a string literal)rK   r&   rf  )getattrrA  r   r  r  ry   r   r  r  r   ro   r  AssertionErrorr   r   r  r   str_typecompile_time_valuer   CnameDecoratorNoderK   )r%   r&   r   r  rp   r  rf  s          r'   handle_functionz(CnameDirectivesTransform.handle_function  sE   t\40??4((%doo6 	LAy!++I9i&8&89&&..&&++w6(;;=f(MO O t9>(HJ J Q**Q(8(88(RT T Q2248OOA&//DHH46;=3	6 t$$r*   N)	r5   r6   r7   r8   rl  r1  r#  r2   r4   r#   r*   r'   rd  rd    s'    %B ()("1r*   rd  c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)ForwardDeclareTypesc                     | j                   }|j                  }|j                  |_        | j                  |       ||_        |S r"   )rg  rZ  rF   )r%   r&   envr"  s       r'   visit_CompilerDirectivesNodez0ForwardDeclareTypes.visit_CompilerDirectivesNode  s;    nn4 r*   c                     |j                   | _        |j                  | j                  _        | j                  |       |S r"   )r`  rg  rZ  rF   r$   s     r'   ri  z$ForwardDeclareTypes.visit_ModuleNode  s3     JJ'+$4 r*   c                     | j                   j                  }d| j                   _        | j                  |       || j                   _        |S Nr   )rg  in_cincluderF   )r%   r&   old_cinclude_flags      r'   visit_CDefExternNodez(ForwardDeclareTypes.visit_CDefExternNode  sC     --99()%4 (9%r*   c                 <    |j                  | j                         |S r"   )rG  rg  r$   s     r'   r2   z&ForwardDeclareTypes.visit_CEnumDefNode  s    T&&'r*   c                     |j                   | j                  j                  vr|j                  | j                         |S r"   )ro   rg  entriesrG  r$   s     r'   r4   z/ForwardDeclareTypes.visit_CStructOrUnionDefNode  s1    99D--555LL**+r*   c                    |j                   | j                  j                  vr|j                  | j                         | j                  j                  |j                      j                  }||j
                  rs|j                  sg|j                  r[|j                  }|j                  D ]@  }|j                  s|j                  j                  s'|j                  j                          B |S r"   )
class_namerg  rz  rG  r  is_extension_typeis_builtin_typer`  cfunc_entriesis_fused"get_all_specialized_function_types)r%   r&   r  r`  entrys        r'   r#  z'ForwardDeclareTypes.visit_CClassDefNode  s    ??$"3"3";";;LL**+  ((9>> 6 6t?S?SX\XbXbJJE,, D::%**"5"5JJAACD r*   N)	r5   r6   r7   rq  ri  rw  r2   r4   r#  r#   r*   r'   rn  rn    s     

r*   rn  c                   x    e Zd Z edd ed      g      Z edd ed      g      Z edd ed      g      Z ed ed      g      Z ed	 ed      g      Z	dZ
d
Z fdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&d& Z' xZ(S )'AnalyseDeclarationsTransformzr
property NAME:
    def __get__(self):
        return ATTR
    def __set__(self, value):
        ATTR = value
    c_classNrB  pipelinez
property NAME:
    def __get__(self):
        return ATTR
    def __set__(self, value):
        ATTR = value
    def __del__(self):
        ATTR = None
    z?
property NAME:
    def __get__(self):
        return ATTR
    a  
cdef class NAME:
    cdef TYPE value
    def __init__(self, MEMBER=None):
        cdef int count
        count = 0
        INIT_ASSIGNMENTS
        if IS_UNION and count > 1:
            raise ValueError, "At most one union member should be specified."
    def __str__(self):
        return STR_FORMAT % MEMBER_TUPLE
    def __repr__(self):
        return REPR_FORMAT % MEMBER_TUPLE
    )r  z;
if VALUE is not None:
    ATTR = VALUE
    count += 1
    r   c                     g | _         t               | _        t        t        |       }|j
                  | _        |j                  |      S r"   )seen_vars_stackr   fused_error_funcsr=   r  r1  _super_visit_FuncDefNoder  )r%   rootsuper_classrC   s      r'   r  z%AnalyseDeclarationsTransform.__call__:  sA    !!$8$?(3(E(E%##D))r*   c                 V    | j                   d   j                  |j                         |S Nr   )r  r   ro   r$   s     r'   r  z+AnalyseDeclarationsTransform.visit_NameNodeB  s$    R $$TYY/r*   c                 L   g | _         | j                  j                  t                      |j	                  | j                                | j                  |       | j                  j                          |j                  j                  j                  | j                          |S r"   )extra_module_declarationsr  r   r   analyse_declarationscurrent_envrF   r@  rs   rL   r  r$   s     r'   ri  z-AnalyseDeclarationsTransform.visit_ModuleNodeF  sx    )+&##CE*!!$"2"2"454   "		t==>r*   c                     | xj                   dz  c_         |j                  | j                                | j                  |       | xj                   dz  c_         |S rt  )	in_lambdar  r  rF   r$   s     r'   r   z-AnalyseDeclarationsTransform.visit_LambdaNodeP  sH    !!!$"2"2"454 !r*   c                 d   | j                  |      }|j                  r|j                  j                  r|j                  rg }|j                  j                  D ]]  }|j
                  s| j                  |      }|j                  |j                         | j                  |       |j                  |       _ |r|j                  xj                  |z  c_
        |j                  dk7  rG|j                  j                  d      s,|j                  j                  d      s| j                  |       |S )Nextern
__reduce____reduce_ex__)visit_ClassDefNoder`  implementedrs   var_entriesneeds_propertycreate_Propertyr  r  r   rL   r.  lookup_inject_pickle_methods)r%   r&   rL   r  rE  s        r'   r#  z0AnalyseDeclarationsTransform.visit_CClassDefNodeW  s    &&t,::$**00TYYE// +''#33E:H11$**=JJx(LL*+ 		5(8+

)),7

))/:++D1r*   c                 	   | j                         }|j                  j                  d   du ry |j                  j                  d   du }g }|j                  j                  }d }d }||j                  d |j                  j                  D               |xs |j                  j                  d      }|xs8 |j                  j                  d      xs |j                  j                  d      }|j                  }||j                  d 	       |ry |D cg c]P  }|j                  j                  s8|j                  j                  |      r|j                  j                  |      s|R }	}|D cg c]  }|j                  j                  s| }
}|s|	s|
r|s|rd
}n9|	rddj                  d |	D              z  }nddj                  d |
D              z  }|rt        |j                   |       t#        dd|iz  dt%        d       g      j'                  i       }|j)                  |j                         | j+                  |       |j,                  j.                  j1                  |       y |D ]O  }|j                  j                  r|j                  j3                  |       |j                  j5                  |       Q |D cg c]  }|j6                   }}t9        |      }d|j:                  z  }t#        d|ddj                  |      z  dj                  |      |j:                  dj                  d t=        |      D              t?        |      dz  dt%        d       g      j'                  i       }|j)                  |j                  j                         | j+                  |       | j@                  j1                  |       t#        d||d   dj                  d |D              t?        |      dk(  rdnd z   d!j                  |D cg c](  }|j                  j                  sd"|j6                  z  * c}xs d#g      d$z  dt%        d       g      j'                  i       }|j)                  |j                         | jC                  ||j                         | j+                  |       | jE                          |j,                  j.                  j1                  |       y c c}w c c}w c c}w c c}w )%Nauto_pickleFTc              3   >   K   | ]  }|j                   d vs|  yw))__weakref____dict__Nr   .0r   s     r'   	<genexpr>zFAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<genexpr>u  s     mQ166QlClqms   	__cinit__r  r  c                     | j                   S r"   r   )r   s    r'   <lambda>zEAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<lambda>y  s
    qvv r*   )r   z2no default __reduce__ due to non-trivial __cinit__z6%s cannot be converted to a Python object for picklingrF  c              3   :   K   | ]  }d |j                   z    ywzself.%sNr   r  s     r'   r  zFAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<genexpr>  s     Y}qrZcfgflflZlY}   zZPickling of struct members such as %s must be explicitly requested with @auto_pickle(True)c              3   :   K   | ]  }d |j                   z    ywr  r   r  s     r'   r  zFAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<genexpr>  s     ;`STI<N;`r  z
                def __reduce_cython__(self):
                    raise TypeError("%(msg)s")
                def __setstate_cython__(self, __pyx_state):
                    raise TypeError("%(msg)s")
                msgr  r  z__pyx_unpickle_%sa  
                def %(unpickle_func_name)s(__pyx_type, long __pyx_checksum, __pyx_state):
                    cdef object __pyx_PickleError
                    cdef object __pyx_result
                    if __pyx_checksum not in %(checksums)s:
                        from pickle import PickleError as __pyx_PickleError
                        raise __pyx_PickleError("Incompatible checksums (0x%%x vs %(checksums)s = (%(members)s))" %% __pyx_checksum)
                    __pyx_result = %(class_name)s.__new__(__pyx_type)
                    if __pyx_state is not None:
                        %(unpickle_func_name)s__set_state(<%(class_name)s> __pyx_result, __pyx_state)
                    return __pyx_result

                cdef %(unpickle_func_name)s__set_state(%(class_name)s __pyx_result, tuple __pyx_state):
                    %(assignments)s
                    if len(__pyx_state) > %(num_members)d and hasattr(__pyx_result, '__dict__'):
                        __pyx_result.__dict__.update(__pyx_state[%(num_members)d])
                z(%s)z, z; c              3   4   K   | ]  \  }}d |d|d  yw)z__pyx_result.z = __pyx_state[]Nr#   )r  ixvs      r'   r  zFAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<genexpr>  s%      -C!B @A"E-Cs   )unpickle_func_name	checksumsmembersr|  assignmentsnum_membersr   ap  
                def __reduce_cython__(self):
                    cdef tuple state
                    cdef object _dict
                    cdef bint use_setstate
                    state = (%(members)s)
                    _dict = getattr(self, '__dict__', None)
                    if _dict is not None:
                        state += (_dict,)
                        use_setstate = True
                    else:
                        use_setstate = %(any_notnone_members)s
                    if use_setstate:
                        return %(unpickle_func_name)s, (type(self), %(checksum)s, None), state
                    else:
                        return %(unpickle_func_name)s, (type(self), %(checksum)s, state)

                def __setstate_cython__(self, __pyx_state):
                    %(unpickle_func_name)s__set_state(self, __pyx_state)
                r   c              3   &   K   | ]	  }d |z    ywr  r#   )r  r  s     r'   r  zFAnalyseDeclarationsTransform._inject_pickle_methods.<locals>.<genexpr>  s     (R1Q(Rs   r   r  z or zself.%s is not NoneFalse)r  checksumr  any_notnone_members)#r  r`  rZ  r  r  r  r  r  	base_typesortis_pyobjectcan_coerce_to_pyobjectcan_coerce_from_pyobjectis_struct_or_unionr  r   rK   r   r:   
substituter  r  rs   rL   r   create_to_py_utility_codecreate_from_py_utility_codero   _calculate_pickle_checksumsr|  r   r   r  enter_scope
exit_scope)r%   r&   rp  auto_pickle_forcedall_membersr  cinitinherited_reducer   non_pystructsr  pickle_funcall_members_namesr  r  unpickle_funcs                    r'   r  z3AnalyseDeclarationsTransform._inject_pickle_methodsi  s    ::  /58!ZZ22=ATIjjooom#))*?*?mm:SYY--k:E/v3993C3CL3QvUXU^U^UeUefuUv--C	 o
 	-.  #
66%%qvv/L/LS/Q23&&2Q2QRU2V 
 
 *GQVV-F-F1GGFw/AJNQTQYQYY}v|Y}Q}}
13688;`X_;`3`a "dhh$& (
 cl(#  =+>*?A BLB  ,,TZZ8JJ{#IIOO"";/ ! <vv))FF44S9FF66s;< 2= =A = =34EFI!4t!F ) *" +=!'$))I*>!>#yy):;"&//#'99 -C%./@%A-C $C $''8#9	!*2 "]4-@,A3C2 DN:b>3 4 ..tzz/?/?@JJ}%**11-@& (( += )!#yy(R@Q(RR]`ar]swx]xVY  A   B,2KKal  9D\]pqpvpv  qC  qC9NQRQWQW9W  9D  9Q  IP  HQ  -R'(4  =+>*?5A4 BLB5 6 ,,TZZ8T4::.JJ{#OOIIOO"";/Y
 HD !>z 9Ds%   AS*1S/S/S4S9+S9c                 n   g }|D ]U  }|j                   }|j                  r*|j                  dvs|j                  |j                        sE|j	                  |       W |rVt        | j                        }|j                  }|j                  |||j                        \  }	}
|
j                  |       ||
g}|S )zd
        Create function calls to the decorators and reassignments to
        the function.
        )rT  rG  )
r  r   ro   lookup_herer   r7  rB   r&   rP  r  )r%   old_decoratorsrp  r&   r  r  rV  	transformr~   r   reassignmentss              r'   _handle_fused_def_decoratorsz9AnalyseDeclarationsTransform._handle_fused_def_decorators  s     
' 	-I&&DLL		!@@		*!!),	- *4<<8IyyH(99*hmm 5A}..s3-(Dr*   c                    |j                   j                  d|j                         | j                  |j                        |_        |j	                  |       t
        j                  j                  |j                  d      }t        j                  |j                  |            }||_
        | j                  |j                  t        j                  |      |      |_        |r| j                  |||      }|S )z#Handle def or cpdef fused functionsr   T)binding)rL   insertpy_funcr  update_fused_defnode_entryr   PyCFunctionNodefrom_defnode	ProxyNodecoerce_to_tempresulting_fused_function_create_assignmentr&  fused_func_assignmentr  )r%   r  rp  r&   pycfuncs        r'   _handle_defz(AnalyseDeclarationsTransform._handle_def	  s     	

!T\\*zz$,,/'',++88t8T%%g&<&<S&AB(/%%)%<%<LL)--g6&=" 44ZdKDr*   c                    ddl m} | j                  s| j                  r| j                  | j                  vr9| j                  rt        |j                  d       nt        |j                  d       | j                  j                  | j                         t        j                  |j                        |_
        |j                  D ];  }|j                  j                  s|j                  j                         d   |_        = |S t        |dd      }|j!                  ||      }|| _        | j#                  |       d| _        |j$                  r| j'                  |||      }|S )z:Create a fused function for a DefNode with fused argumentsr   )	FusedNodezFused lambdas not allowedzCannot nest fused functionsr   r  N)r  r  fused_functionr  r  r   rK   r   r   PassStatNoders   rp   r  r  get_fused_typesrg  FusedCFuncDefNoderF   r  r  )r%   rp  r&   r  r   r  s         r'   _create_fused_functionz3AnalyseDeclarationsTransform._create_fused_function  s   $..""$*@*@@>>$(($?@$(($AB""&&t':':;**4884DIyy =88$$"xx779!<CH= KT<6
**45"4 "<<##JT:Dr*   c                 L   |j                   r|j                  rt        j                  |j                  j
                  |j                  t        j                  |j                  j
                        t        j                  |j                  j
                              |_        yyy)z8Handle cleanup for 'with gil' blocks in nogil functions.)rs   r   finally_except_clauseN)r  has_with_gil_blockr   NogilTryFinallyStatNoders   rK   EnsureGILNode)r%   lenvr&   s      r'   _handle_nogil_cleanupz2AnalyseDeclarationsTransform._handle_nogil_cleanup9  sk    ::$11
 55		YY$22499==A&+&9&9$))--&H	JDI 2:r*   c                    |j                   rm|j                  rad|_        t        |j                  d       t	        j
                  |j                  g t	        j                  |j                              |_        |j                  S )NFzFused generators not supported)rL   rs   )is_generatorhas_fused_argumentsr   rK   r   rM   r  gbodyr$   s     r'   _handle_fusedz*AnalyseDeclarationsTransform._handle_fusedF  sf    !9!9',D$$((<=++DHH24161C1CDHH1MODJ '''r*   c                    | j                         }| j                  j                  t                      |j                  }|j                  |       |j                  j                         D ]^  \  }}|j                  |      r|j                  |      }|r|j                  |||j                         It        |j                  d       ` | j                  |      r| j                  ||      }n>|j                  j!                  |       | j#                  ||       | j%                  |       | j                  j'                          |S )a  
        Analyse a function and its body, as that hasn't happened yet.  Also
        analyse the directive_locals set by @cython.locals().

        Then, if we are a function with fused arguments, replace the function
        (after it has declared itself in the symbol table!) with a
        FusedCFuncDefNode, and analyse its children (which are in turn normal
        functions). If we're a normal function, just analyse the body of the
        function.
        
Not a type)r  r  r   r   local_scopedeclare_argumentsr  r   r  analyse_as_typedeclare_varrK   r   r  r  rs   r  r  r  r@  )r%   r&   rp  r  var	type_noder  s          r'   r1  z.AnalyseDeclarationsTransform.visit_FuncDefNodeP  s     ##CE*t$ #3399; 	7NC##C( 006$$S$	>)--6	7 d#..sD9DII**40&&tT2))$/  "r*   c                 Z   | j                  |      }| j                         }t        |t        j                        r|j
                  r|j                  }t        |t        j                        r)|j                  s|j                  s|j                  |      s|S || j                  ||      gS r"   )r1  r  ry   r   r}   
is_wrapperparent_scopefused_py_funcis_generator_bodyneeds_assignment_synthesis_synthesize_assignmentr%   r&   rp  s      r'   rX  z*AnalyseDeclarationsTransform.visit_DefNodet  s    %%d+ dEMM*t""C4/""d&<&<33C8Kd11$<==r*   c                 $    | j                  |      S r"   )r1  r$   s     r'   visit_GeneratorBodyDefNodez7AnalyseDeclarationsTransform.visit_GeneratorBodyDefNode  s    %%d++r*   c                    |}|j                   s|j                  r&|j                  }|j                   r|j                  r&|j                  rRt	        j
                  |j                  ||j                  j                  t	        j                  |            x}|_
        ns| j                  j                  d      }t        j                  j                  ||      }|j                  |_        |j                   r|j                  |j"                  _        |j                   rd|_        |j$                  |_        | j)                  |||      S )N)r~   pymethdef_cnamecode_objectr  T)is_py_class_scopeis_c_class_scopeouter_scopeis_closure_scoper   InnerFunctionNoderK   r  r  CodeObjectNodepy_cfunc_nodecurrent_directivesr   r  r  r  r  r  r  is_cyfunctionr  )r%   r&   rp  genvr   r  s         r'   r  z3AnalyseDeclarationsTransform._synthesize_assignment  s   $$(=(=##D $$(=(=   '0'B'B4 $

 : :%44T:(< <C$$
 --11)<G++88wGC"D  )-)9)9

&  CK [[&&tS#66r*   c                 l   |j                   rK|j                   d d d   D ]/  }t        j                  |j                  |j                  |g      }1 d |_         t        j                  |j                  t        j                  |j                  |j                        |      }|j                  |       |S )Nr   r^  r   r   )
r  r   r%  rK   r  r   r   r   ro   r  )r%   r~   r   rp  r  assmts         r'   r  z/AnalyseDeclarationsTransform._create_assignment  s    %0026 "	..MM(225""
 #'H**LL""8<<hmmD 	""3'r*   c                    | j                         }|j                  |       |j                  r| j                  j	                  t        | j                  d                | j                  ||j                         |j                  |j                         | j                  |       | j                          | j                  j                          |S |j                  |       | j                  |       |S r  )r  r  r   r  r   r   r  
expr_scopeanalyse_scoped_declarationsrF   r  r@  r	  s      r'   visit_ScopedExprNodez1AnalyseDeclarationsTransform.visit_ScopedExprNode  s     !!#&  ''D,@,@,D(EFT4??3,,T__=t$OO  $$&  ,,S1t$r*   c                 f    | j                  |       |j                  | j                                |S r"   )rF   r  r  r$   s     r'   visit_TempResultFromStatNodez9AnalyseDeclarationsTransform.visit_TempResultFromStatNode  s,    4 !!$"2"2"45r*   c                 D    |j                   dk(  ry | j                  |      S )Nr  )r.  r  r$   s     r'   r  z/AnalyseDeclarationsTransform.visit_CppClassNode  s"    ??h&**400r*   c                      	 y r"   )*r   r  rK   r   r   r  r  r`  r  r   ro   r  init_assignmentr  r   struct_or_union_wrapperr   rM   r  	is_structr*  r  replacerL   r|  shadowrs   ry   r  CSimpleBaseTypeNoder}   rp   kw_onlyr   rX  
declaratorr  basic_pyobject_propertybasic_propertyr  r  r#  )r%   r&   
self_valuer  
attributesr  init_assignmentsattr
str_formatwrapper_class
class_bodyinit_methodarg_templater   templaterE  s                   r'   r4   z8AnalyseDeclarationsTransform.visit_CStructOrUnionDefNode  s
     r*   c                 (    | j                  |       |S r"   rF   r$   s     r'   r.   z2AnalyseDeclarationsTransform.visit_CDeclaratorNode  s    4 r*   c                     |S r"   r#   r$   s     r'   r(   z/AnalyseDeclarationsTransform.visit_CTypeDefNode  r)   r*   c                      y r"   r#   r$   s     r'   r0   z0AnalyseDeclarationsTransform.visit_CBaseTypeNode  s    r*   c                 &    |j                   dk(  r|S y )Npublic)r.  r$   s     r'   r2   z/AnalyseDeclarationsTransform.visit_CEnumDefNode  s    ??h&Kr*   c                 F   |j                   | j                  d   v rt| j                         j                  |j                         }|%|j                  dk7  r:|j
                  j                  s$t        |j                  d|j                   z  d       | j                  |       |S )Nr   r  z,cdef variable '%s' declared after it is usedr   )
ro   r  r  r  r.  r`  r  r   rK   rF   )r%   r&   r  s      r'   visit_CNameDeclaratorNodez6AnalyseDeclarationsTransform.visit_CNameDeclaratorNode$  s    99,,R00$$&--dii8E!1!1X!=44"PSWS\S\"\^_`4 r*   c                 &    | j                  |       y r"   r8  r$   s     r'   r,   z.AnalyseDeclarationsTransform.visit_CVarDefNode-  s    4 r*   c                     | j                  |j                        }|sy t        |      t        u r|d   |_        |g|dd  z   S ||_        |S )Nr   r   )r  r&   r  r   
child_node)r%   r&   rA  s      r'   visit_CnameDecoratorNodez5AnalyseDeclarationsTransform.visit_CnameDecoratorNode2  sQ    ZZ		*

t#(mDO6JqrN**	r*   c           
         |j                   dk(  r0|j                  j                  r| j                  }n(| j                  }n|j                   dk(  r| j
                  }j                  dt        j                  |j                  t        j                  |j                  d      |j                        i|j                        j                  d   }|j                  |_        |j                  |_        |S )	Nr<  readonlyATTRr%   )rK   ro   )rK   r=  r  )rK   r   )r.  r  r  r+  r,  basic_property_ror  r   r  rK   r   ro   rL   rt   )r%   r  r6  rE  s       r'   r  z,AnalyseDeclarationsTransform.create_Property<  s    x'zz%%77..+--H&&00UYY5>5G5GEII\b5c;@::G( 99	 '  $eA	'
 

yyr*   ))r5   r6   r7   r   r:   r,  r+  rF  r$  r#  r  r  r  r  ri  r   r#  r  r  r  r  r  r  r1  rX  r  r  r  r  r   r  r4   r.   r(   r0   r2   r>  r,   rB  r  ra   rb   s   @r'   r  r  
  s>   ! # M$$7#8:N + , M$$7#8: % & M$$7#8	: + , !&') # $ !&'	)O NI*$D0L4$<J("H	>,70  
1H7\

r*   r  c                 2   dj                  |       j                  d      }t        j                  dk\  rddini }g }dD ]A  }	 t	        t
        |      } ||fi |j                         }|j                  d|d d z          C |S # t        t        f$ r Y Ww xY w)	N zutf-8)rr  	   usedforsecurityF)md5sha256sha10xr  )
r  encoder   version_inforg  r   	hexdigestAttributeError
ValueErrorr   )member_namesmember_names_stringhash_kwargsr  	algo_name
mkchecksumr  s          r'   r  r  N  s     ((<077@030@0@F0J$e,PRKI. .		 )4J!"5EEOOQH 	!,-. 	 
+ 		s   'BBBc                   T     e Zd ZdZ fdZddZd Zd Zd Zd Z	d Z
d	 Zd
 Z xZS ) CalculateQualifiedNamesTransformz^
    Calculate and store the '__qualname__' and the global
    module name on some nodes.
    c                     | j                         j                  | _        g | _        t        t        |       }|j
                  | _        |j                  | _        | j                  |       |S r"   )
global_scoper  r9  r=   rZ  r1  r  r  _super_visit_ClassDefNoderF   )r%   r&   _superrC   s      r'   ri  z1CalculateQualifiedNamesTransform.visit_ModuleNodee  s^    ,,.== 7>(.(@(@%)/)B)B&4 r*   c                     |r!| j                   d d  }|j                  |       n| j                   }t        dj                  |            |_        | j
                  |_        y )Nro  )r  r   r   r  qualnamer9  )r%   r&   ro   r`  s       r'   _set_qualnamez.CalculateQualifiedNamesTransform._set_qualnamen  sO    **1-HOOD!**H%chhx&89++r*   c                     |j                   r|j                  s|j                  g| _        y | j                  j	                  |j                         y r"   )is_pyglobalis_pyclass_attrro   r  r   )r%   r  s     r'   _append_entryz.CalculateQualifiedNamesTransform._append_entryw  s9    U%:%:#(::,D&&uzz2r*   c                 `    | j                  ||j                         | j                  |       |S r"   )ra  ro   rF   r$   s     r'   visit_ClassNodez0CalculateQualifiedNamesTransform.visit_ClassNode}  s)    4+4 r*   c                 J    | j                  |       | j                  |       |S r"   )ra  rF   r$   s     r'   visit_PyClassNamespaceNodez;CalculateQualifiedNamesTransform.visit_PyClassNamespaceNode  s#    4 4 r*   c                 `   | j                   d d  }|j                  j                  rJ| j                   r>| j                   d   dk(  r,| j                   j                          | j	                  |       n&| j	                  ||j                  j
                         | j                  |       || _         |S Nr   <locals>)r  r~   r  r@  ra  ro   rF   r%   r&   orig_qualified_names      r'   visit_PyCFunctionNodez6CalculateQualifiedNamesTransform.visit_PyCFunctionNode  s    "11!4==##(;(;@S@STV@W[e@e##%t$tT]]%7%784 1r*   c                 ~   |j                   r| j                  rw| j                  d   dk(  sJ | j                         | j                  d d  }| j                  j                          | j                  |       | j	                  |       || _        |S | j                  ||j
                         | j                  |       |S rk  )r  r  r@  ra  r  ro   r1  rm  s      r'   rX  z.CalculateQualifiedNamesTransform.visit_DefNode  s    ??t22&&r*j8M$:M:MM"&"5"5a"8##%t$))$/"5D  tTYY/""4(r*   c                    | j                   d d  }t        |dd       dk(  r| j                   j                  d       n| j                  |j                         | j                   j                  d       | j                  |       || _         |S )Nro   z<lambda>rl  )r  rg  r   re  r  r  rm  s      r'   r1  z2CalculateQualifiedNamesTransform.visit_FuncDefNode  sy    "11!44&*4&&z2tzz*"":.%%d+1r*   c                     | j                   d d  }t        |dd       xs) | j                         j                  |j                        }| j                  |       | j                  |       || _         |S )Nr  )r  rg  r  r  ro   re  r]  )r%   r&   rn  r  s       r'   r  z3CalculateQualifiedNamesTransform.visit_ClassDefNode  sm    "11!4w- ;!!#//		: 	5!&&t,1r*   r"   )r5   r6   r7   r8   ri  ra  re  rg  ri  ro  rX  r1  r  ra   rb   s   @r'   rZ  rZ  `  s5    ,3
		r*   rZ  c                   $    e Zd Zd Zd Zd Zd Zy)AnalyseExpressionsTransformc                     |j                   j                          |j                  j                  |j                         |_        | j	                  |       |S r"   )r`  infer_typesrs   analyse_expressionsrF   r$   s     r'   ri  z,AnalyseExpressionsTransform.visit_ModuleNode  s?    

 II11$**=	4 r*   c                     |j                   j                          |j                  j                  |j                         |_        | j	                  |       |S r"   )r  rv  rs   rw  rF   r$   s     r'   r1  z-AnalyseExpressionsTransform.visit_FuncDefNode  sC    $$&II11$2B2BC	4 r*   c                     |j                   r5|j                  j                          |j                  |j                        }| j	                  |       |S r"   )r   r  rv  analyse_scoped_expressionsrF   r$   s     r'   r  z0AnalyseExpressionsTransform.visit_ScopedExprNode  sB    OO'')224??CD4 r*   c                     | j                  |       |j                  r"|j                  j                  s|j                  }|S )a  
        Replace index nodes used to specialize cdef functions with fused
        argument types with the Attribute- or NameNode referring to the
        function. We then need to copy over the specialization properties to
        the attribute or name node.

        Because the indexing might be a Python indexing operation on a fused
        function, or (usually) a Cython indexing operation, we need to
        re-analyse the types.
        )rA  is_fused_indexr  is_errorr   r$   s     r'   visit_IndexNodez+AnalyseExpressionsTransform.visit_IndexNode  s3     	tyy'9'999Dr*   N)r5   r6   r7   ri  r1  r  r~  r#   r*   r'   rt  rt    s    r*   rt  c                       e Zd Zd Zd Zy)FindInvalidUseOfFusedTypesc                     |j                   sK|j                  s.|j                  j                  rt	        |j
                  d       |S | j                  |       |S )Nz-Return type is not specified as argument type)r  r  return_typer  r   rK   rF   r$   s     r'   r1  z,FindInvalidUseOfFusedTypes.visit_FuncDefNode  sO     ''))d.>.>.G.Gdhh OP  ""4(r*   c                     |j                   r.|j                   j                  rt        |j                  d       |S | j	                  |       |S )Nz6Invalid use of fused types, type cannot be specialized)r  r  r   rK   rF   r$   s     r'   rH   z)FindInvalidUseOfFusedTypes.visit_ExprNode  s@    99++$((TU  t$r*   N)r5   r6   r7   r1  rH   r#   r*   r'   r  r    s    	r*   r  c                       e Zd Zd Zd Zy)ExpandInplaceOperatorsc                   	 |j                   }|j                  }|j                  j                  r|S t	        |t
        j                        r|S | j                         }d	fd			  	|d      \  }} |j                  di |j                  }t        j                  |j                  |j                  ||d      }|j                  |       |j                  |       |j!                  |       t#        j$                  |j                  ||j'                  |j                  |            }|j)                          |D ]  }t+        ||      } |S # t        $ r |cY S w xY w)Nc                 8   | j                   r| g fS | j                  j                  r|st        |       } | | gfS | j                  rR | j
                        \  }}t        | j                        }t        j                  | j                  ||      ||gz   fS | j                  rC | j                        \  }}t        j                  | j                  || j                        |fS t        | t        j                        rt!        d      t        |       } | | gfS )N)r   index)r=  r  z@Don't allow things like attributes of buffer indexing operations)r   r  r  r   is_subscriptr   r  r   	IndexNoderK   rK  r=  r  r  ry   BufferIndexNoderS  )r&   settingr   tempsr  r=  side_effect_free_references         r'   r  zVExpandInplaceOperators.visit_InPlaceAssignmentNode.<locals>.side_effect_free_reference  s    ||Rx&&w!$'dV|#""8Ce"4::. **488$eLeW\V]o]]""7A
U ..txxSDNN[]bbbD)";";< !cdd!$'dV|#r*   T)r  )operatoroperand1operand2inplacer   r`   r#   )r   r   r  is_cpp_classry   r   r  r  rS  rC   r  
binop_noderK   r  analyse_target_typesanalyse_typesanalyse_operationr   r   	coerce_toreverser
   )
r%   r&   r   r   rp  let_ref_nodesdupbinoptr  s
            @r'   visit_InPlaceAssignmentNodez2ExpandInplaceOperators.visit_InPlaceAssignmentNode  s@   hhhh88  Kc9445K 	$$	!;C!NC cmm+cll+$$TXX040303-1	3 	  %#$))HH#.0
 	 	$A1d#D	$)  	K	s   %E EEc                     |S r"   r#   r$   s     r'   rH   z%ExpandInplaceOperators.visit_ExprNode(	  r5  r*   N)r5   r6   r7   r  rH   r#   r*   r'   r  r    s    3jr*   r  c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	AdjustDefByDirectivesz
    Adjust function and class definitions by the decorator directives:

    @cython.cfunc
    @cython.cclass
    @cython.ccall
    @cython.inline
    @cython.nogil
    c                 X    |j                   | _         d| _        | j                  |       |S r<   )rZ  in_py_classrF   r$   s     r'   ri  z&AdjustDefByDirectives.visit_ModuleNode8	  s(    // 4 r*   c                 p    | j                   }|j                   | _         | j                  |       || _         |S r"   )rZ  rF   )r%   r&   r  s      r'   rq  z2AdjustDefByDirectives.visit_CompilerDirectivesNode>	  s0    //4 (r*   c                    g }d| j                   v r|j                  d       | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|"| j                   d   r|j                  }|	|d}n|d}d| j                   v r'|j	                  d	||||
      }| j                  |      S d| j                   v rJ| j                  rt        |j                  d       n'|j	                  d||||
      }| j                  |      S d|v rt        |j                  d       |rt        |j                  d       | j                  |       |S )Nr'  r  r  returnsannotation_typingrE   r<   ccallT)overridabler,  r  r  
except_valcfuncz#cfunc directive is not allowed hereFz,Python functions cannot be declared 'inline'z+Python functions cannot be declared 'nogil')
rZ  r   r   return_type_annotationas_cfunctionr  r  r   rK   rF   )r%   r&   r,  r  r  return_type_nodes         r'   rX  z#AdjustDefByDirectives.visit_DefNodeE	  si   	t&X&##G,__((5
??..y9#8K(L#::+
0B)
&Jdoo%$$ IU(Z % AD ::d##doo%dhh EF(( %%, ) E zz$''y $((JK$((IJ4 r*   c                     |S r"   r#   r$   s     r'   r   z&AdjustDefByDirectives.visit_LambdaNodei	  r5  r*   c                     d| j                   v r!|j                         }| j                  |      S | j                  }d| _        | j	                  |       || _        |S )Nr   T)rZ  	as_cclassr  r  rF   r%   r&   old_in_pyclasss      r'   r  z*AdjustDefByDirectives.visit_PyClassDefNodem	  sW    t&>>#D::d##!--N#Dt$-DKr*   c                 \    | j                   }d| _         | j                  |       || _         |S r<   )r  rF   r  s      r'   r#  z)AdjustDefByDirectives.visit_CClassDefNodex	  s0    )) 4 )r*   N)
r5   r6   r7   r8   ri  rq  rX  r   r  r#  r#   r*   r'   r  r  -	  s&    "H	r*   r  c                   6    e Zd ZdZd Zd Zd	dZd Zd Zd Z	y)
AlignFunctionDefinitionszq
    This class takes the signatures from a .pxd file and applies them to
    the def methods in a .py file.
    c                     |j                   | _         |j                  | _        t               | _        | j	                  |       |S r"   )r`  rZ  r   r  rF   r$   s     r'   ri  z)AlignFunctionDefinitions.visit_ModuleNode	  s5    ZZ
//!e4 r*   c                 |   | j                   j                  |j                        }|r|j                  r | j	                  |j                         |      S |j                   r|j                   j                  sFt        |j                  d|j                  z         |j                  rt        |j                  d       y |S )N'%s' redeclaredprevious declaration here)	r`  r  ro   	is_cclassr#  r  is_builtin_scoper   rK   r%   r&   pxd_defs      r'   r  z-AlignFunctionDefinitions.visit_PyClassDefNode	  s    **##DII.  //0@'JJ]]'--*H*Hdhh 1DII =>;;'++'BCr*   Nc                     |%| j                   j                  |j                        }|r5|j                  s|S | j                   }|j                  j                   | _         | j                  |       |r| _         |S r"   )r`  r  r|  defined_in_pxdr  rF   )r%   r&   r  r  s       r'   r#  z,AlignFunctionDefinitions.visit_CClassDefNode	  sg    ?jj''8G))**K ++DJ4 $DJr*   c                 4   | j                   j                  |j                        }|r|j                   r|j                   j                  se|j                  sFt        |j                  d|j                  z         |j                  rt        |j                  d       y |j                  |      }|S | j                   j                  rS| j                  d   rD|j                  | j                  vr,|j                         r|j                  | j                         }|S )Nr  r  
auto_cpdef)r`  )r`  r  ro   r  is_cfunctionr   rK   r  is_module_scoperZ  r  is_cdef_func_compatibler  s      r'   rX  z&AlignFunctionDefinitions.visit_DefNode	  s    **##DII.GMM1O1O''dhh 1DII =>;;'++'BC$$W-D  jj((T__\-Jii4#6#66..0$$4::$6D r*   c                     | j                   j                  r/|j                  D ]   \  }}| j                  j	                  |       " |S r"   )r`  r  r   r  r   )r%   r&   ro   r   s       r'   r  z1AlignFunctionDefinitions.visit_FromImportStatNode	  sA     ::%%:: .a##''-.r*   c                     |S r"   r#   r$   s     r'   rH   z'AlignFunctionDefinitions.visit_ExprNode	  r5  r*   r"   )
r5   r6   r7   r8   ri  r  r#  rX  r  rH   r#   r*   r'   r  r  	  s%    

$r*   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)RemoveUnreachableCodec                 x   | j                   d   s|S | j                  |       t        |j                        D ]  \  }}|dz  }|j                  s|t        |j                        k  rG| j                   d   r$t        |j                  |   j                  dd       |j                  d | |_        d|_         |S  |S )Nremove_unreachabler   warn.unreachableUnreachable coder   T)r  rF   r   rL   is_terminatorr   r   rK   )r%   r&   idxr\  s       r'   rR   z(RemoveUnreachableCode.visit_StatListNode	  s    &&';<K4 "4::. 	IC1HC!!TZZ(../AB

3 3 35GK!%DS!1DJ%)"	 r*   c                 b    | j                  |       |j                  j                  rd|_        |S rE   )rF   rs   r  r$   s     r'   visit_IfClauseNodez(RemoveUnreachableCode.visit_IfClauseNode	  s*    4 99""!%Dr*   c                     | j                  |       |j                  r=|j                  j                  r'|j                  D ]  }|j                  r |S  d|_        |S rE   )rF   r  r  r  )r%   r&   clauses      r'   visit_IfStatNodez&RemoveUnreachableCode.visit_IfStatNode	  s\    4  0 0 > >// *++ * &*"r*   c                     | j                  |       |j                  j                  rC|j                  r7| j                  d   r!t        |j                  j                  dd       d |_        |S )Nr  r  r   )rF   rs   r  r  r  r   rK   r$   s     r'   visit_TryExceptStatNodez-RemoveUnreachableCode.visit_TryExceptStatNode	  sZ    4 99""t'7'7&&'9:((,,.@!D#Dr*   c                 b    | j                  |       |j                  j                  rd|_        |S rE   )rF   r   r  r$   s     r'   visit_TryFinallyStatNodez.RemoveUnreachableCode.visit_TryFinallyStatNode	  s,    4 ,,!%Dr*   N)r5   r6   r7   rR   r  r  r  r  r#   r*   r'   r  r  	  s    r*   r  c                   `     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Z xZS )ru   c                     t         t        |           g | _        g | _        g | _        g | _        d| _        d| _        d| _	        y r<   )
r=   ru   r>   yieldsr  finallysexceptshas_return_valuerw   rx   r6  s    r'   r>   zYieldNodeCollector.__init__	  sC     $02 %r*   c                 &    | j                  |       y r"   r8  r$   s     r'   rA  zYieldNodeCollector.visit_Node
  s    4 r*   c                 j    | j                   j                  |       d| _        | j                  |       y rE   )r  r   rw   rF   r$   s     r'   visit_YieldExprNodez&YieldNodeCollector.visit_YieldExprNode
  )    4 4 r*   c                 j    | j                   j                  |       d| _        | j                  |       y rE   )r  r   rx   rF   r$   s     r'   visit_AwaitExprNodez&YieldNodeCollector.visit_AwaitExprNode	
  r  r*   c                     | j                  |       |j                  rd| _        | j                  j	                  |       y rE   )rF   rn   r  r  r   r$   s     r'   visit_ReturnStatNodez'YieldNodeCollector.visit_ReturnStatNode
  s1    4 ::$(D!D!r*   c                 \    | j                  |       | j                  j                  |       y r"   )rF   r  r   r$   s     r'   r  z+YieldNodeCollector.visit_TryFinallyStatNode
  s"    4 T"r*   c                 \    | j                  |       | j                  j                  |       y r"   )rF   r  r   r$   s     r'   r  z*YieldNodeCollector.visit_TryExceptStatNode
  s"    4 D!r*   c                      y r"   r#   r$   s     r'   r  z%YieldNodeCollector.visit_ClassDefNode
      r*   c                      y r"   r#   r$   s     r'   r1  z$YieldNodeCollector.visit_FuncDefNode
  r  r*   c                      y r"   r#   r$   s     r'   r   z#YieldNodeCollector.visit_LambdaNode"
  r  r*   c                      y r"   r#   r$   s     r'   r   z0YieldNodeCollector.visit_GeneratorExpressionNode%
  r  r*   c                      y r"   r#   r$   s     r'   visit_CArgDeclNodez%YieldNodeCollector.visit_CArgDeclNode(
  s     	r*   )r5   r6   r7   r>   rA  r  r  r  r  r  r  r1  r   r   r  ra   rb   s   @r'   ru   ru   	  s?    !!
!
"#"r*   ru   c                   *    e Zd Zd Zd Zd Zd Zd Zy)MarkClosureVisitorc                 6    d| _         | j                  |       |S r<   needs_closurerF   r$   s     r'   ri  z#MarkClosureVisitor.visit_ModuleNode0
  s    "4 r*   c                 z   d| _         | j                  |       | j                   |_         d| _         t               }|j                  |       |j                  rrt        j
                  }|j                  r6t        j                  }|j                  |j                  z   D ]	  }d|_
         n| j                  d   rpt        j                  }n_|j                  r4t        d |j                  D              }t        |j                   d       |S |j                  rt        j"                  }n|S t%        |j                  d      D ]  \  }}||_         |j                  |j(                  z   |j*                  z   D ]	  }d|_         t	        j.                  |j                   |j0                  |j2                  |j                  xr |j                        } ||j                   |j0                  |j4                  |j6                  |j8                  |j:                  |j<                  ||j>                  |j@                  
      }	|	S )	NFTiterable_coroutinec              3   :   K   | ]  }|j                   s|  y wr"   )is_await)r  ys     r'   r  z7MarkClosureVisitor.visit_FuncDefNode.<locals>.<genexpr>G
  s     Cq

Cs   z/'await' not allowed in generators (use 'yield')r   )rK   ro   rs   is_async_gen_body)
rK   ro   rp   rq   rr   rt   r  r  lambda_namer  )!r  rF   ru   r   r   AsyncDefNoderw   AsyncGenNoder  r  in_async_genr  IterableAsyncDefNoderx   nextr   rK   GeneratorDefNoder   	label_numr  r  in_generatorGeneratorBodyDefNodero   rs   rp   rq   rr   rt   r  r  r  )
r%   r&   r   coroutine_type
yield_exprfoundr   retnoder  	coroutines
             r'   r1  z$MarkClosureVisitor.visit_FuncDefNode5
  s   "4 !//!&(	%"//N""!&!3!3"+"2"2Y5F5F"F 3J.2J+3(()=>!&!;!;  CI$4$4CCE%))NOK  "33NK&y'7'7; 	%MAz#$J 	% ((9+=+==	@Q@QQ 	(G#'G 	( **tyytyy"//GI4G4GI #tyytyy]]1B1BT__T%5%5#'#>#>@	 r*   c                     d| _         | j                  |       | j                   |_         d| _         |j                   r"|j                  rt        |j                  d       |S )NFTz1closures inside cpdef functions not yet supported)r  rF   r  r   rK   r$   s     r'   visit_CFuncDefNodez%MarkClosureVisitor.visit_CFuncDefNode_
  sS    "4 !//!$"2"2$((OPr*   c                 f    d| _         | j                  |       | j                   |_         d| _         |S )NFTr  r$   s     r'   r   z#MarkClosureVisitor.visit_LambdaNodeh
  s3    "4 !//!r*   c                 6    | j                  |       d| _        |S rE   )rF   r  r$   s     r'   r  z%MarkClosureVisitor.visit_ClassDefNodeo
  s    4 !r*   N)r5   r6   r7   ri  r1  r  r   r  r#   r*   r'   r  r  .
  s    
(Tr*   r  c                   J     e Zd Z fdZd Zd Zd	dZd Zd Zd Z	d Z
 xZS )
CreateClosureClassesc                 H    t         t        |   |       g | _        d| _        y r<   )r=   r  r>   pathr  rA   s     r'   r>   zCreateClosureClasses.__init__y
  s!    "D27;	r*   c                 J    |j                   | _        | j                  |       |S r"   )r`  rg  rF   r$   s     r'   ri  z%CreateClosureClasses.visit_ModuleNode~
  s!     JJ4 r*   c                    g }g }|j                   j                         D ]g  }|j                  j                         D ]H  \  }}|s	|j                  r|j                  ||f       )|j                  s6|j                  ||f       J i ||fS r"   )r  iter_local_scopesrz  r   from_closurer   
in_closure)r%   r&   r  r  r`  ro   r  s          r'   find_entries_used_in_closuresz2CreateClosureClasses.find_entries_used_in_closures
  s    
%%779 	5E$}}224 5e%% ''u6%%%%tUm45	5 Z''r*   c                    |j                   rl|j                  j                         D ]O  }|j                  j	                         D ]0  }|j
                  r|j                  r|j                  r*d|_        2 Q | j                  |      \  }}|j                          d|_        d|_        |j                  }|j                  j                  }	|	j                  s|	j                   r&|	j"                  }	|	j                  r|	j                   r&|sA| j$                  s|r3|s#|j&                  st)        d      |j&                  }d|_        d|_        |j                   rn'|s|sy |s d|_        |	j.                  |_        d|_        y |j1                  t2        j4                        d|j                  j6                  j9                  dd      }
|j;                  |
|j<                  dd      }d|j>                  _         ||_        |j>                  j                  }d|_!        d|_"        |jF                  s|j                   rd|jH                  d<   tJ        jL                  rtJ        jL                  |jH                  d	<   |re|	jN                  sJ |jQ                  |j<                  t2        jR                  t2        jR                  |	j.                  j>                  d
       d|_        |D ]e  \  }}|jQ                  |j<                  |jT                  s|jV                  nd |j6                  |j>                  d
      }|jX                  s_d|_,        g d|_        |j[                  |j.                         y )NTFz%DefNode does not have assignment noder   ro  __)ro   rK   definingimplementingno_gc_clearfreelist)rK   ro   rf  r  is_cdefr   ).r  r  r  rz  valuesr  rc  
is_cglobalr  r  r  r  needs_outer_scoper  r`  r  r  r  r  r  r   needs_self_codeis_passthroughscope_classnext_idr   closure_class_prefixrf  r&  declare_c_classrK   r  is_final_typeis_internalis_closure_class_scoper   rZ  r   closure_freelist_sizer  r  outer_scope_cnamein_subscopero   is_declared_genericcheck_c_class)r%   r&   target_module_scope
inner_noder`  r  r  r  
func_scopecscoper:  class_scopero   closure_entrys                 r'   create_class_from_scopez,CreateClosureClasses.create_class_from_scope
  s   ));;= 0"]]113 0E!..%2C2CuGWGW+/(00
 $(#E#Ed#K j #!&%%
!!&&&*A*A''F &&&*A*A j))'(OPP!//
).J&%*D"L(,J%%+%7%7J"%)D"  ''(C(CDJJ$$S.0 $33dhh 4  $(

 !&
jj&&"&-1* 1 148K""=1((181N1NK"":.***##)/)A)A*0*B*B)/););)@)@,0	 $ 2
 &*D"% 	6KD%'33II','8'8UZZdkkZZ 4 M ((451	6 "))**@*@Ar*   c                     t        |j                  t        j                        s|S | j                  }d| _        | j                  |j                  | j                  |       | j                  |       || _        |S rE   )ry   r~   r   r}   r  r6  rg  rF   )r%   r&   was_in_lambdas      r'   r   z%CreateClosureClasses.visit_LambdaNode
  s_    $--7K$$T]]D4E4EtL4 &r*   c                 8   | j                   r| j                  |       |S |j                  s| j                  rb| j	                  || j
                         | j                  j                  |       | j                  |       | j                  j                          |S r"   )r  rF   r  r  r6  rg  r   r@  r$   s     r'   r1  z&CreateClosureClasses.visit_FuncDefNode
  ss    >>t$K((t/@/@AIIT"t$IIMMOr*   c                 (    | j                  |       |S r"   r8  r$   s     r'   r  z/CreateClosureClasses.visit_GeneratorBodyDefNode
      4 r*   c                 b    |j                   s| j                  |      S | j                  |       |S r"   )r  r1  rF   r$   s     r'   r  z'CreateClosureClasses.visit_CFuncDefNode
  s/    ))$//t$Kr*   r"   )r5   r6   r7   r>   ri  r  r6  r   r1  r  r  ra   rb   s   @r'   r  r  u
  s.    

(OBb
	r*   r  c                   X     e Zd ZdZ fdZd Zd Zd Zd Zd Z	e
j                  Z xZS )InjectGilHandlinga
  
    Allow certain Python operations inside of nogil blocks by implicitly acquiring the GIL.

    Must run before the AnalyseDeclarationsTransform to make sure the GILStatNodes get
    set up, parallel sections know that the GIL is acquired inside of them, etc.
    c                 8    d| _         t        t        |   |      S r<   )r  r=   r>  r  r%   r  rC   s     r'   r  zInjectGilHandling.__call__  s    
&6t<<r*   c                 b    | j                   r"t        j                  |j                  d|      }|S )zRAllow raising exceptions in nogil sections by wrapping them in a 'with gil' block.r  r  )r  r   r  rK   r$   s     r'   visit_RaiseStatNodez%InjectGilHandling.visit_RaiseStatNode  s&    ::$$TXXUFDr*   c                 v    | j                   }|j                  dk(  | _         | j                  |       || _         |S )Nr  )r  r  rF   r%   r&   	was_nogils      r'   visit_GILStatNodez#InjectGilHandling.visit_GILStatNode  s5    JJ	jjG+
4 
r*   c                     | j                   }t        |j                  t        j                        r4|j                  j                   xr |j                  j
                   | _         | j                  |       || _         |S r"   )r  ry   r*  r   CFuncDeclaratorNodewith_gilrF   rD  s      r'   r  z$InjectGilHandling.visit_CFuncDefNode$  s\    JJ	doou'@'@A..Ot7O7O3ODJ4 
r*   c                 p    | j                   }|j                   | _         | j                  |       || _         |S r"   )r  rF   rD  s      r'   visit_ParallelRangeNodez)InjectGilHandling.visit_ParallelRangeNode,  s0    JJ	ZZ
4 
r*   c                     |S r"   r#   r$   s     r'   rH   z InjectGilHandling.visit_ExprNode3  r5  r*   )r5   r6   r7   r8   r  rB  rF  r  rK  rH   r   r@  rA  ra   rb   s   @r'   r>  r>    s3    = "55Jr*   r>  c                   L     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Z xZS )
GilChecka,  
    Call `node.gil_check(env)` on each node to make sure we hold the
    GIL when we need it.  Raise an error when on Python operations
    inside a `nogil` environment.

    Additionally, raise exceptions for closely nested with gil or with nogil
    statements. The latter would abort Python.
    c                 j    |j                   g| _        d| _        d| _        t        t
        |   |      S r<   )r`  	env_stackr  nogil_declarator_onlyr=   rN  r  r@  s     r'   r  zGilCheck.__call__D  s3    **
 &+"Xt-d33r*   c                    | j                   }|j                  }|rHt        | j                        dkD  r0| j                  d   j                   | _         | j	                  ||       || _         | j	                  |d |       || _         y )Nr   )attrsr  )r  outer_attrsr   rP  rF   )r%   r&   	gil_staterE  rU  s        r'   _visit_scoped_childrenzGilCheck._visit_scoped_childrenM  sr    JJ	&&3t~~.2+11DJt[1
4t[A
r*   c                 F   | j                   j                  |j                         |j                  j                  }|rd| _        |r'|j
                  r|j                  |j                         | j                  ||       d| _        | j                   j                          |S rQ   )rP  r   r  r  rQ  nogil_checkrW  r@  )r%   r&   inner_nogils      r'   r1  zGilCheck.visit_FuncDefNodeX  s    d../&&,,)-D&4++T--.##D+6 &+"r*   c                    | j                   r|j                  r|j                          | j                   }|j                  dk(  }||k(  r;| j                  s/|st	        |j
                  d       nt	        |j
                  d       t        |j                  t        j                        r|j                  j                  \  |_        | j                  ||       |S )Nr  z3Trying to acquire the GIL while it is already held.z;Trying to release the GIL while it was previously released.)r  rY  r  rQ  r   rK   ry   r   r   rM   rL   rW  )r%   r&   rE  is_nogils       r'   rF  zGilCheck.visit_GILStatNodej  s    ::$**JJ	JJ') )C)Cdhh !0 1 dhh !7 8 d))5+=+=> $(#6#6#<#< D##D(3r*   c                 6   |j                   r:d|_         t        j                  |j                  d|      }| j	                  |      S | j                   st        |j                  d       y |j                  | j                  d          | j                  |       |S )NFr  r  z)prange() can only be used without the GILr   )	r  r   r  rK   rF  r   rY  rP  rF   r$   s     r'   rK  z GilCheck.visit_ParallelRangeNode  s|    ::DJ$$TXXW4HD))$//zz$((GH+,4 r*   c                     | j                   st        |j                  d       y |j                  r|j                  | j                  d          | j                  |       |S )Nz5The parallel section may only be used without the GILr   )r  r   rK   rY  rP  rF   r$   s     r'   visit_ParallelWithBlockNodez$GilCheck.visit_ParallelWithBlockNode  sT    zz$(( & ' T^^B/04 r*   c                     | j                   rt        |t        j                        r| j	                  |      S d|_        d|_        | j                  |       |S )zM
        Take care of try/finally statements in nogil code sections.
        NT)r  ry   r   r  rA  rY  is_try_finally_in_nogilrF   r$   s     r'   r  z!GilCheck.visit_TryFinallyStatNode  sL     zzZe.?.?@??4(('+$4 r*   c                 $   | j                   r6| j                  r*|j                  r|j                  | j                   d          |j                  r| j	                  || j                         n| j                  |       | j                  rd|_        |S )Nr   T)rP  r  rY  rU  rW  rF   in_nogil_contextr$   s     r'   rA  zGilCheck.visit_Node  sm    >>djjT-=-=T^^B/0''djj9t$::$(D!r*   )r5   r6   r7   r8   r  rW  r1  rF  rK  r_  r  rA  ra   rb   s   @r'   rN  rN  :  s0    4	$.
	r*   rN  c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)TransformBuiltinMethodszQ
    Replace Cython's own cython.* builtins by the corresponding tree nodes.
    c                 B    |j                   ry | j                  |       |S r"   )declaration_onlyrF   r$   s     r'   r   z2TransformBuiltinMethods.visit_SingleAssignmentNode  s!      t$Kr*   c                 F    | j                  |       | j                  |      S r"   )rF   visit_cython_attributer$   s     r'   r?  z+TransformBuiltinMethods.visit_AttributeNode  s!    4 **400r*   c                 $    | j                  |      S r"   )ri  r$   s     r'   r  z&TransformBuiltinMethods.visit_NameNode  s    **400r*   c                    |j                         }|r:|dk(  r#t        j                  |j                  d      }|S |dk(  r2ddlm} t        j                  |j                  t        |            }|S |dk(  r!t        j                  |j                        }|S |dv rXt        j                  |j                  t        |      | j                         j                         j                  |      	      }|S t        j                  |      r	 |S | j                  j                   j#                  |      r	 |S t%        |j                  d
|z         |S )NrM  Trm   __version__r   )rl  rN  )r   	frozensetrT  ro   r  z>'%s' not a valid cython attribute or is being used incorrectly)r  r   r  rK   r  rl  r  r   NullNoder   r  builtin_scoper  r   rl  rB   cython_scopelookup_qualified_namer   )r%   r&   r  versions       r'   ri  z.TransformBuiltinMethods.visit_cython_attribute  s7   ,,.	K' ))$(($?  n,5 ++DHHM'<RS  g% ))$((3  EE ))$((y9Q040@0@0B0P0P0R0^0^_h0ik  ,,Y7
 	 **@@K  dhh adm mnr*   c                    | j                         }| j                  |       t        |j                        dk(  r|j                  j	                  t        j                  |j                               |j                  sH|j                  j	                  t        j                  |j                  | j                         |             |S rt  )r  rF   r   rp   r   r   GlobalsExprNoderK   r  LocalsExprNodecurrent_scope_node)r%   r&   r  s      r'   visit_ExecStatNodez*TransformBuiltinMethods.visit_ExecStatNode  s    !4 tyy>QIIY66txx@A''		  ,,$"9"9";TCD r*   c                    | j                         }|j                  |      }|r|S |j                  }|dv r|dk(  rFt        |j                        dkD  r.t        | j                  dt        |j                        z         |S |dk(  r^t        |j                        dkD  r,t        | j                  dt        |j                        z         t        |j                        dkD  r|S t        j                  || j                         |      S t        |j                        dkD  r,t        | j                  dt        |j                        z         t        |j                        dkD  r|S |j                  s|j                  rf|j                  r0| j                         }t        j                  |j                        }nt        j                  |      }t        j                  |      S t        d	 |j                   j#                         D              }|D 	cg c]  }	t        j$                  ||	
       }
}	t        j&                  ||
      S c c}	w )N)r  varsr  r   zGBuiltin 'locals()' called with wrong number of args, expected 0, got %drz  r   zGBuiltin 'vars()' called with wrong number of args, expected 0-1, got %dzFBuiltin 'dir()' called with wrong number of args, expected 0-1, got %dc              3   N   K   | ]  }|j                   s|j                     y wr"   r   )r  r   s     r'   r  z9TransformBuiltinMethods._inject_locals.<locals>.<genexpr>  s      Wcchh Ws   %%rm   r"  )r  r  rK   r   rp   r   r   rv  rw  r  r  r&  r  ru  SortedDictKeysNoderd  rz  r  IdentifierStringNoder  )r%   r&   	func_namer  r  rK   r   locals_dictlocal_namesr   r   s              r'   _inject_localsz&TransformBuiltinMethods._inject_locals  s   !  +Khh**H$TYY!);dhh iDII!' (f$tyy>A%$(($m		N%+ ,tyy>A%K++C1H1H1JDQQ499~!dhh hDII!' (499~!%%)=)=))"557G"+"5"5gll"CK"+";";C"@K 33K@@  WT\\5H5H5J WWK +- 33CsC -E -%%c66-s   I
c                    | j                  |       |j                  dv rgt        |j                  t        j
                        rC|j                  j                  }t        |t        j                        r|j                  }||_        |S )Nnot_in)rF   r  ry   r  r   r|  r   NoneCheckNode)r%   r&   r   s      r'   visit_PrimaryCmpNodez,TransformBuiltinMethods.visit_PrimaryCmpNode  sd    4 ==H$$--)E)EFmm''c9#:#:;''C #r*   c                 $    | j                  |      S r"   )r  r$   s     r'   visit_CascadedCmpNodez-TransformBuiltinMethods.visit_CascadedCmpNode  s    ((..r*   c                    | j                         }|j                  |      }|st        |j                        dk7  r|S |j                  j	                  t        j                  |j                               |j                  sH|j                  j	                  t        j                  |j                  | j                         |             |S rt  )r  r  r   rp   r   r   ru  rK   r  rv  rw  )r%   r&   r~  r  r  s        r'   _inject_evalz$TransformBuiltinMethods._inject_eval  s    !  +C		Na'K		22488<=##II((HHd557?@ r*   c                 Z   | j                         }|j                  |      }|s|j                  r|S | j                         }t	        |t
        j                        r$|j                  rt        | j                        dk  r|S | j                  d   \  }}|j                  rd|_
        d|j                  _        t        j                  |j                  |j                         t        j"                  |j                  |j                  d   j$                        g|_        |S |j&                  r}t        j"                  |j                  |j(                  j$                  |j*                        t        j"                  |j                  |j                  d   j$                        g|_        |S )Nr   rS  T)r  r   r   rn  )r  r  rp   rw  ry   r   r}   r   rP  r  requires_classobj
class_cell	is_activer   ClassCellNoderK   r  r   ro   r  r`  r  )r%   r&   r~  r  r  r~   
class_noder4  s           r'   _inject_superz%TransformBuiltinMethods._inject_super+  sU   !  +DIIK**,8U]]38==!#K"&.."4
K(()-H&.2J!!+''HH8+@+@B""488(--2B2G2GHDI  ))""HH:#3#3#8#8$**, ""488(--2B2G2GH	DI r*   c                 
   |j                   j                         }|r|t        j                  v rzt	        |j
                        dk7  r%t        |j                   j                  d|z         nGt        j                  |   |j                   j                  |j
                  d         }n
|t        j                  v rt	        |j
                        dk7  r%t        |j                   j                  d|z         nt        j                  |   |j                   j                  |j
                  d   |j
                  d         }np|dk(  rt	        |j
                        dk7  r"t        |j                   j                  d	       n1|j
                  d   j                  | j                               }|r<t        j                  |j                   j                  ||j
                  d   d
      }nt        |j
                  d   j                  d       n|dk(  rt	        |j
                        dk7  r"t        |j                   j                  d       nc|j
                  d   j                  | j                               }|r-t        j                  |j                   j                  |      }nt        j                  |j                   j                  |j
                  d         }n|dk(  rt	        |j
                        dk7  r"t        |j                   j                  d       nt        j                  |j                   j                  d|j
                  d   |j
                  d         }d|_        n@|dk(  rt	        |j
                        dk7  r"t        |j                   j                  d       nt        j                  |j                   j                  d|j
                  d   |j
                  d         }d|_        n|dk(  r0t        j"                  |j                  t%        d            |_         n~|dk(  r0t        j"                  |j                  t%        d            |_         nI| j&                  j(                  j+                  |      rn#t        |j                   j                  d|z         | j-                  |       t/        |t        j0                        rp|j                   j2                  rZ|j                   j4                  }|dv r| j7                  ||      S |dk(  r| j9                  ||      S |dk(  r| j;                  ||      S |S )Nr   z%s() takes exactly one argumentr   r  r   z %s() takes exactly two arguments)r  r  rK  Dcast() takes exactly two arguments and an optional typecheck keywordFr  r  	typecheckr  rJ  z#sizeof() takes exactly one argument)arg_typecmodz"cmod() takes exactly two arguments%Tcdivz"cdiv() takes exactly two arguments/r   r   rT  z*'%s' not a valid cython language construct)dirr  rz  evalr=   )r  r  rC  r  r   rp   r   rK   r  r  r  r   TypecastNodeSizeofTypeNodeSizeofVarNoder  	cdivisionr   r   rB   rq  rr  rF   ry   r%  r   ro   r  r  r  )r%   r&   r  r  r~  s        r'   visit_SimpleCallNodez,TransformBuiltinMethods.visit_SimpleCallNodeG  s   ==4466HHHtyy>Q&$--++-ORZ-Z[6HHR))499Q<AD8KKKtyy>Q&$--++-PS[-[\6II(S))DIIaL499UV<YDW$tyy>Q&$--++ac  99Q<778H8H8JKD(55 MM--D$))A,Z_ a diil..=Y&tyy>Q&$--++-ST99Q<778H8H8JKD(778I8ITXY(66t}}7H7HRVR[R[\]R^_V#tyy>Q&$--++-RS$//0A0A3		RSVZV_V_`aVbcD%)DNV#tyy>Q&$--++-RS$//0A0A3		RSVZV_V_`aVbcD%)DNV# ) 2 2488-PUBV W_, ) 2 2488-P^B_ `**@@Jdmm''ChNP 	4 dI445$--:O:O**I55**4;;F"((y99G#))$	::r*   c                 h   |j                   j                         }|dk(  r |j                  j                  }|j                  j                  d       }t        |      dk7  s t        |      dkD  st        |      dk(  r%d|vr!t        |j                   j                  d       n|d   j                  | j                               }|rC|j                  dd      }t        j                  |j                   j                  ||d   |      }nt        |d   j                  d	       | j                  |       |S )
NrK  r   r   r  r  r   Fr  r  )r  r  r  rp   r  rj  r   r   rK   r  r  r   r   r  rF   )r%   r&   r  rp   r  r  r  s          r'   visit_GeneralCallNodez-TransformBuiltinMethods.visit_GeneralCallNode  s   ==446w'',,D&&99$?FD	Q#f+/[A%+V*Cdmm'']_ Aw..t/?/?/AB &

; >I$11))d1gQZ\D $q'++|44 r*   N)r5   r6   r7   r8   r   r?  r  ri  rx  r  r  r  r  r  r  r  r#   r*   r'   re  re    sF    11*	%7N	/8DLr*   re  c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )ReplaceFusedTypeChecksa0  
    This is not a transform in the pipeline. It is invoked on the specific
    versions of a cdef function with fused argument types. It filters out any
    type branches that don't match. e.g.

        if fused_t is mytype:
            ...
        elif fused_t in other_fused_type:
            ...
    c                 `    t         t        |           || _        ddlm}  |d      | _        y )Nr   )ConstantFoldingT)
reevaluate)r=   r  r>   r  Optimizer  r  )r%   r  r  rC   s      r'   r>   zReplaceFusedTypeChecks.__init__  s(    $d46&-(D9r*   c                 F    | j                  |       | j                  |      S )zc
        Filters out any if clauses with false compile time type check
        expression.
        )rF   r  r$   s     r'   r  z'ReplaceFusedTypeChecks.visit_IfStatNode  s!    
 	4 ~~d##r*   c                    t        j                  d      5  |j                  j                  | j                        }|j
                  j                  | j                        }d d d        rrt        j                  |j                  d      }t        j                  |j                  d      }| j                  ||j                  j                        }|j                  }|dv rG| j                  ||j
                  j                        }|j                  |      }|dv }|r|s|s|s|S |S |dv rt        |t        j                        r|j                  }|j                   r"t#        |j                  j                  d       |S |j                   s"t#        |j
                  j                  d	       |S t        j$                  |      }	|	D ]   }
|j                  |
      s|d
k(  r|c S |c S  |dk(  r|S |S |S # 1 sw Y   xY w)NT)ignoreFrm   )isis_not==z!=)r  r  )inr  zType is fusedz-Can only use 'in' or 'not in' on a fused typer  r  )r   local_errorsr  r  r  r  r   r  rK   specialize_typer  same_asry   r   CTypedefTypetypedef_base_typer  r   get_specialized_types)r%   r&   type1type2
false_node	true_nodeopis_sameeqtypesspecialized_types              r'   r  z+ReplaceFusedTypeChecks.visit_PrimaryCmpNode  s     - 	B--//0@0@A%--//0@0@A%	B U"++DHHEBJ!**4884@I((0A0ABEB11,,UDMM4E4EF--.<'GB$$6 3 '' eZ%<%<=!33E>>$--++_=" ! $--++IK  '<<UCE,1 2( ==)9:!Tz'0 0'1 12 X~((_	B 	Bs   AG::Hc                     	 |j                  | j                  j                        S # t        $ r t	        |d       |cY S w xY w)NzType is not specific)
specializer  fused_to_specificKeyErrorr   )r%   r  rK   s      r'   r  z&ReplaceFusedTypeChecks.specialize_type  sA    	??4#3#3#E#EFF 	#-.K	s   $' A Ac                 (    | j                  |       |S r"   r8  r$   s     r'   rA  z!ReplaceFusedTypeChecks.visit_Node  r;  r*   )
r5   r6   r7   r8   r>   r  r  r  rA  ra   rb   s   @r'   r  r    s"    	:$0dr*   r  c                   F     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 xZS )	DebugTransformz9
    Write debug information for this Cython module.
    c                     t         t        |   |       t               | _        | j
                  j                  | _        |j                  | _	        g | _
        d| _        y r<   )r=   r  r>   r   visitedrB   gdb_debug_outputwritertbc_filec_output_filenested_funcdefsregister_stepinto)r%   rB   optionsr   rC   s       r'   r>   zDebugTransform.__init__  sN    nd,W5u ,,55#]]  " "'r*   c                    |j                   | j                  _        t        |j                   |j                  d   j
                  | j                        }| j                  j                  d|       | j                  j                  d       | j                  |       | j                  D ]  }| j                  |        d| _        | j                  |       d| _        | j                  j                  d       | j                  j                  d       i }|j                  j                  j!                         D ]m  \  }}|j"                  | j$                  vs|j&                  j)                  d      r;|j*                  j,                  rR|j*                  j.                  ri|||<   o | j1                  |       | j                  j                  d       |S )	Nr   )r9  filename
c_filenameModule	FunctionsTFGlobals__pyx_)full_module_namer  r9  r  rK   r  r  startrF   r  r1  r   serialize_modulenode_as_functionendr`  rz  r   r  r  ro   ru  r  r  r}  serialize_local_variables)r%   r&   rT  nested_funcdefrz  kr  s          r'   ri  zDebugTransform.visit_ModuleNode  su   "33--XXa[))))+
 	h& 	k"4  #22 	3N"">2	3 "&--d3!&K  	i JJ&&,,. 	DAq  4FF%%h/FF''FF,,
	 	&&w/I r*   c           
         | j                   j                  |j                  j                         t	        |dd      r|S | j
                  r| j                  j                  |       |S |j                  d}n |j                  j                  j                  }t        |j                  j                  xs t	        |dd      |j                  j                  ||j                  j                  t        |j                  d               }| j                  j!                  d|	       | j                  j!                  d
       | j#                  |j                  j$                         | j                  j'                  d
       | j                  j!                  d       |j                  j(                  D ]L  }| j                  j!                  |j                         | j                  j'                  |j                         N | j                  j'                  d       | j                  j!                  d       d| _        | j+                  |       d| _        | j                  j'                  d       | j                  j'                  d       |S )Nr  Fr  ro   z	<unknown>r   )ro   rf  pf_cnamer  linenoFunctionrT  Locals	ArgumentsStepIntoFunctionsT)r  r   r  r  rg  r  r  r   r  r  
func_cnamer  ro   r  rK   r  r  r  rz  r  arg_entriesrF   )r%   r&   r  rT  r   s        r'   r1  z DebugTransform.visit_FuncDefNode9  s   ))8894u-K!!  ''-K <<H||))44HFGD&+$F**''++::txx{#% 	j.h&&t'7'7'?'?@Hk"##// 	"CGGMM#((#GGKK!	" 	K )*!%4 !&'(Jr*   c                 z   | j                   r|j                  |j                  j                  r{t        |dd      rn|j                  j
                  Xt        |j                  j
                        }| j                  j                  d|       | j                  j                  d       | j                  |       |S )N	is_calledFr   StepIntoFunctionr  )r  r  r  rg  r  r  r  r  r  r  rF   r%   r&   rT  s      r'   r  zDebugTransform.visit_NameNodee  s    ""II!II""D+u-JJ!!- djj334EGGMM,EM:GGKK*+4 r*   c                     |j                   j                  d      d   }d|z   }d|z   }t        ||dddd      }t        ||	      }| j                  ||       | j                  ||       y
)z
        Serialize the module-level code as a function so the debugger will know
        it's a "relevant frame" and it will know where to set the breakpoint
        for 'break modulename'.
        ro  r   initPyInit_r  1True)ro   rf  r  r  r  is_initmodule_function)rf  N)r  
rpartitionr  !_serialize_modulenode_as_function)r%   r&   ro   	cname_py2	cname_py3	py2_attrs	py3_attrss          r'   r  z/DebugTransform.serialize_modulenode_as_functionw  s~     $$//4R8TM	$	 #)	
	 )4	..tY?..tY?r*   c                 @   | j                   j                  d|       | j                   j                  d       | j                  |j                  j                         | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       d| _        | j                  |       d| _        | j                   j                  d       | j                   j                  d       y )Nr  r  r  r  r  TF)r  r  r  r`  rz  r  r  rF   r  s      r'   r  z0DebugTransform._serialize_modulenode_as_function  s    j.h&&tzz'9'9:Hk"K )*!%4 !&'(Jr*   c                 
   |j                         D ]o  }|j                  s|j                  j                  rd}nd}|j                  rpt
        j                  d|j                  j                  }|j                  j                  j                  d|j                  j                  d|j                  }nP|j                  r,t
        j                  d|j                  }|j                  }n|j                  }|j                  }|j                  sd}nt        |j                  d         }t        |j                  ||||      }| j                   j#                  d|       | j                   j%                  d       r y )	NPythonObjectCObjectz->ro  0r   )ro   rf  r  r  r  LocalVar)r  rf  r  r  r  r   cur_scope_cnameouter_entryr`  r  r  ro   r  rK   r  r  r  r  r  )r%   rz  r  vartyperf  qnamer  rT  s           r'   r  z(DebugTransform.serialize_local_variables  s6   ^^% *	$E;;zz%%(#!! %+$:$:$)$5$5$;$;= ',kk&=&=&L&L&+kk&6&6&+jj2 !!$*$:$:$)KK1,,,,99 UYYq\*ZZ$E GGMM*e,GGKK
#U*	$r*   )r5   r6   r7   r8   r>   ri  r1  r  r  r  r  ra   rb   s   @r'   r  r    s.    ' &P*X$@6 $+$r*   r  r#   )H
__future__r   r}  rG  r  r   r   r   r  r   r   r   r   r   r   r   Visitorr   r   r   r   r   r	   r
   r   r   StringEncodingr   r   r   r   r   r   Coder   r    r:   rd   r   r   r   rf   r   r   r  r   r  r  r3  rC  r  r  r7  rd  rn  r  r  rZ  rt  r  r  r  r  r  ru   r  r  r>  rN  re  r  r  r#   r*   r'   <module>r     s   &    &    6     )/ 9?   /5 EK    &,  39  CI  OU  	    
        2 J J * & 3 ? ? v 6IO IX )\ ( ^ E Q R& Rj,8\"B	C#J!@H6?$4 6r6+-= 6*A%/ A%HlC_.> lC^@O%5 @FC$/1A C$L,20@ ,2^*/ *ZA	< A	H$R| Rj#/ #L ,9\ 9xPO-= PfC CL,O ,^6 6rD DNL? L^36(*: 36ly yxkl k\U- UpS$_ S$r*   