o
    wi,#                     @   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   `/home/ubuntu/sommelier/.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IPythontesting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&   extension_manager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=20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 )
N_test_cython_pyximportcython_pyximport	g = f(10)g      4@z
h = f(-10)hg      4z.pyx)	r&   r1   coder0   r2   user_nsosremoveOSError)r,   r3   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.   r8   r9   r:   r&   r1   r<   r0   r2   r=   r,   r3   r   r   r
   test_cython      
zTestIPythonMagic.test_cythonc                 C   rC   )Nr(   z--name=mymodulez#import mymodule; g = mymodule.f(10)r9   r:   rD   rE   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)r9   皙?r;   r&   r1   cython3_coder0   r2   r=   rE   r   r   r
   test_cython_language_level   
   
z+TestIPythonMagic.test_cython_language_levelc                 C   rI   )Nr(   -3rJ   r9   rK   r;   rL   rE   r   r   r
   test_cython3   rO   zTestIPythonMagic.test_cython3c                 C   rI   )Nr(   z-2rJ   r9   r   r;   rL   rE   r   r   r
   test_cython2   rO   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(   rP   
error	error in msg)r&   r   r1   compile_error_code
assertTruer,   r3   outcaptured_outcaptured_errcaptured_allr   r   r
   test_cython_compile_error_shown      z0TestIPythonMagic.test_cython_compile_error_shownc                 C   rS   )Nr(   z-3 -l=xxxxxxxxz
!rU   rV   rW   )r&   r   r1   r<   rZ   r[   r   r   r
   test_cython_link_error_shown   ra   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   r1   compile_warning_coderZ   )r,   r3   r\   r]   r^   r   r   r
   test_cython_warning_shown   s   z*TestIPythonMagic.test_cython_warning_shownc                 C   rI   )Nr(   z-3 --pgozg = f(10); h = call(10); main()r9   rK   r;   )r&   r1   pgo_cython3_coder0   r2   r=   rE   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=   r1   r2   )r,   r3   r<   r   r   r
   test_extlibs   s
   
zTestIPythonMagic.test_extlibsc                 C   rC   )Nr(   	--verboser8   r9   r:   rD   rE   r   r   r
   test_cython_verbose   rG   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>.MockLogrh      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INFOro   rq   r   r   r   r
   MockLog   s
    rw   )r   	distutilslog)rw   new_logold_logr   r   r
   mock_distutils   s   
zGTestIPythonMagic.test_cython_verbose_thresholds.<locals>.mock_distutilsr(   rk   r8   r9   r:   r.   )
r   r&   r1   r<   r0   r2   r=   rv   ru   ro   )r,   r|   r3   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(   r.   )r&   r1   r<   rZ   r,   r3   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&   r1   r<   rZ   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)rr   rs   rt   classmethodr!   r)   r5   
skip_win32rB   rF   rH   rN   rQ   rR   r`   rb   re   rg   rj   rl   r   r   r   r   r   r   r   r   r
   r    a   s2    



#r    )__doc__r>   r   r   
contextlibr   unittestr   Cython.Buildr   Cython.TestUtilsr   Cython.Compiler.Annotater   IPython.testing.globalipappr"   ImportErrorr   platformr   IPython.core.historyr   enabledr   r<   rM   rf   rY   rd   r    r   r   r   r
   <module>   s>   


