U
    ,d(                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	Z	d dl
mZmZ d dlmZ ejdd ZG dd dee	jZdS )	    N)StringIO)SerialMixincreate_temp_module)
dispatcherc              	   c   s6   z$t }t|}| | |V  W 5 |  | X d S N)removeHandlerr   loggingStreamHandler
addHandler)lhandlerbuffer r   ?/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_jit_module.pycaptured_logs   s    


r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )TestJitModulez
from numba import jit_module

def inc(x):
    return x + 1

def add(x, y):
    return x + y

def inc_add(x):
    y = inc(x)
    return add(x, y)

import numpy as np
mean = np.mean

class Foo(object):
    pass

jit_module({jit_options})
c              	   C   s   t tj}ttj}t| jN}tj|j	}| 
|tjd  | 
tjdd  | | |jtjk W 5 Q R X | 
tj| | 
tj| d S )Nr      )listsyspathdictmodulesr   source_linesosdirname__file__assertEqual
assertTrue__name__)selfsys_path_originalsys_modules_originaltest_moduleZtemp_module_dirr   r   r   test_create_temp_jitted_module1   s    

z,TestJitModule.test_create_temp_jitted_modulec              	   C   sl   z6t tj}ttj}t| j tdW 5 Q R X W n0 tk
rf   | tj| | tj| Y nX d S )NzSomething went wrong!)	r   r   r   r   r   r   r   
ValueErrorr   )r   r    r!   r   r   r   -test_create_temp_jitted_module_with_exception=   s    

z;TestJitModule.test_create_temp_jitted_module_with_exceptionc              	   C   s   t | j}| |jtj | |jtj | |jtj | |j	t
j	k | t|j d\}}| |||j| | ||||j|| | |||j| W 5 Q R X d S )N)g333333?gffffff@)r   r   ZassertIsInstanceincr   Z
DispatcheraddZinc_addr   ZmeannpinspectisclassZFoor   Zpy_func)r   r"   xyr   r   r   test_jit_moduleH   s     

zTestJitModule.test_jit_modulec              	   C   s<   ddddd}t | jf|}| |jj| W 5 Q R X d S )NTFnumpy)nopythonnogilerror_modelboundscheck)r   r   r   r&   targetoptions)r   jit_optionsr"   r   r   r   test_jit_module_jit_optionsY   s    z)TestJitModule.test_jit_module_jit_optionsc              	   C   sZ   d}dddd}t f d|i|.}| |jj| | |jjddd d W 5 Q R X d S )Nz
from numba import jit, jit_module

@jit(nogil=True, forceobj=True)
def inc(x):
    return x + 1

def add(x, y):
    return x + y

jit_module({jit_options})
Tr.   F)r/   r1   r2   r   )r0   Zforceobjr2   )r   r   r'   r3   r&   )r   r   r4   r"   r   r   r   $test_jit_module_jit_options_overridec   s    

z2TestJitModule.test_jit_module_jit_options_overridec              
      s   t d}|t j ddd}t|\ t| jf|B}   dd|j	d|g}| 
t fdd	|D  W 5 Q R X W 5 Q R X d S )
Nnumba.core.decoratorsTr.   )r/   r1   zAuto decorating functionzfrom module {}zwith jit and options: {}c                 3   s   | ]}| kV  qd S r   r   ).0ilogsr   r   	<genexpr>   s     z?TestJitModule.test_jit_module_logging_output.<locals>.<genexpr>)r   	getLoggersetLevelDEBUGr   r   r   getvalueformatr   r   all)r   loggerr4   r"   expectedr   r:   r   test_jit_module_logging_output|   s     


z,TestJitModule.test_jit_module_logging_outputc              
   C   sT   t d}|t j t|,}t| j | | d W 5 Q R X W 5 Q R X d S )Nr7    )	r   r=   r>   INFOr   r   r   r   r@   )r   rC   r;   r   r   r   test_jit_module_logging_level   s
    

z+TestJitModule.test_jit_module_logging_levelN)r   
__module____qualname__r   r#   r%   r-   r5   r6   rE   rH   r   r   r   r   r      s   
r   )r   r   r)   
contextlibr.   r(   r   ior   ZunittestZnumba.tests.supportr   r   Z
numba.corer   contextmanagerr   ZTestCaser   r   r   r   r   <module>   s   

