<s> from pypy . objspace . flow . objspace import UnwrapException <EOL> from pypy . objspace . flow . model import Constant <EOL> from pypy . objspace . flow . operation import OperationName , Arity <EOL> from pypy . interpreter . gateway import ApplevelClass <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . tool . cache import Cache <EOL> def sc_import ( space , fn , args ) : <EOL> args_w , kwds_w = args . unpack ( ) <EOL> assert kwds_w == { } , "<STR_LIT>" % ( fn , ) <EOL> assert len ( args_w ) > <NUM_LIT:0> and len ( args_w ) <= <NUM_LIT:5> , '<STR_LIT>' <EOL> w_name = args_w [ <NUM_LIT:0> ] <EOL> w_None = space . wrap ( None ) <EOL> w_glob , w_loc , w_frm = w_None , w_None , w_None <EOL> if len ( args_w ) > <NUM_LIT:1> : <EOL> w_glob = args_w [ <NUM_LIT:1> ] <EOL> if len ( args_w ) > <NUM_LIT:2> : <EOL> w_loc = args_w [ <NUM_LIT:2> ] <EOL> if len ( args_w ) > <NUM_LIT:3> : <EOL> w_frm = args_w [ <NUM_LIT:3> ] <EOL> if not isinstance ( w_loc , Constant ) : <EOL> raise SyntaxError , "<STR_LIT>" <EOL> if space . do_imports_immediately : <EOL> name , glob , loc , frm = ( space . unwrap ( w_name ) , space . unwrap ( w_glob ) , <EOL> space . unwrap ( w_loc ) , space . unwrap ( w_frm ) ) <EOL> try : <EOL> mod = __import__ ( name , glob , loc , frm ) <EOL> except ImportError , e : <EOL> raise OperationError ( space . w_ImportError , space . wrap ( str ( e ) ) ) <EOL> return space . wrap ( mod ) <EOL> w_glob = Constant ( { } ) <EOL> return space . do_operation ( '<STR_LIT>' , Constant ( __import__ ) , <EOL> w_name , w_glob , w_loc , w_frm ) <EOL> def sc_operator ( space , fn , args ) : <EOL> args_w , kwds_w = args . unpack ( ) <EOL> assert kwds_w == { } , "<STR_LIT>" % ( fn , ) <EOL> opname = OperationName [ fn ] <EOL> if len ( args_w ) != Arity [ opname ] : <EOL> if opname == '<STR_LIT>' and len ( args_w ) == <NUM_LIT:2> : <EOL> args_w = args_w + [ Constant ( None ) ] <EOL> elif opname == '<STR_LIT>' and len ( args_w ) == <NUM_LIT:3> : <EOL> return space . do_operation ( '<STR_LIT>' , Constant ( getattr ) , * args_w ) <EOL> else : <EOL> raise Exception , "<STR_LIT>" % ( <EOL> fn , Arity [ opname ] ) <EOL> if space . config . translation . builtins_can_raise_exceptions : <EOL> w_result = space . do_operation ( opname , * args_w ) <EOL> space . handle_implicit_exceptions ( [ Exception ] ) <EOL> return w_result <EOL> else : <EOL> return getattr ( space , opname ) ( * args_w ) <EOL> class FunctionCache ( Cache ) : <EOL> """<STR_LIT>""" <EOL> def _build ( app ) : <EOL> """<STR_LIT>""" <EOL> dic = { } <EOL> if not app . can_use_geninterp : <EOL> return None <EOL> if app . filename is not None : <EOL> dic [ '<STR_LIT>' ] = app . filename <EOL> dic [ '<STR_LIT>' ] = app . modname <EOL> exec app . code in dic <EOL> return dic <EOL> _build = staticmethod ( _build ) <EOL> class StdOutBuffer : <EOL> linebuf = [ ] <EOL> stdoutbuffer = StdOutBuffer ( ) <EOL> def rpython_print_item ( s ) : <EOL> buf = stdoutbuffer . linebuf <EOL> for c in s : <EOL> buf . append ( c ) <EOL> buf . append ( '<STR_LIT:U+0020>' ) <EOL> def rpython_print_newline ( ) : <EOL> buf = stdoutbuffer . linebuf <EOL> if buf : <EOL> buf [ - <NUM_LIT:1> ] = '<STR_LIT:\n>' <EOL> s = '<STR_LIT>' . join ( buf ) <EOL> del buf [ : ] <EOL> else : <EOL> s = '<STR_LIT:\n>' <EOL> import os <EOL> os . write ( <NUM_LIT:1> , s ) <EOL> compiled_funcs = FunctionCache ( ) <EOL> def sc_applevel ( space , app , name , args_w ) : <EOL> dic = compiled_funcs . getorbuild ( app ) <EOL> if not dic : <EOL> return None <EOL> func = dic [ name ] <EOL> if getattr ( func , '<STR_LIT>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> name = func . _annspecialcase_ [ len ( '<STR_LIT>' ) : ] <EOL> if name == '<STR_LIT>' : <EOL> w_s = space . do_operation ( '<STR_LIT:str>' , * args_w ) <EOL> args_w = ( w_s , ) <EOL> func = globals ( ) [ '<STR_LIT>' + name ] <EOL> else : <EOL> pass <EOL> return space . do_operation ( '<STR_LIT>' , Constant ( func ) , * args_w ) <EOL> def setup ( space ) : <EOL> space . specialcases [ __import__ ] = sc_import <EOL> space . specialcases [ ApplevelClass ] = sc_applevel <EOL> for fn in OperationName : <EOL> space . specialcases [ fn ] = sc_operator </s>
