U
    {±Ëh,#  ã                   @   s  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 zddlZW n ek
r€   dd	„ ZY n
X d
d	„ ZeejdkdƒZzddlmZ de_W n ek
rÄ   Y nX 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.é    N)Úcontextmanager)ÚskipIf)ÚIpythonMagic)Ú
CythonTest)ÚAnnotationCCodeWriterc                 C   s   d S ©N© )Ú_r   r   úG/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Build/Tests/TestIpythonMagic.pyÚskip_if_not_installed   s    r   c                 C   s   | S r   r   )Úcr   r   r
   r      s    Úwin32zSkip on Windows)ÚHistoryManagerFc                  c   s   t jt jf} zFtjt 	¡ t jj
dtjt 	¡ t jj
dg}|\t _t _g }|V  W 5 | \t _t _|D ]$}| d¡ | | ¡ ¡ | ¡  qdX d S )Nr   )Úencoding)ÚsysÚstdoutÚstderrÚseekÚappendÚreadÚcloseÚioÚTextIOWrapperÚBytesIOr   )ÚbackupÚreplacementÚwrapperÚoutputr   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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ÚIPythonZtestingZglobalipappZget_ipythonÚ_ip)Úclsr   r   r
   r    d   s    zTestIPythonMagic.setUpClassc                 C   s   t  | ¡ | jj d¡ d S )NÚcython)r   ÚsetUpr"   Zextension_managerZload_extension)Úselfr   r   r
   r%   i   s    
zTestIPythonMagic.setUpc                 C   s.   | j }| d¡ | ddd¡}|  |d¡ d S )Nz
a=10; b=20Zcython_inlineÚ z
return a+bé   )r"   ÚexÚrun_cell_magicÚassertEqual)r&   ÚipÚresultr   r   r
   Útest_cython_inlinem   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 n tk
r„   Y nX d S )
NZ_test_cython_pyximportZcython_pyximportú	g = f(10)Úgç      4@z
h = f(-10)Úhg      4Àz.pyx)	r"   r*   Úcoder)   r+   Úuser_nsÚosÚremoveÚOSError)r&   r,   Úmodule_namer   r   r
   Útest_cython_pyximports   s    

z&TestIPythonMagic.test_cython_pyximportc                 C   s4   | j }| ddt¡ | d¡ |  |jd d¡ d S )Nr$   r'   r/   r0   r1   ©r"   r*   r3   r)   r+   r4   ©r&   r,   r   r   r
   Útest_cython‚   s    
zTestIPythonMagic.test_cythonc                 C   s4   | j }| ddt¡ | d¡ |  |jd d¡ d S )Nr$   z--name=mymodulez#import mymodule; g = mymodule.f(10)r0   r1   r:   r;   r   r   r
   Útest_cython_nameˆ   s    
z!TestIPythonMagic.test_cython_namec                 C   sF   | j }| ddt¡ | d¡ |  |jd d¡ |  |jd d¡ d S )Nr$   r'   úg = f(10); h = call(10)r0   çš™™™™™É?r2   ©r"   r*   Úcython3_coder)   r+   r4   r;   r   r   r
   Útest_cython_language_level   s
    
z+TestIPythonMagic.test_cython_language_levelc                 C   sF   | j }| ddt¡ | d¡ |  |jd d¡ |  |jd d¡ d S )Nr$   ú-3r>   r0   r?   r2   r@   r;   r   r   r
   Útest_cython3˜   s
    
zTestIPythonMagic.test_cython3c                 C   sF   | j }| ddt¡ | d¡ |  |jd d¡ |  |jd d¡ d S )Nr$   z-2r>   r0   r   r2   r@   r;   r   r   r
   Útest_cython2    s
    
zTestIPythonMagic.test_cython2c              	   C   sT   | j }tƒ }| ddt¡ W 5 Q R X |\}}|d | }| jd|kd| d d S )Nr$   rC   Ú
Úerrorú	error in ©Úmsg)r"   r   r*   Úcompile_error_codeÚ
assertTrue©r&   r,   ÚoutÚcaptured_outÚcaptured_errZcaptured_allr   r   r
   Útest_cython_compile_error_shown¨   s    z0TestIPythonMagic.test_cython_compile_error_shownc              	   C   sT   | j }tƒ }| ddt¡ W 5 Q R X |\}}|d | }| jd|kd| d d S )Nr$   z-3 -l=xxxxxxxxz
!rG   rH   rI   )r"   r   r*   r3   rL   rM   r   r   r
   Útest_cython_link_error_shown³   s    z-TestIPythonMagic.test_cython_link_error_shownc              	   C   s@   | j }tƒ }| ddt¡ W 5 Q R X |\}}|  d|k¡ d S )Nr$   z-3 -fZCWarning)r"   r   r*   Úcompile_warning_coderL   )r&   r,   rN   rO   rP   r   r   r
   Útest_cython_warning_shown¾   s
    z*TestIPythonMagic.test_cython_warning_shownc                 C   sF   | j }| ddt¡ | d¡ |  |jd d¡ |  |jd d¡ d S )Nr$   z-3 --pgozg = f(10); h = call(10); main()r0   r?   r2   )r"   r*   Úpgo_cython3_coder)   r+   r4   r;   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"   r4   r*   r+   )r&   r,   r3   r   r   r
   Útest_extlibsÒ   s
    
zTestIPythonMagic.test_extlibsc                 C   s4   | j }| ddt¡ | d¡ |  |jd d¡ d S )Nr$   ú	--verboser/   r0   r1   r:   r;   r   r   r
   Útest_cython_verboseÞ   s    
z$TestIPythonMagic.test_cython_verbosec              	   C   sº   t dd„ ƒ}| j}|ƒ }| ddt¡ | d¡ W 5 Q R X |  |jd d¡ |  |j|j|jg|j	¡ |ƒ }| ddt¡ | d¡ W 5 Q R X |  |jd d¡ |  |jg|j	¡ d S )	Nc                  s   s>   G dd„ dƒ} | ƒ }t jj}z|t j_|V  W 5 |t j_X d S )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>.MockLogrW   é   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ÚINFOr^   r`   r   r   r   r
   ÚMockLogç   s   rf   )r   Ú	distutilsÚlog)rf   Znew_logZold_logr   r   r
   Úmock_distutilså   s    

zGTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutilsr$   rZ   r/   r0   r1   r'   )
r   r"   r*   r3   r)   r+   r4   re   rd   r^   )r&   ri   r,   Zverbose_logZ
normal_logr   r   r
   Útest_cython_verbose_thresholdsä   s    
ÿz/TestIPythonMagic.test_cython_verbose_thresholdsc                 C   s&   | j }| ddt¡}|  |d k¡ d S )Nr$   r'   )r"   r*   r3   rL   ©r&   r,   Úhtmlr   r   r
   Útest_cython_no_annotate  s    z(TestIPythonMagic.test_cython_no_annotatec                 C   s*   | j }| ddt¡}|  tj|jk¡ d S )Nr$   z
--annotate©r"   r*   r3   rL   r   ZCOMPLETE_CODE_TITLEÚdatark   r   r   r
   Útest_cython_annotate  s    z%TestIPythonMagic.test_cython_annotatec                 C   s*   | j }| ddt¡}|  tj|jk¡ d S )Nr$   z-arn   rk   r   r   r
   Útest_cython_annotate_default  s    z-TestIPythonMagic.test_cython_annotate_defaultc                 C   s*   | j }| ddt¡}|  tj|jk¡ d S )Nr$   z--annotate-fullcrn   rk   r   r   r
   Ú$test_cython_annotate_complete_c_code  s    z5TestIPythonMagic.test_cython_annotate_complete_c_codeN)ra   rb   rc   Úclassmethodr    r%   r.   Ú
skip_win32r9   r<   r=   rB   rD   rE   rQ   rR   rT   rV   rY   r[   rj   rm   rp   rq   rr   r   r   r   r
   r   a   s0   



#r   )Ú__doc__r5   r   r   Ú
contextlibr   Zunittestr   ZCython.Buildr   ZCython.TestUtilsr   ZCython.Compiler.Annotater   ZIPython.testing.globalipappr!   ÚImportErrorr   Úplatformrt   ZIPython.core.historyr   Zenabledr   r3   rA   rU   rK   rS   r   r   r   r   r
   Ú<module>   s8   


