<s> import os <EOL> from pypy . rlib . objectmodel import compute_unique_id <EOL> from pypy . jit . metainterp . resoperation import rop <EOL> from pypy . jit . metainterp . history import Const , ConstInt , Box , BoxInt , ConstAddr , ConstFloat , BoxFloat , AbstractFailDescr <EOL> from pypy . rlib . streamio import open_file_as_stream <EOL> class Logger ( object ) : <EOL> def __init__ ( self , ts , guard_number = False ) : <EOL> self . log_stream = None <EOL> self . ts = ts <EOL> self . guard_number = guard_number <EOL> def create_log ( self , extension = '<STR_LIT>' ) : <EOL> if self . log_stream is not None : <EOL> return self . log_stream <EOL> s = os . environ . get ( '<STR_LIT>' ) <EOL> if not s : <EOL> return None <EOL> s += extension <EOL> try : <EOL> self . log_stream = open_file_as_stream ( s , '<STR_LIT:w>' ) <EOL> except OSError : <EOL> os . write ( <NUM_LIT:2> , "<STR_LIT>" ) <EOL> return None <EOL> return self . log_stream <EOL> def log_loop ( self , inputargs , operations , number = <NUM_LIT:0> , type = None ) : <EOL> if self . log_stream is None : <EOL> return <EOL> if type is not None : <EOL> self . log_stream . write ( "<STR_LIT>" % ( number , <EOL> type , <EOL> len ( operations ) ) ) <EOL> self . _log_operations ( inputargs , operations , { } ) <EOL> def log_bridge ( self , inputargs , operations , number = - <NUM_LIT:1> ) : <EOL> if self . log_stream is None : <EOL> return <EOL> if number != - <NUM_LIT:1> : <EOL> self . log_stream . write ( "<STR_LIT>" % ( number , <EOL> len ( operations ) ) ) <EOL> self . _log_operations ( inputargs , operations , { } ) <EOL> def repr_of_descr ( self , descr ) : <EOL> return descr . repr_of_descr ( ) <EOL> def repr_of_arg ( self , memo , arg ) : <EOL> try : <EOL> mv = memo [ arg ] <EOL> except KeyError : <EOL> mv = len ( memo ) <EOL> memo [ arg ] = mv <EOL> if isinstance ( arg , ConstInt ) : <EOL> return str ( arg . value ) <EOL> elif isinstance ( arg , BoxInt ) : <EOL> return '<STR_LIT:i>' + str ( mv ) <EOL> elif isinstance ( arg , self . ts . ConstRef ) : <EOL> return '<STR_LIT>' + str ( mv ) + '<STR_LIT:)>' <EOL> elif isinstance ( arg , self . ts . BoxRef ) : <EOL> return '<STR_LIT:p>' + str ( mv ) <EOL> elif isinstance ( arg , ConstFloat ) : <EOL> return str ( arg . value ) <EOL> elif isinstance ( arg , BoxFloat ) : <EOL> return '<STR_LIT:f>' + str ( mv ) <EOL> elif isinstance ( arg , self . ts . ConstAddr ) : <EOL> return '<STR_LIT>' + str ( mv ) + '<STR_LIT:)>' <EOL> else : <EOL> return '<STR_LIT:?>' <EOL> def _log_operations ( self , inputargs , operations , memo ) : <EOL> if inputargs is not None : <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( [ self . repr_of_arg ( memo , arg ) for arg in inputargs ] ) <EOL> self . log_stream . write ( '<STR_LIT:[>' + args + '<STR_LIT>' ) <EOL> for i in range ( len ( operations ) ) : <EOL> op = operations [ i ] <EOL> if op . opnum == rop . DEBUG_MERGE_POINT : <EOL> loc = op . args [ <NUM_LIT:0> ] . _get_str ( ) <EOL> self . log_stream . write ( "<STR_LIT>" % ( loc , ) ) <EOL> continue <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( [ self . repr_of_arg ( memo , arg ) for arg in op . args ] ) <EOL> if op . result is not None : <EOL> res = self . repr_of_arg ( memo , op . result ) + "<STR_LIT>" <EOL> else : <EOL> res = "<STR_LIT>" <EOL> is_guard = op . is_guard ( ) <EOL> if op . descr is not None : <EOL> descr = op . descr <EOL> if is_guard and self . guard_number : <EOL> assert isinstance ( descr , AbstractFailDescr ) <EOL> r = "<STR_LIT>" % descr . get_index ( ) <EOL> else : <EOL> r = self . repr_of_descr ( descr ) <EOL> args += '<STR_LIT>' + r <EOL> if is_guard and op . fail_args is not None : <EOL> fail_args = '<STR_LIT>' + "<STR_LIT:U+002CU+0020>" . join ( [ self . repr_of_arg ( memo , arg ) <EOL> for arg in op . fail_args ] ) + '<STR_LIT:]>' <EOL> else : <EOL> fail_args = '<STR_LIT>' <EOL> self . log_stream . write ( res + op . getopname ( ) + <EOL> '<STR_LIT:(>' + args + '<STR_LIT:)>' + fail_args + '<STR_LIT:\n>' ) <EOL> self . log_stream . flush ( ) </s>
