
    -Ph,#                     F   d Z ddlZddlZddl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 	 ddlZd Zn# e$ r d	 ZY nw xY w eej        d
k    d          Z	 ddlmZ de_        n# e$ r Y nw xY wed             ZdZdZedz   ZdZdZe G d de                      ZdS )z&Tests for the Cython magics extension.    N)contextmanager)skipIf)IpythonMagic)
CythonTest)AnnotationCCodeWriterc                     | S N )cs    c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/Build/Tests/TestIpythonMagic.pyskip_if_not_installedr      s        c                     d S r	   r
   )_s    r   r   r      s    tr   win32zSkip on Windows)HistoryManagerFc               #     K   t           j        t           j        f} 	 t          j        t          j                    t           j        j                  t          j        t          j                    t           j        j                  g}|\  t           _        t           _        g }|V  | \  t           _        t           _        |D ]R}|                    d           |                    |	                                           |
                                 Sd S # | \  t           _        t           _        |D ]R}|                    d           |                    |	                                           |
                                 Sw xY w)N)encodingr   )sysstdoutstderrioTextIOWrapperBytesIOr   seekappendreadclose)backupreplacementoutputwrappers       r   capture_outputr#   %   s9     Z#FbjllSZ5HIIIbjllSZ5HIII
 "-
CJ!'
CJ" 	 	GLLOOOMM',,..)))MMOOOO	 	 "(
CJ" 	 	GLLOOOMM',,..)))MMOOOO	s   BD A0Fzdef f(x):
    return 2*x
zAdef f(int x):
    return 2 / x

def call(x):
    return f(*(x,))
z4def main():
    for _ in range(100): call(5)
main()
zUcdef extern from *:
    """
    xxx a=1;
    """
    int a;
def doit():
    return a
z{cdef extern from *:
    """
    #pragma message ( "CWarning" )
    int a = 42;
    """
    int a;
def doit():
    return a
c                       e Zd Zed             Zd Zd Zed             Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zed             Zed             Zd Zd Zd Zd Zd Zd ZdS )TestIPythonMagicc                 |    t          j                     t          j        j                                        | _        d S r	   )r   
setUpClassIPythontestingglobalipappget_ipython_ip)clss    r   r'   zTestIPythonMagic.setUpClassd   s-    /-99;;r   c                 l    t          j        |            | j        j                            d           d S )Ncython)r   setUpr,   extension_managerload_extension)selfs    r   r0   zTestIPythonMagic.setUpi   s2    "11(;;;;;r   c                     | j         }|                    d           |                    ddd          }|                     |d           d S )Nz
a=10; b=20cython_inline z
return a+b   )r,   exrun_cell_magicassertEqual)r3   ipresults      r   test_cython_inlinez#TestIPythonMagic.test_cython_inlinem   sN    X
l""?BEE$$$$$r   c                    | j         }d}|                    d|t                     |                    d           |                     |j        d         d           |                    d|t                     |                    d           |                     |j        d         d           	 t          j        |d	z              d S # t          $ r Y d S w xY w)
N_test_cython_pyximportcython_pyximport	g = f(10)g      4@z
h = f(-10)hg      4z.pyx)	r,   r9   coder8   r:   user_nsosremoveOSError)r3   r;   module_names      r   test_cython_pyximportz&TestIPythonMagic.test_cython_pyximports   s    X.
,k4@@@
kC$///
,k4@@@
lC%000	IkF*+++++ 	 	 	DD	s   /C 
CCc                     | j         }|                    ddt                     |                    d           |                     |j        d         d           d S )Nr/   r6   rA   rB   rC   r,   r9   rE   r8   r:   rF   r3   r;   s     r   test_cythonzTestIPythonMagic.test_cython   sU    X
(B---
kC$/////r   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           d S )Nr/   z--name=mymodulez#import mymodule; g = mymodule.f(10)rB   rC   rM   rN   s     r   test_cython_namez!TestIPythonMagic.test_cython_name   sW    X
($5t<<<
3444C$/////r   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           |                     |j        d         d           d S )Nr/   r6   g = f(10); h = call(10)rB   皙?rD   r,   r9   cython3_coder8   r:   rF   rN   s     r   test_cython_language_levelz+TestIPythonMagic.test_cython_language_level   sr    X
(B555
'(((C*555C*55555r   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           |                     |j        d         d           d S )Nr/   -3rS   rB   rT   rD   rU   rN   s     r   test_cython3zTestIPythonMagic.test_cython3   sr    X
(D,777
'(((C*555C*55555r   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           |                     |j        d         d           d S )Nr/   z-2rS   rB   r   rD   rU   rN   s     r   test_cython2zTestIPythonMagic.test_cython2   sr    X
(D,777
'(((C'222C'22222r   c                     | j         }t                      5 }|                    ddt                     d d d            n# 1 swxY w Y   |\  }}|dz   |z   }|                     d|v d|z              d S )Nr/   rY   
error	error in msg)r,   r#   r9   compile_error_code
assertTruer3   r;   outcaptured_outcaptured_errcaptured_alls         r   test_cython_compile_error_shownz0TestIPythonMagic.test_cython_compile_error_shown   s    X 	Bh.@AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B%("l $d*\9</[<5OPPPPP   ?AAc                     | j         }t                      5 }|                    ddt                     d d d            n# 1 swxY w Y   |\  }}|dz   |z   }|                     d|v d|z              d S )Nr/   z-3 -l=xxxxxxxxz
!r_   r`   ra   )r,   r#   r9   rE   rd   re   s         r   test_cython_link_error_shownz-TestIPythonMagic.test_cython_link_error_shown   s    X 	@h(8$???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@%("l $e+l:</[<5OPPPPPrk   c                     | j         }t                      5 }|                    ddt                     d d d            n# 1 swxY w Y   |\  }}|                     d|v            d S )Nr/   z-3 -fCWarning)r,   r#   r9   compile_warning_coderd   )r3   r;   rf   rg   rh   s        r   test_cython_warning_shownz*TestIPythonMagic.test_cython_warning_shown   s    X 	G h1EFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G &)"l 	
l233333rk   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           |                     |j        d         d           d S )Nr/   z-3 --pgozg = f(10); h = call(10); main()rB   rT   rD   )r,   r9   pgo_cython3_coder8   r:   rF   rN   s     r   test_cython3_pgoz!TestIPythonMagic.test_cython3_pgo   su     X
(J0@AAA
/000C*555C*55555r   c                     | j         }d}d|j        d<   |                    dd|           |                     |j        d         d           d S )Nz1
from libc.math cimport sin
x = sin(0.0)
           xr/   z-l mr   )r,   rF   r9   r:   )r3   r;   rE   s      r   test_extlibszTestIPythonMagic.test_extlibs   sW    X 
3
(FD111C!,,,,,r   c                     | j         }|                    ddt                     |                    d           |                     |j        d         d           d S )Nr/   	--verboserA   rB   rC   rM   rN   s     r   test_cython_verbosez$TestIPythonMagic.test_cython_verbose   sU    X
(K666
kC$/////r   c                    t           d             }| j        } |            5 }|                    ddt                     |                    d           d d d            n# 1 swxY w Y   |                     |j        d         d           |                     |j        |j        |j        g|j	                    |            5 }|                    ddt                     |                    d           d d d            n# 1 swxY w Y   |                     |j        d         d           |                     |j        g|j	                   d S )Nc               3      K    G d d          }  |             }t           j        j        }	 |t           j        _        |V  |t           j        _        d S # |t           j        _        w xY w)Nc                   "    e Zd ZdZdZegZd ZdS )XTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutils.<locals>.MockLogrv      c                 P    | j                             |           | j         d         S )N)
thresholdsr   )r3   vals     r   set_thresholdzfTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutils.<locals>.MockLog.set_threshold   s%    O**3///?2..r   N)__name__
__module____qualname__DEBUGINFOr   r   r
   r   r   MockLogr      s4        "V
/ / / / /r   r   )r   	distutilslog)r   new_logold_logs      r   mock_distutilszGTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutils   s      / / / / / / / / giiG",0G5-4&*-4&***W&*4444s   A A'r/   rz   rA   rB   rC   r6   )
r   r,   r9   rE   r8   r:   rF   r   r   r   )r3   r   r;   verbose_log
normal_logs        r   test_cython_verbose_thresholdsz/TestIPythonMagic.test_cython_verbose_thresholds   s   		5 	5 
	5& X^ 	hT:::EE+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	C$///+*K,={?OP%0	2 	2 	2 ^ 	hD111EE+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	C$///*/*J,ABBBBBs#   2A  A$'A$2DDDc                 z    | j         }|                    ddt                    }|                     |d u            d S )Nr/   r6   )r,   r9   rE   rd   r3   r;   htmls      r   test_cython_no_annotatez(TestIPythonMagic.test_cython_no_annotate  s<    X  2t44%%%%%r   c                     | j         }|                    ddt                    }|                     t          j        |j        v           d S )Nr/   z
--annotater,   r9   rE   rd   r   COMPLETE_CODE_TITLEdatar   s      r   test_cython_annotatez%TestIPythonMagic.test_cython_annotate  sE    X  <>> 	-ARSSSSSr   c                     | j         }|                    ddt                    }|                     t          j        |j        v           d S )Nr/   z-ar   r   s      r   test_cython_annotate_defaultz-TestIPythonMagic.test_cython_annotate_default  sE    X  466 	-ARSSSSSr   c                     | j         }|                    ddt                    }|                     t          j        |j        v            d S )Nr/   z--annotate-fullcr   r   s      r   $test_cython_annotate_complete_c_codez5TestIPythonMagic.test_cython_annotate_complete_c_code  sF    X  +=tDD 	-ATYNOOOOOr   N)r   r   r   classmethodr'   r0   r=   
skip_win32rK   rO   rQ   rW   rZ   r\   rj   rm   rq   rt   rx   r{   r   r   r   r   r   r
   r   r   r%   r%   a   s        < < [<< < <% % %   Z0 0 00 0 06 6 66 6 63 3 3	Q 	Q 	Q	Q 	Q 	Q	4 	4 	4 6 6 Z6 - - Z-0 0 0!C !C !CF& & &
T T TT T TP P P P Pr   r%   )__doc__rG   r   r   
contextlibr   unittestr   Cython.Buildr   Cython.TestUtilsr   Cython.Compiler.Annotater   IPython.testing.globalipappr(   r   ImportErrorplatformr   IPython.core.historyr   enabledr#   rE   rV   rs   rc   rp   r%   r
   r   r   <module>r      s   - , 
			 				 



 % % % % % %       % % % % % % ' ' ' ' ' ' : : : : : :&&&&            VCLG+->??
	333333"N 	 	 	D	   $
   #   	  }P }P }P }P }Pz }P }P }P }P }Ps!   6 A AA( (A0/A0