Esempio n. 1
0
        def method_matches(self, s_s, s_pos):
            assert model.SomeString().contains(s_s)
            assert model.SomeInteger(nonneg=True).contains(s_pos)

            bk = getbookkeeper()
            init_pbc = bk.immutablevalue(Match.__init__)
            bk.emulate_pbc_call((self, "match_init"), init_pbc, [
                model.SomeInstance(bk.getuniqueclassdef(Match)),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True)
            ])
            init_pbc = bk.immutablevalue(rsre_core.StrMatchContext.__init__)
            bk.emulate_pbc_call((self, "str_match_context_init"), init_pbc, [
                model.SomeInstance(
                    bk.getuniqueclassdef(rsre_core.StrMatchContext)),
                bk.newlist(model.SomeInteger(nonneg=True)),
                model.SomeString(),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True),
            ])
            match_context_pbc = bk.immutablevalue(rsre_core.match_context)
            bk.emulate_pbc_call((self, "match_context"), match_context_pbc, [
                model.SomeInstance(
                    bk.getuniqueclassdef(rsre_core.StrMatchContext)),
            ])

            return model.SomeInstance(getbookkeeper().getuniqueclassdef(Match),
                                      can_be_None=True)
Esempio n. 2
0
def test_return():
    @signature(returns=types.str())
    def f():
        return 'a'
    assert getsig(f) == [model.SomeString()]

    @signature(types.str(), returns=types.str())
    def f(x):
        return x
    def g():
        return f('a')
    a = annotate_at(g)
    assert sigof(a, f) == [model.SomeString(), model.SomeString()]
Esempio n. 3
0
    def make_driverhook_graphs(self):
        #
        annhelper = MixLevelHelperAnnotator(self.translator.rtyper)
        for jd in self.jitdrivers_sd:
            jd._get_printable_location_ptr = self._make_hook_graph(
                jd, annhelper, jd.jitdriver.get_printable_location,
                annmodel.SomeString())
            jd._get_unique_id_ptr = self._make_hook_graph(
                jd, annhelper, jd.jitdriver.get_unique_id,
                annmodel.SomeInteger())
            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)
            #
            items = []
            types = ()
            pos = ()
            if jd.jitdriver.get_location:
                assert hasattr(jd.jitdriver.get_location, '_loc_types'), """
                You must decorate your get_location function:

                from rpython.rlib.rjitlog import rjitlog as jl
                @jl.returns(jl.MP_FILENAME, jl.MP_XXX, ...)
                def get_loc(your, green, keys):
                    name = "x.txt" # extract it from your green keys
                    return (name, ...)
                """
                types = jd.jitdriver.get_location._loc_types
                del jd.jitdriver.get_location._loc_types
                #
                for _, type in types:
                    if type == 's':
                        items.append(annmodel.SomeString())
                    elif type == 'i':
                        items.append(annmodel.SomeInteger())
                    else:
                        raise NotImplementedError
            s_Tuple = annmodel.SomeTuple(items)
            jd._get_location_ptr = self._make_hook_graph(
                jd, annhelper, jd.jitdriver.get_location, s_Tuple)
            jd._get_loc_types = types
        annhelper.finish()
Esempio n. 4
0
 def default_arguments(self):
     from rpython.annotator import model as annmodel
     from rpython.rtyper import llannotation
     return [
         llannotation.SomePtr(ll_ptrtype=_CMDPTR),
         annmodel.SomeString()
     ]
Esempio n. 5
0
def test_dict():
    @signature(returns=types.dict(types.str(), types.int()))
    def f():
        return {'a': 1, 'b': 2}
    rettype = getsig(f)[0]
    assert isinstance(rettype, model.SomeDict)
    assert rettype.dictdef.dictkey.s_value   == model.SomeString()
    assert rettype.dictdef.dictvalue.s_value == model.SomeInteger()
Esempio n. 6
0
File: jit.py Progetto: weijiwei/pypy
 def compute_result_annotation(self, s_driver, s_name, s_value):
     from rpython.annotator import model as annmodel
     assert s_name.is_constant()
     if s_name.const == 'enable_opts':
         assert annmodel.SomeString(can_be_None=True).contains(s_value)
     else:
         assert (s_value == annmodel.s_None
                 or annmodel.SomeInteger().contains(s_value))
     return annmodel.s_None
Esempio n. 7
0
def test_str0():
    @signature(types.unicode0(), returns=types.str0())
    def f(u):
        return 'str'

    assert getsig(f) == [
        model.SomeUnicodeString(no_nul=True),
        model.SomeString(no_nul=True)
    ]
Esempio n. 8
0
def test_nullstring():
    def oof(b):
        if b:
            return 'foo'
        else:
            return None

    a = RPythonAnnotator()
    s = a.build_types(oof, [bool])
    assert annmodel.SomeString(can_be_None=True).contains(s)
Esempio n. 9
0
def test_basic():
    @signature(types.int(), types.str(), returns=types.char())
    def f(a, b):
        return b[a]

    assert getsig(f) == [
        model.SomeInteger(),
        model.SomeString(),
        model.SomeChar()
    ]
Esempio n. 10
0
class scoped_str2charp:
    def __init__(self, value):
        if value is not None:
            self.buf = str2charp(value)
        else:
            self.buf = lltype.nullptr(CCHARP.TO)
    __init__._annenforceargs_ = [None, annmodel.SomeString(can_be_None=True)]
    def __enter__(self):
        return self.buf
    def __exit__(self, *args):
        if self.buf:
            free_charp(self.buf)
Esempio n. 11
0
 def make_driverhook_graphs(self):
     s_Str = annmodel.SomeString()
     #
     annhelper = MixLevelHelperAnnotator(self.translator.rtyper)
     for jd in self.jitdrivers_sd:
         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()
Esempio n. 12
0
 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()
     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)
     py.test.raises(Exception, a.build_types, g, [[str]])
     a.build_types(g, [[str0]])  # Does not raise
Esempio n. 13
0
    def stress_combination(self, key_can_be_none, value_can_be_none):
        from rpython.rtyper.lltypesystem.rstr import string_repr
        from rpython.annotator.dictdef import DictKey, DictValue
        from rpython.annotator import model as annmodel
        from rpython.rtyper.test.test_rdict import not_really_random
        rodct = rordereddict

        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 = rodct.OrderedDictRepr(
            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 = rodct.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 = rodct.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:
                rodct.ll_dict_delitem(l_dict, keytable[n])
                referencetable[n] = None
                referencelength -= 1
            elif op <= '6':
                ll_value = string_repr.convert_const(str(values.next()))
                rodct.ll_dict_setitem(l_dict, keytable[n], ll_value)
                if referencetable[n] is None:
                    referencelength += 1
                referencetable[n] = ll_value
            else:
                try:
                    gotvalue = rodct.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_live_items == referencelength
        complete_check()
Esempio n. 14
0
    return rstring_to_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_nonul(loader):
    if readchr(loader) != TYPE_STRING:
        raise ValueError("expected a string")
    length = readlong(loader)
Esempio n. 15
0
import py
import marshal
from rpython.rlib.rmarshal import *
from rpython.annotator import model as annmodel
from rpython.rlib.rarithmetic import LONG_BIT
from rpython.rlib.rfloat import formatd

types_that_can_be_none = [
    [int],
    annmodel.SomeString(can_be_None=True),
    annmodel.s_None,
    {
        int: int
    },
]


def test_marshaller():
    buf = []
    get_marshaller(int)(buf, 5)
    assert marshal.loads(''.join(buf)) == 5

    buf = []
    get_marshaller(int)(buf, -555)
    assert marshal.loads(''.join(buf)) == -555

    buf = []
    get_marshaller(float)(buf, 3.25)
    assert marshal.loads(''.join(buf)) == 3.25

    buf = []
Esempio n. 16
0
 def compute_result_annotation(self, s_ll_str):
     if strtype is str:
         return annmodel.SomeString(can_be_None=True)
     else:
         return annmodel.SomeUnicodeString(can_be_None=True)
Esempio n. 17
0
def str(can_be_None=False):
    return model.SomeString(can_be_None=can_be_None)
Esempio n. 18
0
def str0():
    return model.SomeString(no_nul=True)
Esempio n. 19
0
def test_reprkeys_dont_clash():
    stup1 = annmodel.SomeTuple((annmodel.SomeFloat(), annmodel.SomeInteger()))
    stup2 = annmodel.SomeTuple((annmodel.SomeString(), annmodel.SomeInteger()))
    key1 = stup1.rtyper_makekey()
    key2 = stup2.rtyper_makekey()
    assert key1 != key2
Esempio n. 20
0
    from rpython.jit.metainterp.history import ResOperation

    args = [_cast_to_box(llargs[i]) for i in range(len(llargs))]
    if llres:
        res = _cast_to_box(llres)
    else:
        res = None
    return _cast_to_gcref(ResOperation(no, args, res))


@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(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.SomeInteger())
Esempio n. 21
0
 def getattr(self, s_attr):
     if s_attr.is_constant() and s_attr.const == "name":
         return model.SomeString()
     return super(SomeRule, self).getattr(s_attr)