<s> from __future__ import print_function <EOL> import pypyjit <EOL> import sys <EOL> import time <EOL> import threading <EOL> import py <EOL> import tracebin <EOL> class TestHook ( object ) : <EOL> def thread_spawned ( self ) : <EOL> return threading . _counter > <NUM_LIT:0> <EOL> def test_record_loop ( self ) : <EOL> def f ( n ) : <EOL> while n > <NUM_LIT:0> : <EOL> n -= <NUM_LIT:1> <EOL> with tracebin . record ( ) as recorder : <EOL> f ( <NUM_LIT> ) <EOL> assert len ( recorder . traces ) == <NUM_LIT:1> <EOL> [ trace ] = recorder . traces <EOL> assert trace . root_file == __file__ <EOL> assert trace . root_function == "<STR_LIT:f>" <EOL> assert len ( trace . sections ) == <NUM_LIT:3> <EOL> assert [ s . label for s in trace . sections ] == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def test_record_stdout ( self ) : <EOL> with tracebin . record ( ) as recorder : <EOL> pass <EOL> assert recorder . stdout == "<STR_LIT>" <EOL> assert recorder . stderr == "<STR_LIT>" <EOL> with tracebin . record ( ) as recorder : <EOL> print ( None ) <EOL> assert recorder . stdout == "<STR_LIT>" <EOL> assert recorder . stderr == "<STR_LIT>" <EOL> with tracebin . record ( ) as recorder : <EOL> print ( None , file = sys . stderr ) <EOL> assert recorder . stdout == "<STR_LIT>" <EOL> assert recorder . stderr == "<STR_LIT>" <EOL> def test_record_build_options ( self ) : <EOL> with tracebin . record ( ) as recorder : <EOL> pass <EOL> assert recorder . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] == sys . pypy_translation_info [ "<STR_LIT>" ] <EOL> assert recorder . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] == sys . pypy_translation_info [ "<STR_LIT>" ] <EOL> assert recorder . options [ "<STR_LIT>" ] [ "<STR_LIT>" ] == sys . _mercurial [ <NUM_LIT:2> ] <EOL> @ py . test . mark . xfail <EOL> def test_record_jit_options ( self ) : <EOL> with tracebin . record ( ) as recorder : <EOL> pass <EOL> assert recorder . options [ "<STR_LIT>" ] . viewkeys ( ) == pypyjit . defaults . viewkeys ( ) <EOL> def test_record_runtime ( self ) : <EOL> with tracebin . record ( ) as recorder : <EOL> pass <EOL> assert recorder . runtime < <NUM_LIT:1> <EOL> def test_record_aborts ( self ) : <EOL> def f ( ) : <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> sys . exc_info ( ) <EOL> with tracebin . record ( ) as recorder : <EOL> f ( ) <EOL> assert len ( recorder . aborts ) == <NUM_LIT:1> <EOL> [ abort ] = recorder . aborts <EOL> assert abort . reason == "<STR_LIT>" <EOL> assert abort . pycode is f . __code__ <EOL> assert abort . lineno == <NUM_LIT> <EOL> def test_code_interpolation ( self ) : <EOL> def f ( ) : <EOL> i = <NUM_LIT> <EOL> while i > <NUM_LIT:0> : <EOL> i -= <NUM_LIT:1> <EOL> i <EOL> with tracebin . record ( ) as recorder : <EOL> f ( ) <EOL> [ trace ] = recorder . traces <EOL> [ entry , preamble , loop ] = trace . sections <EOL> assert len ( loop . chunks ) == <NUM_LIT:7> <EOL> assert loop . chunks [ <NUM_LIT:0> ] . get_op_names ( ) == [ "<STR_LIT:label>" ] <EOL> assert loop . chunks [ <NUM_LIT:1> ] . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" <EOL> ] <EOL> assert loop . chunks [ <NUM_LIT:1> ] . linenos == [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> assert loop . chunks [ <NUM_LIT:2> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:3> ] . sourcelines == [ <EOL> """<STR_LIT>""" <EOL> ] <EOL> assert loop . chunks [ <NUM_LIT:3> ] . linenos == [ <NUM_LIT> ] <EOL> assert loop . chunks [ <NUM_LIT:4> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:5> ] . sourcelines == [ <EOL> """<STR_LIT>""" <EOL> ] <EOL> if self . thread_spawned ( ) : <EOL> assert loop . chunks [ <NUM_LIT:6> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> assert loop . chunks [ <NUM_LIT:6> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def test_inlined_function ( self ) : <EOL> def f ( i ) : <EOL> return i - <NUM_LIT:1> <EOL> def main ( ) : <EOL> i = <NUM_LIT> <EOL> while i > <NUM_LIT:0> : <EOL> i = f ( i ) <EOL> i <EOL> with tracebin . record ( ) as recorder : <EOL> main ( ) <EOL> [ trace ] = recorder . traces <EOL> [ entry , preamble , loop ] = trace . sections <EOL> assert len ( loop . chunks ) == <NUM_LIT:10> <EOL> assert loop . chunks [ <NUM_LIT:0> ] . get_op_names ( ) == [ "<STR_LIT:label>" ] <EOL> assert loop . chunks [ <NUM_LIT:1> ] . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> ] <EOL> assert loop . chunks [ <NUM_LIT:2> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:3> ] . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> ] <EOL> assert loop . chunks [ <NUM_LIT:4> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:5> ] . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> ] <EOL> assert loop . chunks [ <NUM_LIT:5> ] . linenos == [ <NUM_LIT> , <NUM_LIT> ] <EOL> assert loop . chunks [ <NUM_LIT:6> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:7> ] . get_op_names ( ) == [ "<STR_LIT>" ] <EOL> assert loop . chunks [ <NUM_LIT:8> ] . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> ] <EOL> if self . thread_spawned ( ) : <EOL> assert loop . chunks [ <NUM_LIT:9> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> assert loop . chunks [ <NUM_LIT:9> ] . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def test_profile ( self ) : <EOL> def g ( ) : <EOL> pass <EOL> def f ( ) : <EOL> g ( ) <EOL> def main ( ) : <EOL> f ( ) <EOL> f ( ) <EOL> f ( ) <EOL> with tracebin . record ( ) as recorder : <EOL> main ( ) <EOL> assert recorder . calls is None <EOL> start = time . time ( ) <EOL> with tracebin . record ( profile = True ) as recorder : <EOL> main ( ) <EOL> assert len ( recorder . calls ) == <NUM_LIT:3> <EOL> [ call1 , call2 , call3 ] = recorder . calls <EOL> assert call1 . func_name == "<STR_LIT>" <EOL> assert len ( call1 . subcalls ) == <NUM_LIT:1> <EOL> assert call2 . func_name == "<STR_LIT>" <EOL> assert ( call2 . start_time - start ) < <NUM_LIT:1> <EOL> assert ( call2 . end_time - call2 . start_time ) < <NUM_LIT:1> <EOL> assert len ( call2 . subcalls ) == <NUM_LIT:3> <EOL> assert { c . func_name for c in call2 . subcalls } == { "<STR_LIT:f>" } <EOL> assert call2 . subcalls [ <NUM_LIT:1> ] . subcalls [ <NUM_LIT:0> ] . func_name == "<STR_LIT:g>" <EOL> def test_profile_exception ( self ) : <EOL> def f ( ) : <EOL> raise ValueError <EOL> def main ( ) : <EOL> try : <EOL> f ( ) <EOL> except ValueError : <EOL> pass <EOL> with tracebin . record ( profile = True ) as recorder : <EOL> main ( ) <EOL> [ _ , call , _ ] = recorder . calls <EOL> assert call . func_name == "<STR_LIT>" <EOL> [ subcall ] = call . subcalls <EOL> assert subcall . func_name == "<STR_LIT:f>" <EOL> def test_profile_builtin_exception ( self ) : <EOL> import math <EOL> with tracebin . record ( profile = True ) as recorder : <EOL> try : <EOL> math . sqrt ( "<STR_LIT:a>" ) <EOL> except TypeError : <EOL> pass <EOL> [ _ , call , _ ] = recorder . calls <EOL> assert call . func_name == "<STR_LIT>" <EOL> assert call . subcalls == [ ] <EOL> def test_trace_profilehook ( self ) : <EOL> def profile ( frame , event , arg ) : <EOL> pass <EOL> def sub ( x , y ) : <EOL> return x - y <EOL> def main ( ) : <EOL> i = <NUM_LIT> <EOL> orig_profile = sys . getprofile ( ) <EOL> sys . setprofile ( profile ) <EOL> try : <EOL> while i > <NUM_LIT:0> : <EOL> i = sub ( i , <NUM_LIT:1> ) <EOL> finally : <EOL> sys . setprofile ( orig_profile ) <EOL> with tracebin . record ( ) as recorder : <EOL> main ( ) <EOL> [ trace ] = recorder . traces <EOL> [ _ , _ , loop ] = trace . sections <EOL> py_profile_chunk = loop . chunks [ <NUM_LIT:9> ] <EOL> assert py_profile_chunk . sourcelines == [ <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> ] <EOL> op_profile_chunk = loop . chunks [ <NUM_LIT:10> ] <EOL> assert op_profile_chunk . get_op_names ( ) == [ "<STR_LIT>" , "<STR_LIT>" ] </s>
