<s> import os <EOL> import platform <EOL> import py <EOL> from py . compat import subprocess <EOL> from pypy . tool . udir import udir <EOL> from pypy . translator . translator import TranslationContext <EOL> from pypy . rpython . test . tool import BaseRtypingTest , OORtypeMixin <EOL> from pypy . rpython . lltypesystem . lltype import typeOf <EOL> from pypy . rpython . ootypesystem import ootype <EOL> from pypy . annotation . model import lltype_to_annotation , SomeString <EOL> from pypy . translator . backendopt . all import backend_optimizations <EOL> from pypy . translator . backendopt . checkvirtual import check_virtual_methods <EOL> from pypy . rpython . ootypesystem import ootype <EOL> from pypy . translator . cli . option import getoption <EOL> from pypy . translator . cli . gencli import GenCli <EOL> from pypy . translator . cli . function import Function <EOL> from pypy . translator . cli . node import Node <EOL> from pypy . translator . cli . cts import CTS <EOL> from pypy . translator . cli . database import LowLevelDatabase <EOL> from pypy . translator . cli . sdk import SDK <EOL> from pypy . translator . cli . entrypoint import BaseEntryPoint <EOL> from pypy . translator . oosupport . support import patch_os , unpatch_os <EOL> def format_object ( TYPE , cts , ilasm ) : <EOL> if TYPE is ootype . Void : <EOL> ilasm . opcode ( '<STR_LIT>' ) <EOL> elif TYPE in ( ootype . Unicode , ootype . UniChar ) : <EOL> type_ = cts . lltype_to_cts ( TYPE ) <EOL> ilasm . call ( '<STR_LIT>' % type_ ) <EOL> else : <EOL> if isinstance ( TYPE , ( ootype . BuiltinType , ootype . Instance , ootype . StaticMethod ) ) and TYPE is not ootype . String : <EOL> type_ = '<STR_LIT:object>' <EOL> else : <EOL> type_ = cts . lltype_to_cts ( TYPE ) <EOL> ilasm . call ( '<STR_LIT>' % type_ ) <EOL> class TestEntryPoint ( BaseEntryPoint ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph_to_call , wrap_exceptions = False ) : <EOL> self . graph = graph_to_call <EOL> self . wrap_exceptions = wrap_exceptions <EOL> def get_name ( self ) : <EOL> return '<STR_LIT>' <EOL> def render ( self , ilasm ) : <EOL> ilasm . begin_function ( '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , '<STR_LIT>' , True , '<STR_LIT>' ) <EOL> RETURN_TYPE = self . graph . getreturnvar ( ) . concretetype <EOL> return_type = self . cts . lltype_to_cts ( RETURN_TYPE ) <EOL> if return_type != CTS . types . void : <EOL> ilasm . locals ( [ ( return_type , '<STR_LIT>' ) ] ) <EOL> if self . wrap_exceptions : <EOL> ilasm . begin_try ( ) <EOL> for i , arg in enumerate ( self . graph . getargs ( ) ) : <EOL> ilasm . opcode ( '<STR_LIT>' ) <EOL> ilasm . opcode ( '<STR_LIT>' % i ) <EOL> ilasm . opcode ( '<STR_LIT>' ) <EOL> arg_type , arg_var = self . cts . llvar_to_cts ( arg ) <EOL> self . __call_convert_method ( ilasm , arg_type ) <EOL> ilasm . call ( self . cts . graph_to_signature ( self . graph ) ) <EOL> if return_type != CTS . types . void : <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . wrap_exceptions : <EOL> ilasm . leave ( '<STR_LIT>' ) <EOL> else : <EOL> ilasm . leave ( '<STR_LIT>' ) <EOL> if self . wrap_exceptions : <EOL> ilasm . end_try ( ) <EOL> for exc in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ilasm . begin_catch ( exc ) <EOL> if getoption ( '<STR_LIT>' ) : <EOL> ilasm . opcode ( '<STR_LIT>' ) <EOL> else : <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> ilasm . leave ( '<STR_LIT>' ) <EOL> ilasm . end_catch ( ) <EOL> ilasm . label ( '<STR_LIT>' ) <EOL> if hasattr ( self . db , '<STR_LIT>' ) : <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ilasm . label ( '<STR_LIT>' ) <EOL> if return_type != CTS . types . void : <EOL> ilasm . opcode ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> format_object ( RETURN_TYPE , self . cts , ilasm ) <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> ilasm . label ( '<STR_LIT>' ) <EOL> ilasm . opcode ( '<STR_LIT>' ) <EOL> ilasm . end_function ( ) <EOL> self . db . pending_function ( self . graph ) <EOL> def __call_convert_method ( self , ilasm , arg_type ) : <EOL> if arg_type == CTS . types . float64 : <EOL> ilasm . call ( '<STR_LIT>' ) <EOL> else : <EOL> ilasm . call ( '<STR_LIT>' % <EOL> ( arg_type , self . __convert_method ( arg_type ) ) ) <EOL> def __convert_method ( self , arg_type ) : <EOL> _conv = { <EOL> CTS . types . int16 : '<STR_LIT>' , <EOL> CTS . types . int32 : '<STR_LIT>' , <EOL> CTS . types . uint32 : '<STR_LIT>' , <EOL> CTS . types . int64 : '<STR_LIT>' , <EOL> CTS . types . uint64 : '<STR_LIT>' , <EOL> CTS . types . bool : '<STR_LIT>' , <EOL> CTS . types . char : '<STR_LIT>' , <EOL> CTS . types . string : '<STR_LIT>' , <EOL> } <EOL> try : <EOL> return _conv [ arg_type ] <EOL> except KeyError : <EOL> assert False , '<STR_LIT>' % arg_type <EOL> def compile_function ( func , annotation = [ ] , graph = None , backendopt = True , <EOL> auto_raise_exc = False , exctrans = False , <EOL> annotatorpolicy = None , nowrap = False ) : <EOL> olddefs = patch_os ( ) <EOL> gen = _build_gen ( func , annotation , graph , backendopt , exctrans , annotatorpolicy , nowrap ) <EOL> gen . generate_source ( ) <EOL> exe_name = gen . build_exe ( ) <EOL> unpatch_os ( olddefs ) <EOL> return CliFunctionWrapper ( exe_name , func . __name__ , auto_raise_exc ) <EOL> def compile_graph ( graph , translator , auto_raise_exc = False , <EOL> exctrans = False , nowrap = False , standalone = False ) : <EOL> gen = _build_gen_from_graph ( graph , translator , exctrans , nowrap , <EOL> standalone = standalone ) <EOL> gen . generate_source ( ) <EOL> exe_name = gen . build_exe ( ) <EOL> name = getattr ( graph , '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> return CliFunctionWrapper ( exe_name , name , auto_raise_exc ) <EOL> def _build_gen ( func , annotation , graph = None , backendopt = True , exctrans = False , <EOL> annotatorpolicy = None , nowrap = False ) : <EOL> try : <EOL> func = func . im_func <EOL> except AttributeError : <EOL> pass <EOL> t = TranslationContext ( ) <EOL> if graph is not None : <EOL> graph . func = func <EOL> ann = t . buildannotator ( policy = annotatorpolicy ) <EOL> inputcells = [ ann . typeannotation ( a ) for a in annotation ] <EOL> ann . build_graph_types ( graph , inputcells ) <EOL> t . graphs . insert ( <NUM_LIT:0> , graph ) <EOL> else : <EOL> ann = t . buildannotator ( policy = annotatorpolicy ) <EOL> ann . build_types ( func , annotation ) <EOL> if getoption ( '<STR_LIT>' ) : <EOL> t . view ( ) <EOL> t . buildrtyper ( type_system = "<STR_LIT>" ) . specialize ( ) <EOL> if backendopt : <EOL> check_virtual_methods ( ootype . ROOT ) <EOL> backend_optimizations ( t ) <EOL> main_graph = t . graphs [ <NUM_LIT:0> ] <EOL> if getoption ( '<STR_LIT>' ) : <EOL> t . view ( ) <EOL> return _build_gen_from_graph ( main_graph , t , exctrans , nowrap ) <EOL> def _build_gen_from_graph ( graph , t , exctrans = False , nowrap = False , standalone = False ) : <EOL> from pypy . translator . cli . entrypoint import get_entrypoint <EOL> if getoption ( '<STR_LIT>' ) : <EOL> tmpdir = py . path . local ( '<STR_LIT:.>' ) <EOL> else : <EOL> tmpdir = udir <EOL> if standalone : <EOL> ep = get_entrypoint ( graph ) <EOL> else : <EOL> ep = TestEntryPoint ( graph , not nowrap ) <EOL> return GenCli ( tmpdir , t , ep , exctrans = exctrans ) <EOL> class CliFunctionWrapper ( object ) : <EOL> def __init__ ( self , exe_name , name = None , auto_raise_exc = False ) : <EOL> self . _exe = exe_name <EOL> self . __name__ = name or exe_name <EOL> self . auto_raise_exc = auto_raise_exc <EOL> def run ( self , * args ) : <EOL> if self . _exe is None : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> if getoption ( '<STR_LIT>' ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> arglist = SDK . runtime ( ) + [ self . _exe ] + map ( str , args ) <EOL> env = os . environ . copy ( ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:C>' <EOL> mono = subprocess . Popen ( arglist , stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , env = env ) <EOL> stdout , stderr = mono . communicate ( ) <EOL> retval = mono . wait ( ) <EOL> return stdout , stderr , retval <EOL> def __call__ ( self , * args ) : <EOL> stdout , stderr , retval = self . run ( * args ) <EOL> assert retval == <NUM_LIT:0> , stderr <EOL> res = eval ( stdout . strip ( ) ) <EOL> if isinstance ( res , tuple ) : <EOL> res = StructTuple ( res ) <EOL> elif isinstance ( res , list ) : <EOL> res = OOList ( res ) <EOL> elif self . auto_raise_exc and isinstance ( res , ExceptionWrapper ) : <EOL> excname = res . class_name <EOL> if excname . startswith ( '<STR_LIT>' ) : <EOL> import exceptions <EOL> raise eval ( excname ) <EOL> else : <EOL> raise res <EOL> return res <EOL> class StructTuple ( tuple ) : <EOL> def __getattr__ ( self , name ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> i = int ( name [ len ( '<STR_LIT>' ) : ] ) <EOL> return self [ i ] <EOL> else : <EOL> raise AttributeError , name <EOL> class OOList ( list ) : <EOL> def ll_length ( self ) : <EOL> return len ( self ) <EOL> def ll_getitem_fast ( self , i ) : <EOL> return self [ i ] <EOL> class InstanceWrapper : <EOL> def __init__ ( self , class_name ) : <EOL> self . class_name = class_name <EOL> class ExceptionWrapper : <EOL> def __init__ ( self , class_name ) : <EOL> self . class_name = class_name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( self . class_name ) <EOL> def get_annotation ( x ) : <EOL> if isinstance ( x , basestring ) and len ( x ) > <NUM_LIT:1> : <EOL> return SomeString ( ) <EOL> else : <EOL> return lltype_to_annotation ( typeOf ( x ) ) <EOL> class CliTest ( BaseRtypingTest , OORtypeMixin ) : <EOL> def __init__ ( self ) : <EOL> self . _func = None <EOL> self . _ann = None <EOL> self . _cli_func = None <EOL> def _compile ( self , fn , args , ann = None , backendopt = True , <EOL> auto_raise_exc = False , exctrans = False , policy = None ) : <EOL> if ann is None : <EOL> ann = [ get_annotation ( x ) for x in args ] <EOL> if self . _func is fn and self . _ann == ann : <EOL> return self . _cli_func <EOL> else : <EOL> self . _cli_func = compile_function ( fn , ann , backendopt = backendopt , <EOL> auto_raise_exc = auto_raise_exc , <EOL> exctrans = exctrans , annotatorpolicy = policy ) <EOL> self . _func = fn <EOL> self . _ann = ann <EOL> return self . _cli_func <EOL> def _skip_win ( self , reason ) : <EOL> if platform . system ( ) == '<STR_LIT>' : <EOL> py . test . skip ( '<STR_LIT>' % reason ) <EOL> def _skip_powerpc ( self , reason ) : <EOL> if platform . processor ( ) == '<STR_LIT>' : <EOL> py . test . skip ( '<STR_LIT>' % reason ) <EOL> def _skip_llinterpreter ( self , reason , skipLL = True , skipOO = True ) : <EOL> pass <EOL> def _get_backendopt ( self , backendopt ) : <EOL> if backendopt is None : <EOL> backendopt = getattr ( self , '<STR_LIT>' , True ) <EOL> return backendopt <EOL> def interpret ( self , fn , args , annotation = None , backendopt = None , exctrans = False , policy = None ) : <EOL> backendopt = self . _get_backendopt ( backendopt ) <EOL> f = self . _compile ( fn , args , annotation , backendopt = backendopt , exctrans = exctrans ) <EOL> res = f ( * args ) <EOL> if isinstance ( res , ExceptionWrapper ) : <EOL> raise res <EOL> return res <EOL> def interpret_raises ( self , exception , fn , args , backendopt = None , exctrans = False ) : <EOL> import exceptions <EOL> backendopt = self . _get_backendopt ( backendopt ) <EOL> try : <EOL> self . interpret ( fn , args , backendopt = backendopt , exctrans = exctrans ) <EOL> except ExceptionWrapper , ex : <EOL> assert issubclass ( eval ( ex . class_name ) , exception ) <EOL> else : <EOL> assert False , '<STR_LIT>' <EOL> float_eq = BaseRtypingTest . float_eq_approx <EOL> def is_of_type ( self , x , type_ ) : <EOL> return True <EOL> def ll_to_string ( self , s ) : <EOL> return s <EOL> def ll_to_unicode ( self , s ) : <EOL> return s <EOL> def ll_to_list ( self , l ) : <EOL> return l <EOL> def ll_to_tuple ( self , t ) : <EOL> return t <EOL> def class_name ( self , value ) : <EOL> return value . class_name . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> def is_of_instance_type ( self , val ) : <EOL> return isinstance ( val , InstanceWrapper ) <EOL> def read_attr ( self , obj , name ) : <EOL> py . test . skip ( '<STR_LIT>' ) </s>
