<s> from . byteplay import ( <EOL> LOAD_ATTR , LOAD_CONST , ROT_TWO , DUP_TOP , CALL_FUNCTION , POP_TOP , LOAD_FAST , <EOL> BUILD_TUPLE , ROT_THREE , UNPACK_SEQUENCE , DUP_TOPX , BINARY_SUBSCR , GET_ITER , <EOL> LOAD_NAME , RETURN_VALUE <EOL> ) <EOL> class CodeTracer ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def load_attr ( self , obj , attr ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def call_function ( self , func , argtuple , argspec ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def binary_subscr ( self , obj , idx ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_iter ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CodeInverter ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def fail ( self ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def load_name ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> self . fail ( ) <EOL> def load_attr ( self , obj , attr , value ) : <EOL> """<STR_LIT>""" <EOL> self . fail ( ) <EOL> def call_function ( self , func , argtuple , argspec , value ) : <EOL> """<STR_LIT>""" <EOL> self . fail ( ) <EOL> def binary_subscr ( self , obj , idx , value ) : <EOL> """<STR_LIT>""" <EOL> self . fail ( ) <EOL> def inject_tracing ( codelist ) : <EOL> """<STR_LIT>""" <EOL> inserts = { } <EOL> for idx , ( op , op_arg ) in enumerate ( codelist ) : <EOL> if op == LOAD_ATTR : <EOL> code = [ <EOL> ( DUP_TOP , None ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_TWO , None ) , <EOL> ( LOAD_CONST , op_arg ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( POP_TOP , None ) , <EOL> ] <EOL> inserts [ idx ] = code <EOL> elif op == CALL_FUNCTION : <EOL> n_stack_args = ( op_arg & <NUM_LIT> ) + <NUM_LIT:2> * ( ( op_arg >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> code = [ <EOL> ( BUILD_TUPLE , n_stack_args ) , <EOL> ( DUP_TOPX , <NUM_LIT:2> ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_THREE , None ) , <EOL> ( LOAD_CONST , op_arg ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( POP_TOP , None ) , <EOL> ( UNPACK_SEQUENCE , n_stack_args ) , <EOL> ( BUILD_TUPLE , n_stack_args ) , <EOL> ( UNPACK_SEQUENCE , n_stack_args ) , <EOL> ] <EOL> inserts [ idx ] = code <EOL> elif op == BINARY_SUBSCR : <EOL> code = [ <EOL> ( DUP_TOPX , <NUM_LIT:2> ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_THREE , None ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( POP_TOP , None ) , <EOL> ] <EOL> inserts [ idx ] = code <EOL> elif op == GET_ITER : <EOL> code = [ <EOL> ( DUP_TOP , None ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_TWO , None ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( POP_TOP , None ) , <EOL> ] <EOL> inserts [ idx ] = code <EOL> new_code = [ ] <EOL> for idx , code_op in enumerate ( codelist ) : <EOL> if idx in inserts : <EOL> new_code . extend ( inserts [ idx ] ) <EOL> new_code . append ( code_op ) <EOL> return new_code <EOL> def inject_inversion ( codelist ) : <EOL> """<STR_LIT>""" <EOL> opcode , oparg = codelist [ - <NUM_LIT:2> ] <EOL> new_code = codelist [ : - <NUM_LIT:2> ] <EOL> if opcode == LOAD_NAME and len ( codelist ) == <NUM_LIT:3> : <EOL> new_code . extend ( [ <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( LOAD_CONST , oparg ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( RETURN_VALUE , None ) , <EOL> ] ) <EOL> elif opcode == LOAD_ATTR : <EOL> new_code . extend ( [ <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_TWO , None ) , <EOL> ( LOAD_CONST , oparg ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( RETURN_VALUE , None ) , <EOL> ] ) <EOL> elif opcode == CALL_FUNCTION : <EOL> n_stack_args = ( oparg & <NUM_LIT> ) + <NUM_LIT:2> * ( ( oparg >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> new_code . extend ( [ <EOL> ( BUILD_TUPLE , n_stack_args ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_THREE , None ) , <EOL> ( LOAD_CONST , oparg ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( RETURN_VALUE , None ) , <EOL> ] ) <EOL> elif opcode == BINARY_SUBSCR : <EOL> new_code . extend ( [ <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( LOAD_ATTR , '<STR_LIT>' ) , <EOL> ( ROT_THREE , None ) , <EOL> ( LOAD_FAST , '<STR_LIT>' ) , <EOL> ( CALL_FUNCTION , <NUM_LIT> ) , <EOL> ( RETURN_VALUE , None ) , <EOL> ] ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return new_code </s>
