o
    f$                     @   s"  d Z ddlm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 zddlZW n eyE   d	d
 ZY nw dd
 Ze	ejdkdZe	ejdd dkdZzddlmZ de_W n	 eyr   Y nw edd ZdZdZed ZdZdZ eG dd deZ!dS )z&Tests for the Cython magics extension.    )absolute_importN)contextmanager)skipIf)IpythonMagic)
CythonTest)AnnotationCCodeWriterc                 C   s   d S N )_r	   r	   _/var/www/html/mig_web/myenv/lib/python3.10/site-packages/Cython/Build/Tests/TestIpythonMagic.pyskip_if_not_installed      r   c                 C   s   | S r   r	   )cr	   r	   r   r      r   win32zSkip on Windows   )r      zDisabled in Py2.7)HistoryManagerFc                  c   s    t jt jf} z?tjt t jjdtjt t jjdg}|\t _t _g }|V  W | \t _t _|D ]}|d ||	  |
  q2d S | \t _t _|D ]}|d ||	  |
  qOw )N)encodingr   )sysstdoutstderrioTextIOWrapperBytesIOr   seekappendreadclose)backupreplacementoutputwrapperr	   r	   r   capture_output(   s*   



r"   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                   @   s   e Zd Zedd Zdd Zdd Zedd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zeedd Zedd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*TestIPythonMagicc                 C   s   t   tjj | _d S r   )r   
setUpClassIPythontestingglobalipappget_ipython_ip)clsr	   r	   r   r$   g   s   zTestIPythonMagic.setUpClassc                 C   s   t |  | jjd d S )Ncython)r   setUpr)   extension_managerload_extension)selfr	   r	   r   r,   l   s   
zTestIPythonMagic.setUpc                 C   s.   | 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)r/   ipresultr	   r	   r   test_cython_inlinep   s   
z#TestIPythonMagic.test_cython_inlinec                 C   s   | j }d}|d|t |d | |jd d |d|t |d | |jd d z
t|d	  W d S  tyC   Y d S w )
N_test_cython_pyximportcython_pyximport	g = f(10)g      4@z
h = f(-10)hg      4z.pyx)	r)   r4   coder3   r5   user_nsosremoveOSError)r/   r6   module_namer	   r	   r   test_cython_pyximportv   s   

z&TestIPythonMagic.test_cython_pyximportc                 C   4   | j }|ddt |d | |jd d d S )Nr+   r1   r;   r<   r=   r)   r4   r?   r3   r5   r@   r/   r6   r	   r	   r   test_cython      
zTestIPythonMagic.test_cythonc                 C   rF   )Nr+   z--name=mymodulez#import mymodule; g = mymodule.f(10)r<   r=   rG   rH   r	   r	   r   test_cython_name   s   
z!TestIPythonMagic.test_cython_namec                 C   s|   | j }|ddt |d tjd dk r*| |jd d | |jd d d S | |jd d | |jd d d S )	Nr+   r1   g = f(10); h = call(10)r      r<   r>   皙?)r)   r4   cython3_coder3   r   version_infor5   r@   rH   r	   r	   r   test_cython_language_level   s   
z+TestIPythonMagic.test_cython_language_levelc                 C   F   | j }|ddt |d | |jd d | |jd d d S )Nr+   -3rL   r<   rN   r>   r)   r4   rO   r3   r5   r@   rH   r	   r	   r   test_cython3   
   
zTestIPythonMagic.test_cython3c                 C   rR   )Nr+   z-2rL   r<   r   r>   rT   rH   r	   r	   r   test_cython2   rV   zTestIPythonMagic.test_cython2c                 C   h   | j }t }|ddt W d    n1 sw   Y  |\}}|d | }| jd|v d| d d S )Nr+   rS   
error	error in msg)r)   r"   r4   compile_error_code
assertTruer/   r6   outcaptured_outcaptured_errcaptured_allr	   r	   r   test_cython_compile_error_shown      z0TestIPythonMagic.test_cython_compile_error_shownc                 C   rX   )Nr+   z-3 -l=xxxxxxxxz
!rZ   r[   r\   )r)   r"   r4   r?   r_   r`   r	   r	   r   test_cython_link_error_shown   rf   z-TestIPythonMagic.test_cython_link_error_shownc                 C   sT   | j }t }|ddt W d    n1 sw   Y  |\}}| d|v  d S )Nr+   z-3 -fCWarning)r)   r"   r4   compile_warning_coder_   )r/   r6   ra   rb   rc   r	   r	   r   test_cython_warning_shown   s   z*TestIPythonMagic.test_cython_warning_shownc                 C   rR   )Nr+   z-3 --pgozg = f(10); h = call(10); main()r<   rN   r>   )r)   r4   pgo_cython3_coder3   r5   r@   rH   r	   r	   r   test_cython3_pgo   s
   
z!TestIPythonMagic.test_cython3_pgoc                 C   s8   | 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)   r@   r4   r5   )r/   r6   r?   r	   r	   r   test_extlibs   s
   
zTestIPythonMagic.test_extlibsc                 C   rF   )Nr+   	--verboser;   r<   r=   rG   rH   r	   r	   r   test_cython_verbose   rJ   z$TestIPythonMagic.test_cython_verbosec                 C   s   t dd }| j}| }|ddt |d W d    n1 s#w   Y  | |jd d | |j|j|jg|j	 | }|ddt |d W d    n1 sXw   Y  | |jd d | |jg|j	 d S )	Nc                  s   sF    G dd d} |  }t jj}z|t j_|V  W |t j_d S |t j_w )Nc                   @   s"   e Zd ZdZdZegZdd ZdS )zXTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutils.<locals>.MockLogrm   r   c                 S   s   | j | | j d S )N)
thresholdsr   )r/   valr	   r	   r   set_threshold   s   
zfTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutils.<locals>.MockLog.set_thresholdN)__name__
__module____qualname__DEBUGINFOrs   ru   r	   r	   r	   r   MockLog   s
    r{   )r   	distutilslog)r{   new_logold_logr	   r	   r   mock_distutils   s   
zGTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutilsr+   rp   r;   r<   r=   r1   )
r   r)   r4   r?   r3   r5   r@   rz   ry   rs   )r/   r   r6   verbose_log
normal_logr	   r	   r   test_cython_verbose_thresholds   s"   
z/TestIPythonMagic.test_cython_verbose_thresholdsc                 C   s&   | j }|ddt}| |d u  d S )Nr+   r1   )r)   r4   r?   r_   r/   r6   htmlr	   r	   r   test_cython_no_annotate  s   z(TestIPythonMagic.test_cython_no_annotatec                 C   *   | j }|ddt}| tj|jv d S )Nr+   z
--annotater)   r4   r?   r_   r   COMPLETE_CODE_TITLEdatar   r	   r	   r   test_cython_annotate     z%TestIPythonMagic.test_cython_annotatec                 C   r   )Nr+   z-ar   r   r	   r	   r   test_cython_annotate_default  r   z-TestIPythonMagic.test_cython_annotate_defaultc                 C   s*   | j }|ddt}| tj|jv  d S )Nr+   z--annotate-fullcr   r   r	   r	   r   $test_cython_annotate_complete_c_code"  r   z5TestIPythonMagic.test_cython_annotate_complete_c_codeN)rv   rw   rx   classmethodr$   r,   r8   
skip_win32rE   rI   rK   rQ   rU   rW   re   rg   rj   	skip_py27rl   ro   rq   r   r   r   r   r   r	   r	   r	   r   r#   d   s4    


#r#   )"__doc__
__future__r   rA   r   r   
contextlibr   unittestr   Cython.Buildr   Cython.TestUtilsr   Cython.Compiler.Annotater   IPython.testing.globalipappr%   ImportErrorr   platformr   rP   r   IPython.core.historyr   enabledr"   r?   rO   rk   r^   ri   r#   r	   r	   r	   r   <module>   sB   


