U
    9%eE                     @   s~  d Z ddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZmZmZmZ dd
lm Z  ddl!m"Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+Z+ddddgZ,ej-.e	e	Z/G dd de+j*Z0G dd de+j*Z1G dd de+j*Z2G dd de+j*Z3G dd de'e+j*Z4G dd de'e*Z5e6dkrze+7  dS )z"
Unspecified error handling tests
    N)jitnjittypedint64types)errors)	structref)overload	intrinsicoverload_methodoverload_attribute)CompilerBase)TranslateByteCode	FixupArgsIRProcessing)NopythonTypeInferenceDeadCodeEliminationNoPythonBackendNativeLowering)PassManager)_err_reasons)skip_parfors_unsupportedoverride_configSerialMixinskip_unless_cffiskip_unless_scipyTestCase            c                   @   s   e Zd Zdd ZdS )TestErrorHandlingBeforeLoweringc              
   C   sT   dd }t tfD ]>}| tj}||d W 5 Q R X d}| |t|j qd S )Nc                    s   | d   fdd}|S )z return the closure r   c                    s   |   S N xzr#   ]/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_errorhandling.pyinner'   s    zmTestErrorHandlingBeforeLowering.test_unsupported_make_function_return_inner_func.<locals>.func.<locals>.innerr#   )r%   r)   r#   r&   r(   func#   s    z^TestErrorHandlingBeforeLowering.test_unsupported_make_function_return_inner_func.<locals>.funcr   z%Cannot capture the non-constant value)r   r   assertRaisesr   TypingErrorassertInstr	exception)selfr*   Zpipelineraisesexpectedr#   r#   r(   0test_unsupported_make_function_return_inner_func"   s    zPTestErrorHandlingBeforeLowering.test_unsupported_make_function_return_inner_funcN)__name__
__module____qualname__r3   r#   r#   r#   r(   r!       s   r!   c                   @   s   e Zd Zdd ZdS )TestUnsupportedReportingc              	   C   sD   t dd }| tj}|  W 5 Q R X d}| |t|j d S )Nc                   S   s   t dddg d S )Nr   r   r   )npZ
asanyarrayr#   r#   r#   r(   r*   7   s    zFTestUnsupportedReporting.test_unsupported_numpy_function.<locals>.funcz4Use of unsupported NumPy function 'numpy.asanyarray'r   r+   r   r,   r-   r.   r/   )r0   r*   r1   r2   r#   r#   r(   test_unsupported_numpy_function5   s    
z8TestUnsupportedReporting.test_unsupported_numpy_functionN)r4   r5   r6   r:   r#   r#   r#   r(   r7   3   s   r7   c                   @   sX   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dd Z
dd Zdd ZdS )TestMiscErrorHandlingc                 C   s*   t dd }tjdgtjd}|| d S )Nc                 S   s   d|  S N
   r#   r$   r#   r#   r(   fnI   s    zHTestMiscErrorHandling.test_use_of_exception_for_flow_control.<locals>.fng      ?)Zdtype)r   r8   arrayZfloat64)r0   r>   ar#   r#   r(   &test_use_of_exception_for_flow_controlD   s    
z<TestMiscErrorHandling.test_use_of_exception_for_flow_controlc              
   C   sP   dd }dd }||fD ]2}|  t}|  W 5 Q R X | dt|j qd S )Nc                   S   s   t dd S )Ntest_string	Exceptionr#   r#   r#   r(   foo_commentedV   s    z_TestMiscErrorHandling.test_commented_func_definition_is_not_a_definition.<locals>.foo_commentedc                   S   s   t ddS )z> def docstring containing def might match function definition!rB   NrC   r#   r#   r#   r(   foo_docstringZ   s    z_TestMiscErrorHandling.test_commented_func_definition_is_not_a_definition.<locals>.foo_docstringrB   )r+   rD   r-   r.   r/   )r0   rE   rF   r*   r1   r#   r#   r(   2test_commented_func_definition_is_not_a_definitionP   s    zHTestMiscErrorHandling.test_commented_func_definition_is_not_a_definitionc              	   C   sd   G dd dt }t|ddd }| tj}|tddg W 5 Q R X d}| |t|j d S )	Nc                   @   s   e Zd Zdd ZdS )zFTestMiscErrorHandling.test_use_of_ir_unknown_loc.<locals>.TestPipelinec                 S   sn   d}t |}|td |td |td |td |td |td |td |	  |gS )	NZbad_DCE_pipelinezanalyzing bytecodezfix up argszprocessing IRZDCEznopython frontendznative loweringznopython mode backend)
r   Zadd_passr   r   r   r   r   r   r   finalize)r0   namepmr#   r#   r(   define_pipelinesg   s    zWTestMiscErrorHandling.test_use_of_ir_unknown_loc.<locals>.TestPipeline.define_pipelinesN)r4   r5   r6   rK   r#   r#   r#   r(   TestPipelinef   s   rL   )Zpipeline_classc                 S   s   dS )Nr   r#   r@   r#   r#   r(   fw   s    z;TestMiscErrorHandling.test_use_of_ir_unknown_loc.<locals>.fr   r   z File "unknown location", line 0:)	r   r   r+   r   r,   iterr-   r.   r/   )r0   rL   rN   r1   r2   r#   r#   r(   test_use_of_ir_unknown_locd   s    
z0TestMiscErrorHandling.test_use_of_ir_unknown_locc              	   C   sF   |  tj}|  W 5 Q R X ddg}|D ]}| |t|j q*d S )NzThe use of az'in globals, is not supported as globals)r+   r   r,   r-   r.   r/   )r0   r*   r1   r2   exr#   r#   r(   check_write_to_globals   s
    z,TestMiscErrorHandling.check_write_to_globalsc                 C   s   t dd }| | d S )Nc                   S   s   dt d< d S Nr=   r   )_global_listr#   r#   r#   r(   foo   s    zMTestMiscErrorHandling.test_handling_of_write_to_reflected_global.<locals>.foor   rR   r0   rU   r#   r#   r(   *test_handling_of_write_to_reflected_global   s    
z@TestMiscErrorHandling.test_handling_of_write_to_reflected_globalc                 C   s   t dd }| | d S )Nc                   S   s   dt d< d S rS   )_global_dictr#   r#   r#   r(   rU      s    zNTestMiscErrorHandling.test_handling_of_write_to_typed_dict_global.<locals>.foorV   rW   r#   r#   r(   +test_handling_of_write_to_typed_dict_global   s    
zATestMiscErrorHandling.test_handling_of_write_to_typed_dict_globalc              	   C   sJ   t dddd }| tj}|  W 5 Q R X d}| |t|j d S )NT)parallelc                  S   s   t dD ]} qd S r<   )Zprange)ir#   r#   r(   rU      s    zPTestMiscErrorHandling.test_handling_forgotten_numba_internal_import.<locals>.foozC'prange' looks like a Numba internal function, has it been importedr9   )r0   rU   r1   r2   r#   r#   r(   -test_handling_forgotten_numba_internal_import   s    
zCTestMiscErrorHandling.test_handling_forgotten_numba_internal_importc              
   C   sX   dd }d}t ddtfD ]8}| tj}||  W 5 Q R X | |t|j qd S )Nc                   S   s   dd t dD  d S )Nc                 s   s   | ]
}|V  qd S r"   r#   ).0r%   r#   r#   r(   	<genexpr>   s     zdTestMiscErrorHandling.test_handling_unsupported_generator_expression.<locals>.foo.<locals>.<genexpr>r=   )ranger#   r#   r#   r(   rU      s    zQTestMiscErrorHandling.test_handling_unsupported_generator_expression.<locals>.fooz-The use of yield in a closure is unsupported.T)Zforceobj)r   r   r+   r   ZUnsupportedErrorr-   r.   r/   )r0   rU   r2   decr1   r#   r#   r(   .test_handling_unsupported_generator_expression   s    zDTestMiscErrorHandling.test_handling_unsupported_generator_expressionc              	   C   sD   t dd }d}| tj}|  W 5 Q R X | |t|j d S )Nc                   S   s   t S r"   rM   r#   r#   r#   r(   rU      s    zCTestMiscErrorHandling.test_handling_undefined_variable.<locals>.fooz"NameError: name 'a' is not definedr9   )r0   rU   r2   r1   r#   r#   r(    test_handling_undefined_variable   s    
z6TestMiscErrorHandling.test_handling_undefined_variableN)r4   r5   r6   rA   rG   rP   rR   rX   rZ   r   r]   rb   rc   r#   r#   r#   r(   r;   B   s   
r;   c                   @   s|   e Z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dd ZdS )TestErrorMessagesc              	      s   ddd  t  fdd}t fdd}| tj}|  W 5 Q R X t|j}| td 	 d	 | | | d S )
NZspecific_reasonc                   S   s   d S r"   r#   r#   r#   r#   r(   rU      s    z2TestErrorMessages.test_specific_error.<locals>.fooc                      s   t  d S r"   )r   NumbaValueErrorr#   )given_reasonr#   r(   ol_foo   s    z5TestErrorMessages.test_specific_error.<locals>.ol_fooc                      s
      d S r"   r#   r#   rU   r#   r(   call_foo   s    z7TestErrorMessages.test_specific_error.<locals>.call_foospecific_errorr   )
r	   r   r+   r   r,   r.   r/   r-   error_reasons
splitlinesr0   rg   ri   r1   excstrr#   )rU   rf   r(   test_specific_error   s    
z%TestErrorMessages.test_specific_errorc              	      s`   dd  t  dd }t fdd}| tj}|  W 5 Q R X t|j}| d| d S )Nc                   S   s   d S r"   r#   r#   r#   r#   r(   rU      s    z2TestErrorMessages.test_no_match_error.<locals>.fooc                   S   s   d S r"   r#   r#   r#   r#   r(   rg      s    z5TestErrorMessages.test_no_match_error.<locals>.ol_fooc                      s
      d S r"   r#   r#   rh   r#   r(   ri      s    z7TestErrorMessages.test_no_match_error.<locals>.call_foozNo match)r	   r   r+   r   r,   r.   r/   r-   rm   r#   rh   r(   test_no_match_error   s    

z%TestErrorMessages.test_no_match_errorc              	   C   s~   t dd }| tj}|  W 5 Q R X t|j}| td  d | t	j
ddd}d| d	}| ||| d
S )z Checks that the reported source location for an overload is the
        overload implementation source, not the actual function source from the
        target library.c                   S   s   t jd d S )Nchars)r8   ZlinalgZsvdr#   r#   r#   r(   rU      s    zDTestErrorMessages.test_error_function_source_is_correct.<locals>.foorj   r   numbar8   z	linalg.pyz'Overload in function 'svd_impl': File: :N)r   r+   r   r,   r.   r/   r-   rk   rl   ospathjoinformat)r0   rU   r1   rn   expected_filer2   r#   r#   r(   %test_error_function_source_is_correct   s    

z7TestErrorMessages.test_error_function_source_is_correctc              	   C   sP   t dd }| tj}|  W 5 Q R X t|j}| d| | d| d S )Nc                   S   s   dd S )Nr@   r   r#   r#   r#   r#   r(   rU      s    z<TestErrorMessages.test_concrete_template_source.<locals>.foozOverload of function 'add'	No match.r   r+   r   r,   r.   r/   r-   r0   rU   r1   rn   r#   r#   r(   test_concrete_template_source   s    

z/TestErrorMessages.test_concrete_template_sourcec              	   C   sD   t dd }| tj}|  W 5 Q R X t|j}| d| d S )Nc                   S   s   t dS Nr   )lenr#   r#   r#   r(   rU     s    z<TestErrorMessages.test_abstract_template_source.<locals>.foozOverload of function 'len'r{   r|   r#   r#   r(   test_abstract_template_source
  s    

z/TestErrorMessages.test_abstract_template_sourcec              	   C   sD   t dd }| tj}|  W 5 Q R X t|j}| d| d S )Nc                   S   s
   t d S r"   )r8   Zangler#   r#   r#   r(   rU     s    z<TestErrorMessages.test_callable_template_source.<locals>.fooz6No implementation of function Function(<function angler{   r|   r#   r#   r(   test_callable_template_source  s    

z/TestErrorMessages.test_callable_template_sourcec              	      s   dd  t  dd }t fdd}| tj}|  W 5 Q R X t|j}| d| tj	
dd	d
}d| d}| ||| | d| d S )Nc                 S   s   d S r"   r#   r$   r#   r#   r(   bar%  s    zDTestErrorMessages.test_overloadfunction_template_source.<locals>.barc                 S   s   d S r"   r#   r$   r#   r#   r(   ol_bar(  s    zGTestErrorMessages.test_overloadfunction_template_source.<locals>.ol_barc                      s    dS r~   r#   r#   r   r#   r(   rU   ,  s    zDTestErrorMessages.test_overloadfunction_template_source.<locals>.fooz
<numerous>rr   testsztest_errorhandling.pyz"Overload of function 'bar': File: rs   rz   )r	   r   r+   r   r,   r.   r/   ZassertNotInrt   ru   rv   r-   rw   )r0   r   rU   r1   rn   rx   Zexpected_olr#   r   r(   %test_overloadfunction_template_source#  s    


z7TestErrorMessages.test_overloadfunction_template_sourcec              	      s   d dt  fddtfdd}| tj}|  W 5 Q R X t|j}| td 	 d | |  | | | | d	| d S )
Nzx must be literalzarray.ndim must be 1c                    sF   t |tjst |jdkr*tt||}dd }||fS )Nr   c                 S   s   d S r"   r#   )contextZbuilder	signatureargsr#   r#   r(   codegenL  s    zSTestErrorMessages.test_intrinsic_template_source.<locals>.myintrin.<locals>.codegen)
isinstancer   ZIntegerLiteralr   ZRequireLiteralValuendimre   Zintp)Z	typingctxr%   arrsigr   )given_reason1given_reason2r#   r(   myintrinB  s    


zBTestErrorMessages.test_intrinsic_template_source.<locals>.myintrinc                     s   t d}  d|  d S )N)r   r   r   )r8   Zzeros)r   )r   r#   r(   call_intrinP  s    
zETestErrorMessages.test_intrinsic_template_source.<locals>.call_intrinrj   r   zIntrinsic in function)
r
   r   r+   r   r,   r.   r/   r-   rk   rl   )r0   r   r1   rn   r#   )r   r   r   r(   test_intrinsic_template_source=  s    
z0TestErrorMessages.test_intrinsic_template_sourcec              	   C   sX   t tjddd }tdd }| tj}|  W 5 Q R X t|j}| 	d| d S )N
isnonsensec                 S   s   d S r"   r#   r0   r#   r#   r(   ol_unicode_isnonsensea  s    zTTestErrorMessages.test_overloadmethod_template_source.<locals>.ol_unicode_isnonsensec                   S   s   d   d S Nabcr   r#   r#   r#   r(   rU   e  s    zBTestErrorMessages.test_overloadmethod_template_source.<locals>.foo,Overload of function 'ol_unicode_isnonsense')
r   r   UnicodeTyper   r+   r   r,   r.   r/   r-   r0   r   rU   r1   rn   r#   r#   r(   #test_overloadmethod_template_source^  s    



z5TestErrorMessages.test_overloadmethod_template_sourcec              	   C   sX   t tjddd }tdd }| tj}|  W 5 Q R X t|j}| 	d| d S )Nr   c                 S   s   d S r"   r#   r   r#   r#   r(   r   r  s    zWTestErrorMessages.test_overloadattribute_template_source.<locals>.ol_unicode_isnonsensec                   S   s
   dj  d S r   r   r#   r#   r#   r(   rU   v  s    zETestErrorMessages.test_overloadattribute_template_source.<locals>.foor   )
r   r   r   r   r+   r   r,   r.   r/   r-   r   r#   r#   r(   &test_overloadattribute_template_sourceo  s    



z8TestErrorMessages.test_overloadattribute_template_sourcec              	      sT   ddl m  t fdd}| tj}|  W 5 Q R X t|j}| d| d S )Nr   c_cosc                      s    d d S Nr@   r#   r#   r   r#   r(   rU     s    zMTestErrorMessages.test_external_function_pointer_template_source.<locals>.foo.Type Restricted Function in function 'unknown')	Znumba.tests.ctypes_usecasesr   r   r+   r   r,   r.   r/   r-   r|   r#   r   r(   .test_external_function_pointer_template_source  s    
z@TestErrorMessages.test_external_function_pointer_template_sourcec              	      sb   ddl m} |  |j t fdd}| tj}|  W 5 Q R X t|j	}| 
d| d S )Nr   )cffi_usecasesc                      s    d d S r   r#   r#   r*   r#   r(   rU     s    zITestErrorMessages.test_cffi_function_pointer_template_source.<locals>.foor   )Znumba.testsr   initZcffi_cosr   r+   r   r,   r.   r/   r-   )r0   modrU   r1   rn   r#   r   r(   *test_cffi_function_pointer_template_source  s    
z<TestErrorMessages.test_cffi_function_pointer_template_sourcec              	   C   sz   t jG dd dtj}G dd dt j}t ||ddg | tj}|ddd	 W 5 Q R X t	|j
}| d
| d S )Nc                   @   s   e Zd ZdS )z;TestErrorMessages.test_missing_source.<locals>.ParticleTypeN)r4   r5   r6   r#   r#   r#   r(   ParticleType  s   r   c                   @   s   e Zd Zdd ZdS )z7TestErrorMessages.test_missing_source.<locals>.Particlec                 S   s   t j| |S r"   )r   StructRefProxy__new__)clsposmassr#   r#   r(   r     s    z?TestErrorMessages.test_missing_source.<locals>.Particle.__new__N)r4   r5   r6   r   r#   r#   r#   r(   Particle  s   r   r   r   r   r   )r   r   z#missing a required argument: 'mass')r   registerr   Z	StructRefr   Zdefine_proxyr+   r   r,   r.   r/   r-   )r0   r   r   r1   rn   r#   r#   r(   test_missing_source  s    
z%TestErrorMessages.test_missing_sourceN)r4   r5   r6   ro   rp   r   ry   r}   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r(   rd      s   
!
rd   c                   @   s   e Zd Zdd ZdS )"TestDeveloperSpecificErrorMessagesc              
   C   s\   dd }t dd* | tj}td| W 5 Q R X W 5 Q R X t|j}| d| d S )Nc                 S   s   |  d d S )N)maxr$   r#   r#   r(   rU     s    zPTestDeveloperSpecificErrorMessages.test_bound_function_error_string.<locals>.fooZDEVELOPER_MODEr   zvoid(int64[:,:])ztoo many positional arguments)r   r+   r   r,   r   r.   r/   r-   r|   r#   r#   r(    test_bound_function_error_string  s     
zCTestDeveloperSpecificErrorMessages.test_bound_function_error_stringN)r4   r5   r6   r   r#   r#   r#   r(   r     s   r   c                   @   s`   e Zd 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	dS )TestCapturedErrorHandlingzjChecks that the way errors are captured changes depending on the env
    var "NUMBA_CAPTURED_ERRORS".
    c                    s   dd  t  dd }t  tdtj dtfdtjffD ]^\}}td|F | 	|}t
d	 fd
d}W 5 Q R X d}| |t|j W 5 Q R X qBW 5 Q R X d S )Nc                 S   s   d S r"   r#   r$   r#   r#   r(   r     s    z=TestCapturedErrorHandling.test_error_in_overload.<locals>.barc                 S   s   | j  dd }|S )Nc                 S   s   d S r"   r#   r$   r#   r#   r(   impl  s    zNTestCapturedErrorHandling.test_error_in_overload.<locals>.ol_bar.<locals>.impl)Zsome_invalid_attr)r%   r   r#   r#   r(   r     s    z@TestCapturedErrorHandling.test_error_in_overload.<locals>.ol_barignore	new_style	old_styleZCAPTURED_ERRORSvoid(int64)c                    s    |  d S r"   r#   r$   r   r#   r(   rU     s    z=TestCapturedErrorHandling.test_error_in_overload.<locals>.fooz+object has no attribute 'some_invalid_attr')r	   warningscatch_warningssimplefilterr   NumbaPendingDeprecationWarningAttributeErrorr,   r   r+   r   r-   r.   r/   )r0   r   styleZ	err_classr1   rU   r2   r#   r   r(   test_error_in_overload  s     

z0TestCapturedErrorHandling.test_error_in_overloadZNUMBA_CAPTURED_ERRORSr   )envvarsc              
      s   t dtj dd  t dd }| tjJ}| tj td fdd}W 5 Q R X | 	d	t
|j d
 j W 5 Q R X d S )Nalwaysc                 S   s   d S r"   r#   r$   r#   r#   r(   r     s    zATestCapturedErrorHandling.test_old_style_deprecation.<locals>.barc                 S   s   t dd S NzInvalid attributer   r$   r#   r#   r(   r     s    zDTestCapturedErrorHandling.test_old_style_deprecation.<locals>.ol_barr   c                    s    |  d S r"   r#   r$   r   r#   r(   rU     s    zATestCapturedErrorHandling.test_old_style_deprecation.<locals>.foozMCode using Numba extension API maybe depending on 'old_style' error-capturingr   )r   r   r   r   r	   ZassertWarnsr+   r,   r   r-   r.   messager0   r   ZwarnsrU   r#   r   r(   test_old_style_deprecation  s    
z4TestCapturedErrorHandling.test_old_style_deprecationc              
      s   t dtj dd  t dd }t jddF}| tj td fd	d
}W 5 Q R X | j	t
|ddd W 5 Q R X d S )Nr   c                 S   s   d S r"   r#   r$   r#   r#   r(   r     s    zDTestCapturedErrorHandling.test_old_style_no_deprecation.<locals>.barc                 S   s   t dd S r   )r   r,   r$   r#   r#   r(   r     s    zGTestCapturedErrorHandling.test_old_style_no_deprecation.<locals>.ol_barTrecordr   c                    s    |  d S r"   r#   r$   r   r#   r(   rU     s    zDTestCapturedErrorHandling.test_old_style_no_deprecation.<locals>.foor    There should not be any warningsmsg)r   r   r   r   r	   r   r+   r,   r   assertEqualr   r   r#   r   r(   test_old_style_no_deprecation  s    
z7TestCapturedErrorHandling.test_old_style_no_deprecationr   c              
      s   t dtj dd  t dd }t jddD}| t td fd	d
}W 5 Q R X | j	t
|ddd W 5 Q R X d S )Nr   c                 S   s   d S r"   r#   r$   r#   r#   r(   r     s    zATestCapturedErrorHandling.test_new_style_no_warnings.<locals>.barc                 S   s   t dd S r   r   r$   r#   r#   r(   r     s    zDTestCapturedErrorHandling.test_new_style_no_warnings.<locals>.ol_barTr   r   c                    s    |  d S r"   r#   r$   r   r#   r(   rU   #  s    zATestCapturedErrorHandling.test_new_style_no_warnings.<locals>.foor   r   r   )r   r   r   r   r	   r   r+   r   r   r   r   r   r#   r   r(   test_new_style_no_warnings  s    
z4TestCapturedErrorHandling.test_new_style_no_warningsN)
r4   r5   r6   __doc__r   r   Zrun_test_in_subprocessr   r   r   r#   r#   r#   r(   r     s   

r   __main__)8r   numpyr8   rt   r   rr   r   r   r   r   r   Z
numba.corer   Znumba.experimentalr   Znumba.extendingr	   r
   r   r   Znumba.core.compilerr   Znumba.core.untyped_passesr   r   r   Znumba.core.typed_passesr   r   r   r   Znumba.core.compiler_machineryr   Znumba.core.types.functionsr   rk   Znumba.tests.supportr   r   r   r   r   r   ZunittestrT   DictemptyrY   r!   r7   r;   rd   r   r   r4   mainr#   r#   r#   r(   <module>   s2    z vk
