<s> from pypy . conftest import gettestobjspace , option <EOL> from pypy . tool . udir import udir <EOL> import py <EOL> from py . test import skip <EOL> import sys , os <EOL> class BytecodeTrace ( list ) : <EOL> def get_opnames ( self , prefix = "<STR_LIT>" ) : <EOL> return [ op . getopname ( ) for op in self <EOL> if op . getopname ( ) . startswith ( prefix ) ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . bytecode , list . __repr__ ( self ) ) <EOL> ZERO_OP_BYTECODES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class PyPyCJITTests ( object ) : <EOL> def run_source ( self , source , * testcases ) : <EOL> source = py . code . Source ( source ) <EOL> filepath = self . tmpdir . join ( '<STR_LIT>' % self . counter ) <EOL> logfilepath = filepath . new ( ext = '<STR_LIT>' ) <EOL> self . __class__ . counter += <NUM_LIT:1> <EOL> f = filepath . open ( '<STR_LIT:w>' ) <EOL> print >> f , source <EOL> print >> f , py . code . Source ( """<STR_LIT>""" ) <EOL> for testcase in testcases * <NUM_LIT:2> : <EOL> print >> f , "<STR_LIT>" % testcase <EOL> print >> f , "<STR_LIT>" <EOL> f . close ( ) <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> child_stdin , child_stdout = os . popen2 ( '<STR_LIT>' % ( <EOL> logfilepath , self . pypy_c , filepath ) ) <EOL> child_stdin . close ( ) <EOL> result = child_stdout . read ( ) <EOL> child_stdout . close ( ) <EOL> assert result <EOL> assert result . splitlines ( ) [ - <NUM_LIT:1> ] . strip ( ) == '<STR_LIT>' <EOL> assert logfilepath . check ( ) <EOL> opslogfile = logfilepath . new ( ext = '<STR_LIT>' ) <EOL> self . parse_loops ( opslogfile ) <EOL> def parse_loops ( self , opslogfile ) : <EOL> from pypy . jit . metainterp . test . oparser import parse , split_logs_into_loops <EOL> assert opslogfile . check ( ) <EOL> logs = opslogfile . read ( ) <EOL> parts = split_logs_into_loops ( logs ) <EOL> self . loops = [ parse ( part , no_namespace = True ) for part in parts <EOL> if "<STR_LIT>" not in part ] <EOL> self . sliced_loops = [ ] <EOL> for loop in self . loops : <EOL> for op in loop . operations : <EOL> if op . getopname ( ) == "<STR_LIT>" : <EOL> sliced_loop = BytecodeTrace ( ) <EOL> sliced_loop . bytecode = op . args [ <NUM_LIT:0> ] . _get_str ( ) . rsplit ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> self . sliced_loops . append ( sliced_loop ) <EOL> else : <EOL> sliced_loop . append ( op ) <EOL> self . check_0_op_bytecodes ( ) <EOL> def check_0_op_bytecodes ( self ) : <EOL> for bytecodetrace in self . sliced_loops : <EOL> if bytecodetrace . bytecode not in ZERO_OP_BYTECODES : <EOL> continue <EOL> assert not bytecodetrace <EOL> def get_by_bytecode ( self , name ) : <EOL> return [ ops for ops in self . sliced_loops if ops . bytecode == name ] <EOL> def test_f1 ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT> ] , <NUM_LIT> ) ) <EOL> def test_factorial ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:5> ] , <NUM_LIT> ) , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT> L ) ) <EOL> def test_factorialrec ( self ) : <EOL> skip ( "<STR_LIT>" ) <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:5> ] , <NUM_LIT> ) , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT> L ) ) <EOL> def test_richards ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' % ( sys . path , ) , <EOL> ( [ ] , <NUM_LIT> ) ) <EOL> def test_simple_call ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT:20> ) , <EOL> ( [ <NUM_LIT> ] , <NUM_LIT:32> ) ) <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> assert ops [ <NUM_LIT:0> ] . get_opnames ( ) == [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> assert not ops [ <NUM_LIT:1> ] <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> for bytecode in ops : <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( bytecode . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:10> <EOL> def test_method_call ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT:20> ) , <EOL> ( [ <NUM_LIT> ] , <NUM_LIT:32> ) ) <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> assert not ops [ <NUM_LIT:0> ] . get_opnames ( "<STR_LIT>" ) <EOL> assert not ops [ <NUM_LIT:0> ] . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( ops [ <NUM_LIT:0> ] . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:8> <EOL> assert not ops [ <NUM_LIT:1> ] <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> for bytecode in ops : <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( bytecode . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:9> <EOL> assert len ( ops [ <NUM_LIT:1> ] ) < len ( ops [ <NUM_LIT:0> ] ) <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> assert ops [ <NUM_LIT:0> ] . get_opnames ( ) == [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> assert not ops [ <NUM_LIT:1> ] <EOL> def test_default_and_kw ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT:20> ) , <EOL> ( [ <NUM_LIT> ] , <NUM_LIT:32> ) ) <EOL> ops = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert len ( ops ) == <NUM_LIT:2> <EOL> for bytecode in ops : <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( ops [ <NUM_LIT:0> ] . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT> <EOL> assert len ( ops [ <NUM_LIT:1> ] . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:3> <EOL> def test_virtual_instance ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:20> ] , <NUM_LIT:20> ) , <EOL> ( [ <NUM_LIT> ] , <NUM_LIT:32> ) ) <EOL> callA , callisinstance = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert not callA . get_opnames ( "<STR_LIT>" ) <EOL> assert not callA . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( callA . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:9> <EOL> assert not callisinstance . get_opnames ( "<STR_LIT>" ) <EOL> assert not callisinstance . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( callisinstance . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:2> <EOL> bytecode , = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert bytecode . get_opnames ( ) == [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def test_mixed_type_loop ( self ) : <EOL> self . run_source ( '''<STR_LIT>''' , <EOL> ( [ <NUM_LIT:20> ] , ( <NUM_LIT:20> , True ) ) , <EOL> ( [ <NUM_LIT> ] , ( <NUM_LIT:32> , True ) ) ) <EOL> bytecode , = self . get_by_bytecode ( "<STR_LIT>" ) <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert not bytecode . get_opnames ( "<STR_LIT>" ) <EOL> assert len ( bytecode . get_opnames ( "<STR_LIT>" ) ) <= <NUM_LIT:3> <EOL> class AppTestJIT ( PyPyCJITTests ) : <EOL> def setup_class ( cls ) : <EOL> if not option . runappdirect : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> cls . space = gettestobjspace ( usemodules = [ '<STR_LIT>' ] ) <EOL> cls . tmpdir = udir . join ( '<STR_LIT>' ) <EOL> cls . tmpdir . ensure ( dir = <NUM_LIT:1> ) <EOL> cls . counter = <NUM_LIT:0> <EOL> cls . pypy_c = sys . executable <EOL> class TestJIT ( PyPyCJITTests ) : <EOL> def setup_class ( cls ) : <EOL> if option . pypy_c is None : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> cls . tmpdir = udir . join ( '<STR_LIT>' ) <EOL> cls . tmpdir . ensure ( dir = <NUM_LIT:1> ) <EOL> cls . counter = <NUM_LIT:0> <EOL> cls . pypy_c = option . pypy_c </s>
