o
    8Î®cb  ã                   @   s<   d dl mZmZ d dlmZmZ edƒG dd„ deƒƒZdS )é    )ÚerrorsÚir)Úregister_rewriteÚRewritezbefore-inferencec                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚRewriteConstRaisesa?  
    Rewrite IR statements of the kind `raise(value)`
    where `value` is the result of instantiating an exception with
    constant arguments
    into `static_raise(exception_type, constant args)`.

    This allows lowering in nopython mode, where one can't instantiate
    exception instances from runtime data.
    c                 C   s   t |tƒo	t|tƒS )N)Ú
isinstanceÚtypeÚ
issubclassÚ	Exception)ÚselfÚconst© r   úG/tmp/pip-target-vg8gfxp4/lib/python/numba/core/rewrites/static_raise.pyÚ_is_exception_type   s   z%RewriteConstRaises._is_exception_typec                 C   sx   t |tƒr#|  |d ¡sd}t ||d f |¡‚|d t|d ƒfS |  |¡r,|dfS t |tƒr4d}nd}t ||¡‚)z0
        Break down constant exception.
        r   z-Encountered unsupported exception constant %ré   NzDDirectly raising a string constant as an exception is not supported.z8Encountered unsupported constant type used for exception)r   Útupler   r   ÚUnsupportedErrorÚstr)r   r   ÚlocÚmsgr   r   r   Ú_break_constant   s   


z"RewriteConstRaises._break_constantc                 C   sÂ   i  | _ }i  | _}|| _| tjtjf¡D ]@}|jd u r"d\}}	n| |j¡}
|jj	}|  
|
|¡\}}	t|tjƒrA||	f||< qt|tjƒrN||	f||< qtd t|ƒ¡ƒ‚t|ƒt|ƒ dkS )N)NNzunexpected: {}r   )ÚraisesÚ	tryraisesÚblockÚ
find_instsr   ÚRaiseÚTryRaiseÚ	exceptionÚinfer_constantr   r   r   Ú
ValueErrorÚformatr   Úlen)r   Úfunc_irr   ÚtypemapÚ	calltypesr   r   ÚinstÚexc_typeÚexc_argsr   r   r   r   r   Úmatch'   s   



zRewriteConstRaises.matchc                 C   s–   | j  ¡ }| ¡  | j jD ];}|| jv r)| j| \}}t |||j¡}| |¡ q|| j	v rC| j	| \}}t 
|||j¡}| |¡ q| |¡ q|S )zC
        Rewrite all matching setitems as static_setitems.
        )r   ÚcopyÚclearÚbodyr   r   ÚStaticRaiser   Úappendr   ÚStaticTryRaise)r   Ú	new_blockr%   r&   r'   Únew_instr   r   r   Úapply>   s   


zRewriteConstRaises.applyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r1   r   r   r   r   r      s    
r   N)Ú
numba.corer   r   Únumba.core.rewritesr   r   r   r   r   r   r   Ú<module>   s    