o
    wi?                  	   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlZddl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 d
dlmZmZmZ d
dlmZ ddl m!Z! d dl"Z#d dl$Z%d dl&m'Z' dd Z(G dd deeZ)ed4ddZ*d4ddZ+d4ddZ,dd Z-edd Z.i Z/e.dZ0d5ddZ1d d! Z2e+ej34e d"ddd#d#dddf	d$d%Z5d&Z6dddej7d'fd(d)Z8de5_9e:d*j;Z<d+d, Z=e:d-Z>d.d/ Z?d0d1 Z@G d2d3 d3ZAdS )6    N)Distribution	Extension)	build_ext   )Context)default_optionsCompilationOptionsget_directive_defaults)CythonTransformEnvTransform)SkipDeclarations)parse_from_strings   )strip_string_literals	cythonizecached_function)get_cython_cache_dir)Pipeline)ExtensionFileLoaderc                 C   s2   t jj| t| |d}t j|}|j| |S )N)loader)	importlibutilspec_from_file_locationr   module_from_specr   exec_module)namepathspecmodule r   P/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/Cython/Build/Inline.pyload_dynamic   s   r!   c                       s0   e Zd Z fddZdd Z fddZ  ZS )UnboundSymbolsc                    s   t t| jd d t | _d S )N)context)superr   __init__setunbound)self	__class__r   r    r%   $   s   zUnboundSymbols.__init__c                 C   s"   |   |js| j|j |S N)current_envlookupr   r'   addr(   noder   r   r    visit_NameNode'   s   zUnboundSymbols.visit_NameNodec                    s   t  | | jS r+   )r$   __call__r'   r/   r)   r   r    r2   +   s   zUnboundSymbols.__call__)__name__
__module____qualname__r%   r1   r2   __classcell__r   r   r)   r    r"   #   s    r"   c                 C   s   |d u rt g t ttd}ddlm} td| }t|dD ]}|d u r&q||}t	||r1 nqdd l
}tt |tt| S )Noptionsr   )AnalyseDeclarationsTransformz(tree fragment)pyxr   )r   r	   r   r   Compiler.ParseTreeTransformsr9   r   r   create_pipeline
isinstancebuiltinstupler"   r&   dir)coder#   r9   treephaser>   r   r   r    unbound_symbols0   s   

rD   c                 C   s   t | }|tu r
dS t| |S )Nlong)typeint	safe_type)argr#   py_typer   r   r    unsafe_typeA   s   
rK   c                 C   s   t | }|ttttfv r|jS |tu rdS |tu rdS |tu r!dS dt	j
v r8t| t	j
d jr8d| jj| jf S |jD ]'}|jdv rE dS |j|jdd	}|rb||j}|jrbd
|j|jf   S q;dS )Nzdouble complexdoublebintnumpyz"numpy.ndarray[numpy.%s_t, ndim=%s])__builtin__r>   objectF)need_pxdz%s.%s)rF   listr?   dictstrr3   complexfloatboolsysmodulesr=   ndarraydtyper   ndim__mro__r4   find_moduler-   is_type)rI   r#   rJ   	base_typer   entryr   r   r    rH   I   s*   

rH   c                  C   s,   t  } |  }| | t| }|  |S r+   )r   find_config_filesparse_config_filesr   finalize_options)distconfig_filesbuild_extensionr   r   r    _get_build_extensiona   s   
rh   c                 C   s   t t| t ttdS )Nr7   )r   rR   r	   r   r   )cython_include_dirsr   r   r    _create_contextl   s
   rj   ).c                 C   s   |D ]H}|| vrJ|d u s|d u r%t  jjj}|d u r|j}|d u r%|j}t|ts.t|}||v r9|| | |< q||v rD|| | |< qtd|  qd S )NzCouldn't find %r)inspectcurrentframef_backf_locals	f_globalsr=   rS   print)kwdsrD   localsglobalssymbolcalling_framer   r   r    _populate_unboundy   s"   
rw   c                 C   s.   | |t jt j|tjf}tt|d	 S )Nzutf-8)
rX   version_info
executableCython__version__hashlibsha256rT   encode	hexdigest)	orig_codearg_sigslanguage_levelkeyr   r   r    _inline_key   s   r   inlineFc
           ,         sj  d u rdd |rt t|nt |rt|ni }|	d u r$d|vr$d}	|	d ur,|	|d< d }t| }|d urmt||| t }t fdd|D }t	| ||	}t| ||f}|d urmdd |D }|| S | }t
| \} }t| } |d u rt jjj}|d u rt jjj}zt|  t|< }t||| W n ty   |std Y nw g }t D ]\}}|tu r|d	|  |= qt}t fd
d|D }|d u rt	|||	}d| }|tjv rtj| }n)d }tjd u rt }|dt_tj|}tj ||tj }tj!|s#t"| |s-tj#|sg }g }g }t$%d}|D ]1\}} |&|}!|!rj|d|!' d   |!' d dkrjdd l(}"||")  |d q:t*| \}#}$d dd |D }%dd ||#|%|$d }&| D ]\}'}(|&+|'|(}&qtj ||d })t,|)d}*z|*-|& W |*.  n|*.  w t/||)g|pd |pd |pd d}+|d u rt }t0|+g|pdg||d|_1tj2|)|_3||_4|5  tj6dkrtj7dkrt8tj| t9||}W d    n	1 sw   Y  nt9||}|j:t|||f< fdd|D }|j:| S )Nc                 S   s   dS )NrP   r   )xr   r   r    <lambda>   s    zcython_inline.<locals>.<lambda>r   3c                    s   g | ]\}}| |fqS r   r   ).0rI   value)ctxget_typer   r    
<listcomp>       z!cython_inline.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r   r   r   rI   r   r   r    r          z>Could not parse code as a string (to extract unbound symbols).z
cimport cython as %sc                    s   g | ]}|  |fqS r   r   r   r   r   rr   r   r    r      r   _cython_inline_ z([.\w]+)[.]z
cimport %sr   rN   )NPY_NO_DEPRECATED_APINPY_1_7_API_VERSIONz, c                 S   s   g | ]}d | qS )z%s %sr   )r   ar   r   r    r      r   zf
%(module_body)s
%(cimports)s
def __invoke(%(params)s):
%(func_body)s
    return locals()
            
)cimportsmodule_bodyparams	func_bodyz.pyxw)r   sourcesinclude_dirsextra_compile_argsdefine_macrosrk   )include_pathcompiler_directivesquietwin32)      c                    s   g | ]} | qS r   r   r   )rr   r   r    r     r   );rj   r?   _cython_inline_default_contextrS   _cython_inline_cachegetrw   sorteditemsr   r   strip_common_indentrl   rm   rn   ro   rp   rD   AssertionErrorrq   rR   cython_moduleappendrX   rY   cython_inlineso_extrh   get_ext_filenameosr   abspathjoinexistsmakedirsisfilerecompilematchgroupsrN   get_includeextract_func_codereplaceopenwritecloser   r   
extensionsdirname
build_temp	build_librunplatformrx   add_dll_directoryr!   __invoke),rA   r   lib_dirri   cython_compiler_directivesforcer   rs   rt   r   rr   key_hash_unbound_symbolsargsr   invokearg_listr   literalsr   r   rI   	arg_namesmodule_namer   rg   module_pathcflagsr   c_include_dirs	qualifiedrF   _mrN   r   r   r   module_coder   r   pyx_filefh	extensionr   r   r    r      s   











r   a=  
def __PYX_repeat_benchmark(benchmark, timer, size_t number):
    cdef size_t i

    t0 = timer()
    for i in range(number):
        benchmark()
    t1 = timer()
    return t1 - t0

def __PYX_make_benchmark():
    {setup_code}

    def __PYX_run_benchmark():
        {benchmark_code}

    return __PYX_run_benchmark
	   c                    s  ddl }|r
t|nd}|  rt| nd} |rt|jnd}tj||d || d d}t|||d}	|	d	 |	d
 fddt	 t
rSdnd}
r[|
d n|
d fdd}|  | fddt|D }d   fdd|D }|fS )a  Benchmark a Cython code string similar to 'timeit'.

    'setup_code': string of setup code that will be run before taking the timings.

    'import_module': a module namespace to run the benchmark in
                     (usually a compiled Cython module).

    'directives': Cython directives to use when compiling the benchmark code.

    'timer': The timer function. Defaults to 'time.perf_counter', returning float seconds.
             Nanosecond timers are detected (and can only be used) if they return integers.

    'repeat': The number of timings to take and return.

    Returns a tuple: (list of single-loop timings, number of loops run for each)
    r   Nr   pass    z        )
setup_codebenchmark_code)r   rs   __PYX_make_benchmark__PYX_repeat_benchmarkc                    sF     }t  }t   z|| }W |rt   |S |r"t   w w r+   )gc	isenableddisableenable)number	benchmarkgcoldtiming)make_benchmarkrepeat_benchmarktimerr   r    timeit\  s   
zcymeit.<locals>.timeiti ʚ;g      ?   c                     sr   d} 	 dD ].}| | }|}t |rtntsJ | kr"|  S r3|dk r3|dkr3td q| d9 } q)Nr   T)r   r   r   
   z&Timer seems to return non-ns timings: )r=   rG   rV   RuntimeError)ijr   
time_taken)min_runtimer   r   timer_returns_nanosecondsr   r    	autorangep  s   zcymeit.<locals>.autorangec                    s   g | ]} qS r   r   )r   r   )r   r   r   r    r     s    zcymeit.<locals>.<listcomp>r   c                    s$   g | ]}r|   n| qS r   r   )r   r   )halfr   r   r   r    r     s    )textwrapr   strip
__import____dict___CYMEIT_TEMPLATEformatindentr   r=   rG   range)rA   r   import_module
directivesr   repeatr   module_namespacecymeit_code	namespace
one_secondr   timingsr   )r   r   r   r   r   r   r   r   r    cymeit1  s<   r  z\Sc                 C   s   d }|   }|D ]}t|}|sq| }|| dkrq|d u s$||kr&|}qt|D ]\}}t|}|rA|rA|||d  dkrBq+||d  ||< q+d|S )N#r   r   )
splitlines_find_non_spacestart	enumerater   )rA   
min_indentlinesliner   r  ixr   r   r    r     s$   
r   zI^((cdef +(extern|class))|cimport|(from .+ cimport)|(from .+ import +[*]))c                 C   sl   g }g }|}|  dd} | d}|D ]}|ds$t|r"|}n|}|| qd|dd| fS )N	 r   r   z
    )r   split
startswithmodule_statementr   r   r   )rA   r   functioncurrentr  r  r   r   r    r     s   


r   c                 C   s>   |  d}| d d dkrd| |d d   S | |d d  S )N:r   lambdaz	return %sr   )index)sourcer  r   r   r    get_body  s   
r"  c                   @   s   e Zd Zdd Zdd ZdS )RuntimeCompiledFunctionc                 C   s   || _ tt|| _d S r+   )_fr"  rl   	getsource_body)r(   fr   r   r    r%     s   z RuntimeCompiledFunction.__init__c                 O   s:   t j| jg|R i |}t| jf| jj| jjd|S )N)rs   rt   )rl   getcallargsr$  r   r&  __globals__)r(   r   rr   allr   r   r    r2     s    z RuntimeCompiledFunction.__call__N)r3   r4   r5   r%   r2   r   r   r   r    r#    s    r#  r+   )NN)Br   r|   rl   r   r   rX   timedistutils.corer   r   distutils.command.build_extr   rz   Compiler.Mainr   Compiler.Optionsr   r   r	   Compiler.Visitorr
   r   r;   r   Compiler.TreeFragmentr   Dependenciesr   r   r   Cacher   Compilerr   cythonr   importlib.utilr   importlib.machineryr   r!   r"   rD   rK   rH   rh   rj   r   r   rw   r   r   r   r   r  perf_counterr  r   r   searchr  r   r  r   r"  r#  r   r   r   r    <module>   sb    





 
b

