
    wgH>                         d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZ  edd      Z edd	d
      Z eddd      Z G d de      Zy)    N)
namedtuple)ir)typescgutilserrorsconfig)	PYVERSION_NRT_Meminfo_Functions)alloc
alloc_dtoralloc_alignedNRT_MemInfo_alloc_safeNRT_MemInfo_alloc_dtor_safeNRT_MemInfo_alloc_safe_alignedNRT_MemInfo_allocNRT_MemInfo_alloc_dtorNRT_MemInfo_alloc_alignedc                      e Zd ZdZd Zd Zd Zed        Zd Zd Z	ed        Z
d	 Zed
        Zd Zed        Zd Zed        Zd Zed        Zd Zed        Zd Zed        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"y!)"
NRTContextzF
    An object providing access to NRT APIs in the lowering pass.
    c                 n    || _         || _        t        j                  rt        | _        y t        | _        y )N)_context_enabledr   	DEBUG_NRT_NRT_MEMINFO_SAFE_API_meminfo_api_NRT_MEMINFO_DEFAULT_API)selfcontextenableds      _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/numba/core/runtime/context.py__init__zNRTContext.__init__   s+      5D 8D    c                 F    | j                   st        j                  d      y )NzNRT required but not enabled)r   r   NumbaRuntimeError)r   s    r    _require_nrtzNRTContext._require_nrt(   s     }}**+IJJ r"   c                 B     t        j                          fd       }|S )Nc                 l     | |g|i |}d}t        j                  | j                  |||       |S )Nz'Allocation failed (probably too large).)msg)r   guard_memory_errorr   )r   builderargskwargsmemptrr(   funcs         r    wrapz+NRTContext._check_null_result.<locals>.wrap-   s;    $9$9&9F;C&&t}}gv3OMr"   )	functoolswraps)r.   r/   s   ` r    _check_null_resultzNRTContext._check_null_result,   s%    			 
	
 r"   c                 &    | j                  ||      S )z
        Low-level allocate a new memory area of `size` bytes. The result of the
        call is checked and if it is NULL, i.e. allocation failed, then a
        MemoryError is raised.
        )allocate_uncheckedr   r*   sizes      r    allocatezNRTContext.allocate5   s     &&w55r"   c                 *   | j                          |j                  }t        j                  t        j
                  t        j                  g      }t	        j                  ||d      }|j                  j                  d       |j                  ||g      S )z
        Low-level allocate a new memory area of `size` bytes. Returns NULL to
        indicate error/failure to allocate.
        NRT_Allocatenoaliasr%   moduler   FunctionTyper   	voidptr_tintp_tget_or_insert_functionreturn_valueadd_attributecallr   r*   r6   modfntyfns         r    r4   zNRTContext.allocate_unchecked>   so    
 	nnw007>>2BC++C~F
%%i0||B''r"   c                     | j                          |j                  }t        j                  t        j                         t
        j                  g      }t        j                  ||d      }|j                  ||g      S )zI
        Low-level free a memory area allocated with allocate().
        NRT_Free	r%   r<   r   r=   VoidTyper   r>   r@   rC   )r   r*   ptrrE   rF   rG   s         r    freezNRTContext.freeK   s_     	nnr{{}w/@/@.AB++CzB||B&&r"   c                 &    | j                  ||      S )z
        Allocate a new MemInfo with a data payload of `size` bytes.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )meminfo_alloc_uncheckedr5   s      r    meminfo_alloczNRTContext.meminfo_allocV   s     ++GT::r"   c                 R   | j                          |j                  }t        j                  t        j
                  t        j                  g      }t	        j                  ||| j                  j                        }|j                  j                  d       |j                  ||g      S )z
        Allocate a new MemInfo with a data payload of `size` bytes.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        r:   )r%   r<   r   r=   r   r>   r?   r@   r   r   rA   rB   rC   rD   s         r    rO   z"NRTContext.meminfo_alloc_uncheckedb   s     	nnw007>>2BC++C,0,=,=,C,CE
%%i0||B''r"   c                 (    | j                  |||      S )a  
        Allocate a new MemInfo with a data payload of `size` bytes and a
        destructor `dtor`.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )meminfo_alloc_dtor_uncheckedr   r*   r6   dtors       r    meminfo_alloc_dtorzNRTContext.meminfo_alloc_dtors   s     00$EEr"   c                    | j                          |j                  }t        j                  t        j
                  t        j                  t        j
                  g      }t	        j                  ||| j                  j                        }|j                  j                  d       |j                  |||j                  |t        j
                        g      S )z
        Allocate a new MemInfo with a data payload of `size` bytes and a
        destructor `dtor`.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        r:   )r%   r<   r   r=   r   r>   r?   r@   r   r   rA   rB   rC   bitcastr   r*   r6   rU   rE   rF   rG   s          r    rS   z'NRTContext.meminfo_alloc_dtor_unchecked   s     	nnw00 '0A0ABD++C,0,=,=,H,HJ
%%i0||B!(w7H7H!I!K L 	Lr"   c                 (    | j                  |||      S )a{  
        Allocate a new MemInfo with an aligned data payload of `size` bytes.
        The data pointer is aligned to `align` bytes.  `align` can be either
        a Python int or a LLVM uint32 value.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )meminfo_alloc_aligned_unchecked)r   r*   r6   aligns       r    meminfo_alloc_alignedz NRTContext.meminfo_alloc_aligned   s     33GT5IIr"   c                 "   | j                          |j                  }t        j                  d      }t        j                  t
        j                  t
        j                  |g      }t        j                  ||| j                  j                        }|j                  j                  d       t        |t              r+| j                  j!                  t"        j$                  |      }n|j&                  |k(  sJ d       |j)                  |||g      S )a<  
        Allocate a new MemInfo with an aligned data payload of `size` bytes.
        The data pointer is aligned to `align` bytes.  `align` can be either
        a Python int or a LLVM uint32 value.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
            r:   zalign must be a uint32)r%   r<   r   IntTyper=   r   r>   r?   r@   r   r   rA   rB   
isinstanceintr   get_constantr   uint32typerC   )r   r*   r6   r\   rE   u32rF   rG   s           r    r[   z*NRTContext.meminfo_alloc_aligned_unchecked   s     	nnjjnw007>>32GH++C,0,=,=,K,KM
%%i0eS!MM..u||UCE::$>&>>$||Bu..r"   c                 &    | j                  ||      S )a  
        Allocate a MemInfo pointing to a variable-sized data area.  The area
        is separately allocated (i.e. two allocations are made) so that
        re-allocating it doesn't change the MemInfo's address.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )meminfo_new_varsize_uncheckedr5   s      r    meminfo_new_varsizezNRTContext.meminfo_new_varsize   s     11'4@@r"   c                 *   | j                          |j                  }t        j                  t        j
                  t        j                  g      }t	        j                  ||d      }|j                  j                  d       |j                  ||g      S )aI  
        Allocate a MemInfo pointing to a variable-sized data area.  The area
        is separately allocated (i.e. two allocations are made) so that
        re-allocating it doesn't change the MemInfo's address.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        NRT_MemInfo_new_varsizer:   r;   rD   s         r    rh   z(NRTContext.meminfo_new_varsize_unchecked   ss     	nnw007>>2BC++C,EG
%%i0||B''r"   c                 (    | j                  |||      S )a8  
        Like meminfo_new_varsize() but also set the destructor for
        cleaning up references to objects inside the allocation.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )"meminfo_new_varsize_dtor_uncheckedrT   s       r    meminfo_new_varsize_dtorz#NRTContext.meminfo_new_varsize_dtor   s     66wdKKr"   c                    | j                          |j                  }t        j                  t        j
                  t        j                  t        j
                  g      }t	        j                  ||d      }|j                  |||g      S )z
        Like meminfo_new_varsize() but also set the destructor for
        cleaning up references to objects inside the allocation.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        NRT_MemInfo_new_varsize_dtor)	r%   r<   r   r=   r   r>   r?   r@   rC   rY   s          r    rm   z-NRTContext.meminfo_new_varsize_dtor_unchecked   so     	nnw00 '0A0ABD++57||Bt--r"   c                 (    | j                  |||      S )a:  
        Allocate a new data area for a MemInfo created by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Contrary to realloc(), this always allocates a new area and doesn't
        copy the old data.  This is useful if resizing a container needs
        more than simply copying the data area (e.g. for hash tables).

        The old pointer will have to be freed with meminfo_varsize_free().

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )meminfo_varsize_alloc_uncheckedr   r*   meminfor6   s       r    meminfo_varsize_allocz NRTContext.meminfo_varsize_alloc   s     33GWdKKr"   c                 *    | j                  |||d      S )a  
        Allocate a new data area for a MemInfo created by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Contrary to realloc(), this always allocates a new area and doesn't
        copy the old data.  This is useful if resizing a container needs
        more than simply copying the data area (e.g. for hash tables).

        The old pointer will have to be freed with meminfo_varsize_free().

        Returns NULL to indicate error/failure to allocate.
        NRT_MemInfo_varsize_alloc_call_varsize_allocrs   s       r    rr   z*NRTContext.meminfo_varsize_alloc_unchecked  s"     ''$(CE 	Er"   c                 (    | j                  |||      S )a  
        Reallocate a data area allocated by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )!meminfo_varsize_realloc_uncheckedrs   s       r    meminfo_varsize_reallocz"NRTContext.meminfo_varsize_realloc  s     55gwMMr"   c                 *    | j                  |||d      S )z
        Reallocate a data area allocated by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Returns NULL to indicate error/failure to allocate.
        NRT_MemInfo_varsize_reallocrx   rs   s       r    r{   z,NRTContext.meminfo_varsize_realloc_unchecked&  s"     ''$(EG 	Gr"   c                    | j                          |j                  }t        j                  t        j                         t
        j                  t
        j                  g      }t        j                  ||d      }|j                  |||f      S )z
        Free a memory area allocated for a NRT varsize object.
        Note this does *not* free the NRT object itself!
        NRT_MemInfo_varsize_freerJ   )r   r*   rt   rL   rE   rF   rG   s          r    meminfo_varsize_freezNRTContext.meminfo_varsize_free0  sq    
 	nnr{{} ' 1 173D3DEG++C,FH||B#//r"   c                 J   | j                          |j                  }t        j                  t        j
                  t        j
                  t        j                  g      }t	        j                  |||      }|j                  j                  d       |j                  |||g      S )Nr:   r;   )r   r*   rt   r6   funcnamerE   rF   rG   s           r    ry   zNRTContext._call_varsize_alloc>  s{    nnw00 ' 1 17>>BD++Cx@
%%i0||B$00r"   c                     | j                          ddlm} |j                  }t	        j
                  ||d      }|j                  ||g      S )z
        Given a MemInfo pointer, return a pointer to the allocated data
        managed by it.  This works for MemInfos allocated with all the
        above methods.
        r   )meminfo_data_tyNRT_MemInfo_data_fast)r%   numba.core.runtime.nrtdynmodr   r<   r   r@   rC   )r   r*   rt   r   rE   rG   s         r    meminfo_datazNRTContext.meminfo_dataH  sG     	@nn++C,CE||B	**r"   c                 4   | j                   j                  |   }|j                  |      }g }|j                         r%|j	                  ||      }|j                  ||f       |D ]1  \  }}	 |	|      }
| j                  |||
      }|j                  |       3 |S )zCReturn a list of *(type, meminfo)* inside the given value.
        )r   data_model_managertraversehas_nrt_meminfoget_nrt_meminfoappendget_meminfosextend)r   r*   tyval	datamodelmembersmeminfosmimtypgetterfieldinner_meminfoss               r    r   zNRTContext.get_meminfosW  s     MM44R8	$$W-$$&**7C8BOORH%# 	,LD&3KE!..weDNOON+	, r"   c                 N   | j                          ddlm} | j                  |||      }|D ]w  \  }}|j                  }	t        j                  |	||      }
|
j                  d   j                  d       |
j                  d   j                  d       |j                  |
|g       y y)zGCall function of *funcname* on every meminfo found in *value*.
        r   )incref_decref_tyr:   	nocaptureN)
r%   r   r   r   r<   r   r@   r+   rB   rC   )r   r*   typvaluer   r   r   _r   rE   rG   s              r    _call_incref_decrefzNRTContext._call_incref_decrefh  s     	A$$Wc59 	#EAr..C//5E08:B GGAJ$$Y/GGAJ$$[1LLbT"	#r"   c                 ,    | j                  |||d       y)zG
        Recursively incref the given *value* and its members.
        
NRT_increfNr   r   r*   r   r   s       r    increfzNRTContext.increfz       	  #ulCr"   c                 ,    | j                  |||d       y)zG
        Recursively decref the given *value* and its members.
        
NRT_decrefNr   r   s       r    decrefzNRTContext.decref  r   r"   c                     | j                          t        j                  t        j                  d      }|j
                  }t        j                  ||d      }|j                  |d      S )zGCalls NRT_get_api(), which returns the NRT API function table.
         NRT_get_api)r%   r   r=   r   r>   r<   r@   rC   )r   r*   rF   rE   rG   s        r    get_nrt_apizNRTContext.get_nrt_api  sT     	w00"5nn++C}E||B##r"   c                 @   | j                   }|j                  }|j                  |      }|j                  }|j	                  t        j                  ||            }t        dk  r-|j                  |      5  | j                  |       ddd       |S |S # 1 sw Y   |S xY w)z(Check if an exception is raised
        )      N)
r   	call_convcheck_try_statusexcinfonot_r   is_nullr	   if_then
eh_end_try)r   r*   ctxcc	trystatusr   
has_raiseds          r    eh_checkzNRTContext.eh_check  s     mm]]''0	##\\'//'7"CD
w, )()z)s   5BBc                 V    | j                   }|j                  }|j                  |       y)zBegin a try-block.
        N)r   r   set_try_statusr   r*   r   r   s       r    eh_tryzNRTContext.eh_try  s$     mm]]
'"r"   c                 V    | j                   }|j                  }|j                  |       y)zEnd a try-block
        N)r   r   unset_try_statusr   s       r    r   zNRTContext.eh_end_try  s$     mm]]
G$r"   N)#__name__
__module____qualname____doc__r!   r%   r2   r7   r4   rM   rP   rO   rV   rS   r]   r[   ri   rh   rn   rm   ru   rr   r|   r{   r   ry   r   r   r   r   r   r   r   r   r   r   r"   r    r   r      s,   9K 6 6(	' 	; 	;(" 
F 
FL( J J/0 A A(& 
L 
L.$ L L E  N NG01+"#$DD$#%r"   r   )r0   collectionsr   llvmliter   
numba.corer   r   r   r   numba.core.utilsr	   r
   r   r   objectr   r   r"   r    <module>r      sh     "  5 5 & $$<%67  //G/L/OQ 
 22E2J2MO 
Q% Q%r"   