<s> import os , sys <EOL> from pypy . rlib . objectmodel import we_are_translated <EOL> AUTO_DEBUG = os . getenv ( '<STR_LIT>' ) <EOL> RECORD_INTERPLEVEL_TRACEBACK = True <EOL> class OperationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , w_type , w_value , tb = None ) : <EOL> if w_type is None : <EOL> from pypy . tool . error import FlowingError <EOL> raise FlowingError ( w_value ) <EOL> self . w_type = w_type <EOL> self . w_value = w_value <EOL> self . application_traceback = tb <EOL> if not we_are_translated ( ) : <EOL> self . debug_excs = [ ] <EOL> def clear ( self , space ) : <EOL> self . w_type = space . w_None <EOL> self . w_value = space . w_None <EOL> self . application_traceback = None <EOL> if not we_are_translated ( ) : <EOL> del self . debug_excs [ : ] <EOL> def match ( self , space , w_check_class ) : <EOL> "<STR_LIT>" <EOL> return space . exception_match ( self . w_type , w_check_class ) <EOL> def async ( self , space ) : <EOL> "<STR_LIT>" <EOL> return ( self . match ( space , space . w_SystemExit ) or <EOL> self . match ( space , space . w_KeyboardInterrupt ) ) <EOL> def __str__ ( self ) : <EOL> "<STR_LIT>" <EOL> return '<STR_LIT>' % ( self . w_type , self . w_value ) <EOL> def errorstr ( self , space ) : <EOL> "<STR_LIT>" <EOL> if space is None : <EOL> exc_typename = str ( self . w_type ) <EOL> exc_value = str ( self . w_value ) <EOL> else : <EOL> w = space . wrap <EOL> if space . is_w ( space . type ( self . w_type ) , space . w_str ) : <EOL> exc_typename = space . str_w ( self . w_type ) <EOL> else : <EOL> exc_typename = space . str_w ( <EOL> space . getattr ( self . w_type , w ( '<STR_LIT>' ) ) ) <EOL> if space . is_w ( self . w_value , space . w_None ) : <EOL> exc_value = "<STR_LIT>" <EOL> else : <EOL> try : <EOL> exc_value = space . str_w ( space . str ( self . w_value ) ) <EOL> except OperationError : <EOL> exc_value = "<STR_LIT>" <EOL> if not exc_value : <EOL> return exc_typename <EOL> else : <EOL> return '<STR_LIT>' % ( exc_typename , exc_value ) <EOL> def getframe ( self ) : <EOL> "<STR_LIT>" <EOL> if self . application_traceback : <EOL> return self . application_traceback . frame <EOL> else : <EOL> return None <EOL> def record_interpreter_traceback ( self ) : <EOL> """<STR_LIT>""" <EOL> if not we_are_translated ( ) : <EOL> if RECORD_INTERPLEVEL_TRACEBACK : <EOL> self . debug_excs . append ( sys . exc_info ( ) ) <EOL> def print_application_traceback ( self , space , file = None ) : <EOL> "<STR_LIT>" <EOL> if file is None : file = sys . stderr <EOL> self . print_app_tb_only ( file ) <EOL> print >> file , self . errorstr ( space ) <EOL> def print_app_tb_only ( self , file ) : <EOL> "<STR_LIT>" <EOL> tb = self . application_traceback <EOL> if tb : <EOL> import linecache <EOL> print >> file , "<STR_LIT>" <EOL> while tb is not None : <EOL> co = tb . frame . pycode <EOL> lineno = tb . lineno <EOL> fname = co . co_filename <EOL> if fname . startswith ( '<STR_LIT>' ) : <EOL> lines = fname . split ( '<STR_LIT:\n>' ) <EOL> fname = lines [ <NUM_LIT:0> ] . strip ( ) <EOL> try : <EOL> l = lines [ lineno ] <EOL> except IndexError : <EOL> l = '<STR_LIT>' <EOL> else : <EOL> l = linecache . getline ( fname , lineno ) <EOL> print >> file , "<STR_LIT>" % fname , <EOL> print >> file , "<STR_LIT>" , lineno , "<STR_LIT>" , co . co_name <EOL> if l : <EOL> if l . endswith ( '<STR_LIT:\n>' ) : <EOL> l = l [ : - <NUM_LIT:1> ] <EOL> l = "<STR_LIT:U+0020>" + l . lstrip ( ) <EOL> print >> file , l <EOL> tb = tb . next <EOL> def print_detailed_traceback ( self , space = None , file = None ) : <EOL> """<STR_LIT>""" <EOL> import traceback , cStringIO <EOL> if file is None : file = sys . stderr <EOL> f = cStringIO . StringIO ( ) <EOL> for i in range ( len ( self . debug_excs ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> print >> f , "<STR_LIT>" <EOL> traceback . print_tb ( self . debug_excs [ i ] [ <NUM_LIT:2> ] , file = f ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> debug_print ( '<STR_LIT>' . join ( [ '<STR_LIT>' + line for line in f . readlines ( ) ] ) , file ) <EOL> if self . debug_excs : <EOL> from pypy . tool import tb_server <EOL> tb_server . publish_exc ( self . debug_excs [ - <NUM_LIT:1> ] ) <EOL> self . print_app_tb_only ( file ) <EOL> print >> file , '<STR_LIT>' , self . errorstr ( space ) <EOL> if AUTO_DEBUG : <EOL> import debug <EOL> debug . fire ( self ) <EOL> def normalize_exception ( self , space ) : <EOL> """<STR_LIT>""" <EOL> w_type = self . w_type <EOL> w_value = self . w_value <EOL> if space . full_exceptions : <EOL> while space . is_true ( space . isinstance ( w_type , space . w_tuple ) ) : <EOL> w_type = space . getitem ( w_type , space . wrap ( <NUM_LIT:0> ) ) <EOL> if ( space . abstract_isclass_w ( w_type ) and <EOL> is_valid_exception_class ( space , w_type ) ) : <EOL> if space . is_w ( w_value , space . w_None ) : <EOL> w_value = space . call_function ( w_type ) <EOL> w_type = space . abstract_getclass ( w_value ) <EOL> else : <EOL> w_valuetype = space . abstract_getclass ( w_value ) <EOL> if space . abstract_issubclass_w ( w_valuetype , w_type ) : <EOL> w_type = w_valuetype <EOL> else : <EOL> if space . full_exceptions and space . is_true ( <EOL> space . isinstance ( w_value , space . w_tuple ) ) : <EOL> w_value = space . call ( w_type , w_value ) <EOL> else : <EOL> w_value = space . call_function ( w_type , w_value ) <EOL> w_type = space . abstract_getclass ( w_value ) <EOL> elif space . full_exceptions and space . is_w ( space . type ( w_type ) , <EOL> space . w_str ) : <EOL> space . warn ( "<STR_LIT>" , <EOL> space . w_DeprecationWarning ) <EOL> else : <EOL> w_inst = w_type <EOL> w_instclass = space . abstract_getclass ( w_inst ) <EOL> if not is_valid_exception_class ( space , w_instclass ) : <EOL> instclassname = w_instclass . getname ( space , '<STR_LIT:?>' ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( instclassname , ) ) <EOL> raise OperationError ( space . w_TypeError , space . wrap ( msg ) ) <EOL> if not space . is_w ( w_value , space . w_None ) : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> w_value = w_inst <EOL> w_type = w_instclass <EOL> self . w_type = w_type <EOL> self . w_value = w_value <EOL> def write_unraisable ( self , space , where , w_object = None ) : <EOL> if w_object is None : <EOL> objrepr = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> objrepr = space . str_w ( space . repr ( w_object ) ) <EOL> except OperationError : <EOL> objrepr = '<STR_LIT:?>' <EOL> msg = '<STR_LIT>' % ( self . errorstr ( space ) , <EOL> where , objrepr ) <EOL> try : <EOL> space . call_method ( space . sys . get ( '<STR_LIT>' ) , '<STR_LIT>' , space . wrap ( msg ) ) <EOL> except OperationError : <EOL> pass <EOL> def is_valid_exception_class ( space , w_type ) : <EOL> """<STR_LIT>""" <EOL> if not space . full_exceptions : <EOL> return True <EOL> try : <EOL> return space . is_true ( <EOL> space . issubtype ( w_type , space . w_BaseException ) ) <EOL> except OperationError , e : <EOL> if not e . match ( space , space . w_TypeError ) : <EOL> raise <EOL> return True <EOL> from pypy . tool . ansi_print import ansi_print <EOL> def debug_print ( text , file = None , newline = True ) : <EOL> ansi_print ( text , esc = "<STR_LIT>" , file = file , newline = newline ) <EOL> try : <EOL> WindowsError <EOL> except NameError : <EOL> _WINDOWS = False <EOL> else : <EOL> _WINDOWS = True <EOL> def wrap_windowserror ( space , e ) : <EOL> from pypy . rlib import rwin32 <EOL> winerror = e . winerror <EOL> try : <EOL> msg = rwin32 . FormatError ( winerror ) <EOL> except ValueError : <EOL> msg = '<STR_LIT>' % winerror <EOL> exc = space . w_WindowsError <EOL> w_error = space . call_function ( exc , <EOL> space . wrap ( winerror ) , <EOL> space . wrap ( msg ) ) <EOL> return OperationError ( exc , w_error ) <EOL> def wrap_oserror ( space , e , exception_name = '<STR_LIT>' ) : <EOL> assert isinstance ( e , OSError ) <EOL> if _WINDOWS and isinstance ( e , WindowsError ) : <EOL> return wrap_windowserror ( space , e ) <EOL> errno = e . errno <EOL> try : <EOL> msg = os . strerror ( errno ) <EOL> except ValueError : <EOL> msg = '<STR_LIT>' % errno <EOL> exc = getattr ( space , exception_name ) <EOL> w_error = space . call_function ( exc , <EOL> space . wrap ( errno ) , <EOL> space . wrap ( msg ) ) <EOL> return OperationError ( exc , w_error ) <EOL> wrap_oserror . _annspecialcase_ = '<STR_LIT>' </s>
