o
    8ήc=                     @   s  d 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  m  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, ddl-Z-g dZ.ej/0eeZ1G dd de-j2Z3G dd de-j2Z4G dd de-j2Z5G dd de-j2Z6G dd de-j2Z7G dd de+e-j2Z8G dd de+e-j2Z9e:dkre-;  dS 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_scipy)            c                   @      e Zd Zdd ZdS )TestErrorHandlingBeforeLoweringc              	   C   sh   dd }t tfD ])}| tj}||d W d    n1 s!w   Y  d}| |t|j qd S )Nc                    s   | d   fdd}|S )z return the closure r   c                    s   |   S N xzr"   E/tmp/pip-target-vg8gfxp4/lib/python/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)   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           r    c                   @   r   )TestUnsupportedReportingc                 C   sX   t dd }| tj}|  W d    n1 sw   Y  d}| |t|j d S )Nc                   S   s   t g d d S )N)r   r   r   )np
asanyarrayr"   r"   r"   r'   r)   6   s   zFTestUnsupportedReporting.test_unsupported_numpy_function.<locals>.funcz4Use of unsupported NumPy function 'numpy.asanyarray'r   r*   r   r+   r,   r-   r.   )r/   r)   r1   r2   r"   r"   r'   test_unsupported_numpy_function4   s   
z8TestUnsupportedReporting.test_unsupported_numpy_functionN)r4   r5   r6   r<   r"   r"   r"   r'   r8   2   r7   r8   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'   fnH      zHTestMiscErrorHandling.test_use_of_exception_for_flow_control.<locals>.fng      ?)dtype)r   r9   arrayfloat64)r/   r@   ar"   r"   r'   &test_use_of_exception_for_flow_controlC   s   
z<TestMiscErrorHandling.test_use_of_exception_for_flow_controlc              	   C   sd   dd }dd }||fD ]#}|  t}|  W d    n1 s!w   Y  | dt|j qd S )Nc                   S      t d)Ntest_string	Exceptionr"   r"   r"   r'   foo_commentedU   rA   z_TestMiscErrorHandling.test_commented_func_definition_is_not_a_definition.<locals>.foo_commentedc                   S   rG   )z> def docstring containing def might match function definition!rH   rI   r"   r"   r"   r'   foo_docstringY   rA   z_TestMiscErrorHandling.test_commented_func_definition_is_not_a_definition.<locals>.foo_docstringrH   )r*   rJ   r,   r-   r.   )r/   rK   rL   r)   r1   r"   r"   r'   2test_commented_func_definition_is_not_a_definitionO   s   zHTestMiscErrorHandling.test_commented_func_definition_is_not_a_definitionc                 C   sx   G dd dt }t|ddd }| tj}|tddg W d    n1 s*w   Y  d}| |t|j d S )	Nc                   @   r   )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 )	Nbad_DCE_pipelinezanalyzing bytecodezfix up argszprocessing IRDCEznopython frontendznative loweringznopython mode backend)
r   add_passr   r   r   r   r   r   r   finalize)r/   namepmr"   r"   r'   define_pipelinesf   s   zWTestMiscErrorHandling.test_use_of_ir_unknown_loc.<locals>.TestPipeline.define_pipelinesN)r4   r5   r6   rT   r"   r"   r"   r'   TestPipelinee       rU   )pipeline_classc                 S   s   dS )Nr   r"   rE   r"   r"   r'   fv      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.   )r/   rU   rY   r1   r2   r"   r"   r'   test_use_of_ir_unknown_locc   s   
z0TestMiscErrorHandling.test_use_of_ir_unknown_locc                 C   sZ   |  tj}|  W d    n1 sw   Y  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.   )r/   r)   r1   r2   exr"   r"   r'   check_write_to_globals   s   z,TestMiscErrorHandling.check_write_to_globalsc                 C      t dd }| | d S )Nc                   S      dt d< d S Nr?   r   )_global_listr"   r"   r"   r'   foo      zMTestMiscErrorHandling.test_handling_of_write_to_reflected_global.<locals>.foor   r^   r/   rc   r"   r"   r'   *test_handling_of_write_to_reflected_global      
z@TestMiscErrorHandling.test_handling_of_write_to_reflected_globalc                 C   r_   )Nc                   S   r`   ra   )_global_dictr"   r"   r"   r'   rc      rd   zNTestMiscErrorHandling.test_handling_of_write_to_typed_dict_global.<locals>.foore   rf   r"   r"   r'   +test_handling_of_write_to_typed_dict_global   rh   zATestMiscErrorHandling.test_handling_of_write_to_typed_dict_globalc                 C   s^   t dddd }| tj}|  W d    n1 sw   Y  d}| |t|j d S )NT)parallelc                  S   s   t dD ]} qd S r>   )prange)ir"   r"   r'   rc      s   zPTestMiscErrorHandling.test_handling_forgotten_numba_internal_import.<locals>.foozC'prange' looks like a Numba internal function, has it been importedr;   )r/   rc   r1   r2   r"   r"   r'   -test_handling_forgotten_numba_internal_import   s   
zCTestMiscErrorHandling.test_handling_forgotten_numba_internal_importc              	   C   sl   dd }d}t ddtfD ]&}| tj}||  W d    n1 s%w   Y  | |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'   rc      s   zQTestMiscErrorHandling.test_handling_unsupported_generator_expression.<locals>.fooz-The use of yield in a closure is unsupported.T)forceobj)r   r   r*   r   UnsupportedErrorr,   r-   r.   )r/   rc   r2   decr1   r"   r"   r'   .test_handling_unsupported_generator_expression   s   zDTestMiscErrorHandling.test_handling_unsupported_generator_expressionc                 C   sX   t dd }d}| tj}|  W d    n1 sw   Y  | |t|j d S )Nc                   S   s   t S r!   rX   r"   r"   r"   r'   rc      rZ   zCTestMiscErrorHandling.test_handling_undefined_variable.<locals>.fooz"NameError: name 'a' is not definedr;   )r/   rc   r2   r1   r"   r"   r'    test_handling_undefined_variable   s   
z6TestMiscErrorHandling.test_handling_undefined_variableN)r4   r5   r6   rF   rM   r\   r^   rg   rj   r   rn   ru   rv   r"   r"   r"   r'   r=   A   s    
r=   c                   @   r   )"TestConstantInferenceErrorHandlingc                 C   sr   t dd }| tj}|dd W d    n1 sw   Y  d}d}| |t|j | |t|j d S )Nc                 S   s   | |kr
t d| || | S )NzEqual numbers: %i %irI   )rE   br"   r"   r'   problem   s   zDTestConstantInferenceErrorHandling.test_basic_error.<locals>.problemr   r   z3Constant inference not possible for: arg(0, name=a)z-raise Exception("Equal numbers: %i %i", a, b))r   r*   r   ConstantInferenceErrorr,   r-   r.   )r/   ry   r1   msg1msg2r"   r"   r'   test_basic_error   s   
z3TestConstantInferenceErrorHandling.test_basic_errorN)r4   r5   r6   r}   r"   r"   r"   r'   rw      r7   rw   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ejddd Zdd ZdS )TestErrorMessagesc                    s   ddd  t  fdd}t fdd}| tj}|  W d    n1 s,w   Y  t|j}| td 	 d	 | | | d S )
Nspecific_reasonc                   S      d S r!   r"   r"   r"   r"   r'   rc         z2TestErrorMessages.test_specific_error.<locals>.fooc                      s
   t  r!   )r   NumbaValueErrorr"   )given_reasonr"   r'   ol_foo      
z5TestErrorMessages.test_specific_error.<locals>.ol_fooc                      
      d S r!   r"   r"   rc   r"   r'   call_foo   r   z7TestErrorMessages.test_specific_error.<locals>.call_foospecific_errorr   )
r	   r   r*   r   r+   r-   r.   r,   error_reasons
splitlinesr/   r   r   r1   excstrr"   )rc   r   r'   test_specific_error   s   
z%TestErrorMessages.test_specific_errorc                    st   dd  t  dd }t fdd}| tj}|  W d    n1 s(w   Y  t|j}| d| d S )Nc                   S   r   r!   r"   r"   r"   r"   r'   rc      r   z2TestErrorMessages.test_no_match_error.<locals>.fooc                   S   r   r!   r"   r"   r"   r"   r'   r      rZ   z5TestErrorMessages.test_no_match_error.<locals>.ol_fooc                      r   r!   r"   r"   r   r"   r'   r      r   z7TestErrorMessages.test_no_match_error.<locals>.call_foozNo match)r	   r   r*   r   r+   r-   r.   r,   r   r"   r   r'   test_no_match_error   s   

z%TestErrorMessages.test_no_match_errorc                 C   s   t dd }| tj}|  W d   n1 sw   Y  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)r9   linalgsvdr"   r"   r"   r'   rc      s   zDTestErrorMessages.test_error_function_source_is_correct.<locals>.fooNr   r   numbar9   z	linalg.pyz'Overload in function 'svd_impl': File: :)r   r*   r   r+   r-   r.   r,   r   r   ospathjoinformat)r/   rc   r1   r   expected_filer2   r"   r"   r'   %test_error_function_source_is_correct   s   

z7TestErrorMessages.test_error_function_source_is_correctc                 C   sd   t dd }| tj}|  W d    n1 sw   Y  t|j}| d| | d| d S )Nc                   S   s   dd S )NrE   r   r"   r"   r"   r"   r'   rc     rA   z<TestErrorMessages.test_concrete_template_source.<locals>.foozOverload of function 'add'	No match.r   r*   r   r+   r-   r.   r,   r/   rc   r1   r   r"   r"   r'   test_concrete_template_source  s   

z/TestErrorMessages.test_concrete_template_sourcec                 C   X   t dd }| tj}|  W d    n1 sw   Y  t|j}| d| d S )Nc                   S   s   t dS Nr   )lenr"   r"   r"   r'   rc     rA   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   r   )Nc                   S   s
   t dS r   )r9   angler"   r"   r"   r'   rc   *  r   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 d    n1 s(w   Y  t|j}| d| tj	
dd	d
}d| d}| ||| | d| d S )Nc                 S   r   r!   r"   r#   r"   r"   r'   bar7  r   zDTestErrorMessages.test_overloadfunction_template_source.<locals>.barc                 S   r   r!   r"   r#   r"   r"   r'   ol_bar:  rZ   zGTestErrorMessages.test_overloadfunction_template_source.<locals>.ol_barc                      s    dS r   r"   r"   r   r"   r'   rc   >  rA   zDTestErrorMessages.test_overloadfunction_template_source.<locals>.fooz
<numerous>r   testsztest_errorhandling.pyz"Overload of function 'bar': File: r   r   )r	   r   r*   r   r+   r-   r.   assertNotInr   r   r   r,   r   )r/   r   rc   r1   r   r   expected_olr"   r   r'   %test_overloadfunction_template_source5  s    


z7TestErrorMessages.test_overloadfunction_template_sourcec                    s   d dt  fddtfdd}| tj}|  W d    n1 s)w   Y  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   r   r!   r"   )contextbuilder	signatureargsr"   r"   r'   codegen^  r   zSTestErrorMessages.test_intrinsic_template_source.<locals>.myintrin.<locals>.codegen)
isinstancer   IntegerLiteralr   RequireLiteralValuendimr   intp)	typingctxr$   arrsigr   )given_reason1given_reason2r"   r'   myintrinT  s   


zBTestErrorMessages.test_intrinsic_template_source.<locals>.myintrinc                     s   t d}  d|  d S )N)r   r   r   )r9   zeros)r   )r   r"   r'   call_intrinb  s   
zETestErrorMessages.test_intrinsic_template_source.<locals>.call_intrinr   r   zIntrinsic in function)
r
   r   r*   r   r+   r-   r.   r,   r   r   )r/   r   r1   r   r"   )r   r   r   r'   test_intrinsic_template_sourceO  s   
z0TestErrorMessages.test_intrinsic_template_sourcec                 C   l   t tjddd }tdd }| tj}|  W d    n1 s$w   Y  t|j}| 	d| d S )N
isnonsensec                 S   r   r!   r"   r/   r"   r"   r'   ol_unicode_isnonsenses  rZ   zTTestErrorMessages.test_overloadmethod_template_source.<locals>.ol_unicode_isnonsensec                   S   s   d   d S Nabcr   r"   r"   r"   r'   rc   w  rd   zBTestErrorMessages.test_overloadmethod_template_source.<locals>.foo,Overload of function 'ol_unicode_isnonsense')
r   r   UnicodeTyper   r*   r   r+   r-   r.   r,   r/   r   rc   r1   r   r"   r"   r'   #test_overloadmethod_template_sourcep     



z5TestErrorMessages.test_overloadmethod_template_sourcec                 C   r   )Nr   c                 S   r   r!   r"   r   r"   r"   r'   r     rZ   zWTestErrorMessages.test_overloadattribute_template_source.<locals>.ol_unicode_isnonsensec                   S   s
   dj  d S r   r   r"   r"   r"   r'   rc     r   zETestErrorMessages.test_overloadattribute_template_source.<locals>.foor   )
r   r   r   r   r*   r   r+   r-   r.   r,   r   r"   r"   r'   &test_overloadattribute_template_source  r   z8TestErrorMessages.test_overloadattribute_template_sourcec                    sh   ddl m  t fdd}| tj}|  W d    n1 s"w   Y  t|j}| d| d S )Nr   c_cosc                          d d S NrE   r"   r"   r   r"   r'   rc     rd   zMTestErrorMessages.test_external_function_pointer_template_source.<locals>.foo.Type Restricted Function in function 'unknown')	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_sourcezCFFI not supportedc                    sv   ddl m} |  |j t fdd}| tj}|  W d    n1 s)w   Y  t|j	}| 
d| d S )Nr   )cffi_usecasesc                      r   r   r"   r"   r)   r"   r'   rc     rd   zITestErrorMessages.test_cffi_function_pointer_template_source.<locals>.foor   )numba.testsr   initcffi_cosr   r*   r   r+   r-   r.   r,   )r/   modrc   r1   r   r"   r   r'   *test_cffi_function_pointer_template_source  s   
z<TestErrorMessages.test_cffi_function_pointer_template_sourcec                 C   s   t jG dd dtj}G dd dt j}t ||ddg | tj}|ddd	 W d    n1 s5w   Y  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                   @   r   )z7TestErrorMessages.test_missing_source.<locals>.Particlec                 S   s   t j| |S r!   )r   StructRefProxy__new__)clsposmassr"   r"   r'   r        z?TestErrorMessages.test_missing_source.<locals>.Particle.__new__N)r4   r5   r6   r   r"   r"   r"   r'   Particle  rV   r   r   r   r   r   )r   r   z#missing a required argument: 'mass')r   registerr   	StructRefr   define_proxyr*   r   r+   r-   r.   r,   )r/   r   r   r1   r   r"   r"   r'   test_missing_source  s   
z%TestErrorMessages.test_missing_sourceN)r4   r5   r6   r   r   r   r   r   r   r   r   r   r   r   r   unittest
skipUnlesscffi_support	SUPPORTEDr   r   r"   r"   r"   r'   r~      s     
!
r~   c                   @   r   )"TestDeveloperSpecificErrorMessagesc              	   C   s   dd }t dd$ | tj}td| W d    n1 s!w   Y  W d    n1 s0w   Y  t|j}| d| d S )Nc                 S   s   |  d d S )N)maxr#   r"   r"   r'   rc     r   zPTestDeveloperSpecificErrorMessages.test_bound_function_error_string.<locals>.fooDEVELOPER_MODEr   zvoid(int64[:,:])zargs not supported)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     r7   r   c                   @   s   e Zd ZdZ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 }dtfdtjffD ]C\}}td|2 | |}td fd	d
}W d    n1 s9w   Y  d}| |t|j	 W d    n1 sSw   Y  qd S )Nc                 S   r   r!   r"   r#   r"   r"   r'   r     r   z=TestCapturedErrorHandling.test_error_in_overload.<locals>.barc                 S   s   | j  dd }|S )Nc                 S   r   r!   r"   r#   r"   r"   r'   impl  r   zNTestCapturedErrorHandling.test_error_in_overload.<locals>.ol_bar.<locals>.impl)some_invalid_attr)r$   r   r"   r"   r'   r     s   z@TestCapturedErrorHandling.test_error_in_overload.<locals>.ol_bar	new_style	old_styleCAPTURED_ERRORSzvoid(int64)c                    s    |  d S r!   r"   r#   r   r"   r'   rc     rd   z=TestCapturedErrorHandling.test_error_in_overload.<locals>.fooz+object has no attribute 'some_invalid_attr')
r	   AttributeErrorr   r+   r   r*   r   r,   r-   r.   )r/   r   style	err_classr1   rc   r2   r"   r   r'   test_error_in_overload  s    
z0TestCapturedErrorHandling.test_error_in_overloadN)r4   r5   r6   __doc__r   r"   r"   r"   r'   r     s    r   __main__)<r   numpyr9   r   r   r   r   r   r   r   
numba.corer   numba.core.typing.cffi_utilscoretyping
cffi_utilsr   numba.experimentalr   numba.extendingr	   r
   r   r   numba.core.compilerr   numba.core.untyped_passesr   r   r   numba.core.typed_passesr   r   r   r   numba.core.compiler_machineryr   numba.core.types.functionsr   r   numba.tests.supportr   r   r   r   r   rb   Dictemptyri   TestCaser    r8   r=   rw   r~   r   r   r4   mainr"   r"   r"   r'   <module>   s8    z v