o
    fxr                     @   s  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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 dSddZG dd deZdZdddddZdZdZdZdZdZdZdZdZdZdTdd ZG d!d" d"eZ d#d$ Z!d%d& Z"d'd( Z#d)d* Z$d+d, Z%d-d. Z&d/d0 Z'd1d2 Z(d3d4 Z)d5d6 Z*d7d8 Z+d9d: Z,d;d< Z-d=d> Z.d?d@ Z/G dAdB dBeZ0dCdD Z1dUdEdFZ2dUdGdHZ3e4ej5dIZ6e3dJe6dKZ7e3dLZ8e3dMZ9e3dNZ:e3dOZ;e3dPe6dKZ<e3dQe6dKZ=e3dRZ>dS )V    )absolute_import   )CythonTransform)
ModuleNode)CompileError)CythonUtilityCode)UtilityCodeTempitaUtilityCode)Options)Interpreter)
PyrexTypes)Naming)Symtabc                    sF   ddl m} || } |dkr!d|  d fdd| dD } | S )Nr   dedent 
c                    s   g | ]} | qS  r   .0xindentr   R/var/www/html/mig_web/myenv/lib/python3.10/site-packages/Cython/Compiler/Buffer.py
<listcomp>       zdedent.<locals>.<listcomp>)textwrapr   joinsplit)textreindentr   r   r   r   r      s   r   c                       s<   e Zd ZdZdZ fddZdd Zdd Zdd	 Z  Z	S )
IntroduceBufferAuxiliaryVarsFc                    sB   t |tsJ d| _tt| |}| jrt|j t	|j |S )Nr   )

isinstancer   max_ndimsuperr!   __call__buffers_existsuse_bufstruct_declare_codescopeuse_py2_buffer_functions)selfnoderesult	__class__r   r   r%       s   

z%IntroduceBufferAuxiliaryVars.__call__c           
         sf  j  }dd |D }t|dkr|jdd d d| _dd |D }t|dkr-d| _|D ]\d	krBtjtrBd| _ nq/~tt	rUt|dkrUt
jd
|D ]Sjjjrdt
jdjj}|jtjkryt
jdtj |j| jkr|j| _fdd tjtjftjtjff} fdd|D \}}	t||	_qW|_| _d S )Nc                 S      g | ]
\}}|j jr|qS r   )type	is_bufferr   nameentryr   r   r   r   3       z=IntroduceBufferAuxiliaryVars.handle_scope.<locals>.<listcomp>r   c                 S   s   | j S N)r3   )r4   r   r   r   <lambda>5   s    z;IntroduceBufferAuxiliaryVars.handle_scope.<locals>.<lambda>)keyTc                 S   r/   r   )r0   is_memoryviewslicer2   r   r   r   r   8   r5   
memoryviewz'Buffer vars not allowed in module scopez-Buffers with pointer types not yet supported.z1Buffer ndims exceeds Options.buffer_max_dims = %dc                    s0    |}jd || jd} jrd|_|S )N)r3   cnamer0   posT)mangledeclare_varr<   is_argused)r0   prefixr;   aux_var)r4   r3   r+   r(   r   r   decvarS   s   z9IntroduceBufferAuxiliaryVars.handle_scope.<locals>.decvarc                    s   g | ]	\}} ||qS r   r   )r   r0   rA   )rC   r   r   r   ^       )entriesitemslensortr&   r"   utility_code_definitionr   using_memoryviewr   r   r<   r0   dtypeis_ptrr3   ndimr
   buffer_max_dimsr#   r   c_pyx_buffer_nd_typer   pybuffernd_prefixc_pyx_buffer_typepybufferstruct_prefixr   	BufferAux
buffer_auxbuffer_entriesr(   )
r*   r+   r(   scope_itemsbufvarsmemviewslicevarsbuftypeauxvars
pybufferndrcbufferr   )rC   r4   r3   r+   r(   r   handle_scope.   sF   


	

z)IntroduceBufferAuxiliaryVars.handle_scopec                 C      |  ||j | | |S r6   )r]   r(   visitchildrenr*   r+   r   r   r   visit_ModuleNodee      
z-IntroduceBufferAuxiliaryVars.visit_ModuleNodec                 C   r^   r6   )r]   local_scoper_   r`   r   r   r   visit_FuncDefNodej   rb   z.IntroduceBufferAuxiliaryVars.visit_FuncDefNode)
__name__
__module____qualname__r&   rJ   r%   r]   ra   rd   __classcell__r   r   r-   r   r!      s    7r!   )rK   rM   modenegative_indicescastfullTF)rM   ri   rj   rk   z"%s" is not a buffer optionzToo many buffer optionsz#"%s" buffer option already suppliedz"%s" missingz[Only allowed buffer modes are: "c", "fortran", "full", "strided" (as a compile-time string)z#ndim must be a non-negative integerz0dtype must be "object", numeric type or a structz"%s" must be a booleanNc              	      s  |du rt }tj|||dd\}}t|tkr t|d d ti | D ]\}\}}|tvr7t|t	| ||< q&t
t|D ] \}\}}|tvrRt|t	| |v r]t|t| ||< qAtD ]!}|vrz|| |< W qd ty   |rt t| Y qdw qdd}	|	r|	jrt td}
|
rt|
tr|
dk rt td	}|r|d
vrt t fdd}|d |d S )aO  
    Must be called during type analysis, as analyse is called
    on the dtype argument.

    posargs and dictargs should consist of a list and a dict
    of tuples (value, pos). Defaults should be a dict of values.

    Returns a dict containing all the options a buffer can have and
    its value (with the positions stripped).
    N)r   rK   )type_env	type_argsr   rK   rM   r   ri   )rl   stridedcfortranc                    s&    | }t|tst t|  d S r6   )getr"   boolr   ERR_BUF_BOOL)r3   r   	globalposoptionsr   r   assert_bool   s   

z+analyse_buffer_options.<locals>.assert_boolrj   rk   )buffer_defaultsr   interpret_compiletime_optionsrG   buffer_positional_options_countr   ERR_BUF_TOO_MANYrF   buffer_optionsERR_BUF_OPTION_UNKNOWNzipERR_BUF_DUPKeyErrorERR_BUF_MISSINGrs   is_extension_typeERR_BUF_DTYPEr"   intERR_BUF_NDIMERR_BUF_MODE)rw   envposargsdictargsdefaultsneed_completer3   valuer<   rK   rM   ri   ry   r   rv   r   analyse_buffer_options   sR   









r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )BufferEntryc                 C   s<   || _ |j| _|jjj| _d| j | _|jj| _|   d S )Nz%s.rcbuffer->pybuffer.buf)	r4   r0   rT   buflocal_nd_varr;   buf_ptrbuffer_ptr_typebuf_ptr_typeinit_attributes)r*   r4   r   r   r   __init__   s   
zBufferEntry.__init__c                 C   s"   |   | _|  | _|  | _d S r6   )get_buf_shapevarsshapeget_buf_stridevarsstridesget_buf_suboffsetvars
suboffsetsr*   r   r   r   r      s   

zBufferEntry.init_attributesc                 C   
   |  dS )Nz%s.diminfo[%d].suboffsets_for_all_ndimr   r   r   r   r         
z!BufferEntry.get_buf_suboffsetvarsc                 C   r   )Nz%s.diminfo[%d].stridesr   r   r   r   r   r      r   zBufferEntry.get_buf_stridevarsc                 C   r   )Nz%s.diminfo[%d].shaper   r   r   r   r   r      r   zBufferEntry.get_buf_shapevarsc                    s    fddt jjD S )Nc                    s   g | ]	} j |f qS r   )r;   r   isr*   r   r   r      rD   z-BufferEntry._for_all_ndim.<locals>.<listcomp>)ranger0   rM   )r*   r   r   r   r   r      s   zBufferEntry._for_all_ndimc                 C   s>  g }| j j}| j j}|dkr4t||  |  D ]\}}}|| || || qd| }	t}
n9|dkr?d| }	t}
n|dkrJd| }	t	}
n|dkrUd| }	t
}
nJ t||  D ]\}}|| || q^|	|jjvr|jj|	 |jd
 }|jd }|
|||	|d | j }d|	|| jd|f }|S )Nrl   z__Pyx_BufPtrFull%ddrp   z__Pyx_BufPtrStrided%ddrq   z__Pyx_BufPtrCContig%ddrr   z__Pyx_BufPtrFortranContig%ddFutility_code_protoutility_code_def)r3   ndz%s(%s, %s, %s), )r0   rM   ri   r   r   r   appendbuf_lookup_full_codebuf_lookup_strided_codebuf_lookup_c_codebuf_lookup_fortran_codeglobalstateutility_codesaddr   empty_declaration_coder   r   )r*   codeindex_cnamesparamsr   ri   r   r   ofuncnamefuncgen	protocodedefcodebuf_ptr_type_codeptrcoder   r   r   generate_buffer_lookup_code   sH   






z'BufferEntry.generate_buffer_lookup_codeN)
re   rf   rg   r   r   r   r   r   r   r   r   r   r   r   r      s    r   c                 C   sh   d}|j }|dkr|d7 }n|dkr|d7 }n|dkr |d7 }n|dkr)|d	7 }nJ | jr2|d7 }|S )NPyBUF_FORMATrl   z| PyBUF_INDIRECTrp   z| PyBUF_STRIDESrq   z| PyBUF_C_CONTIGUOUSrr   z| PyBUF_F_CONTIGUOUSFz| PyBUF_WRITABLE)ri   writable_needed)rT   buffer_typeflagsri   r   r   r   	get_flags  s   



r   c                 C   s   | j }d|j_d|j_d S NT)rT   r   r@   	rcbuf_var)r4   rT   r   r   r   used_buffer_aux_vars  s   r   c           	      C   s   | j | jj}}|jj}ddg}|dkr|d g }t| jjD ]}|D ]}|d||||||f  q%q!|d	| d S )Nr   r   rl   r   z1%s.diminfo[%d].%s = %s.rcbuffer->pybuffer.%s[%d];r   )
rT   r0   ri   r   r;   r   r   rM   putlnr   )		buf_entryr   rT   ri   pybuffernd_structfldnameslnr   fldnamer   r   r    put_unpack_buffer_aux_into_scope   s   

r   c                 C   sV   | j }|jj}|jj}|d|  |d|  |d|  |d||f  d S )Nz%s.pybuffer.buf = NULL;z%s.refcount = 0;z%s.data = NULL;z%s.rcbuffer = &%s;)rT   r   r;   r   r   )r4   r   bufauxr   pybuffer_structr   r   r   put_init_vars3  s   r   c                 C   sf   | j }t|| j|| j}|d |d| jj   ||d| | |d t| | d S )N{'__Pyx_BufFmt_StackElem __pyx_stack[%d];%s == -1})	rT   get_getbuffer_callr;   r0   r   rK   struct_nesting_deptherror_goto_ifr   )r4   r   r<   rT   	getbufferr   r   r   put_acquire_arg_bufferA  s   

r   c                 C   s$   | j t | d|jjj  d S )N0__Pyx_SafeReleaseBuffer(&%s.rcbuffer->pybuffer);)r   use_utility_codeacquire_utility_coder   rT   r   r;   )r   r4   r   r   r   put_release_buffer_codeO  s   r   c           	      C   sD   |j }t|j}t||}|jj}t| |j}| j	t
 dt  S )Nz__Pyx_GetBufferAndValidate(&%(pybuffernd_struct)s.rcbuffer->pybuffer, (PyObject*)%(obj_cname)s, &%(dtype_typeinfo)s, %(flags)s, %(ndim)d, %(cast)d, __pyx_stack))rM   r   rk   r   r   r;   get_type_information_cnamerK   r   r   r   locals)	r   	obj_cnamerT   r   rM   rk   r   r   dtype_typeinfor   r   r   r   T  s   

r   c              	      s  |j |j}}|jj}t||}	 d  d|j   t d||}
|r d|   j	j
tjdd} d||
| f   d d	|   t fd
dtdD } d|   d d|
|      d|   jt  d  d  d|   d  d|  |D ]} j	| q d t|    ||  j	| n5 d d|
|     d| t|tjd|f    |  d t|   d  d dS )aJ  
    Generate code for reassigning a buffer variables. This only deals with getting
    the buffer auxiliary structure and variables set up correctly, the assignment
    itself and refcounting is the responsibility of the caller.

    However, the assignment operation may throw an exception so that the reassignment
    never happens.

    Depending on the circumstances there are two possible outcomes:
    - Old buffer released, new acquired, rhs assigned to lhs
    - Old buffer released, new acquired which fails, reaqcuire old lhs buffer
      (which may or may not succeed).
    r   r   z%sr   F
manage_refz%s = %s;	if (%s) {%s < 0c                 3   s"    | ]} j jtjd dV  qdS )Fr   N)	funcstateallocate_tempr   py_object_type)r   _r   r   r   	<genexpr>  s    z'put_assign_to_buffer.<locals>.<genexpr>   zPyErr_Fetch(&%s, &%s, &%s);r   z/Py_XDECREF(%s); Py_XDECREF(%s); Py_XDECREF(%s);z!__Pyx_RaiseBufferFallbackError();z} else {zPyErr_Restore(%s, %s, %s);r   z%s = %s = %s = 0;zA%s = %s; __Pyx_INCREF(Py_None); %s.rcbuffer->pybuffer.buf = NULL;Py_NoneN)rT   r0   r   r;   r   r   rK   r   r   r   r   r   
c_int_typeunlikelytupler   r   r   raise_buffer_fallback_coderelease_tempr   error_goto_if_negtypecastr   
error_gotoput)	lhs_cname	rhs_cnamer   is_initializedr<   r   rT   r   r   r   r   retcode_cname	exc_tempstr   r   r   put_assign_to_bufferb  sR   









r   c              
   C   s  |d o|}|d r|j jtjdd}|d|  tt|||  D ]V\}	\}
}}|
dkr_|d|  |rQ|d||f  |d	|d
| ||	f  n	|d||	f  |	d |
dkrfd}nd}|d	|d|||f ||	f  q$|r|j
t d}n|j
t d}|d|d|   |d||f  ||| |d |j | n|rt|||  D ]\}
}}|
dkr|d|||f  q| ||S )ae  
    Generates code to process indices and calculate an offset into
    a buffer. Returns a C string which gives a pointer which can be
    read from or written to at will (it is an expression so caller should
    store it in a temporary if it is used more than once).

    As the bounds checking can have any number of combinations of unsigned
    arguments, smart optimizations etc. we insert it directly in the function
    body. The lookup however is delegated to a inline function that is instantiated
    once per ndim (lookup with suboffsets tend to get quite complicated).

    entry is a BufferEntry
    
wraparoundboundscheckFr   z%s = -1;r   zif (%s < 0) {z	%s += %s;zif (%s) %s = %d;r   z%s = %d;z} else  z(size_t)z
%s >= %s%s __Pyx_RaiseBufferIndexErrorNogil__Pyx_RaiseBufferIndexErrorr   z%s != -1z%s(%s);r   zif (%s < 0) %s += %s;)r   r   r   r   r   	enumerater   r   r   r   r   r   raise_indexerror_nogilraise_indexerror_coder   r   r   )r4   index_signedsr   
directivesr<   r   rj   in_nogil_contextfailed_dim_tempdimsignedr;   r   rk   funcr   r   r   put_buffer_lookup_code  sL   "



r  c                 C   s   |  t d S r6   )r   buffer_struct_declare_coder   r   r   r   r'     s   r'   c                 C   s   d dd t|D }| d||||f  d dd t|D }| d||f  |td||f d d	d t|D  d
  dS )
    Generates a buffer lookup function for the right number
    of dimensions. The function gives back a void* at the right location.
    r   c                 S      g | ]	}d |||f qS )zi%d, s%d, o%dr   r   r   r   r   r     rD   z(buf_lookup_full_code.<locals>.<listcomp>z1#define %s(type, buf, %s) (type)(%s_imp(buf, %s))c                 S   r  )z.Py_ssize_t i%d, Py_ssize_t s%d, Py_ssize_t o%dr   r   r   r   r   r     rD   z1static CYTHON_INLINE void* %s_imp(void* buf, %s);zf
        static CYTHON_INLINE void* %s_imp(void* buf, %s) {
          char* ptr = (char*)buf;
        r   c                 S   s    g | ]}t d ||||f qS )zX          ptr += s%d * i%d;
          if (o%d >= 0) ptr = *((char**)ptr) + o%d;
        r   r   r   r   r   r     s
    
z
return ptr;
}N)r   r   r   r   )protodefinr3   r   	macroargsfuncargsr   r   r   r     s   


r   c                 C   sH   d dd t|D }d dd t|D }| d|||f  dS )r  r   c                 S      g | ]}d ||f qS zi%d, s%dr   r   r   r   r   r         z+buf_lookup_strided_code.<locals>.<listcomp> + c                 S   r  z	i%d * s%dr   r   r   r   r   r     r  z1#define %s(type, buf, %s) (type)((char*)buf + %s)N)r   r   r   r  r  r3   r   argsoffsetr   r   r   r     s   r   c                 C   sl   |dkr|  d|  d	S ddd t|D }ddd t|d D }|  d||||d f  d	S )
z
    Similar to strided lookup, but can assume that the last dimension
    doesn't need a multiplication as long as.
    Still we keep the same signature for now.
    r   .#define %s(type, buf, i0, s0) ((type)buf + i0)r   c                 S   r  r  r   r   r   r   r   r     r  z%buf_lookup_c_code.<locals>.<listcomp>r  c                 S   r  r  r   r   r   r   r   r     r  9#define %s(type, buf, %s) ((type)((char*)buf + %s) + i%d)Nr   r   r   r  r   r   r   r     s
   r   c                 C   sf   |dkr|  d|  d
S ddd t|D }ddd td|D }|  d|||d	f  d
S )zB
    Like C lookup, but the first index is optimized instead.
    r   r  r   c                 S   r  r  r   r   r   r   r   r   %  r  z+buf_lookup_fortran_code.<locals>.<listcomp>r  c                 S   r  r  r   r   r   r   r   r   &  r  r  r   Nr   r  r   r   r   r     s
   r   c                 C   s   |  t  d S r6   )r   GetAndReleaseBufferUtilityCoder  r   r   r   r)   *  s   r)   c                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )r!  NFc                 C      d S r6   r   r   r   r   r   r   6     z'GetAndReleaseBufferUtilityCode.__init__c                 C   s
   t |tS r6   )r"   r!  )r*   otherr   r   r   __eq__9  r   z%GetAndReleaseBufferUtilityCode.__eq__c                 C   s   dS )NiFosr   r   r   r   r   __hash__<  r#  z'GetAndReleaseBufferUtilityCode.__hash__c                 K   r"  r6   r   )r*   kwargsr   r   r   get_tree?  s    z'GetAndReleaseBufferUtilityCode.get_treec                    s   |d }|d }|j j}|jj g t  fdd| tjddtdd}||j	}||
|j|d	 }|| || d S )
Nr   r   c                    s   | v rd S  |  | jD ]}| q| jD ]>}t|jtr!q|j}|jrV|  u r/|js/qd  }}|j	j
D ]}|jdkrB|j}q7|jdkrJ|j}q7|rV|j||f qd S )N__getbuffer____releasebuffer__)r   cimported_modulestype_entriesr"   rI   r   r0   r   r@   r(   pyfunc_entriesr3   
func_cnamer   typeptr_cname)r(   mer   releasers   r   cython_scopefind_buffer_typestypesvisited_scopesr   r   r5  J  s*   



zBGetAndReleaseBufferUtilityCode.put_code.<locals>.find_buffer_typesGetAndReleaseBufferBuffer.c)r6  )	from_filecontextr   )module_noder(   r;  r4  setr	   loaddictformat_coder  inject_string_constantsimplr   )r*   outputr   
proto_coder   	util_coder  rB  r   r3  r   put_codeA  s$   
z'GetAndReleaseBufferUtilityCode.put_code)
re   rf   rg   requiresis_cython_utilityr   r%  r&  r(  rF  r   r   r   r   r!  .  s    r!  c                 C   s6   | j rdS | jr
dS | js| jrd}nd}||   S )Nobjectptrnn_r   )is_pyobjectrL   
is_typedefis_struct_or_unionspecialization_name)rK   rA   r   r   r   mangle_dtype_namel  s   rP  c              	      sB  t |}d| }d| }|jrdS du r| dkrJ | jjvr jj|  jd }g }|jrF|jrF||j |j	}|js:|j
oL| }| }	| rXd}na|jr|j}
|jrd|
j}
|
j}t|dksoJ  fd	d
|D }|jd| dd |jr|j }n| }t||D ]\}}|jd||j||jf dd q|jddd |jddd nJ t|}d}d}|tju rd|	 }d}n/|jrd|	 }d| }n#|s|jrd}n|jrd}n|jrd}|jrd}n
|j rd}nJ |d}||||	d!dd
 |D pdt||||f	}|j|| dd |S )ar  
    Output the run-time type information (__Pyx_TypeInfo) for given dtype,
    and return the name of the type info struct.

    Structs with two floats of the same size are encoded as complex numbers.
    One can separate between complex numbers declared as struct or with native
    encoding by inspecting to see if the fields field of the type is
    filled in.
    z__Pyx_TypeInfo_%sz__Pyx_StructFields_%sz<error>Nr   FtypeinfoNULLc                    s   g | ]}t  |jd  qS )r   )r   r0   )r   fr   maxdepthr   r   r     s    z.get_type_information_cname.<locals>.<listcomp>z!static __Pyx_StructField %s[] = {T)safez   {&%s, "%s", offsetof(%s, %s)},z  {NULL, NULL, 0}z};0z__PYX_IS_UNSIGNED(%s)z'H'z%s ? 'U' : 'I'z'C'z'R'z'S'__PYX_BUF_FLAGS_PACKED_STRUCTz'O'zLstatic __Pyx_TypeInfo %s = { "%s", %s, sizeof(%s), { %s }, %s, %s, %s, %s };r   c                 S   s   g | ]}t |qS r   )strr   r   r   r   r     r   )"rP  is_errorr   r   r   r   is_arrayr   size	base_typerN  can_be_complexr   is_simple_buffer_dtype	is_structr(   is_cv_qualifiedbase_type_scopevar_entriesrG   r   cv_base_typer   r3   r;   rY  r   c_char_typeis_int
is_complexis_floatpackedrL  r   )r   rK   rU  
namesuffixr3   structinfo_nametypecode
arraysizescomplex_possibledeclcodestruct_scopefieldsr6  struct_typerS  rQ  repr   is_unsigned	typegrouptupr   rT  r   r   z  s   





r   c                 K   s4   |d u rt j| dfi |S tj| dfd|i|S )Nr9  r;  )r   r>  r	   )util_code_namer;  r'  r   r   r   load_buffer_utility  s   rx  )max_dimsBufferStructDeclare)r;  BufferFormatStructsBufferIndexErrorBufferIndexErrorNogilBufferFallbackErrorBufferGetAndValidateBufferFormatCheckTypeInfoToFormat)r   r   r6   )?
__future__r   Visitorr   r   Errorsr   r   r   Coder	   r   r
   r   r   r   r   r   r!   r~   rz   r|   r   r}   r   r   r   r   r   ru   r   rI  r   r   r   r   r   r   r   r   r   r  r'   r   r   r   r   r)   r!  rP  r   rx  r?  rN   r;  r  buffer_formats_declare_coder  r  r   r   buffer_format_check_code_typeinfo_to_format_coder   r   r   r   <module>   sl    
[
EGHB>

_