U
    ?h                     @   s   d dl Z d dlZd dlmZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ zd dlZdZW n ek
r|   dZY nX dZee eZeG d	d
 d
eZG dd deZedkre  dS )    N)dedent)mock)TestCasetemp_directoryoverride_env_configrun_in_subprocess)configTFz*pyyaml needed for configuration file testsc                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )
TestConfigFc                    s"   t d| _d| _tt|   d S )NZ
config_tmpi	  )r   tmppathZmaxDiffsuperr	   setUpself	__class__ I/var/www/html/venv/lib/python3.8/site-packages/numba/tests/test_config.pyr      s    
zTestConfig.setUpc                 C   s   t j| jdS )zT
        Creates a mock launch location.
        Returns the location path.
        )dir)tempfilemkdtempr
   r   r   r   r   mock_cfg_location    s    zTestConfig.mock_cfg_locationc              	   C   s:   t j|tj}t|d}tj||dd W 5 Q R X dS )z<
        Injects a mock configuration at 'location'
        ZwtF)Zdefault_flow_styleN)ospathjoinr   Z_config_fnameopenyamldump)r   locationcfgZtmpcfgfr   r   r   inject_mock_cfg'   s    zTestConfig.inject_mock_cfgc                 C   s.   t  }ttD ]}| rtt|||< q|S )z8
        Gets the current numba config settings
        )dictr   r   isuppergetattr)r   storexr   r   r   get_settings/   s
    zTestConfig.get_settingsc              	   C   s|   t  }|  }t | tdd |  }W 5 Q R X | || z"tdd |  }W 5 Q R X W 5 t | X ||fS )z
        Returns a config "original" from a location with no config file
        and then the impact of applying the supplied cfg dictionary as
        a config file at a location in the returned "current".
        _)r   getcwdr   chdirr   r&   r    )r   r   Zoriginal_cwdZ
launch_diroriginalcurrentr   r   r   create_config_effect9   s    
zTestConfig.create_config_effectc                 C   s   d}t jt jfD ]n}| ||di\}}| ||k | || || k | || d || || | || qd S )NZCOLOR_SCHEMEZlight_bg)strupperlowerr,   Z
assertTrueassertEqualpop)r   keycaseorigcurrr   r   r   test_configZ   s    

zTestConfig.test_configc                 C   s   |  i \}}| || d S N)r,   r0   )r   r4   r5   r   r   r   test_empty_configh   s    zTestConfig.test_empty_configc           	      C   sv   t j }d|d< d}d| d}tt||d\}}d}|d}| || d	}| || | ||d d S )
NZnot_a_known_styleZNUMBA_CAPTURED_ERRORSzthe source compiledz0from numba import njit
@njit
def foo():
	print('z')
foo())envzyEnvironment variable 'NUMBA_CAPTURED_ERRORS' is defined but its associated value 'not_a_known_style' could not be parsed.zutf-8zZThe parse failed with exception: Invalid style in NUMBA_CAPTURED_ERRORS: not_a_known_style)r   environcopyr   r   decodeassertIn)	r   Znew_envZsource_compiledcodeouterrexpectederr_msgZex_expectedr   r   r   !test_illegal_error_style_handlingm   s    

z,TestConfig.test_illegal_error_style_handling)__name__
__module____qualname__Z_numba_parallel_test_r   r   r    r&   r,   r6   r8   rC   __classcell__r   r   r   r   r	      s   
!r	   c                   @   s   e Zd Zdd Zejddiddd Zejddidd	d
 Zejddiddd Ze dd Z	ejddiddd Z
dS )TestNumbaOptLevelc           	   
      s   ddl m}m} |j| |jj| ddlm} d fdd}tj	j
|d|d< t}|d	d
   W 5 Q R X t|j W 5 Q R X d S )Nr   )r   njit)
CPUCodegenzexpected side effectc                     s    |  td S r7   )r0   RuntimeError)argskwargsrA   r   Zside_effect_messager   r   side_effect   s    z,TestNumbaOptLevel.check.<locals>.side_effectZ_module_pass_manager)rO   c                   S   s   dS )N.r   r   r   r   r   <lambda>       z)TestNumbaOptLevel.check.<locals>.<lambda>)Znumbar   rI   r0   OPTZ
_raw_valueZnumba.core.codegenrJ   r   patchobjectZassertRaisesrK   r=   r-   	exception)	r   rA   Z	opt_value	raw_valuer   rI   rJ   rO   Zraisesr   rN   r   check   s    
zTestNumbaOptLevel.checkZ	NUMBA_OPTmax)envvarsc                 C   s    ddddd}|  |dd d S )NTF   cheapZloop_vectorizeZslp_vectorizeoptZcostrX   rW   r   rA   r   r   r   test_opt_max   s    zTestNumbaOptLevel.test_opt_max3c                 C   s    ddddd}|  |dd d S NFr   r[   r\   rZ   r^   r_   r   r   r   
test_opt_3   s    zTestNumbaOptLevel.test_opt_30c                 C   s    ddddd}|  |dd d S )NFr   r[   r\   r^   r_   r   r   r   
test_opt_0   s    zTestNumbaOptLevel.test_opt_0c                 C   s    ddddd}|  |dd d S rb   r^   r_   r   r   r   test_opt_default   s    z"TestNumbaOptLevel.test_opt_defaultinvalidc                 C   s    ddddd}|  |dd d S rb   r^   r_   r   r   r   test_opt_invalid   s    z"TestNumbaOptLevel.test_opt_invalidN)rD   rE   rF   rW   r   Zrun_test_in_subprocessr`   rc   re   rf   rh   r   r   r   r   rH      s   



	rH   __main__)r   r   textwrapr   Zunittestr   Znumba.tests.supportr   r   r   r   Z
numba.corer   r   Z
_HAVE_YAMLImportErrorZ	_skip_msgZskipIfZ
needs_yamlr	   rH   rD   mainr   r   r   r   <module>   s$   
kO