<s> from numba import ir <EOL> from . import register_rewrite , Rewrite <EOL> @ register_rewrite ( '<STR_LIT>' ) <EOL> class RewriteConstRaises ( Rewrite ) : <EOL> """<STR_LIT>""" <EOL> def _is_exception_type ( self , const ) : <EOL> return isinstance ( const , type ) and issubclass ( const , Exception ) <EOL> def _break_constant ( self , interp , const ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( const , BaseException ) : <EOL> return const . __class__ , const . args <EOL> elif self . _is_exception_type ( const ) : <EOL> return const , None <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> % ( const , ) ) <EOL> def match ( self , interp , block , typemap , calltypes ) : <EOL> self . raises = raises = { } <EOL> self . block = block <EOL> for inst in block . find_insts ( ir . Raise ) : <EOL> if inst . exception is None : <EOL> exc_type , exc_args = None , None <EOL> else : <EOL> const = interp . infer_constant ( inst . exception ) <EOL> exc_type , exc_args = self . _break_constant ( interp , const ) <EOL> raises [ inst ] = exc_type , exc_args <EOL> return len ( raises ) > <NUM_LIT:0> <EOL> def apply ( self ) : <EOL> """<STR_LIT>""" <EOL> new_block = self . block . copy ( ) <EOL> new_block . clear ( ) <EOL> for inst in self . block . body : <EOL> if inst in self . raises : <EOL> exc_type , exc_args = self . raises [ inst ] <EOL> new_inst = ir . StaticRaise ( exc_type , exc_args , inst . loc ) <EOL> new_block . append ( new_inst ) <EOL> else : <EOL> new_block . append ( inst ) <EOL> return new_block </s>
