def test_oohelper(): S = ootype.Instance('S', ootype.ROOT, {'x': Signed, 'y': Signed}) def f(s, z): #assert we_are_translated() return s.x * s.y + z def g(s): #assert we_are_translated() return s.x + s.y F = ootype.StaticMethod([S, Signed], Signed) G = ootype.StaticMethod([S], Signed) def h(x, y, z): s = ootype.new(S) s.x = x s.y = y fsm = llhelper(F, f) gsm = llhelper(G, g) assert typeOf(fsm) == F return fsm(s, z) + fsm(s, z * 2) + gsm(s) res = h(8, 5, 2) assert res == 99 res = interpret(h, [8, 5, 2], type_system='ootype') assert res == 99
def op_oonewcustomdict(self, DICT, eq_func, eq_obj, eq_method_name, hash_func, hash_obj, hash_method_name): eq_name, interp_eq = \ wrap_callable(self.llinterpreter, eq_func, eq_obj, eq_method_name) EQ_FUNC = ootype.StaticMethod([DICT._KEYTYPE, DICT._KEYTYPE], ootype.Bool) sm_eq = ootype.static_meth(EQ_FUNC, eq_name, _callable=interp_eq) hash_name, interp_hash = \ wrap_callable(self.llinterpreter, hash_func, hash_obj, hash_method_name) HASH_FUNC = ootype.StaticMethod([DICT._KEYTYPE], ootype.Signed) sm_hash = ootype.static_meth(HASH_FUNC, hash_name, _callable=interp_hash) # XXX: is it fine to have StaticMethod type for bound methods, too? return ootype.oonewcustomdict(DICT, sm_eq, sm_hash)
def convert_const(self, dictobj): if dictobj is None: return self.DICT._defl() if not isinstance(dictobj, dict) and not isinstance( dictobj, objectmodel.r_dict): raise TyperError("expected a dict: %r" % (dictobj, )) try: key = Constant(dictobj) return self.dict_cache[key] except KeyError: self.setup() l_dict = ll_newdict(self.DICT) if self.custom_eq_hash: interp = llinterp.LLInterpreter(self.rtyper) EQ_FUNC = ootype.StaticMethod( [self.DICT._KEYTYPE, self.DICT._KEYTYPE], ootype.Bool) sm_eq = self.__get_func(interp, self.r_rdict_eqfn, dictobj.key_eq, EQ_FUNC) HASH_FUNC = ootype.StaticMethod([self.DICT._KEYTYPE], ootype.Signed) sm_hash = self.__get_func(interp, self.r_rdict_hashfn, dictobj.key_hash, HASH_FUNC) l_dict.ll_set_functions(sm_eq, sm_hash) self.dict_cache[key] = l_dict r_key = self.key_repr r_value = self.value_repr if self.custom_eq_hash: for dictkeycont, dictvalue in dictobj._dict.items(): llkey = r_key.convert_const(dictkeycont.key) llvalue = r_value.convert_const(dictvalue) llhash = dictkeycont.hash l_dictkeycont = objectmodel._r_dictkey_with_hash( l_dict._dict, llkey, llhash) l_dict._dict._dict[l_dictkeycont] = llvalue else: for dictkey, dictvalue in dictobj.items(): llkey = r_key.convert_const(dictkey) llvalue = r_value.convert_const(dictvalue) l_dict.ll_set(llkey, llvalue) return l_dict
def test_call_descr_extra_info(): FUNC = ootype.StaticMethod([], ootype.Signed) ARGS = () descr1 = CliCPU.calldescrof(FUNC, ARGS, ootype.Signed, "hello") extrainfo = descr1.get_extra_info() assert extrainfo == "hello" descr2 = CliCPU.calldescrof(FUNC, ARGS, ootype.Signed, "hello") assert descr2 is descr1 descr3 = CliCPU.calldescrof(FUNC, ARGS, ootype.Signed) assert descr3 is not descr1 assert descr3.get_extra_info() is None
def specialize_call(self, hop): ARGS = [r.lowleveltype for r in hop.args_r] RESULT = hop.r_result.lowleveltype if hop.rtyper.type_system.name == 'lltypesystem': FUNCTYPE = lltype.FuncType(ARGS, RESULT) funcptr = lltype.functionptr(FUNCTYPE, func.__name__, _callable=func, _debugexc=True) cfunc = hop.inputconst(lltype.Ptr(FUNCTYPE), funcptr) else: FUNCTYPE = ootype.StaticMethod(ARGS, RESULT) sm = ootype._static_meth(FUNCTYPE, _name=func.__name__, _callable=func) cfunc = hop.inputconst(FUNCTYPE, sm) args_v = hop.inputargs(*hop.args_r) return hop.genop('direct_call', [cfunc] + args_v, hop.r_result)
def read_code(): from pypy.module.marshal.interp_marshal import dumps filename = 'pypyjit_demo.py' source = readfile(filename) ec = space.getexecutioncontext() code = ec.compiler.compile(source, filename, 'exec', 0) return llstr(space.str_w(dumps(space, code, space.wrap(2)))) if BACKEND == 'c': FPTR = lltype.Ptr(lltype.FuncType([], lltype.Ptr(STR))) read_code_ptr = llhelper(FPTR, read_code) else: llstr = oostr FUNC = ootype.StaticMethod([], ootype.String) read_code_ptr = llhelper(FUNC, read_code) def entry_point(): from pypy.module.marshal.interp_marshal import loads code = loads(space, space.wrap(hlstr(read_code_ptr()))) assert isinstance(code, PyCode) code.exec_code(space, w_dict, w_dict) def test_run_translation(): from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy from pypy.rpython.test.test_llinterp import get_interpreter # first annotate and rtype
def get_FuncType(self, ARGS, RESULT): FUNCTYPE = ootype.StaticMethod(ARGS, RESULT) return FUNCTYPE, FUNCTYPE
def constant_func(self, name, inputtypes, rettype, graph, **kwds): FUNC_TYPE = ootype.StaticMethod(inputtypes, rettype) fn_ptr = ootype.static_meth(FUNC_TYPE, name, graph=graph, **kwds) return Constant(fn_ptr, FUNC_TYPE)
resulttype=hop.r_result.lowleveltype) class Entry(ExtRegistryEntry): _type_ = _fieldinfo def compute_annotation(self): return SomeOOInstance(CLR.System.Reflection.FieldInfo._INSTANCE) from pypy.translator.cli.query import CliNamespace CLR = CliNamespace(None) CLR._buildtree() known_delegates = { ootype.StaticMethod([ootype.Signed], ootype.Signed): CLR.pypy.test.DelegateType_int__int_1, ootype.StaticMethod([ootype.Signed] * 2, ootype.Signed): CLR.pypy.test.DelegateType_int__int_2, ootype.StaticMethod([ootype.Signed] * 3, ootype.Signed): CLR.pypy.test.DelegateType_int__int_3, ootype.StaticMethod([ootype.Signed] * 5, ootype.Signed): CLR.pypy.test.DelegateType_int__int_5, ootype.StaticMethod([ootype.Signed] * 27, ootype.Signed): CLR.pypy.test.DelegateType_int__int_27, ootype.StaticMethod([ootype.Signed] * 100, ootype.Signed): CLR.pypy.test.DelegateType_int__int_100 } known_delegates_class = {}
from pypy.annotation import model as annmodel from pypy.rpython.ootypesystem import ootype from pypy.rpython.ootypesystem.ootype import meth, Meth, Char, Signed, Float, String,\ ROOT, overload, Instance, new from pypy.translator.cli.test.runtest import CliTest from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\ NativeInstance, CLR, box, unbox, OverloadingResolver, NativeException,\ native_exc, new_array, init_array, typeof, eventhandler, clidowncast,\ fieldinfo_for_const, classof, cast_record_to_object, cast_object_to_record System = CLR.System ArrayList = CLR.System.Collections.ArrayList OpCodes = System.Reflection.Emit.OpCodes DynamicMethod = System.Reflection.Emit.DynamicMethod Utils = CLR.pypy.runtime.Utils FUNCTYPE = ootype.StaticMethod([ootype.Signed, ootype.Signed], ootype.Signed) class TestDotnetAnnotation(object): def test_overloaded_meth_string(self): C = Instance("test", ROOT, {}, { 'foo': overload(meth(Meth([Char], Signed)), meth(Meth([String], Float)), resolver=OverloadingResolver), 'bar': overload(meth(Meth([Signed], Char)), meth(Meth([Float], String)), resolver=OverloadingResolver) })
def fresh_ref(self): O = ootype.StaticMethod([], ootype.Signed) o = O._example() return ootype.cast_to_object(o)
def create_low_leveltype(self): l_args = [r_arg.lowleveltype for r_arg in self.args_r] l_retval = self.r_result.lowleveltype return ootype.StaticMethod(l_args, l_retval)
def compute_result_annotation(self, s_value): assert isinstance(s_value, annmodel.SomeOOInstance) assert s_value.ootype is CLR.System.Object._INSTANCE return annmodel.SomeOOObject() def specialize_call(self, hop): v_obj = hop.inputarg(hop.args_r[0], arg=0) return hop.genop('oodowncast', [v_obj], hop.r_result.lowleveltype) from pypy.translator.cli.query import CliNamespace CLR = CliNamespace(None) CLR._buildtree() known_delegates = { ootype.StaticMethod([], ootype.Signed): CLR.pypy.test.DelegateType_int__0, ootype.StaticMethod([ootype.Signed, ootype.Float], ootype.Float): CLR.pypy.test.DelegateType_double_int_double, ootype.StaticMethod([ootype.Float], ootype.Float): CLR.pypy.test.DelegateType_double__double_1, ootype.StaticMethod([ootype.Bool], ootype.Bool): CLR.pypy.test.DelegateType_bool_bool_1, ootype.StaticMethod([ootype.Char], ootype.Char): CLR.pypy.test.DelegateType_char_char_1, ootype.StaticMethod([ootype.Signed], ootype.Void): CLR.pypy.test.DelegateType_void_int_1, ootype.StaticMethod([ootype.Signed], ootype.Signed): CLR.pypy.test.DelegateType_int__int_1, ootype.StaticMethod([ootype.Signed] * 2, ootype.Signed): CLR.pypy.test.DelegateType_int__int_2,