
    Ae$                         d dl mZ ddlmZmZ ddlmZ ddlmZ ddlmZ  G d dej                        Z
 G d	 d
e      Z G d dej                        ZddZy)    )absolute_import   )parse_from_stringsStringParseContext)Symtab)Naming)Codec                   ,     e Zd Zd Z fdZddZ xZS )NonManglingModuleScopec                     || _         d | _        |j                  dd      | _        t	        j
                  j                  | g|i | y )NcppF)prefixcython_scopepopr   r   ModuleScope__init__)selfr   argskws       ;lib/python3.12/site-packages/Cython/Compiler/UtilityCode.pyr   zNonManglingModuleScope.__init__   sA     66%'##D64626    c                 <    d|_         t        t        |   |||      S )NT)usedsuperr   add_imported_entry)r   nameentrypos	__class__s       r   r   z)NonManglingModuleScope.add_imported_entry   s"    
+TEdESVWWr   c                     |rR|t         j                  t         j                  t         j                  t         j                  fv r| j
                  }||S t        j                  j                  | |      S N)	r   typeobj_prefixfunc_prefix
var_prefixpyfunc_prefixr   r   r   mangle)r   r   r   s      r   r&   zNonManglingModuleScope.mangle   s^    &//1C1CVEVEVX^XlXlmm #T**%%,,T6::r   r!   )__name__
__module____qualname__r   r   r&   __classcell__)r   s   @r   r   r   	   s    7X;r   r   c                       e Zd ZdZddZy)CythonUtilityCodeContextNc                    |rt        d      || j                  k7  r(|| j                  vrt        d      | j                  |   S | j                  )t	        | j
                  |d | | j                        | _        | j                  S )Nz/Relative imports not supported in utility code.z%Only the cython cimport is supported.)parent_modulecontextr   )AssertionErrormodule_namemodulesscoper   r   r   )r   r1   relative_tor   need_pxdabsolute_fallbacks         r   find_modulez$CythonUtilityCodeContext.find_module#   s     !RSS$***$,,.$%LMM||K00::/[dPTPXPXZDJ zzr   )NNTT)r'   r(   r)   r3   r7    r   r   r,   r,       s    Er   r,   c                   `    e Zd ZdZdZ	 	 	 ddZd Zd Zd ZddZ	d	 Z
edd
       Z	 	 ddZy)CythonUtilityCodeaR  
    Utility code written in the Cython language itself.

    The @cname decorator can set the cname for a function, method of cdef class.
    Functions decorated with @cname('c_func_name') get the given cname.

    For cdef classes the rules are as follows:
        obj struct      -> <cname>_obj
        obj type ptr    -> <cname>_type
        methods         -> <class_cname>_<method_cname>

    For methods the cname decorator is optional, but without the decorator the
    methods will not be prototyped. See Cython.Compiler.CythonScope and
    tests/run/cythonscope.pyx for examples.
    TNc
                 ,   i }
|Mddl m} |j                         D ]  \  }}t        ||      s|||<   ||
|<    t	        j
                  ||||      }|| _        || _        || _        || _	        |xs g | _
        || _        |	| _        || _        |
| _        y )Nr   )BaseType)
PyrexTypesr<   items
isinstancer	   sub_tempitaimplr   filer   requires
from_scopeouter_module_scopecompiler_directivescontext_types)r   rA   r   r   rC   rB   rD   r/   rF   rE   rG   r<   keyvalues                 r   r   zCythonUtilityCode.__init__F   s     ,%mmo /
UeX.#&GCL).M#&/ ##D'4>D			 B$"4#6 *r   c                 f    t        |t              r!| j                         |j                         k(  S y)NF)r?   r:   _equality_params)r   others     r   __eq__zCythonUtilityCode.__eq__a   s-    e./((*e.D.D.FFFr   c                     | j                   }t        |t              r|j                  }t        |t              r| j                  || j
                  fS r!   )rE   r?   r   outer_scoperA   rF   )r   rO   s     r   rK   z"CythonUtilityCode._equality_paramsg   sG    --&<=%11K &<=yy+t'?'???r   c                 ,    t        | j                        S r!   )hashrA   )r   s    r   __hash__zCythonUtilityCode.__hash__m   s    DIIr   c                 V    ddl m} |g}ddlm}m} t         j                   j                  |r|j                         nd      } j                  |_	        ||_
        t         j                   j                  |d      }|j                  |d|	      }	|r4g }
|	D ]+  }|
j                  |       t        |
|j                         s+ n |
}	|j#                  |      }|j$                  }|j'                  |	||
      }	d } j(                  r.|j'                  |	 | j(                        |j                   
      }	 j*                  D ][  }t        |t,              st/        |d      s!|r$|j'                  |	 ||j0                  j2                        |j                   
      }	]  j4                  r# fd}|j'                  |	||j                   
      }	 j6                  r# fd}|j'                  |	||j                   
      }	|j9                  |	|d      \  }}|rJ |       | _        |S )Nr   )AutoTestDictTransform)PipelineParseTreeTransformsF)rF   r   T)r/   allow_struct_enum_decoratorpyx)exclude_classes)beforec                       fd}|S )Nc                 >    | j                   j                         | S r!   )r3   merge_in)module_noder3   s    r   merge_scope_transformzNCythonUtilityCode.get_tree.<locals>.merge_scope.<locals>.merge_scope_transform   s    !!**51""r   r8   )r3   r_   s   ` r   merge_scopez/CythonUtilityCode.get_tree.<locals>.merge_scope   s    # )(r   treec                 >    j                   | j                  _        | S r!   )rE   r3   rO   )r^   r   s    r   scope_transformz3CythonUtilityCode.get_tree.<locals>.scope_transform   s    040G0G!!-""r   c                     j                   j                         D ]+  \  }}| j                  j                  ||d d      }d|_        - | S )Nextern)
visibilityT)rG   r>   r3   declare_typein_cinclude)r^   r   typer   r   s       r   rc   z3CythonUtilityCode.get_tree.<locals>.scope_transform   sU    "&"4"4":":"< -JD$'--::4tX`:aE(,E%- #"r   )	printtree)AnalysedTreeTransformsrT    rU   rV   r,   r   rF   is_cppr   r   r   rA   create_pipelineappendr?   AnalyseDeclarationsTransformCnameDirectivesTransformInterpretCompilerDirectivesinsert_into_pipelinerD   rC   r:   hasattrra   r3   rE   rG   run_pipeline)r   entries_onlyr   rT   excludesrU   rV   r/   ra   pipelinept	transformrZ   r`   deprc   errs   `                 r   get_treezCythonUtilityCode.get_treep   s6   A **3*II4+C+C)5##%5B +!IItyy'tU++GUH+UA a!4!Q!QR
 H'@@I	 %@@0098> 1 @	) ??44+doo6*GG 5 IH == 	MC#01gc66JS_#88k#((..9.KK 9 M	M ""#  44/*GG 5 IH #  44/*GG 5 IH ++Hde+Ld	r   c                      y r!   r8   )r   outputs     r   put_codezCythonUtilityCode.put_code   s    r   c                 Z     | j                   ||fi |}|j                  |j                  fS )zR
        Load a utility code as a string. Returns (proto, implementation)
        )loadprotorA   )clsutil_code_name	from_filekwargsutils        r   load_as_stringz CythonUtilityCode.load_as_string   s.    
 sxx	<V<zz499$$r   c                    | j                  d|      }|j                  j                  }|j                  d       |j                  d       |j                  d       |j                  d       |j	                         D ]  }| |_        ||_         |j                  }|j                  |d|       ||_        | j                  D ]"  }	|	j                  s|	j                  ||       $ |S )	z
        Declare all entries from the utility code in dest_scope. Code will only
        be included for used entries. If module_name is given, declare the
        type entries with that name.
        T)rv   r   r'   __file____builtins____doc__)merge_unused	whitelist)r   )r~   r3   entriesr   valuesutility_code_definitionr   r]   rC   is_cython_utilitydeclare_in_scope)
r   
dest_scoper   r   r   ra   r   r   original_scoper|   s
             r   r   z"CythonUtilityCode.declare_in_scope   s     }}$\}J**$$JJN#I^^% 	E,0E)EJ	 NS
== 	LC$$$$Zl$K	L r   )	__pyxutilrl   NNNNNN)FNr!   )FNN)r'   r(   r)   r   r   r   rM   rK   rR   r~   r   classmethodr   r   r8   r   r   r:   r:   3   s\      CGOS$(+6@L\ % % EI#'r   r:   c                 @    t        | g|i |j                  |       y)z`
    Declare some declarations given as Cython code in declaration_string
    in scope env.
    N)r:   r   )declaration_stringenvprivate_typer   r   s        r   declare_declarations_in_scoper      s"     (:4:6:KKCPr   N)T)
__future__r   TreeFragmentr   r   rl   r   r   r	   r   r   r,   UtilityCodeBaser:   r   r8   r   r   <module>r      sN    & @   ;V// ;.1 &q,, qhQr   