
    sh[t                        S SK r S SKrS SKrS SKrS SKr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Jr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJrJ r J!r!J"r"J#r#J$r$J%r%  S SK&J'r'  S SK(r(\(RR                  " \R                  RU                  S5      (       + S5      r+\(RR                  " \RX                  " 5       S;  S5      r- " S S\.5      r/ " S S\(R@                  5      r0 " S S\(R@                  5      r1 " S S\(R@                  5      r2 " S S\ 5      r3 " S S\(R@                  5      r4\$ " S S\\ 5      5       r5 " S S\ 5      r6\7S :X  a  \(Rp                  " 5         gg)!    Nnjit)types)rtsysnrtopt_nrt_pythonnrt)	intrinsicinclude_path)	signature)impl_ret_untracked)irNRT_get_api)EnableNRTStatsMixinTestCasetemp_directoryimport_dynamicskip_if_32bitskip_unless_cffirun_in_subprocess)
cpu_targetlinuxzlinux only test)i386x86_64zx86 only testc                   $    \ rS rSrSrS rS rSrg)Dummy%   r   c                 @    [        U 5      =R                  S-  sl        g N   typealiveselfs    h/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/tests/test_nrt.py__init__Dummy.__init__(       T
A    c                 @    [        U 5      =R                  S-  sl        g r    r"   r%   s    r'   __del__Dummy.__del__+   r*   r+    N)__name__
__module____qualname____firstlineno__r$   r(   r-   __static_attributes__r/   r+   r'   r   r   %   s    Er+   r   c                   "    \ rS rSrSrSrS rSrg)TestNrtMemInfoNotInitialized/   zb
Unit test for checking the use of the NRT fails if the
initialization sequence has not been run.
Fc                 f   SSSS.nUR                  5        Hv  u  p# U R                  [        5       nS[        l        [        [        U5      nU" U6   S S S 5        SnU R                  U[        WR                  5      5        S[        l        Mx     g ! , (       d  f       NE= f! S[        l        f = f)Nr/   )r/   r/   )r/   )librarymeminfo_newmeminfo_allocFz'Runtime must be initialized before use.T)	itemsassertRaisesRuntimeErrorr   _initgetattrassertInstr	exception)r&   methodsmethargsraisesfnmsgs          r'   test_init_fail+TestNrtMemInfoNotInitialized.test_init_fail6   s     "*$)
 "--/JD	#&&|4"'EK -BI 5
 @c3v'7'7#89" *44 #s"   B#!B/B#
B 	B##B0r/   N)r0   r1   r2   r3   __doc___numba_parallel_test_rJ   r4   r/   r+   r'   r6   r6   /   s     "#r+   r6   c                   `   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
\S	 5       rS
 rSrU =r$ )TestNrtMemInfoI   z*
Unit test for core MemInfo functionality
c                    > S[         l        [        R                  " [        R
                  5        [        [        U ]#  5         g )Nr   )	r   r$   r   
initializer   target_contextsuperrO   setUpr&   	__class__s    r'   rU   TestNrtMemInfo.setUpN   s+    223nd)+r+   c                 4   [        5       nU R                  [         R                  S5        Sn[        R                  " X!5      nU R                  UR
                  S5        AU R                  [         R                  S5        UR                  5         U R                  UR
                  S5        U R                  [         R                  S5        UR                  5         U R                  UR
                  S5        AU R                  [         R                  S5        g )Nr!      J[=    r   )r   assertEqualr$   r   r:   refcountacquirerelease)r&   daddrmis       r'   test_meminfo_refct_1#TestNrtMemInfo.test_meminfo_refct_1U   s    Ga(t'a(a(


a(a(


a(a(r+   c                 |   [        5       nU R                  [         R                  S5        Sn[        R                  " X!5      nU R                  UR
                  S5        AU R                  [         R                  S5        [        S5       H  nUR                  5         M     U R                  UR
                  S5        U R                  [         R                  S5        [        S5       H  nUR                  5         M     U R                  UR
                  S5        AU R                  [         R                  S5        g )Nr!   rZ   d   e   r   )	r   r\   r$   r   r:   r]   ranger^   r_   )r&   r`   ra   rb   ct_s         r'   test_meminfo_refct_2#TestNrtMemInfo.test_meminfo_refct_2f   s    Ga(t'a(a(*BJJL g.a(sAJJL a(a(r+   c                    [        5       nU R                  [         R                  S5        Sn[        R                  " X!5      nU R                  UR
                  S5        [        U5      nU R                  UR
                  S5        U R                  X#R                  5        U R                  UR                  5        U R                  X4R                  5        U R                  UR                  5        U R                  UR                  S5        U R                  UR                  S5        AAU R                  [         R                  S5        AU R                  [         R                  S5        g )Nr!   rZ   r   )r   r\   r$   r   r:   r]   
memoryviewdataassertFalsereadonlyassertIsobj
assertTruec_contiguousitemsizendim)r&   r`   ra   rb   mviews        r'   test_fake_memoryview#TestNrtMemInfo.test_fake_memoryviewy   s   Ga(t'a(2a(ww'(b))$**++Q'a(a(r+   c                    SSK JnJnJnJn  [
        R                  " [
        R                  5      nUR                  S-  n[        R                  " USS9nUR                  nU" U" UR                  5      U" US-  5      5      n	[        S5       H"  n
U R                  U	R                  U
   S5        M$     [        S5       H  n
U
S-   U	R                  U
'   M     [        U5      nU R                  UR                   U5        U R#                  UR$                  5        U R'                  X{R(                  5        U R+                  UR,                  5        U R                  UR                  S5        U R                  UR.                  S5        A[
        R0                  " X[R                   UR                  -  US9nA[
        R2                  R5                  [
        R6                  " UR8                  5      S-   U5        US-  n[        S5       H%  n
U R                  U	R                  U
   U
S	-   5        M'     U R                  UR                   R                  U5        Ag )
Nr   c_uint32c_void_pPOINTERcast
   Tsafe   K r!   dtypeshapebufferr[   )ctypesr}   r~   r   r   npr   uint32rv   r   r;   ro   rh   r\   contentsrn   nbytesrp   rq   rr   rs   rt   ru   rw   ndarraytestingassert_equalarangesize)r&   r}   r~   r   r   r   bytesizerb   ra   c_arrirx   arrs                r'   test_memoryviewTestNrtMemInfo.test_memoryview   s   <<#>>B&  5wwXbgg&2(>?rAU^^A.
;  rA !AENN1 2x0(b))$**++Q'jjuLLENN,J %'


		#(( 3a 7=q rAU^^A.A6  	$/r+   c                    SSK JnJnJnJn  [
        R                  " [
        R                  5      nUR                  S-  n[        R                  " USS9nU R                  UR                  S5        UR                  nU" U" U5      U" US-  5      5      n	[        S5       H"  n
U R                  U	R                  U
   S5        M$     [        S5       H  n
U
S-   U	R                  U
'   M     [
        R                   " XVUR                  -  US9nU R                  UR                  S5        A[
        R"                  R%                  [
        R&                  " UR(                  5      S-   U5        US-  n[        S5       H%  n
U R                  U	R                  U
   U
S	-   5        M'     U R                  UR                   R                  U5        Ag )
Nr   r|   r   Tr   r!   r   r   r[   )r   r}   r~   r   r   r   r   r   rv   r   r;   r\   r]   ro   rh   r   r   r   r   r   r   )r&   r}   r~   r   r   r   r   rb   ra   r   r   r   s               r'   test_bufferTestNrtMemInfo.test_buffer   sg   <<#>>B&  5a(wwXd^WX]%;<rAU^^A.
;  rA !AENN1  jju,F "$a(


		#(( 3a 7=q rAU^^A.A6  	$/r+   c                 .   [         R                  R                  S-  S-  nS H^  nU R                  [        5       n[
        R                  " XS9  S S S 5        U R                  SU S3[        WR                  5      5        M`     g ! , (       d  f       N<= f)N   r[   TFr   zRequested allocation of z bytes failed.)
r   size_tmaxvalr=   MemoryErrorr   r;   rA   rB   rC   )r&   r   predrG   s       r'   test_allocate_invalid_size)TestNrtMemInfo.test_allocate_invalid_size   sw     ||""a'1,!D"";/6##D4 0MM4TF.If../1 "//s   B
B	c                     SnS H`  nU R                  [        5       n[        R                  " XS9  S S S 5        SU S3nU R	                  U[        WR                  5      5        Mb     g ! , (       d  f       N>= f)Nir   r   z,Cannot allocate a negative number of bytes: .)r=   
ValueErrorr   r;   rA   rB   rC   )r&   r   r   rG   rI   s        r'   test_allocate_negative_size*TestNrtMemInfo.test_allocate_negative_size   sd     !D"":.&##D4 /@aHCMM#s6#3#345	 "..s   A**
A8	r/   )r0   r1   r2   r3   rL   rU   rc   rk   ry   r   r   r   r   r   r4   __classcell__rW   s   @r'   rO   rO   I   sF    ,)")&),$RH 1 16 6r+   rO   c                   $    \ rS rSrSrS rS rSrg)TestTracemalloc   z:
Test NRT-allocated memory can be tracked by tracemalloc.
c                 .    SS K nWR                  5          UR	                  5       nU" 5       nUR	                  5       nAUR                  US5      UR                  5         $ ! [         a    U R                  S5         Nxf = f! UR                  5         f = f)Nr   ztracemalloc not availablelineno)tracemallocImportErrorskipTeststarttake_snapshot
compare_tostop)r&   funcr   beforeresafters         r'   measure_memory_diff#TestTracemalloc.measure_memory_diff   s    	7 		 ..0F&C--/E##FH5  	7MM56	7 s   A! 9B !A?>A?Bc                 x  ^^^	 Sm[         R                  m	[        UU	4S j5       mU4S jnU4S jnUR                  R                  S-   nT" 5         U R                  U5      nUS   nU R                  UR                  T5        U R                  UR                  TS-  SS	9  UR                  S   nU R                  [        R                  R                  UR                  5      S
5        U R                  UR                  U5        U R                  U5      nUS   nU R                  UR                  TS-  5        g )Ni@B c                  2   > [         R                  " T T5      $ )z$
Allocate and return a large array.
)r   empty)Nr   s   r'   alloc_nrt_memory7TestTracemalloc.test_snapshot.<locals>.alloc_nrt_memory
  s    
 88Au%%r+   c                     > T " 5       $ Nr/   r   s   r'   keep_memory2TestTracemalloc.test_snapshot.<locals>.keep_memory  s    #%%r+   c                     > T " 5         g r   r/   r   s   r'   release_memory5TestTracemalloc.test_snapshot.<locals>.release_memory  s	    r+   r!   r   g=
ףp=?zpUnexpected allocation overhead encountered. May be due to difference in CPython builds or running under coverage)rI   ztest_nrt.pyg{Gz?)r   int8r   __code__co_firstlinenor   assertGreaterEqualr   
assertLess	tracebackr\   ospathbasenamefilenamer   )
r&   r   r   alloc_linenodiffstatframer   r   r   s
          @@@r'   test_snapshotTestTracemalloc.test_snapshot  s   		& 
	&	&	 #++::Q> 	 ''4Aw		1-		1u9@ 	 	B q!))%..9=I|4 ''7Aw		1t8,r+   r/   N)r0   r1   r2   r3   rL   r   r   r4   r/   r+   r'   r   r      s     (-r+   r   c                   P    \ rS rSrS rS rS rS r\R                  S 5       r
Srg)	TestNRTIssuei1  c                   ^^ [         S 5       m[         U4S j5       m[         U4S j5       nSn[        R                  R                  US45      nU" X#5      nUR                  X#5      n[        R                  R                  XT5        g)z?
GitHub Issue #1244 https://github.com/numba/numba/issues/1244
c                 H    U u  p[         R                  " US-  US-  -   5      $ )Nr[   )mathsqrt)vectorxys      r'   calculate_2D_vector_magNTestNRTIssue.test_issue_with_refct_op_pruning.<locals>.calculate_2D_vector_mag6  s$    DA99Q!Va1f_--r+   c                    > [         R                  " S[         R                  S9nT" U 5      nU u  p4X2-  US'   XB-  US'   U$ Nr[   r   r   r!   )r   r   float64)r   normalized_vectormagr   r   r   s        r'   normalize_2D_vectorJTestNRTIssue.test_issue_with_refct_op_pruning.<locals>.normalize_2D_vector<  sI     ""** =)&1CDA#$7a #$7a $$r+   c                    > [         R                  " U S4[         R                  S9n[        U 5       H!  nX   nT" U5      nUS   X#S4'   US   X#S4'   M#     U$ r   )r   r   r   rh   )num_vectorsvectorsnormalized_vectorsr   r   r   r   s         r'   normalize_vectorsHTestNRTIssue.test_issue_with_refct_op_pruning.<locals>.normalize_vectorsH  sj    !#;*:"**!M;' $7$?!+<Q+?"a4(+<Q+?"a4( ( &%r+   r   r[   N)r   r   randompy_funcr   assert_almost_equal)r&   r   r   test_vectorsgotexpectedr   r   s         @@r'    test_issue_with_refct_op_pruning-TestNRTIssue.test_issue_with_refct_op_pruning2  s     
	. 
	.
 
		% 
		% 
	& 
	& yy''a(89:$,,[G


&&x5r+   c           
      b   S n[        [        R                  " [        R                  [        R                  " [        R
                  SS5      45      " 5       5      " U5      nU" 5       u  p4U R                  US5        U R                  U[        R                  " S[        R
                  S95        g )Nc                  L    S[         R                  " S[         R                  S94$ )Ng        r!   r   )r   zerosint32r/   r+   r'   f.TestNRTIssue.test_incref_after_cast.<locals>.f`  s    "((333r+   r!   Cy                r   )	r   r   Tuple
complex128Arrayr   assertPreciseEqualr   r   )r&   r  cfunczr   s        r'   test_incref_after_cast#TestNRTIssue.test_incref_after_cast]  s    	4
 ekk5#3#3#(;;u{{As#C#F G J KKLN2&RXXarxx%@Ar+   c                     [         S 5       nU" 5       u  p#[        R                  R                  X#5        [        R                  R                  U[        R                  " S[        R
                  S95        g )Nc                      [         R                  " S[         R                  S9n [         R                  " S[         R                  S9nXS S  4$ )Nr   r   )r   onesr   )abs     r'   r  5TestNRTIssue.test_refct_pruning_issue_1511.<locals>.fl  s5    "**-A"**-Ad7Nr+   r   r   )r   r   r   r   r  r   )r&   r  r  r  s       r'   test_refct_pruning_issue_1511*TestNRTIssue.test_refct_pruning_issue_1511k  sT    		 
	
 s


%


2772RZZ#@Ar+   c                     [         S 5       n[        R                  " / SQ/5      nUR                  USS5      nU" USS5      nU R	                  X45        g )Nc                     [         R                  " U S:H  5      n[        US   5      S:X  a  SnSnOUS   S   nUS   S   nUS-
  U-   nUS-
  U-   nXg4$ )Nr!   r   )r   wherelen)imager   r   next_locy_offsetx_offset
next_loc_x
next_loc_ys           r'   udt7TestNRTIssue.test_refct_pruning_issue_1526.<locals>.udtw  sn    xx
+H8A;1$#A;q>#A;q>a%8+Ja%8+J))r+   )
r!   r   r!   r   r!   r   r   r!   r   r   r!      )r   r   arrayr   r\   )r&   r  r  expectr   s        r'   test_refct_pruning_issue_1526*TestNRTIssue.test_refct_pruning_issue_1526v  sU    		* 
	* HH456Q1%!Ql%r+   c                 $   SSK Jn  U R                  [        R                  5        US 5       nU R                  [        R                  5        U R                  U" 5       UR                  5       5        U R                  [        R                  5        g )Nr   r   c                      g)N{   r/   r/   r+   r'   foo8TestNRTIssue.test_no_nrt_on_njit_decoration.<locals>.foo  s    r+   )numbar   rp   r   r?   r\   r   rt   )r&   r   r(  s      r'   test_no_nrt_on_njit_decoration+TestNRTIssue.test_no_nrt_on_njit_decoration  so     	 	% 
	 
	 	% 	. 	$r+   r/   N)r0   r1   r2   r3   r   r
  r  r#  r   run_test_in_subprocessr+  r4   r/   r+   r'   r   r   1  s3    )6VB	B&. $$% %%r+   r   c                   h    \ rS rSrSrS r\R                  " S5      S 5       r\	\
S 5       5       rSrg)	TestRefCtPruningi  a  
define i32 @"MyFunction"(i8** noalias nocapture %retptr, { i8*, i32 }** noalias nocapture %excinfo, i8* noalias nocapture readnone %env, double %arg.vt.0, double %arg.vt.1, double %arg.vt.2, double %arg.vt.3, double %arg.bounds.0, double %arg.bounds.1, double %arg.bounds.2, double %arg.bounds.3, i8* %arg.xs.0, i8* nocapture readnone %arg.xs.1, i64 %arg.xs.2, i64 %arg.xs.3, double* nocapture readonly %arg.xs.4, i64 %arg.xs.5.0, i64 %arg.xs.6.0, i8* %arg.ys.0, i8* nocapture readnone %arg.ys.1, i64 %arg.ys.2, i64 %arg.ys.3, double* nocapture readonly %arg.ys.4, i64 %arg.ys.5.0, i64 %arg.ys.6.0, i8* %arg.aggs_and_cols.0.0, i8* nocapture readnone %arg.aggs_and_cols.0.1, i64 %arg.aggs_and_cols.0.2, i64 %arg.aggs_and_cols.0.3, i32* nocapture %arg.aggs_and_cols.0.4, i64 %arg.aggs_and_cols.0.5.0, i64 %arg.aggs_and_cols.0.5.1, i64 %arg.aggs_and_cols.0.6.0, i64 %arg.aggs_and_cols.0.6.1) local_unnamed_addr {
entry:
tail call void @NRT_incref(i8* %arg.xs.0)
tail call void @NRT_incref(i8* %arg.ys.0)
tail call void @NRT_incref(i8* %arg.aggs_and_cols.0.0)
%.251 = icmp sgt i64 %arg.xs.5.0, 0
br i1 %.251, label %B42.preheader, label %B160

B42.preheader:                                    ; preds = %entry
%0 = add i64 %arg.xs.5.0, 1
br label %B42

B42:                                              ; preds = %B40.backedge, %B42.preheader
%lsr.iv3 = phi i64 [ %lsr.iv.next, %B40.backedge ], [ %0, %B42.preheader ]
%lsr.iv1 = phi double* [ %scevgep2, %B40.backedge ], [ %arg.xs.4, %B42.preheader ]
%lsr.iv = phi double* [ %scevgep, %B40.backedge ], [ %arg.ys.4, %B42.preheader ]
%.381 = load double, double* %lsr.iv1, align 8
%.420 = load double, double* %lsr.iv, align 8
%.458 = fcmp ole double %.381, %arg.bounds.1
%not..432 = fcmp oge double %.381, %arg.bounds.0
%"$phi82.1.1" = and i1 %.458, %not..432
br i1 %"$phi82.1.1", label %B84, label %B40.backedge

B84:                                              ; preds = %B42
%.513 = fcmp ole double %.420, %arg.bounds.3
%not..487 = fcmp oge double %.420, %arg.bounds.2
%"$phi106.1.1" = and i1 %.513, %not..487
br i1 %"$phi106.1.1", label %B108.endif.endif.endif, label %B40.backedge

B160:                                             ; preds = %B40.backedge, %entry
tail call void @NRT_decref(i8* %arg.ys.0)
tail call void @NRT_decref(i8* %arg.xs.0)
tail call void @NRT_decref(i8* %arg.aggs_and_cols.0.0)
store i8* null, i8** %retptr, align 8
ret i32 0

B108.endif.endif.endif:                           ; preds = %B84
%.575 = fmul double %.381, %arg.vt.0
%.583 = fadd double %.575, %arg.vt.1
%.590 = fptosi double %.583 to i64
%.630 = fmul double %.420, %arg.vt.2
%.638 = fadd double %.630, %arg.vt.3
%.645 = fptosi double %.638 to i64
tail call void @NRT_incref(i8* %arg.aggs_and_cols.0.0)              ; GONE 1
tail call void @NRT_decref(i8* null)                                ; GONE 2
tail call void @NRT_incref(i8* %arg.aggs_and_cols.0.0), !noalias !0 ; GONE 3
%.62.i.i = icmp slt i64 %.645, 0
%.63.i.i = select i1 %.62.i.i, i64 %arg.aggs_and_cols.0.5.0, i64 0
%.64.i.i = add i64 %.63.i.i, %.645
%.65.i.i = icmp slt i64 %.590, 0
%.66.i.i = select i1 %.65.i.i, i64 %arg.aggs_and_cols.0.5.1, i64 0
%.67.i.i = add i64 %.66.i.i, %.590
%.84.i.i = mul i64 %.64.i.i, %arg.aggs_and_cols.0.5.1
%.87.i.i = add i64 %.67.i.i, %.84.i.i
%.88.i.i = getelementptr i32, i32* %arg.aggs_and_cols.0.4, i64 %.87.i.i
%.89.i.i = load i32, i32* %.88.i.i, align 4, !noalias !3
%.99.i.i = add i32 %.89.i.i, 1
store i32 %.99.i.i, i32* %.88.i.i, align 4, !noalias !3
tail call void @NRT_decref(i8* %arg.aggs_and_cols.0.0), !noalias !0 ; GONE 4
tail call void @NRT_decref(i8* %arg.aggs_and_cols.0.0)              ; GONE 5
br label %B40.backedge

B40.backedge:                                     ; preds = %B108.endif.endif.endif, %B84, %B42
%scevgep = getelementptr double, double* %lsr.iv, i64 1
%scevgep2 = getelementptr double, double* %lsr.iv1, i64 1
%lsr.iv.next = add i64 %lsr.iv3, -1
%.294 = icmp sgt i64 %lsr.iv.next, 1
br i1 %.294, label %B42, label %B160
}
    c                 b   U R                   n[        UR                  5       5      nU Vs/ s H  nSU;   d  M  UPM     nnU Vs/ s H  nSU;   d  M  UPM     nn[        R                  " U5      n[        UR                  5       5      nU Vs/ s H  nSU;   d  M  UPM     nnU Vs/ s H  nSU;   d  M  UPM     n	nU R                  XH5        U R                  XY5        [        U5      [        U5      -
  n
[        U5      [        U	5      -
  nX-  nU R                  XU-  5        SR                  U5      nS H%  nSR                  U5      nU R                  X5        M'     U R                  [        [        UR                  5       5      5      [        U5      5        g s  snf s  snf s  snf s  snf )N
NRT_incref
NRT_decref
)r!   r[            z	; GONE {})sample_llvm_irlist
splitlinesr   _remove_redundant_nrt_refctassertNotEqualsetr\   joinformatrA   r  )r&   input_irinput_lineslnbefore_increfsbefore_decrefs	output_iroutput_linesafter_increfsafter_decrefspruned_increfspruned_decrefscombinedpruned_linesr   gones                   r'   test_refct_pruning_op_recognize0TestRefCtPruning.test_refct_pruning_op_recognize  s   &&8..01'2I{lb6H"{I'2I{lb6H"{I 66x@	I0023&2Illb6HlI&2Illb6HlI 	N:N:^,s=/AA^,s=/AA "2N#BCyy* !A%%a(DMM$- ! 	T,"9"9";<=s8}M5 JI
 JIs.   
FF
F"F"
F'F''
F,5F,z3Pass removed as it was buggy. Re-enable when fixed.c                 d  ^^^ [         S 5       m[         S 5       m[         UU4S j5       m[        SS9U4S j5       nUR                  S5        [        UR                  UR                  S   5      5      n[        [        R                  " S	U5      5      nU R                  [        U5      S5        g
)ztestcase from #2350c                 X    [         R                  " U5      (       d  X!U 4==   S-  ss'   g g r    r   isnanr   r   aggfields       r'   _append_non_naITestRefCtPruning.test_refct_pruning_with_branches.<locals>._append_non_na  s#    88E??qD	Q	 #r+   c                     [         R                  " U5      (       d4  [         R                  " X!U 4   5      (       a  X2X4'   g X!U 4==   U-  ss'   g g r   rQ  rS  s       r'   _appendBTestRefCtPruning.test_refct_pruning_with_branches.<locals>._append  sB    88E??88C1I&& %I1I&I	 #r+   c                 *   > T" XX#5        T" XX#5        g r   r/   )r   r   rT  rU  rY  rV  s       r'   appendATestRefCtPruning.test_refct_pruning_with_branches.<locals>.append  s    1,A#%r+   T)no_cpython_wrapperc                    > [        U R                  S   5       H+  n[        U R                  S   5       H  nT" X2X5        M     M-     g )Nr   r!   )rh   r   )r   rU  r   jr\  s       r'   extendATestRefCtPruning.test_refct_pruning_with_branches.<locals>.extend&  s;    399Q<(syy|,A1, - )r+   z(f4[:,::1], f4)r   z!(NRT_incref|NRT_decref)\([^\)]+\)N)
r   compilerB   inspect_llvm
signaturesr8  refinditerr\   r  )r&   ra  llvmirrefopsrY  rV  r\  s       @@@r'    test_refct_pruning_with_branches1TestRefCtPruning.test_refct_pruning_with_branches  s     
	 
	 
	' 
	' 
	& 
	& 
	&	- 
'	- 	() V(():):1)=>?bkk"FOPVa(r+   c                    ^ [         R                  " 5         [         R                  " 5         [         R                  " 5         [         R                  " 5         [
        S 5       m[        S/5      U4S j5       nU R                  U" S5      S5        g)zlThe InlineAsm class from llvmlite.ir has no 'name' attr the refcount
pruning pass should be tolerant to thisc                 z    S n[        [        R                  [        R                  [        R                  5      U4$ )Nc           	         Uu  pE[         R                  " [         R                  " S5      [         R                  " S5      [         R                  " S5      /5      nUR                  USSXE4SSS9n[	        XUR
                  U5      $ )N    zmov $2, $0; imul $1, $0z=&r,r,rasm_mulF)nameside_effect)r   FunctionTypeIntTypeasmr   return_type)cgctxbuildersigrF   arg_0arg_1ftymuls           r'   codegen>TestRefCtPruning.test_inline_asm.<locals>.bar.<locals>.codegen@  sv    !%oobjjnrzz"~79zz"~7G Hkk#'@)#(.y.3 " 5 *%#//3OOr+   )r   r   r   )tyctxr   r   r~  s       r'   bar-TestRefCtPruning.test_inline_asm.<locals>.bar>  s*    P U[[%++u{{CWLLr+   zint32(int32)c                 $   > U S-  n T" U S5      nU$ )Nr!   r[   r/   )r   r	  r  s     r'   r(  -TestRefCtPruning.test_inline_asm.<locals>.fooJ  s    FAAq	AHr+   r      N)llvmrR   initialize_native_targetinitialize_native_asmprinterinitialize_native_asmparserr
   r   r\   )r&   r(  r  s     @r'   test_inline_asm TestRefCtPruning.test_inline_asm4  sy    
 	%%'))+((*			M 
		M 
~		 
 	
 	R"%r+   r/   N)r0   r1   r2   r3   r7  rM  unittestskiprj  
linux_onlyx86_onlyr  r4   r/   r+   r'   r/  r/    sM    FNPN> ]]HI") J")H &  &r+   r/  c                   J   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	rU =r$ )
TestNrtExternalCFFIiS  z?Testing the use of externally compiled C code that use NRT
    c                 J   > [         R                    [        [        U ]  5         g r   )r   rS   rT   r  rU   rV   s    r'   rU   TestNrtExternalCFFI.setUpW  s    !!!4.0r+   c                    SSK Jn  U" 5       nUR                  X[        5       /S9  UR	                  U5        [        SR                  U5      5      nUR                  US9  [        R                  R                  U5         [        U5      n[        R                  R                  U5        XW4$ ! [        R                  R                  U5        f = f)Nr   FFI)include_dirszcffi_test_{})tmpdir)cffir  
set_sourcer   cdefr   r>  rc  sysr   r\  r   remove)r&   rq  sourcer  r  ffir  mods           r'   compile_cffi_module'TestNrtExternalCFFI.compile_cffi_module\  s    et<>2BC 5 5d ;<6"	$ &CHHOOF#x HHOOF#s   B. .!Cc                 t    SSK Jn  U" 5       nUR                  S[        R                  S   5      nU" 5       nU$ )Nr   r  zvoid* (*)()get_api)r  r  r   r   	c_helpers)r&   r  r  nrt_get_apitables        r'   get_nrt_api_table%TestNrtExternalCFFI.get_nrt_api_tablel  s3    ehh}k.C.CI.NOr+   c                 T   SR                  U R                  R                  5      nSnSnU R                  XU5      u  pEU R	                  UR
                  R                  S5        U R                  5       nUR
                  R                  U5      nU R	                  UR
                  R                  S5        [        UR                  SU5      5      n[        R                  " U5      n	U R	                  U	R                  S5        A	U R	                  UR
                  R                  S5        g )	Nz{}_test_manage_memoryaz  
#include <stdio.h>
#include "numba/core/runtime/nrt_external.h"

int status = 0;

void my_dtor(void *ptr) {
    free(ptr);
    status = 0xdead;
}

NRT_MemInfo* test_nrt_api(NRT_api_functions *nrt) {
    void * data = malloc(10);
    NRT_MemInfo *mi = nrt->manage_memory(data, my_dtor);
    nrt->acquire(mi);
    nrt->release(mi);
    status = 0xa110c;
    return mi;
}
        z;
void* test_nrt_api(void *nrt);
extern int status;
        r   i
 r   r!   i  )r>  rW   r0   r  r\   libstatusr  test_nrt_apiintr   r	   MemInfor]   )
r&   rq  r  r  r  r  r  outmi_addrrb   s
             r'   test_manage_memory&TestNrtExternalCFFI.test_manage_memoryt  s    &--dnn.E.EF(
 ++D$?+&&(gg""5)1chhx-.[[!a(0r+   c                    SR                  U R                  R                  5      nSnSnU R                  XU5      u  pEU R	                  5       nS[
        R                  " [
        R                  5      R                  -  nUR                  R                  Xg5      n[        UR                  SU5      5      n	[        R                  " U	5      n
U R                  U
R                   S5        UR#                  UR                  SR                  U5      U
R$                  5      5      n[
        R&                  " S[
        R                  US	9n[
        R(                  R+                  U/ S
Q5        g )Nz{}_test_allocatea7  
#include <stdio.h>
#include "numba/core/runtime/nrt_external.h"

NRT_MemInfo* test_nrt_api(NRT_api_functions *nrt, size_t n) {
    size_t *data = NULL;
    NRT_MemInfo *mi = nrt->allocate(n);
    data = nrt->get_data(mi);
    data[0] = 0xded;
    data[1] = 0xabc;
    data[2] = 0xdef;
    return mi;
}
        z(void* test_nrt_api(void *nrt, size_t n);r4  r   r!   z	char [{}])r4  )r   r   r   )i  i
  i  )r>  rW   r0   r  r  r   r   intprv   r  r  r  r   r	   r  r\   r]   r   ro   r   r   r   )r&   rq  r  r  r  r  r  numbytesr  r  rb   r   r   s                r'   test_allocate!TestNrtExternalCFFI.test_allocate  s   !(()@)@A :++D$?&&(rxx(111gg""53chhx-.[[!a(CHH[%7%7%A277KLjjt2776B


%:;r+   c                     SSK Jn  [        S 5       nU" 5       n[        UR	                  SU R                  5       5      5      nU" 5       nU R                  XE5        g )Nr   r  c                      [        5       $ r   r   r/   r+   r'   r  6TestNrtExternalCFFI.test_get_api.<locals>.test_nrt_api  s
    = r+   r   )r  r  r   r  r   r  r\   )r&   r  r  r  r"  r   s         r'   test_get_api TestNrtExternalCFFI.test_get_api  sS    		! 
	! eSXXh(>(>(@ABn%r+   r/   )r0   r1   r2   r3   rL   rU   r  r  r  r  r  r4   r   r   s   @r'   r  r  S  s-    1
 '1R<@
& 
&r+   r  c                   J    \ 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)TestNrtStatisticsi  c                 8    [         R                  " 5       U l        g r   )r   memsys_stats_enabled_TestNrtStatistics__stats_stater%   s    r'   rU   TestNrtStatistics.setUp  s    (==?r+   c                 |    U R                   (       a  [        R                  " 5         g [        R                  " 5         g r   )r  r   memsys_enable_statsmemsys_disable_statsr%   s    r'   tearDownTestNrtStatistics.tearDown  s#    ++-,,.r+   c                 `    Sn[         R                  R                  5       nSUS'   [        XS9  g )Na  if 1:
        from numba import njit
        import numpy as np
        from numba.core.runtime import rtsys, _nrt_python
        from numba.core.registry import cpu_target

        @njit
        def foo():
            return np.arange(10)[0]

        # initialize the NRT before use
        rtsys.initialize(cpu_target.target_context)
        assert _nrt_python.memsys_stats_enabled()
        orig_stats = rtsys.get_allocation_stats()
        foo()
        new_stats = rtsys.get_allocation_stats()
        total_alloc = new_stats.alloc - orig_stats.alloc
        total_free = new_stats.free - orig_stats.free
        total_mi_alloc = new_stats.mi_alloc - orig_stats.mi_alloc
        total_mi_free = new_stats.mi_free - orig_stats.mi_free

        expected = 1
        assert total_alloc == expected
        assert total_free == expected
        assert total_mi_alloc == expected
        assert total_mi_free == expected
        1NUMBA_NRT_STATSenv)r   environcopyr   )r&   srcr  s      r'   test_stats_env_var_explicit_on0TestNrtStatistics.test_stats_env_var_explicit_on  s.    8 jjoo!$#'r+   c                     Sn[        X!S9  g )Na  if 1:
        from numba import njit
        import numpy as np
        from numba.core.runtime import rtsys, _nrt_python

        @njit
        def foo():
            return np.arange(10)[0]

        assert _nrt_python.memsys_stats_enabled() == False
        try:
            rtsys.get_allocation_stats()
        except RuntimeError as e:
            assert "NRT stats are disabled." in str(e)
        r  )r   )r&   r  r  s      r'   check_env_var_off#TestNrtStatistics.check_env_var_off  s     	#'r+   c                 l    [         R                  R                  5       nSUS'   U R                  U5        g )N0r  )r   r  r  r  r&   r  s     r'   test_stats_env_var_explicit_off1TestNrtStatistics.test_stats_env_var_explicit_off  s+    jjoo!$s#r+   c                     [         R                  R                  5       nUR                  SS 5        U R	                  U5        g )Nr  )r   r  r  popr  r  s     r'   test_stats_env_var_default_off0TestNrtStatistics.test_stats_env_var_default_off  s1     jjoo!4(s#r+   c                 t   [         S 5       n[        R                  " 5         U R                  [        R                  " 5       5        [        S5       H  n[        R                  " 5       n[        R                  " 5         U R                  [        R                  " 5       5        U" 5         [        R                  " 5         U R                  [        R                  " 5       5        [        R                  " 5       nU" 5         [        R                  " 5       nU R                  X45        U R                  XE5        M     g )Nc                  f    [         R                  " S5      n [         R                  " SU S   -  5      $ )Nr4  r6  r   )r   r  r   )tmps    r'   r(  7TestNrtStatistics.test_stats_status_toggle.<locals>.foo  s&    ''!*C99QQZ((r+   r[   )r   r   r  rt   r  rh   r   get_allocation_statsr  rp   r\   r   )r&   r(  r   stats_1stats_2stats_3s         r'   test_stats_status_toggle*TestNrtStatistics.test_stats_status_toggle  s    		) 
	)
 	'')88:;qA002G,,.[==?@E++-OOK<<>?002GE002GW.OOG-- r+   c                 H   [         R                  " 5         U R                  [         R                  " 5       5        U R	                  [
        5       n[        R                  " 5         S S S 5        U R                  S[        WR                  5      5        g ! , (       d  f       N4= f)NNRT stats are disabled.)r   r  rp   r  r=   r>   r   r  rA   rB   rC   )r&   rG   s     r'   5test_rtsys_stats_query_raises_exception_when_disabledGTestNrtStatistics.test_rtsys_stats_query_raises_exception_when_disabled=  sm     	((*99;<|,&&( - 	/V5E5E1FG -,s   B
B!c                    SnU H  n[        [        SU 35      nU R                  US9   [        R                  " 5         U R	                  [        R
                  " 5       5        U R                  [        5       nU" 5         S S S 5        U R                  S[        WR                  5      5        S S S 5        M     g ! , (       d  f       N@= f! , (       d  f       M  = f)N)allocfreemi_allocmi_freememsys_get_stats_)
stats_funcr  )r@   r   subTestr  rp   r  r=   r>   rA   rB   rC   )r&   method_variationsrE   r  rG   s        r'   <test_nrt_explicit_stats_query_raises_exception_when_disabledNTestNrtStatistics.test_nrt_explicit_stats_query_raises_exception_when_disabledI  s     E%D 0A$.HIJ4002  !A!A!CD&&|4L 57V=M=M9NO 54 & 54	 54s$   AC9B;-C;
C	C
C	)__stats_stateN)r0   r1   r2   r3   rU   r  r  r  r  r  r  r  r  r4   r/   r+   r'   r  r    s4    @/ (D(&$$".H
HPr+   r  __main__)9r   r   platformr  rf  numpyr   r*  r   
numba.corer   numba.core.runtimer   r   r   r	   numba.core.extendingr
   r   numba.core.typingr   numba.core.imputilsr   llvmliter   llvmlite.bindingbindingr  numba.core.unsafe.nrtr   numba.tests.supportr   r   r   r   r   r   r   numba.core.registryr   r  skipIf
startswithr  machiner  objectr   r6   rO   r   r   r/  r  r  r0   mainr/   r+   r'   <module>r     sS    	  
 	     9 ' 2   -F F F +  __!8!8!AA.0
??8++-5GG*,F #8#4#4 #4e6X&& e6P=-h'' =-@r%8 r%jj&x(( j&Z s&-x s& s&lKP KP\ zMMO r+   