
    shH>                         S SK r S SKJr  S SKJr  S SKJrJrJrJ	r	  S SK
Jr  \" SS5      r\" SS	S
5      r\" SSS5      r " S S\5      rg)    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                   "   \ rS rSrSrS rS rS r\S 5       rS r	S r
\S	 5       rS
 r\S 5       rS r\S 5       rS r\S 5       rS r\S 5       rS r\S 5       rS r\S 5       rS rS rS rS rS rS rS rS rS r S r!S  r"S! r#S"r$g#)$
NRTContext   z>
An object providing access to NRT APIs in the lowering pass.
c                 t    Xl         X l        [        R                  (       a  [        U l        g [        U l        g )N)_context_enabledr   	DEBUG_NRT_NRT_MEMINFO_SAFE_API_meminfo_api_NRT_MEMINFO_DEFAULT_API)selfcontextenableds      n/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/core/runtime/context.py__init__NRTContext.__init__   s'      5D 8D    c                 R    U R                   (       d  [        R                  " S5      eg )NzNRT required but not enabled)r   r   NumbaRuntimeError)r   s    r!   _require_nrtNRTContext._require_nrt(   s     }}**+IJJ r$   c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                 d   > T" X/UQ70 UD6nSn[         R                  " U R                  XUS9  U$ )Nz'Allocation failed (probably too large).)msg)r   guard_memory_errorr   )r   builderargskwargsmemptrr+   funcs         r!   wrap+NRTContext._check_null_result.<locals>.wrap-   s7    $9$9&9F;C&&t}}g3OMr$   )	functoolswraps)r1   r2   s   ` r!   _check_null_resultNRTContext._check_null_result,   s%    			 
	
 r$   c                 $    U R                  X5      $ )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!   allocateNRTContext.allocate5   s     &&w55r$   c                 *   U R                  5         UR                  n[        R                  " [        R
                  [        R                  /5      n[        R                  " X4S5      nUR                  R                  S5        UR                  XR/5      $ )zk
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-   r;   modfntyfns         r!   r9   NRTContext.allocate_unchecked>   sk    
 	nnw007>>2BC++C~F
%%i0||B''r$   c                     U R                  5         UR                  n[        R                  " [        R                  " 5       [
        R                  /5      n[
        R                  " X4S5      nUR                  XR/5      $ )z9
Low-level free a memory area allocated with allocate().
NRT_Free	r'   rB   r   rC   VoidTyper   rD   rF   rI   )r   r-   ptrrK   rL   rM   s         r!   freeNRTContext.freeK   s[     	nnr{{}w/@/@.AB++CzB||B&&r$   c                 $    U R                  X5      $ )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_uncheckedr:   s      r!   meminfo_allocNRTContext.meminfo_allocV   s     ++G::r$   c                 R   U R                  5         UR                  n[        R                  " [        R
                  [        R                  /5      n[        R                  " X4U R                  R                  5      nUR                  R                  S5        UR                  XR/5      $ )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'   rB   r   rC   r   rD   rE   rF   r   r   rG   rH   rI   rJ   s         r!   rW   "NRTContext.meminfo_alloc_uncheckedb   s{     	nnw007>>2BC++C,0,=,=,C,CE
%%i0||B''r$   c                 &    U R                  XU5      $ )z
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-   r;   dtors       r!   meminfo_alloc_dtorNRTContext.meminfo_alloc_dtors   s     00EEr$   c                    U R                  5         UR                  n[        R                  " [        R
                  [        R                  [        R
                  /5      n[        R                  " XEU R                  R                  5      nUR                  R                  S5        UR                  XbUR                  U[        R
                  5      /5      $ )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'   rB   r   rC   r   rD   rE   rF   r   r   rG   rH   rI   bitcastr   r-   r;   r_   rK   rL   rM   s          r!   r]   '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                 &    U R                  XU5      $ )aC  
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-   r;   aligns       r!   meminfo_alloc_aligned NRTContext.meminfo_alloc_aligned   s     33G5IIr$   c                 0   U R                  5         UR                  n[        R                  " S5      n[        R                  " [
        R                  [
        R                  U/5      n[
        R                  " XFU R                  R                  5      nUR                  R                  S5        [        U[        5      (       a+  U R                  R!                  ["        R$                  U5      nOUR&                  U:X  d   S5       eUR)                  XrU/5      $ )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'   rB   r   IntTyperC   r   rD   rE   rF   r   r   rG   rH   
isinstanceintr   get_constantr   uint32typerI   )r   r-   r;   rh   rK   u32rL   rM   s           r!   rg   *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::$>&>>$||Bu..r$   c                 $    U R                  X5      $ )aP  
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_uncheckedr:   s      r!   meminfo_new_varsizeNRTContext.meminfo_new_varsize   s     11'@@r$   c                 *   U R                  5         UR                  n[        R                  " [        R
                  [        R                  /5      n[        R                  " X4S5      nUR                  R                  S5        UR                  XR/5      $ )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.

Returns NULL to indicate error/failure to allocate.
NRT_MemInfo_new_varsizer@   rA   rJ   s         r!   rv   (NRTContext.meminfo_new_varsize_unchecked   so     	nnw007>>2BC++C,EG
%%i0||B''r$   c                 &    U R                  XU5      $ )a  
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_uncheckedr^   s       r!   meminfo_new_varsize_dtor#NRTContext.meminfo_new_varsize_dtor   s     66wdKKr$   c                    U R                  5         UR                  n[        R                  " [        R
                  [        R                  [        R
                  /5      n[        R                  " XES5      nUR                  XbU/5      $ )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'   rB   r   rC   r   rD   rE   rF   rI   rd   s          r!   r}   -NRTContext.meminfo_new_varsize_dtor_unchecked   sk     	nnw00 '0A0ABD++57||Bt--r$   c                 &    U R                  XU5      $ )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-   meminfor;   s       r!   meminfo_varsize_alloc NRTContext.meminfo_varsize_alloc   s     33GdKKr$   c                 (    U R                  XUS5      $ )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_allocr   s       r!   r   *NRTContext.meminfo_varsize_alloc_unchecked  s      ''$(CE 	Er$   c                 &    U R                  XU5      $ )z
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_uncheckedr   s       r!   meminfo_varsize_realloc"NRTContext.meminfo_varsize_realloc  s     55gMMr$   c                 (    U R                  XUS5      $ )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_reallocr   r   s       r!   r   ,NRTContext.meminfo_varsize_realloc_unchecked&  s      ''$(EG 	Gr$   c                    U R                  5         UR                  n[        R                  " [        R                  " 5       [
        R                  [
        R                  /5      n[
        R                  " XES5      nUR                  XbU45      $ )zi
Free a memory area allocated for a NRT varsize object.
Note this does *not* free the NRT object itself!
NRT_MemInfo_varsize_freerQ   )r   r-   r   rS   rK   rL   rM   s          r!   meminfo_varsize_freeNRTContext.meminfo_varsize_free0  sm    
 	nnr{{} ' 1 173D3DEG++C,FH||B#//r$   c                 J   U R                  5         UR                  n[        R                  " [        R
                  [        R
                  [        R                  /5      n[        R                  " XVU5      nUR                  R                  S5        UR                  XrU/5      $ )Nr@   rA   )r   r-   r   r;   funcnamerK   rL   rM   s           r!   r   NRTContext._call_varsize_alloc>  sw    nnw00 ' 1 17>>BD++Cx@
%%i0||B$00r$   c                     U R                  5         SSKJn  UR                  n[        R
                  " XCS5      nUR                  XR/5      $ )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   rB   r   rF   rI   )r   r-   r   r   rK   rM   s         r!   meminfo_dataNRTContext.meminfo_dataH  sC     	@nn++C,CE||B	**r$   c                 :   U R                   R                  U   nUR                  U5      n/ nUR                  5       (       a#  UR	                  X5      nUR                  X'45        U H0  u  pU	" U5      n
U R                  XU
5      nUR                  U5        M2     U$ )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   NRTContext.get_meminfosW  s     MM44R8	$$W-$$&&**78BOORH%#LD3KE!..weDNOON+ $ r$   c                 L   U R                  5         SSKJn  U R                  XU5      nU Hv  u  pxUR                  n	[
        R                  " XU5      n
U
R                  S   R                  S5        U
R                  S   R                  S5        UR                  X/5        Mx     g)zGCall function of *funcname* on every meminfo found in *value*.
        r   )incref_decref_tyr@   	nocaptureN)
r'   r   r   r   rB   r   rF   r.   rH   rI   )r   r-   typvaluer   r   r   _r   rK   rM   s              r!   _call_incref_decrefNRTContext._call_incref_decrefh  s     	A$$W59EA..C//08:B GGAJ$$Y/GGAJ$$[1LLT" r$   c                 *    U R                  XUS5        g)z7
Recursively incref the given *value* and its members.

NRT_increfNr   r   r-   r   r   s       r!   increfNRTContext.increfz       	  ulCr$   c                 *    U R                  XUS5        g)z7
Recursively decref the given *value* and its members.

NRT_decrefNr   r   s       r!   decrefNRTContext.decref  r   r$   c                     U R                  5         [        R                  " [        R                  S5      nUR
                  n[        R                  " X2S5      nUR                  US5      $ )zGCalls NRT_get_api(), which returns the NRT API function table.
         NRT_get_api)r'   r   rC   r   rD   rB   rF   rI   )r   r-   rL   rK   rM   s        r!   get_nrt_apiNRTContext.get_nrt_api  sR     	w00"5nn++C}E||B##r$   c                 L   U R                   nUR                  nUR                  U5      nUR                  nUR	                  [
        R                  " X5      5      n[        S:  a-  UR                  U5         U R                  U5        SSS5        U$ U$ ! , (       d  f       U$ = f)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_checkNRTContext.eh_check  s     mm]]''0	##\\'//'"CD
w,( -z -,s   6B
B#c                 V    U R                   nUR                  nUR                  U5        g)zBegin a try-block.
        N)r   r   set_try_statusr   r-   r   r   s       r!   eh_tryNRTContext.eh_try  s$     mm]]
'"r$   c                 V    U R                   nUR                  nUR                  U5        g)zEnd a try-block
        N)r   r   unset_try_statusr   s       r!   r   NRTContext.eh_end_try  s$     mm]]
G$r$   )r   r   r   N)%__name__
__module____qualname____firstlineno____doc__r"   r'   r6   r<   r9   rT   rX   rW   r`   r]   ri   rg   rw   rv   r~   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__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   )r4   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$   