
    ڏDj:                     .   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y)    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 | _        |j                  dd      | _        t	        j
                  j                  | g|i | y )NcppF)prefixcython_scopepopr   r   ModuleScope__init__)selfr   argskws       _C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\Cython/Compiler/UtilityCode.pyr   zNonManglingModuleScope.__init__   sA     66%'##D64626    c                 4    d|_         t        | 	  |||      S )NT)usedsuperadd_imported_entry)r   nameentrypos	__class__s       r   r   z)NonManglingModuleScope.add_imported_entry   s    
w)$s;;r   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<;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   r4   from_moduler    need_pxdabsolute_fallbackrelative_imports          r   find_modulez$CythonUtilityCodeContext.find_module)   s     !RSS$***$,,.$%LMM||K00::/[dPTPXPXZDJ zz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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.
    Tc
                 ,   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   rE   r   r   rG   rF   rH   r2   rJ   rI   rK   r@   keyvalues                 r   r   zCythonUtilityCode.__init__L   s     ,%mmo /
UeX.#&GCL).M#&/ ##D'4>D			 B$"4#6 *r   c                 f    t        |t              r!| j                         |j                         k(  S y)NF)rC   r>   _equality_params)r   others     r   __eq__zCythonUtilityCode.__eq__g   s-    e./((*e.D.D.FFFr   c                     | j                   }t        |t              r|j                  }t        |t              r| j                  || j
                  fS r#   )rI   rC   r   outer_scoperE   rJ   )r   rS   s     r   rO   z"CythonUtilityCode._equality_paramsm   sG    --&<=%11K &<=yy+t'?'???r   c                 ,    t        | j                        S r#   )hashrE   )r   s    r   __hash__zCythonUtilityCode.__hash__s   s    DIIr   c                     ddl m} |g}ddlm}m} t         j                   j                  |r|j                         nd|r|j                  j                  nd       } j                  |_        ||_        t         j                   j                  |dd      }|j                  |d|	      }	|r4g }
|	D ]+  }|
j!                  |       t#        ||j$                        s+ n |
}	|j'                  |      }|j(                  }|j+                  |	||
      }	d } j,                  r.|j+                  |	 | j,                        |j$                  
      }	 j.                  D ][  }t#        |t0              st3        |d      s!|r$|j+                  |	 ||j4                  j6                        |j$                  
      }	]  j8                  r# fd}|j+                  |	||j$                  
      }	 j:                  r# fd}|j+                  |	||j$                  
      }	|j=                  |	|d      \  }}|rJ |       | _        |S )Nr   )AutoTestDictTransform)PipelineParseTreeTransformsF)rJ   r   optionsT)r2   allow_struct_enum_decoratorin_utility_codepyx)exclude_classes)beforec                       fd}|S )Nc                 >    | j                   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""r   r<   )r6   re   s   ` r   merge_scopez/CythonUtilityCode.get_tree.<locals>.merge_scope   s    # )(r   treec                 >    j                   | j                  _        | S r#   )rI   r6   rS   )rd   r   s    r   scope_transformz3CythonUtilityCode.get_tree.<locals>.scope_transform   s    040G0G!!-""r   c                     t               }j                  j                         D ]C  \  }}t        |d|      }| j                  j                  ||d d      }||ur||_        d|_        E | S )Nr   extern)
visibilityT)objectrK   rB   getattrr6   declare_typer   in_cinclude)rd   dummy_entryr   typeold_type_entryr   r   s         r   ri   z3CythonUtilityCode.get_tree.<locals>.scope_transform   s{    $h"&"4"4":":"< -JD$ &-T7K%HN'--::4tX`:aE%[8%3
(,E%- #"r   )	printtree)AnalysedTreeTransformsrX    rY   rZ   r/   r   rJ   is_cppr2   r[   r   r   r   rE   create_pipelineappendrC   AnalyseDeclarationsTransformCnameDirectivesTransformInterpretCompilerDirectivesinsert_into_pipelinerH   rG   r>   hasattrrg   r6   rI   rK   run_pipeline)r   entries_onlyr   rX   excludesrY   rZ   r2   rg   pipelinept	transformr`   rf   depri   errs   `                 r   get_treezCythonUtilityCode.get_treev   sM   A **3*II4+C+C)5##%54@L((00dL +!IItyy't " ++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#   r<   )r   globalstateused_bys      r   put_codezCythonUtilityCode.put_code   s    r   c                     t        j                  d|      r t        j                  j	                  ||      S t        |   ||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   sB    99]N3 ##//	JJw|NI@@@r   c                 Z     | j                   ||fi |}|j                  |j                  fS )zR
        Load a utility code as a string. Returns (proto, implementation)
        )r   protorE   )r   r   r   r   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)r   r   r)   __file____builtins____doc__)merge_unused	allowlistr   )r   r6   entriesr   valuesutility_code_definitionr   rc   rG   is_cython_utilitydeclare_in_scope)
r   
dest_scoper   r   r   rg   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   c                 T    ddl m} t        |      }d}|D ]  }|| v s| |   ||<    |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   sP     	1"&':";%M! . 	ID))0B40H'-	I '&r   )	__pyxutilrv   NNNNNN)FNr#   FNN)r)   r*   r+   r   r   r   rQ   rO   rV   r   r   classmethodr   r   r   staticmethodr   r,   r-   s   @r   r>   r>   9   s      CGOS$(+6@Un A A % % EI#': ' 'r   r>   c                   *     e Zd Z fdZd fd	Z xZS )TemplatedFileSourceDescriptorc                 4    t         |   ||       || _        y r#   )r   r   _context)r   filenamepath_descriptionr2   r!   s       r   r   z&TemplatedFileSourceDescriptor.__init__  s    #34r   c                 f   t         |   ||      5 }|j                         }t        j                  || j
                  | j                        }t        j                  t        j                  |j                  |j                              |j                  |      }d d d        |S # 1 sw Y   S xY w)N)encodingerrors)r   get_file_objectreadr
   rD   r   r   ioTextIOWrapperBytesIOencoder   )r   r   error_handlingfdataretstreamr!   s          r   r   z-TemplatedFileSourceDescriptor.get_file_object  s    W$X~> 	v!668D""4FC%%bjjAJJ1G&HSTS]S]ftuF		v
 	v
 s   B
B&&B0)NN)r)   r*   r+   r   r   r,   r-   s   @r   r   r     s      r   r   c                   *    e Zd Zd Zd Z	 	 ddZd Zy)CythonSharedUtilityCodec                 J    || _         || _        || _        || _        d | _        y r#   )	_pxd_name_shared_utility_qualified_nametemplate_contextrG   _shared_library_scope)r   pxd_nameshared_utility_qualified_namer   rG   s        r   r   z CythonSharedUtilityCode.__init__#  s(    !.K+ 0 %)"r   c                    |}|j                  | j                  d      D ]  \  }}|j                  ||      } t        j                  j                  t        j                  j                  t        j                        d   d| j                        }	 | j                  j                  dt        j                        t        j                  j                  |      d   z   }t        ||| j                        }d|_        |j!                  ||| j                        \  }}	|	\  }
}|
|f|j"                  | j                  <   d|_        |r|	 |S # t&        $ r Y |S w xY w)	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*  sR    % ; ;D<_<_qv ; w 	FD*((*(EE	F ww||GGMM&//*1-NN

	::BB3ORTRYRYRbRbcoRpqrRssH7hPTPePefK*.K'!--k5$BeBefKC)/&]I4A93MG  0$(E!	    		s   B6E 	EENc                    | j                   |j                  j                  vr | j                  |j                        | _        | j
                  D ]&  }|j                  s|j                  t        |       ( | j                  j                  D ])  }|j                  |j                  ||j                         + | j                  j                  D ])  }|j                  |j                  ||j                         + |S )Nr   )r   r2   r   r;   r   rG   r   r   r6   c_class_entriesr   r   r    var_entries)r   r   r   r   r   r   es          r   r   z(CythonSharedUtilityCode.declare_in_scopeB  s    >>!5!5!B!BB)-)9)9,:N:N)OD&== 	GC$$$$U$F	G ++;; 	<A))!&&!QUU;	<++77 	<A))!&&!QUU;	<r   c                     | j                   |j                  j                  vr | j                  |j                        | _        | j                  S r#   )r   r2   r   r;   r   )r   r   s     r   get_shared_library_scopez0CythonSharedUtilityCode.get_shared_library_scopeO  s?    >>!5!5!B!BB)-)9)9,:N:N)OD&)))r   r   )r)   r*   r+   r   r;   r   r   r<   r   r   r   r   "  s     *0 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   U  s"     (:4:6:KKCPr   )T)r   TreeFragmentr   r   Scanningr   Errorsr   rv   r   r	   r
   r   os.pathr   r   r   r   r   r/   UtilityCodeBaser>   r   AbstractUtilityCoder   r   r<   r   r   <module>r     s     @ *        	 	;V// ;.1 &W',, W't$8 0*d66 0*fQr   