a
    bkq                     @   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Z2dVdGdHZ3e4ej5dIZ6e3dJe6dKZ7e3dLZ8e3dMZ9e3dNZ:e3dOZ;e3dPe6dKZ<e3dQe6dKZ=e3dRZ>dS )W    )absolute_import   )CythonTransform)
ModuleNode)CompileError)CythonUtilityCode)UtilityCodeTempitaUtilityCode)Options)Interpreter)
PyrexTypes)Naming)Symtabc                    sF   ddl m} || } |dkrBd|  d fdd| dD } | S )Nr   dedent 
c                    s   g | ]} | qS  r   .0xindentr   f/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Compiler/Buffer.py
<listcomp>       zdedent.<locals>.<listcomp>)textwrapr   joinsplit)textZ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           
         sh  j  }dd |D }t|dkr:|jdd d d| _dd |D }t|dkrZd| _|D ]&\d	kr^tjtr^d| _ qq^~tt	rt|dkrt
jd
|D ]jjjrt
jdjj}|jtjkrt
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||	_q|_| _d S )Nc                 S   s   g | ]\}}|j jr|qS r   )typeZ	is_bufferr   nameentryr   r   r   r   3   r   z=IntroduceBufferAuxiliaryVars.handle_scope.<locals>.<listcomp>r   c                 S   s   | j S N)r0   )r1   r   r   r   <lambda>5   r   z;IntroduceBufferAuxiliaryVars.handle_scope.<locals>.<lambda>)keyTc                 S   s   g | ]\}}|j jr|qS r   )r.   Zis_memoryviewslicer/   r   r   r   r   8   r   
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)r0   cnamer.   posT)ZmangleZdeclare_varr7   Zis_argused)r.   prefixr6   Zaux_var)r1   r0   r*   r'   r   r   decvarS   s    z9IntroduceBufferAuxiliaryVars.handle_scope.<locals>.decvarc                    s   g | ]\}} ||qS r   r   )r   r.   r9   )r:   r   r   r   ^   r   )entriesitemslensortr%   r!   utility_code_definitionr   using_memoryviewr   r   r7   r.   dtypeis_ptrr0   ndimr
   buffer_max_dimsr"   r   Zc_pyx_buffer_nd_typer   Zpybuffernd_prefixZc_pyx_buffer_typeZpybufferstruct_prefixr   Z	BufferAux
buffer_auxZbuffer_entriesr'   )
r)   r*   r'   Zscope_itemsZbufvarsZmemviewslicevarsbuftypeZauxvarsZ
pybufferndZrcbufferr   )r:   r1   r0   r*   r'   r   handle_scope.   sD    

	

z)IntroduceBufferAuxiliaryVars.handle_scopec                 C   s   |  ||j | | |S r2   )rG   r'   visitchildrenr)   r*   r   r   r   visit_ModuleNodee   s    
z-IntroduceBufferAuxiliaryVars.visit_ModuleNodec                 C   s   |  ||j | | |S r2   )rG   Zlocal_scoperH   rI   r   r   r   visit_FuncDefNodej   s    
z.IntroduceBufferAuxiliaryVars.visit_FuncDefNode)
__name__
__module____qualname__r%   r@   r$   rG   rJ   rK   __classcell__r   r   r,   r   r       s   7r    )rA   rC   modenegative_indicescastfullTF)rC   rP   rQ   rR   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rnt|t	| ||< qLt
t|D ]@\}\}}|tvrt|t	| |v rt|t| ||< qtD ]F}|vrz|| |< W q ty   |rt t| Y q0 qȈd}	|	r2|	jr2t td}
|
rbt|
trX|
dk rbt 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   rA   )Ztype_envZ	type_argsr   rA   rC   r   rP   )rS   stridedcfortranc                    s&    | }t|ts"t t|  d S r2   )getr!   boolr   ERR_BUF_BOOL)r0   r   	globalposoptionsr   r   assert_bool   s    

z+analyse_buffer_options.<locals>.assert_boolrQ   rR   )buffer_defaultsr   Zinterpret_compiletime_optionsr=   buffer_positional_options_countr   ERR_BUF_TOO_MANYr<   buffer_optionsERR_BUF_OPTION_UNKNOWNzipERR_BUF_DUPKeyErrorERR_BUF_MISSINGrX   is_extension_typeERR_BUF_DTYPEr!   intERR_BUF_NDIMERR_BUF_MODE)r\   envZposargsZdictargsdefaultsZneed_completer0   valuer7   rA   rC   rP   r^   r   r[   r   analyse_buffer_options   sL    








rp   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)	r1   r.   rE   buflocal_nd_varr6   buf_ptrZbuffer_ptr_typebuf_ptr_typeinit_attributes)r)   r1   r   r   r   __init__   s    
zBufferEntry.__init__c                 C   s"   |   | _|  | _|  | _d S r2   )get_buf_shapevarsshapeget_buf_stridevarsstridesget_buf_suboffsetvars
suboffsetsr)   r   r   r   ru      s    

zBufferEntry.init_attributesc                 C   s
   |  dS )Nz%s.diminfo[%d].suboffsets_for_all_ndimr}   r   r   r   r{      s    z!BufferEntry.get_buf_suboffsetvarsc                 C   s
   |  dS )Nz%s.diminfo[%d].stridesr~   r}   r   r   r   ry      s    zBufferEntry.get_buf_stridevarsc                 C   s
   |  dS )Nz%s.diminfo[%d].shaper~   r}   r   r   r   rw      s    zBufferEntry.get_buf_shapevarsc                    s    fddt jjD S )Nc                    s   g | ]} j |f qS r   )r6   r   isr)   r   r   r      r   z-BufferEntry._for_all_ndim.<locals>.<listcomp>)ranger.   rC   )r)   r   r   r   r   r      s    zBufferEntry._for_all_ndimc                 C   sD  g }| j j}| j j}|dkrht||  |  D ](\}}}|| || || q0d| }	t}
nv|dkr~d| }	t}
n4|dkrd| }	t	}
n|dkrd| }	t
}
nd	sJ t||  D ]\}}|| || q|	|jjvr|jj|	 |jd
 }|jd }|
|||	|d | j }d|	|| jd|f }|S )NrS   z__Pyx_BufPtrFull%ddrU   z__Pyx_BufPtrStrided%ddrV   z__Pyx_BufPtrCContig%ddrW   z__Pyx_BufPtrFortranContig%ddFutility_code_protoutility_code_def)r0   ndz%s(%s, %s, %s), )r.   rC   rP   rd   ry   r{   appendbuf_lookup_full_codebuf_lookup_strided_codebuf_lookup_c_codebuf_lookup_fortran_codeglobalstateutility_codesaddrt   empty_declaration_coders   r   )r)   codeindex_cnamesparamsr   rP   r   r   ofuncnameZfuncgenZ	protocodeZdefcodeZbuf_ptr_type_codeZptrcoder   r   r   generate_buffer_lookup_code   sH    






z'BufferEntry.generate_buffer_lookup_codeN)
rL   rM   rN   rv   ru   r{   ry   rw   r   r   r   r   r   r   rq      s   rq   c                 C   sl   d}|j }|dkr|d7 }n>|dkr.|d7 }n,|dkr@|d7 }n|dkrR|d	7 }nd
sZJ | jrh|d7 }|S )NZPyBUF_FORMATrS   z| PyBUF_INDIRECTrU   z| PyBUF_STRIDESrV   z| PyBUF_C_CONTIGUOUSrW   z| PyBUF_F_CONTIGUOUSFz| PyBUF_WRITABLE)rP   Zwritable_needed)rE   buffer_typeflagsrP   r   r   r   	get_flags  s    



r   c                 C   s   | j }d|j_d|j_d S )NT)rE   rr   r8   	rcbuf_var)r1   rE   r   r   r   used_buffer_aux_vars  s    r   c           	      C   s   | j | jj }}|jj}ddg}|dkr2|d g }t| jjD ](}|D ]}|d||||||f  qJqB|d	| d S )Nrz   rx   rS   r|   z1%s.diminfo[%d].%s = %s.rcbuffer->pybuffer.%s[%d];r   )
rE   r.   rP   rr   r6   r   r   rC   putlnr   )		buf_entryr   rE   rP   pybuffernd_structZfldnameslnr   Z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;)rE   rr   r6   r   r   )r1   r   Zbufauxr   Zpybuffer_structr   r   r   put_init_vars2  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})	rE   get_getbuffer_callr6   r.   r   rA   struct_nesting_depthZerror_goto_ifr   )r1   r   r7   rE   	getbufferr   r   r   put_acquire_arg_buffer@  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   rE   rr   r6   )r   r1   r   r   r   put_release_buffer_codeN  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))rC   rj   rR   r   rr   r6   get_type_information_cnamerA   r   r   r   locals)	r   Z	obj_cnamerE   r   rC   rR   r   r   Zdtype_typeinfor   r   r   r   S  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	| nj 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Z
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;ZPy_NoneN)rE   r.   rr   r6   r   r   rA   r   r   r   r   r   
c_int_typeunlikelytupler   r   r   raise_buffer_fallback_coderelease_tempr   Zerror_goto_if_negZtypecastr   
error_gotoput)Z	lhs_cnameZ	rhs_cnamer   Zis_initializedr7   r   rE   r   r   r   r   Zretcode_cnameZ	exc_tempstr   r   r   put_assign_to_buffera  sR    









r   c              
   C   s  |d o
|}|d rr|j jtjdd}|d|  tt|||  D ]\}	\}
}}|
dkr|d|  |r|d||f  |d	|d
| ||	f  n|d||	f  |	d |
dkrd}nd}|d	|d|||f ||	f  qJ|r|j
t d}n|j
t d}|d|d|   |d||f  ||| |d |j | nB|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
    Z
wraparoundZ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%sZ __Pyx_RaiseBufferIndexErrorNogilZ__Pyx_RaiseBufferIndexErrorr   z%s != -1z%s(%s);r   zif (%s < 0) %s += %s;)r   r   r   r   r   	enumeraterd   rw   r   r   r   r   raise_indexerror_nogilraise_indexerror_coder   r   r   )r1   Zindex_signedsr   Z
directivesr7   r   rQ   Zin_nogil_contextZfailed_dim_tempZdimsignedr6   rx   rR   funcr   r   r   put_buffer_lookup_code  sJ    
"




r   c                 C   s   |  t d S r2   )r   buffer_struct_declare_coderm   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   s   g | ]}d |||f qS )zi%d, s%d, o%dr   r   r   r   r   r     r   z(buf_lookup_full_code.<locals>.<listcomp>z1#define %s(type, buf, %s) (type)(%s_imp(buf, %s))c                 S   s   g | ]}d |||f qS )z.Py_ssize_t i%d, Py_ssize_t s%d, Py_ssize_t o%dr   r   r   r   r   r     r   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definr0   r   Z	macroargsZ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   s   g | ]}d ||f qS zi%d, s%dr   r   r   r   r   r   
  r   z+buf_lookup_strided_code.<locals>.<listcomp> + c                 S   s   g | ]}d ||f qS 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   r0   r   argsoffsetr   r   r   r     s    r   c                 C   sj   |dkr|  d|  nNd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   s   g | ]}d ||f qS r   r   r   r   r   r   r     r   z%buf_lookup_c_code.<locals>.<listcomp>r   c                 S   s   g | ]}d ||f qS 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   sd   |dkr|  d|  nHd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   s   g | ]}d ||f qS r   r   r   r   r   r   r   $  r   z+buf_lookup_fortran_code.<locals>.<listcomp>r   c                 S   s   g | ]}d ||f qS r   r   r   r   r   r   r   %  r   r   r   Nr   r   r   r   r   r     s
    r   c                 C   s   |  t  d S r2   )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   s   d S r2   r   r}   r   r   r   rv   5  s    z'GetAndReleaseBufferUtilityCode.__init__c                 C   s
   t |tS r2   )r!   r   )r)   otherr   r   r   __eq__8  s    z%GetAndReleaseBufferUtilityCode.__eq__c                 C   s   dS )NiFosr   r}   r   r   r   __hash__;  s    z'GetAndReleaseBufferUtilityCode.__hash__c                 K   s   d S r2   r   )r)   kwargsr   r   r   get_tree>  r   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rBq0|j}|jr0|  u r^|js^q0d  }}|j	j
D ]&}|jdkr|j}qn|jdkrn|j}qn|r0|j||f q0d S )NZ__getbuffer__Z__releasebuffer__)r   Zcimported_modulesZtype_entriesr!   r?   r   r.   rh   r8   r'   Zpyfunc_entriesr0   Z
func_cnamer   Ztypeptr_cname)r'   mer   releaserX   r   cython_scopefind_buffer_typestypesZvisited_scopesr   r   r   I  s$    



zBGetAndReleaseBufferUtilityCode.put_code.<locals>.find_buffer_typesZGetAndReleaseBufferBuffer.c)r   )	from_filecontextr   )Zmodule_noder'   r   r   setr	   loaddictZformat_coder   Zinject_string_constantsimplr   )r)   outputr   Z
proto_coderm   Z	util_coder   r   r   r   r   put_code@  s$    
z'GetAndReleaseBufferUtilityCode.put_code)
rL   rM   rN   requiresZis_cython_utilityrv   r   r   r   r   r   r   r   r   r   -  s   r   c                 C   s:   | j r
dS | jrdS | js | jr&d}nd}||   S d S )NobjectptrZnn_r   )is_pyobjectrB   Z
is_typedefis_struct_or_unionZspecialization_name)rA   r9   r   r   r   mangle_dtype_namek  s    r   c              	      sL  t |}d| }d| }|jr"dS du r2| dkrBdsBJ | jjvrH jj|  jd }g }|jr|jr||j |j	}qr|j
o| }| }	| rd}n|jr\|j}
|jr|
j}
|
j}t|dksJ  fd	d
|D }|jd| dd t||D ],\}}|jd||j| |jf dd q|jddd |jddd n
dsfJ t|}d}d}|tju rd|	 }d}nr|jrd|	 }d| }nX|s|jrd}nD|jrd}n6|jrd}|jrd}n|jrd}nds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typeinfoZNULLc                    s   g | ]}t  |jd  qS )r   )r   r.   )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IS_UNSIGNED(%s)z'H'z%s ? 'U' : 'I'z'C'z'R'z'S'Z__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   )!r   Zis_errorr   r   r   r   Zis_arrayr   sizeZ	base_typer   Zcan_be_complexr   Zis_simple_buffer_dtypeZ	is_structr'   Zis_constZconst_base_type_scopeZvar_entriesr=   r   rd   r0   r6   r   r   Zc_char_typeZis_intZ
is_complexZis_floatpackedr   r   )r   rA   r   Z
namesuffixr0   Zstructinfo_nametypecodeZ
arraysizesZcomplex_possibleZdeclcodeZstruct_scopefieldsr   r   r   repr   Zis_unsignedZ	typegrouptupr   r   r   r   y  s    





r   c                 K   s8   |d u rt j| dfi |S tj| dfd|i|S d S )Nr   r   )r   r   r	   )Zutil_code_namer   r   r   r   r   load_buffer_utility  s    r  )Zmax_dimsZBufferStructDeclare)r   ZBufferFormatStructsZBufferIndexErrorZBufferIndexErrorNogilZBufferFallbackErrorZBufferGetAndValidateZBufferFormatCheckZTypeInfoToFormat)r   )NT)N)N)?
__future__r   ZVisitorr   r   ZErrorsr   r   r   ZCoder	   r   r
   r   r   r   r   r   r    rb   r_   r`   rc   ra   re   rg   rl   rk   ri   rZ   rp   r   rq   r   r   r   r   r   r   r   r   r   r&   r   r   r   r   r(   r   r   r   r  r   rD   r   r   Zbuffer_formats_declare_coder   r   r   r   Zbuffer_format_check_codeZ_typeinfo_to_format_coder   r   r   r   <module>   sj   
[
EGHB>
W
