def stress_combination(self, key_can_be_none, value_can_be_none): from pypy.rpython.lltypesystem.rstr import string_repr from pypy.annotation.dictdef import DictKey, DictValue from pypy.annotation import model as annmodel print print "Testing combination with can_be_None: keys %s, values %s" % ( key_can_be_none, value_can_be_none) class PseudoRTyper: cache_dummy_values = {} dictrepr = rdict.DictRepr(PseudoRTyper(), string_repr, string_repr, DictKey(None, annmodel.SomeString(key_can_be_none)), DictValue(None, annmodel.SomeString(value_can_be_none))) dictrepr.setup() print dictrepr.lowleveltype for key, value in dictrepr.DICTENTRY._adtmeths.items(): print ' %s = %s' % (key, value) l_dict = rdict.ll_newdict(dictrepr.DICT) referencetable = [None] * 400 referencelength = 0 values = not_really_random() keytable = [string_repr.convert_const("foo%d" % n) for n in range(len(referencetable))] def complete_check(): for n, refvalue in zip(range(len(referencetable)), referencetable): try: gotvalue = rdict.ll_dict_getitem(l_dict, keytable[n]) except KeyError: assert refvalue is None else: assert gotvalue == refvalue for x in not_really_random(): n = int(x*100.0) # 0 <= x < 400 op = repr(x)[-1] if op <= '2' and referencetable[n] is not None: rdict.ll_dict_delitem(l_dict, keytable[n]) referencetable[n] = None referencelength -= 1 elif op <= '6': ll_value = string_repr.convert_const(str(values.next())) rdict.ll_dict_setitem(l_dict, keytable[n], ll_value) if referencetable[n] is None: referencelength += 1 referencetable[n] = ll_value else: try: gotvalue = rdict.ll_dict_getitem(l_dict, keytable[n]) except KeyError: assert referencetable[n] is None else: assert gotvalue == referencetable[n] if 1.38 <= x <= 1.39: complete_check() print 'current dict length:', referencelength assert l_dict.num_items == referencelength complete_check()
def test_reprkeys_dont_clash(): stup1 = annmodel.SomeTuple((annmodel.SomeFloat(), annmodel.SomeInteger())) stup2 = annmodel.SomeTuple((annmodel.SomeString(), annmodel.SomeInteger())) rtyper = RPythonTyper(annrpython.RPythonAnnotator(None)) key1 = rtyper.makekey(stup1) key2 = rtyper.makekey(stup2) assert key1 != key2
def specialize(self, dont_simplify_again=False, crash_on_first_typeerror=True): """Main entry point: specialize all annotated blocks of the program.""" self.crash_on_first_typeerror = crash_on_first_typeerror # specialize depends on annotator simplifications assert dont_simplify_again in (False, True) # safety check if not dont_simplify_again: self.annotator.simplify() # first make sure that all functions called in a group have exactly # the same signature, by hacking their flow graphs if needed self.type_system.perform_normalizations(self) self.exceptiondata.finish(self) # new blocks can be created as a result of specialize_block(), so # we need to be careful about the loop here. self.already_seen = {} self.specialize_more_blocks() if self.exceptiondata is not None: self.exceptiondata.make_helpers(self) self.specialize_more_blocks() # for the helpers just made if self.type_system.name == 'ootypesystem': self.attach_methods_to_subclasses() # from pypy.annotation import listdef ldef = listdef.ListDef(None, annmodel.SomeString()) self.list_of_str_repr = self.getrepr(annmodel.SomeList(ldef))
def make_driverhook_graphs(self): from pypy.rlib.jit import BaseJitCell bk = self.rtyper.annotator.bookkeeper classdef = bk.getuniqueclassdef(BaseJitCell) s_BaseJitCell_or_None = annmodel.SomeInstance(classdef, can_be_None=True) s_BaseJitCell_not_None = annmodel.SomeInstance(classdef) s_Str = annmodel.SomeString() # annhelper = MixLevelHelperAnnotator(self.translator.rtyper) for jd in self.jitdrivers_sd: jd._set_jitcell_at_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.set_jitcell_at, annmodel.s_None, s_BaseJitCell_not_None) jd._get_jitcell_at_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.get_jitcell_at, s_BaseJitCell_or_None) jd._get_printable_location_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.get_printable_location, s_Str) jd._confirm_enter_jit_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.confirm_enter_jit, annmodel.s_Bool, onlygreens=False) jd._can_never_inline_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.can_never_inline, annmodel.s_Bool) jd._should_unroll_one_iteration_ptr = self._make_hook_graph(jd, annhelper, jd.jitdriver.should_unroll_one_iteration, annmodel.s_Bool) annhelper.finish()
def compute_result_annotation(self, s_driver, s_name, s_value): from pypy.annotation import model as annmodel assert s_name.is_constant() if not self.bookkeeper.immutablevalue(DEFAULT).contains(s_value): if s_name.const == 'enable_opts': assert annmodel.SomeString(can_be_None=True).contains(s_value) else: assert annmodel.SomeInteger().contains(s_value) return annmodel.s_None
def test_nullstring(): def oof(b): if b: return 'foo' else: return None a = RPythonAnnotator() s = a.build_types(oof, [bool]) assert s == annmodel.SomeString(can_be_None=True)
def make_driverhook_graphs(self): from pypy.rlib.jit import BaseJitCell bk = self.rtyper.annotator.bookkeeper classdef = bk.getuniqueclassdef(BaseJitCell) s_BaseJitCell_or_None = annmodel.SomeInstance(classdef, can_be_None=True) s_BaseJitCell_not_None = annmodel.SomeInstance(classdef) s_Str = annmodel.SomeString() # annhelper = MixLevelHelperAnnotator(self.translator.rtyper) self.set_jitcell_at_ptr = self._make_hook_graph( annhelper, self.jitdriver.set_jitcell_at, annmodel.s_None, s_BaseJitCell_not_None) self.get_jitcell_at_ptr = self._make_hook_graph( annhelper, self.jitdriver.get_jitcell_at, s_BaseJitCell_or_None) self.can_inline_ptr = self._make_hook_graph(annhelper, self.jitdriver.can_inline, annmodel.s_Bool) self.get_printable_location_ptr = self._make_hook_graph( annhelper, self.jitdriver.get_printable_location, s_Str) annhelper.finish()
def test_list_of_str0(self): str0 = annmodel.SomeString(no_nul=True) def os_execve(l): pass register_external(os_execve, [[str0]], None) def f(l): return os_execve(l) policy = AnnotatorPolicy() policy.allow_someobjects = False a = RPythonAnnotator(policy=policy) a.build_types(f, [[str]]) # Does not raise assert a.translator.config.translation.check_str_without_nul == False # Now enable the str0 check, and try again with a similar function a.translator.config.translation.check_str_without_nul = True def g(l): return os_execve(l) raises(Exception, a.build_types, g, [[str]]) a.build_types(g, [[str0]]) # Does not raise
return parts_to_float(*break_up_float(s)) add_loader(annmodel.SomeFloat(), load_float) def dump_string_or_none(buf, x): if x is None: dump_none(buf, x) else: buf.append(TYPE_STRING) w_long(buf, len(x)) buf += x add_dumper(annmodel.SomeString(can_be_None=True), dump_string_or_none) def load_single_char(loader): if readchr(loader) != TYPE_STRING or readlong(loader) != 1: raise ValueError("expected a character") return readchr(loader) add_loader(annmodel.SomeChar(), load_single_char) def load_string(loader): if readchr(loader) != TYPE_STRING: raise ValueError("expected a string") length = readlong(loader)
def compute_result_annotation(self, s_ll_str): return annmodel.SomeString()
import py import marshal from pypy.rlib.rmarshal import * from pypy.annotation import model as annmodel from pypy.rlib.rarithmetic import formatd types_that_can_be_none = [ [int], annmodel.SomeString(can_be_None=True), annmodel.s_None, ] def test_marshaller(): buf = [] get_marshaller(int)(buf, 5) assert marshal.loads(''.join(buf)) == 5 buf = [] get_marshaller(float)(buf, 3.25) assert marshal.loads(''.join(buf)) == 3.25 buf = [] get_marshaller(str)(buf, "hello, world") assert marshal.loads(''.join(buf)) == "hello, world" buf = [] get_marshaller(bool)(buf, False) assert marshal.loads(''.join(buf)) is False buf = []
res = _cast_to_box(llres) return _cast_to_gcref(ResOperation(no, args, res)) @register_helper(annmodel.SomePtr(llmemory.GCREF)) def boxint_new(no): from pypy.jit.metainterp.history import BoxInt return _cast_to_gcref(BoxInt(no)) @register_helper(annmodel.SomeInteger()) def resop_getopnum(llop): return _cast_to_resop(llop).getopnum() @register_helper(annmodel.SomeString(can_be_None=True)) def resop_getopname(llop): return llstr(_cast_to_resop(llop).getopname()) @register_helper(annmodel.SomePtr(llmemory.GCREF)) def resop_getarg(llop, no): return _cast_to_gcref(_cast_to_resop(llop).getarg(no)) @register_helper(annmodel.s_None) def resop_setarg(llop, no, llbox): _cast_to_resop(llop).setarg(no, _cast_to_box(llbox)) @register_helper(annmodel.SomePtr(llmemory.GCREF))
def compute_result_annotation(self, s_ll_str): if strtype is str: return annmodel.SomeString() else: return annmodel.SomeUnicodeString()