
    -Ph7                     0   d dl Z ddlmZ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Zd dlZd dlZ G d
 de	j                  Z G d de          Z G d dej                  Z G d de          Z G d dej                  ZddZdS )    N   )parse_from_stringsStringParseContext)FileSourceDescriptor)CompileError)Symtab)Naming)Code)Optionsc                   ,     e Zd Zd Z fdZddZ xZS )NonManglingModuleScopec                     || _         d | _        |                    dd          | _        t	          j        j        | g|R i | d S )NcppF)prefixcython_scopepopr   r   ModuleScope__init__)selfr   argskws       [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/Compiler/UtilityCode.pyr   zNonManglingModuleScope.__init__   sP     66%''#D64666266666    c                 X    d|_         t                                          |||          S )NT)usedsuperadd_imported_entry)r   nameentrypos	__class__s       r   r   z)NonManglingModuleScope.add_imported_entry   s&    
ww))$s;;;r   Nc                     |r=|t           j        t           j        t           j        t           j        fv r| j        }||S t          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\     	;&/1CVEVX^Xlmmm #VTT**%,,T6:::r   r#   )__name__
__module____qualname__r   r   r(   __classcell__r!   s   @r   r   r      s[        7 7 7< < < < <; ; ; ; ; ; ; ;r   r   c                       e Zd ZdZddZdS )CythonUtilityCodeContextNTFc                     |rt          d          || j        k    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   r4   from_moduler    need_pxdabsolute_fallbackrelative_imports          r   find_modulez$CythonUtilityCodeContext.find_module)   s     	T !RSSS$***$,..$%LMMM|K00:/[dPTPXZ Z ZDJ zr   )NNTTF)r)   r*   r+   r6   r;    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 fd            Zedd            Z	 	 ddZed             Z xZS )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.
    T	__pyxutil Nc
                 2   i }
|Qddl m} |                                D ]\  }}t          ||          r
|||<   ||
|<    t	          j        ||||          }|| _        || _        || _        || _	        |pg | _
        || _        |	| _        || _        |
| _        d S )Nr   )BaseType)
PyrexTypesrB   items
isinstancer
   sub_tempitaimplr   filer   requires
from_scopeouter_module_scopecompiler_directivescontext_types)r   rG   r   r   rI   rH   rJ   r2   rL   rK   rM   rB   keyvalues                 r   r   zCythonUtilityCode.__init__L   s     ,,,,,,%mmoo / /
UeX.. /#&GCL).M#&#D'4>>D			 B$"4#6 *r   c                     t          |t                    r*|                                 |                                k    S dS )NF)rE   r>   _equality_params)r   others     r   __eq__zCythonUtilityCode.__eq__g   s=    e.// 	((**e.D.D.F.FFF5r   c                     | j         }t          |t                    r|j        }t          |t                    | j        || j        fS r#   )rK   rE   r   outer_scoperG   rL   )r   rU   s     r   rQ   z"CythonUtilityCode._equality_paramsm   sL    -&<== 	2%1K &<== 	2y+t'???r   c                 *    t          | j                  S r#   )hashrG   )r   s    r   __hash__zCythonUtilityCode.__hash__s   s    DIr   Fc                 "    ddl m} |g}ddlm}m} t           j         j        |r|                                nd          } j	        |_	        ||_
        t           j         j        |dd          }|                    |d|	          }	|r5g }
|	D ].}|
                    |           t          ||j                  r n/|
}	|                    |          }|j        }|                    |	||
          }	d } j        r+|                    |	 | j                  |j        
          }	 j        D ]Y}t          |t,                    rBt/          |d          r2|s0|                    |	 ||j        j                  |j        
          }	Z j        r" fd}|                    |	||j        
          }	 j        r" fd}|                    |	||j        
          }	|                    |	|d          \  }}|r
J |            | _        |S )Nr   )AutoTestDictTransform)PipelineParseTreeTransformsF)rL   r   T)r2   allow_struct_enum_decoratorin_utility_codepyx)exclude_classes)beforec                       fd}|S )Nc                 <    | j                                        | S r#   )r6   merge_in)module_noder6   s    r   merge_scope_transformzNCythonUtilityCode.get_tree.<locals>.merge_scope.<locals>.merge_scope_transform   s     !**5111""r   r<   )r6   rf   s   ` r   merge_scopez/CythonUtilityCode.get_tree.<locals>.merge_scope   s$    # # # # # )(r   treec                 *    j         | j        _        | S r#   )rK   r6   rU   )re   r   s    r   scope_transformz3CythonUtilityCode.get_tree.<locals>.scope_transform   s    040G!-""r   c                     t                      }j                                        D ]F\  }}t          |d|          }| j                            ||d d          }||ur||_        d|_        G| S )Nr   extern)
visibilityT)objectrM   rD   getattrr6   declare_typer   in_cinclude)re   dummy_entryr   typeold_type_entryr   r   s         r   rj   z3CythonUtilityCode.get_tree.<locals>.scope_transform   s    $hh"&"4":":"<"< - -JD$ &-T7K%H%HN'-::4tX`:aaE%[88%3
(,E%%""r   )	printtree)AnalysedTreeTransformsrZ   r@   r[   r\   r/   r   rL   is_cppr   r   r   rG   create_pipelineappendrE   AnalyseDeclarationsTransformCnameDirectivesTransformInterpretCompilerDirectivesinsert_into_pipelinerJ   rI   r>   hasattrrh   r6   rK   rM   run_pipeline)r   entries_onlyr   rZ   excludesr[   r\   r2   rh   pipelinept	transformra   rg   deprj   errs   `                 r   get_treezCythonUtilityCode.get_treev   s   AAAAAA **33333333*I4+C)5@##%%%5B B B +!Ity't " " " ++GUH+UU 	A  a!4!QRR E H'@@II	 %@0098> 1 @ @	) 	) 	) ? 	I44++do66*G 5 I IH = 	M 	MC#011 Mgc66J6J MS_ M#88kk#(.99.K 9 M M " 	I# # # # #  44/*G 5 I IH  	I# # # # #  44/*G 5 I IH ++Hde+LLd	r   c                     d S r#   r<   )r   outputs     r   put_codezCythonUtilityCode.put_code   s    r   c                     t          j        d|          r t          j                            ||          S  t                      j        ||fi |S )Nz[.]c(pp)?::)researchr
   UtilityCodeload_cachedr   load)clsutil_code_name	from_filekwargsr!   s       r   r   zCythonUtilityCode.load   sS    9]N33 	K #//	JJJuww|NI@@@@@r   c                 <     | j         ||fi |}|j        |j        fS )zR
        Load a utility code as a string. Returns (proto, implementation)
        )r   protorG   )r   r   r   r   utils        r   load_as_stringz CythonUtilityCode.load_as_string   s.    
 sx	<<V<<z49$$r   c                    |                      d|          }|j        j        }|                    d           |                    d           |                    d           |                    d           |                                D ]}| |_        ||_        |j        }|                    |d|           ||_        | j        D ] }	|	j	        r|	
                    ||           !|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)r   r   r)   __file____builtins____doc__)merge_unused	allowlistr   )r   r6   entriesr   valuesutility_code_definitionr   rd   rI   is_cython_utilitydeclare_in_scope)
r   
dest_scoper   r   r   rh   r   r   original_scoper   s
             r   r   z"CythonUtilityCode.declare_in_scope   s     }}$\}JJ*$JJN###I^^%% 	 	E,0E)EJJNSSS
= 	L 	LC$ L$$Zl$KKKr   c                 \    ddl m} t          |          }d}|D ]}|| v r| |         ||<   |S )a   
        Cython utility code should usually only pick up a few directives from the
        environment (those that intentionally control its function) and ignore most
        other compiler directives. This function provides a sensible default list
        of directives to copy.
        r   )_directive_defaults)bindingalways_allow_keywordsallow_none_for_extension_argsauto_pickleccomplexc_string_typec_string_encodingzoptimize.inline_defnode_callszoptimize.unpack_method_callsz&optimize.unpack_method_calls_in_pyinitzoptimize.use_switch)r   r   dict)current_directivesr   utility_code_directivesinherited_directive_namesr   s        r   filter_inherited_directivesz-CythonUtilityCode.filter_inherited_directives   sd     	100000"&':";";%M! . 	I 	ID)))0B40H'-&&r   )r?   r@   NNNNNN)FNr#   FNN)r)   r*   r+   r   r   r   rS   rQ   rX   r   r   classmethodr   r   r   staticmethodr   r,   r-   s   @r   r>   r>   9   s)          CGOS$(+ + + +6  @ @ @  T T T Tl   A A A A [A % % % [% EI#'   : ' ' \' ' ' ' 'r   r>   c                   *     e Zd Z fdZd fd	Z xZS )TemplatedFileSourceDescriptorc                 Z    t                                          ||           || _        d S r#   )r   r   _context)r   filenamepath_descriptionr2   r!   s       r   r   z&TemplatedFileSourceDescriptor.__init__  s)    #3444r   Nc                 p   t                                          ||          5 }|                                }t          j        || j        | j                  }t          j        t          j	        |
                    |j                            |j        |          }d d d            n# 1 swxY w Y   |S )N)encodingerrors)r   get_file_objectreadr
   rF   r   r   ioTextIOWrapperBytesIOencoder   )r   r   error_handlingfdataretstreamr!   s          r   r   z-TemplatedFileSourceDescriptor.get_file_object  s    WW$$X~>> 	v!6688D"4FFC%bjAJ1G1G&H&HSTS]ftuuuF		v 	v 	v 	v 	v 	v 	v 	v 	v 	v 	v 	v 	v 	v 	v
 s   A;B++B/2B/)NN)r)   r*   r+   r   r   r,   r-   s   @r   r   r     sV                          r   r   c                   ,    e Zd Zd Zd Z	 	 ddZd ZdS )CythonSharedUtilityCodec                 L    || _         || _        || _        || _        d | _        d S r#   )	_pxd_name_shared_utility_qualified_nametemplate_contextrI   _shared_library_scope)r   pxd_nameshared_utility_qualified_namer   rI   s        r   r   z CythonSharedUtilityCode.__init__"  s.    !.K+ 0 %)"""r   c                    |}|                     | j        d          D ]\  }}|                    ||          }t          j                            t          j                            t          j                  d         d| j	                  }	 | j        
                    dt          j                  t          j                            |          d         z   }t          ||| j                  }d|_        |                    ||| j                  \  }}	|	\  }
}|
|f|j        | j	        <   d|_        |r|n# t&          $ r Y nw xY w|S )	NF)r:   )
as_packager   Utility.r   T)_split_qualified_namer   find_submoduleospathjoinsplitCythonr   r   replacesepsplitextr   r   r^   process_pxdutility_pxdspxd_file_loadedr   )r   r2   r6   r   
is_packagepxd_pathnamerel_pathsource_descr   resultpxd_codenodes	pxd_scopes               r   r;   z#CythonSharedUtilityCode.find_module)  sX    % ; ;D<_qv ; w w 	F 	FD*((*(EEEEw||GMM&/**1-N
 

	:BB3OORTRYRbRbcoRpRpqrRssH7hPTPeffK*.K'!--k5$BeffKC)/&]I4A93MG 0$(E! 	 	 	 	D	s   B(D9 9
EEFNc                 $   | j         |j        j        vr|                     |j                  | _        | j        D ]%}|j        r|                    t          |           &| j        j	        D ]#}|
                    |j        ||j                   $|S )Nr   )r   r2   r   r;   r   rI   r   r   r6   c_class_entriesr   r   r    )r   r   r   r   r   r   es          r   r   z(CythonSharedUtilityCode.declare_in_scopeA  s    >!5!BBB)-)9)9,:N)O)OD&= 	G 	GC$ G$$U$FFF+; 	< 	<A))!&!QU;;;;r   c                 t    | j         |j        j        vr|                     |j                  | _        | j        S r#   )r   r2   r   r;   r   )r   r   s     r   get_shared_library_scopez0CythonSharedUtilityCode.get_shared_library_scopeL  s7    >!5!BBB)-)9)9,:N)O)OD&))r   r   )r)   r*   r+   r   r;   r   r   r<   r   r   r   r   !  s_        * * *  0 EI#'	 	 	 	* * * * *r   r   Tc                 L    t          | g|R i |                    |           dS )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   R  s7     (:4:::6::KKCPPPPPr   )T)r   TreeFragmentr   r   Scanningr   Errorsr   r@   r   r	   r
   r   os.pathr   r   r   r   r   r/   UtilityCodeBaser>   r   AbstractUtilityCoder   r   r<   r   r   <module>r      s    @ @ @ @ @ @ @ @ * * * * * *                                      				 				; ; ; ; ;V/ ; ; ;.    1   &V' V' V' V' V', V' V' V'r    $8   .* .* .* .* .*d6 .* .* .*bQ Q Q Q Q Qr   