o
    e+                     @   s   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 dej	Z
G d	d
 d
eZG dd dejZdddZdS )    )absolute_import   )parse_from_stringsStringParseContext)Symtab)Naming)Codec                       s.   e Zd Zdd Z fddZdddZ  ZS )	NonManglingModuleScopec                 O   s8   || _ d | _|dd| _tjj| g|R i | d S )NcppF)prefixcython_scopepopr
   r   ModuleScope__init__)selfr   argskw r   KD:\Projects\ConvertPro\env\Lib\site-packages\Cython/Compiler/UtilityCode.pyr      s   zNonManglingModuleScope.__init__c                    s   d|_ tt| |||S )NT)usedsuperr	   add_imported_entry)r   nameentrypos	__class__r   r   r      s   z)NonManglingModuleScope.add_imported_entryNc                 C   s<   |r|t jt jt jt jfv r| j}d||f S tj| |S )Nz%s%s)	r   Ztypeobj_prefixZfunc_prefixZ
var_prefixZpyfunc_prefixr   r   r   mangle)r   r   r   r   r   r   r      s
   zNonManglingModuleScope.mangleN)__name__
__module____qualname__r   r   r   __classcell__r   r   r   r   r	   	   s    r	   c                   @   s   e Zd ZdZdddZdS )CythonUtilityCodeContextNTFc                 C   sZ   |rt d|| jkr|| jvrt d| j| S | jd u r*t| j|d | | jd| _| 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   r'   Zfrom_moduler   Zneed_pxdZabsolute_fallbackZrelative_importr   r   r   find_module#   s   



z$CythonUtilityCodeContext.find_module)NNTTF)r   r    r!   r)   r*   r   r   r   r   r#       s    r#   c                   @   sv   e Zd ZdZdZ			dddZdd	 Zd
d Zdd ZdddZ	dd Z
edddZ		dddZedd Zd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
                 C   s   i }
|d ur*ddl m} | D ]\}}t||r!|||< ||
|< qt||||}|| _|| _|| _|| _	|p9g | _
|| _|	| _|| _|
| _d S )Nr   )BaseType)Z
PyrexTypesr.   items
isinstancer   Zsub_tempitaimplr   filer   requires
from_scopeouter_module_scopecompiler_directivescontext_types)r   r1   r   r   r3   r2   r4   r%   r6   r5   r7   r.   keyvaluer   r   r   r   F   s$   	


zCythonUtilityCode.__init__c                 C   s   t |tr|  | kS dS )NF)r0   r+   _equality_params)r   otherr   r   r   __eq__a   s   
zCythonUtilityCode.__eq__c                 C   s.   | j }t|tr|j}t|ts| j|| jfS r   )r5   r0   r	   outer_scoper1   r6   )r   r=   r   r   r   r:   g   s
   

z"CythonUtilityCode._equality_paramsc                 C   s
   t | jS r   )hashr1   r   r   r   r   __hash__m   s   
zCythonUtilityCode.__hash__Fc                    s  ddl m} |g}ddlm}m} t j j|r| ndd} j	|_	||_
t j j|dd}|j|d|d	}	|rQg }
|	D ]}|
| t||jrN nq?|
}	||}|j}|j|	||d
}	dd } jrt|j|	| j|jd
}	 jD ]}t|trt|dr|s|j|	||jj|jd
}	qw jr fdd}|j|	||jd
}	 jr fdd}|j|	||jd
}	|j|	|dd\}}|rJ || _|S )Nr   )AutoTestDictTransform)PipelineParseTreeTransformsF)r6   r
   T)r%   Zallow_struct_enum_decoratorpyx)Zexclude_classes)beforec                    s    fdd}|S )Nc                    s   | j   | S r   )r)   merge_inmodule_noder)   r   r   merge_scope_transform   s   zNCythonUtilityCode.get_tree.<locals>.merge_scope.<locals>.merge_scope_transformr   )r)   rJ   r   rI   r   merge_scope   s   z/CythonUtilityCode.get_tree.<locals>.merge_scopetreec                    s    j | j_| S r   )r5   r)   r=   rG   r?   r   r   scope_transform   s   
z3CythonUtilityCode.get_tree.<locals>.scope_transformc                    sR   t  } j D ]\}}t|d|}| jj||d dd}||ur#||_d|_q| S )Nr   Zextern)Z
visibilityT)objectr7   r/   getattrr)   Zdeclare_typer   Zin_cinclude)rH   Zdummy_entryr   typeZold_type_entryr   r?   r   r   rM      s   )Z	printtree)ZAnalysedTreeTransformsrA   r-   rB   rC   r#   r   r6   Zis_cppr   r   r   r1   Zcreate_pipelineappendr0   ZAnalyseDeclarationsTransformZCnameDirectivesTransformZInterpretCompilerDirectivesZinsert_into_pipeliner4   r3   r+   hasattrrL   r)   r5   r7   Zrun_pipeline)r   entries_onlyr   rA   excludesrB   rC   r%   rL   ZpipelineptZ	transformrE   rK   deprM   errr   r?   r   get_treep   sn   



zCythonUtilityCode.get_treec                 C   s   d S r   r   )r   outputr   r   r   put_code   s   zCythonUtilityCode.put_codec                 K   s    | j ||fi |}|j|jfS )zR
        Load a utility code as a string. Returns (proto, implementation)
        )loadprotor1   )clsZutil_code_name	from_filekwargsutilr   r   r   load_as_string   s   z CythonUtilityCode.load_as_stringc           
      C   s   | j d|d}|jj}|d |d |d |d | D ]}| |_||_q#|j}|j|d|d ||_| jD ]}	|	j	rI|	j
||d q=|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)rS   r   r   __file____builtins____doc__)Zmerge_unused	allowlist)r   )rY   r)   entriesr   valuesZutility_code_definitionr   rF   r3   is_cython_utilitydeclare_in_scope)
r   Z
dest_scoper   r   rf   rL   rg   r   Zoriginal_scoperW   r   r   r   rj      s"   




z"CythonUtilityCode.declare_in_scopec                 C   s:   ddl m} t|}d}|D ]}|| v r| | ||< q|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)ZbindingZalways_allow_keywordsZallow_none_for_extension_argsZauto_pickleZccomplexZc_string_typeZc_string_encodingzoptimize.inline_defnode_callszoptimize.unpack_method_callsz&optimize.unpack_method_calls_in_pyinitzoptimize.use_switch)Optionsrk   dict)Zcurrent_directivesrk   Zutility_code_directivesZinherited_directive_namesr   r   r   r   filter_inherited_directives   s   z-CythonUtilityCode.filter_inherited_directives)r,   r-   NNNNNN)FNr   )FNN)r   r    r!   re   ri   r   r<   r:   r@   rY   r[   classmethodrb   rj   staticmethodrn   r   r   r   r   r+   3   s&    

U
r+   Tc                 O   s    t | g|R i || dS )z`
    Declare some declarations given as Cython code in declaration_string
    in scope env.
    N)r+   rj   )Zdeclaration_stringenvZprivate_typer   r`   r   r   r   declare_declarations_in_scope  s    rr   N)T)
__future__r   ZTreeFragmentr   r   r-   r   r   r   r   r	   r#   ZUtilityCodeBaser+   rr   r   r   r   r   <module>   s     Q