o
    j,#                     @   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y?   dd	 ZY nw d
d	 ZeejdkdZzddlmZ de_W n	 ey`   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.    N)contextmanager)skipIf)IpythonMagic)
CythonTest)AnnotationCCodeWriterc                 C   s   d S N )_r   r   d/var/www/html/Deteccion_Ine/venv/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)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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globalipapp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 d S  tyC   Y d S w )
NZ_test_cython_pyximportZcython_pyximport	g = f(10)g      4@z
h = f(-10)hg      4z.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   4   | j }|ddt |d | |jd d d S )Nr&   r)   r1   r2   r3   r$   r,   r5   r+   r-   r6   r(   r.   r   r   r
   test_cython      
zTestIPythonMagic.test_cythonc                 C   r<   )Nr&   z--name=mymodulez#import mymodule; g = mymodule.f(10)r2   r3   r=   r>   r   r   r
   test_cython_name   s   
z!TestIPythonMagic.test_cython_namec                 C   F   | j }|ddt |d | |jd d | |jd d d S )Nr&   r)   g = f(10); h = call(10)r2   皙?r4   r$   r,   cython3_coder+   r-   r6   r>   r   r   r
   test_cython_language_level   
   
z+TestIPythonMagic.test_cython_language_levelc                 C   rB   )Nr&   -3rC   r2   rD   r4   rE   r>   r   r   r
   test_cython3   rH   zTestIPythonMagic.test_cython3c                 C   rB   )Nr&   z-2rC   r2   r   r4   rE   r>   r   r   r
   test_cython2   rH   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&   rI   
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      z0TestIPythonMagic.test_cython_compile_error_shownc                 C   rL   )Nr&   z-3 -l=xxxxxxxxz
!rN   rO   rP   )r$   r   r,   r5   rS   rT   r   r   r
   test_cython_link_error_shown   rY   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 -fZCWarning)r$   r   r,   compile_warning_coderS   )r(   r.   rU   rV   rW   r   r   r
   test_cython_warning_shown   s   z*TestIPythonMagic.test_cython_warning_shownc                 C   rB   )Nr&   z-3 --pgozg = f(10); h = call(10); main()r2   rD   r4   )r$   r,   pgo_cython3_coder+   r-   r6   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$   r6   r,   r-   )r(   r.   r5   r   r   r
   test_extlibs   s
   
zTestIPythonMagic.test_extlibsc                 C   r<   )Nr&   	--verboser1   r2   r3   r=   r>   r   r   r
   test_cython_verbose   r@   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>.MockLogr_      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INFOrf   rh   r   r   r   r
   MockLog   s
    rn   )r   	distutilslog)rn   Znew_logZold_logr   r   r
   mock_distutils   s   
zGTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutilsr&   rb   r1   r2   r3   r)   )
r   r$   r,   r5   r+   r-   r6   rm   rl   rf   )r(   rq   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 u  d S )Nr&   r)   )r$   r,   r5   rS   r(   r.   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$   r,   r5   rS   r   ZCOMPLETE_CODE_TITLEdatars   r   r   r
   test_cython_annotate     z%TestIPythonMagic.test_cython_annotatec                 C   rv   )Nr&   z-arw   rs   r   r   r
   test_cython_annotate_default  rz   z-TestIPythonMagic.test_cython_annotate_defaultc                 C   s*   | j }|ddt}| tj|jv  d S )Nr&   z--annotate-fullcrw   rs   r   r   r
   $test_cython_annotate_complete_c_code  rz   z5TestIPythonMagic.test_cython_annotate_complete_c_codeN)ri   rj   rk   classmethodr!   r'   r0   
skip_win32r;   r?   rA   rG   rJ   rK   rX   rZ   r\   r^   ra   rc   rr   ru   ry   r{   r|   r   r   r   r
   r    a   s2    



#r    )__doc__r7   r   r   
contextlibr   Zunittestr   ZCython.Buildr   ZCython.TestUtilsr   ZCython.Compiler.Annotater   ZIPython.testing.globalipappr"   ImportErrorr   platformr~   ZIPython.core.historyr   enabledr   r5   rF   r]   rR   r[   r    r   r   r   r
   <module>   s>   


