示例#1
0
 def test_dict_iteration(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     assert [hlstr(entry.key)
             for entry in self._ll_iter(ll_d)] == ["k", "j"]
示例#2
0
def test_streq_checknull_char():
    func = LLtypeHelpers._ll_2_str_eq_checknull_char.im_func
    assert func(llstr("wor"), "x") == False
    assert func(llstr("w"), "x") == False
    assert func(llstr(""), "x") == False
    assert func(llstr("x"), "x") == True
    assert func(llstr(None), "x") == False
示例#3
0
def test_streq_nonnull_char():
    func = LLtypeHelpers._ll_2_str_eq_nonnull_char.im_func
    assert func(llstr("wor"), "x") == False
    assert func(llstr("w"), "x") == False
    assert func(llstr(""), "x") == False
    assert func(llstr("x"), "x") == True
    py.test.raises(AttributeError, func, llstr(None), "x")
示例#4
0
def replace_count_str_str_str(input, sub, by, cnt, maxcount):
    from rpython.rtyper.annlowlevel import llstr, hlstr
    if cnt > maxcount and maxcount > 0:
        cnt = maxcount
    diff_len = len(by) - len(sub)
    try:
        result_size = ovfcheck(diff_len * cnt)
        result_size = ovfcheck(result_size + len(input))
    except OverflowError:
        raise

    s = llstr(input)
    by_ll = llstr(by)
    newstr = s.malloc(result_size)
    sublen = len(sub)
    bylen = len(by)
    inputlen = len(input)
    dst = 0
    start = 0
    while maxcount != 0:
        next = find(input, sub, start, inputlen)
        if next < 0:
            break
        s.copy_contents(s, newstr, start, dst, next - start)
        dst += next - start
        s.copy_contents(by_ll, newstr, 0, dst, bylen)
        dst += bylen
        start = next + sublen
        maxcount -= 1  # NB. if it's already < 0, it stays < 0
    s.copy_contents(s, newstr, start, dst, len(input) - start)
    assert dst - start + len(input) == result_size
    return hlstr(newstr), cnt
示例#5
0
def test_streq_nonnull_char():
    func = LLtypeHelpers._ll_2_str_eq_nonnull_char.im_func
    assert func(llstr("wor"), "x") == False
    assert func(llstr("w"), "x") == False
    assert func(llstr(""), "x") == False
    assert func(llstr("x"), "x") == True
    py.test.raises(AttributeError, func, llstr(None), "x")
    def test_set_param_enable_opts(self):
        from rpython.rtyper.annlowlevel import llstr, hlstr

        myjitdriver = JitDriver(greens = [], reds = ['n'])
        class A(object):
            def m(self, n):
                return n-1

        def g(n):
            while n > 0:
                myjitdriver.can_enter_jit(n=n)
                myjitdriver.jit_merge_point(n=n)
                n = A().m(n)
            return n
        def f(n, enable_opts):
            set_param(None, 'enable_opts', hlstr(enable_opts))
            return g(n)

        # check that the set_param will override the default
        res = self.meta_interp(f, [10, llstr('')])
        assert res == 0
        self.check_resops(new_with_vtable=1)

        res = self.meta_interp(f, [10, llstr(ALL_OPTS_NAMES)],
                               enable_opts='')
        assert res == 0
        self.check_resops(new_with_vtable=0)
示例#7
0
    def test_set_param_enable_opts(self):
        from rpython.rtyper.annlowlevel import llstr, hlstr

        myjitdriver = JitDriver(greens = [], reds = ['n'])
        class A(object):
            def m(self, n):
                return n-1

        def g(n):
            while n > 0:
                myjitdriver.can_enter_jit(n=n)
                myjitdriver.jit_merge_point(n=n)
                n = A().m(n)
            return n
        def f(n, enable_opts):
            set_param(None, 'enable_opts', hlstr(enable_opts))
            return g(n)

        # check that the set_param will override the default
        res = self.meta_interp(f, [10, llstr('')])
        assert res == 0
        self.check_resops(new_with_vtable=1)

        res = self.meta_interp(f, [10, llstr(ALL_OPTS_NAMES)],
                               enable_opts='')
        assert res == 0
        self.check_resops(new_with_vtable=0)
示例#8
0
def test_streq_checknull_char():
    func = LLtypeHelpers._ll_2_str_eq_checknull_char.im_func
    assert func(llstr("wor"), "x") == False
    assert func(llstr("w"), "x") == False
    assert func(llstr(""), "x") == False
    assert func(llstr("x"), "x") == True
    assert func(llstr(None), "x") == False
示例#9
0
 def test_dict_creation_2(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     llab = llstr("ab")
     llb = llstr("b")
     rordereddict.ll_dict_setitem(ll_d, llab, 1)
     rordereddict.ll_dict_setitem(ll_d, llb, 2)
     assert rordereddict.ll_dict_getitem(ll_d, llb) == 2
示例#10
0
def test_streq_slice_nonnull():
    p1 = llstr("hello world")
    p2 = llstr("wor")
    func = LLtypeHelpers._ll_4_str_eq_slice_nonnull.im_func
    assert func(p1, 6, 3, p2) == True
    assert func(p1, 6, 2, p2) == False
    assert func(p1, 5, 3, p2) == False
    py.test.raises(AttributeError, func, p1, 2, 1, llstr(None))
示例#11
0
def test_streq_slice_checknull():
    p1 = llstr("hello world")
    p2 = llstr("wor")
    func = LLtypeHelpers._ll_4_str_eq_slice_checknull.im_func
    assert func(p1, 6, 3, p2) == True
    assert func(p1, 6, 2, p2) == False
    assert func(p1, 5, 3, p2) == False
    assert func(p1, 2, 1, llstr(None)) == False
 def test_dict_del_not_lastitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 13)
     rordereddict.ll_dict_setitem(ll_d, llstr("def"), 15)
     rordereddict.ll_dict_delitem(ll_d, llstr("abc"))
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 2
     assert count_items(ll_d, rordereddict.DELETED) == 1
示例#13
0
 def test_clear(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("l"), 1)
     rordereddict.ll_dict_clear(ll_d)
     assert ll_d.num_live_items == 0
示例#14
0
 def test_setdefault(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     assert rordereddict.ll_dict_setdefault(ll_d, llstr("j"), 42) == 42
     assert rordereddict.ll_dict_getitem(ll_d, llstr("j")) == 42
     assert rordereddict.ll_dict_setdefault(ll_d, llstr("k"), 42) == 1
     assert rordereddict.ll_dict_getitem(ll_d, llstr("k")) == 1
示例#15
0
 def test_clear(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("l"), 1)
     rordereddict.ll_dict_clear(ll_d)
     assert ll_d.num_live_items == 0
示例#16
0
 def test_simple(self):
     sb = StringBuilderRepr.ll_new(3)
     StringBuilderRepr.ll_append_char(sb, 'x')
     StringBuilderRepr.ll_append(sb, llstr("abc"))
     StringBuilderRepr.ll_append_slice(sb, llstr("foobar"), 2, 5)
     StringBuilderRepr.ll_append_multiple_char(sb, 'y', 3)
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "xabcobayyy"
示例#17
0
def test_streq_slice_nonnull():
    p1 = llstr("hello world")
    p2 = llstr("wor")
    func = LLtypeHelpers._ll_4_str_eq_slice_nonnull.im_func
    assert func(p1, 6, 3, p2) == True
    assert func(p1, 6, 2, p2) == False
    assert func(p1, 5, 3, p2) == False
    py.test.raises(AttributeError, func, p1, 2, 1, llstr(None))
示例#18
0
 def test_setdefault(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     assert rordereddict.ll_dict_setdefault(ll_d, llstr("j"), 42) == 42
     assert rordereddict.ll_dict_getitem(ll_d, llstr("j")) == 42
     assert rordereddict.ll_dict_setdefault(ll_d, llstr("k"), 42) == 1
     assert rordereddict.ll_dict_getitem(ll_d, llstr("k")) == 1
示例#19
0
 def test_dict_del_not_lastitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 13)
     rordereddict.ll_dict_setitem(ll_d, llstr("def"), 15)
     rordereddict.ll_dict_delitem(ll_d, llstr("abc"))
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 2
     assert count_items(ll_d, rordereddict.DELETED) == 1
示例#20
0
 def test_bug_remove_deleted_items(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(15):
         rordereddict.ll_dict_setitem(ll_d, llstr(chr(i)), 5)
     for i in range(15):
         rordereddict.ll_dict_delitem(ll_d, llstr(chr(i)))
     rordereddict.ll_prepare_dict_update(ll_d, 7)
示例#21
0
def test_streq_slice_checknull():
    p1 = llstr("hello world")
    p2 = llstr("wor")
    func = LLtypeHelpers._ll_4_str_eq_slice_checknull.im_func
    assert func(p1, 6, 3, p2) == True
    assert func(p1, 6, 2, p2) == False
    assert func(p1, 5, 3, p2) == False
    assert func(p1, 2, 1, llstr(None)) == False
示例#22
0
 def test_bug_remove_deleted_items(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(15):
         rordereddict.ll_dict_setitem(ll_d, llstr(chr(i)), 5)
     for i in range(15):
         rordereddict.ll_dict_delitem(ll_d, llstr(chr(i)))
     rordereddict.ll_prepare_dict_update(ll_d, 7)
示例#23
0
 def test_dict_creation_2(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     llab = llstr("ab")
     llb = llstr("b")
     rordereddict.ll_dict_setitem(ll_d, llab, 1)
     rordereddict.ll_dict_setitem(ll_d, llb, 2)
     assert rordereddict.ll_dict_getitem(ll_d, llb) == 2
示例#24
0
 def test_copy(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     ll_d2 = rordereddict.ll_dict_copy(ll_d)
     for ll_d3 in [ll_d, ll_d2]:
         assert rordereddict.ll_dict_getitem(ll_d3, llstr("k")) == 1
         assert rordereddict.ll_dict_get(ll_d3, llstr("j"), 42) == 2
         assert rordereddict.ll_dict_get(ll_d3, llstr("i"), 42) == 42
 def test_dict_del_lastitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     py.test.raises(KeyError, rordereddict.ll_dict_delitem, ll_d, llstr("abc"))
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 13)
     py.test.raises(KeyError, rordereddict.ll_dict_delitem, ll_d, llstr("def"))
     rordereddict.ll_dict_delitem(ll_d, llstr("abc"))
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 1
     assert count_items(ll_d, rordereddict.DELETED) == 1
     py.test.raises(KeyError, rordereddict.ll_dict_getitem, ll_d, llstr("abc"))
示例#26
0
 def test_dict_del_lastitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     py.test.raises(KeyError, rordereddict.ll_dict_delitem, ll_d, llstr("abc"))
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 13)
     py.test.raises(KeyError, rordereddict.ll_dict_delitem, ll_d, llstr("def"))
     rordereddict.ll_dict_delitem(ll_d, llstr("abc"))
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 1
     assert count_items(ll_d, rordereddict.DELETED) == 1
     py.test.raises(KeyError, rordereddict.ll_dict_getitem, ll_d, llstr("abc"))
示例#27
0
 def test_dict_store_get(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(20):
         for j in range(i):
             assert rordereddict.ll_dict_getitem(ll_d, llstr(str(j))) == j
         rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
     assert ll_d.num_live_items == 20
     for i in range(20):
         assert rordereddict.ll_dict_getitem(ll_d, llstr(str(i))) == i
示例#28
0
 def test_copy(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     ll_d2 = rordereddict.ll_dict_copy(ll_d)
     for ll_d3 in [ll_d, ll_d2]:
         assert rordereddict.ll_dict_getitem(ll_d3, llstr("k")) == 1
         assert rordereddict.ll_dict_get(ll_d3, llstr("j"), 42) == 2
         assert rordereddict.ll_dict_get(ll_d3, llstr("i"), 42) == 42
示例#29
0
 def test_dict_store_get(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(20):
         for j in range(i):
             assert rordereddict.ll_dict_getitem(ll_d, llstr(str(j))) == j
         rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
     assert ll_d.num_live_items == 20
     for i in range(20):
         assert rordereddict.ll_dict_getitem(ll_d, llstr(str(i))) == i
示例#30
0
 def test_update(self):
     DICT = self._get_str_dict()
     ll_d1 = rordereddict.ll_newdict(DICT)
     ll_d2 = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d1, llstr("k"), 5)
     rordereddict.ll_dict_setitem(ll_d1, llstr("j"), 6)
     rordereddict.ll_dict_setitem(ll_d2, llstr("i"), 7)
     rordereddict.ll_dict_setitem(ll_d2, llstr("k"), 8)
     rordereddict.ll_dict_update(ll_d1, ll_d2)
     for key, value in [("k", 8), ("i", 7), ("j", 6)]:
         assert rordereddict.ll_dict_getitem(ll_d1, llstr(key)) == value
示例#31
0
 def test_update(self):
     DICT = self._get_str_dict()
     ll_d1 = rordereddict.ll_newdict(DICT)
     ll_d2 = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d1, llstr("k"), 5)
     rordereddict.ll_dict_setitem(ll_d1, llstr("j"), 6)
     rordereddict.ll_dict_setitem(ll_d2, llstr("i"), 7)
     rordereddict.ll_dict_setitem(ll_d2, llstr("k"), 8)
     rordereddict.ll_dict_update(ll_d1, ll_d2)
     for key, value in [("k", 8), ("i", 7), ("j", 6)]:
         assert rordereddict.ll_dict_getitem(ll_d1, llstr(key)) == value
示例#32
0
    def test_ll_find_rfind(self):
        llstr = self.string_to_ll

        for i in range(50):
            n1 = random.randint(0, 10)
            s1 = ''.join([random.choice("ab") for i in range(n1)])
            n2 = random.randint(0, 5)
            s2 = ''.join([random.choice("ab") for i in range(n2)])
            res = LLHelpers.ll_find(llstr(s1), llstr(s2), 0, n1)
            assert res == s1.find(s2)
            res = LLHelpers.ll_rfind(llstr(s1), llstr(s2), 0, n1)
            assert res == s1.rfind(s2)
 def test_dict_creation(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     lls = llstr("abc")
     rordereddict.ll_dict_setitem(ll_d, lls, 13)
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 1
     assert rordereddict.ll_dict_getitem(ll_d, llstr("abc")) == 13
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 13
     rordereddict.ll_dict_setitem(ll_d, lls, 42)
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 42
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 43)
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 43
示例#34
0
 def test_dict_store_get_del(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(20):
         for j in range(0, i, 2):
             assert rordereddict.ll_dict_getitem(ll_d, llstr(str(j))) == j
         rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
         if i % 2 != 0:
             rordereddict.ll_dict_delitem(ll_d, llstr(str(i)))
     assert ll_d.num_live_items == 10
     for i in range(0, 20, 2):
         assert rordereddict.ll_dict_getitem(ll_d, llstr(str(i))) == i
示例#35
0
 def test_dict_store_get_del(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     for i in range(20):
         for j in range(0, i, 2):
             assert rordereddict.ll_dict_getitem(ll_d, llstr(str(j))) == j
         rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
         if i % 2 != 0:
             rordereddict.ll_dict_delitem(ll_d, llstr(str(i)))
     assert ll_d.num_live_items == 10
     for i in range(0, 20, 2):
         assert rordereddict.ll_dict_getitem(ll_d, llstr(str(i))) == i
示例#36
0
 def test_dict_creation(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     lls = llstr("abc")
     rordereddict.ll_dict_setitem(ll_d, lls, 13)
     assert count_items(ll_d, rordereddict.FREE) == rordereddict.DICT_INITSIZE - 1
     assert rordereddict.ll_dict_getitem(ll_d, llstr("abc")) == 13
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 13
     rordereddict.ll_dict_setitem(ll_d, lls, 42)
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 42
     rordereddict.ll_dict_setitem(ll_d, llstr("abc"), 43)
     assert rordereddict.ll_dict_getitem(ll_d, lls) == 43
示例#37
0
文件: test_rstr.py 项目: charred/pypy
    def test_ll_find_rfind(self):
        llstr = self.string_to_ll

        for i in range(50):
            n1 = random.randint(0, 10)
            s1 = ''.join([random.choice("ab") for i in range(n1)])
            n2 = random.randint(0, 5)
            s2 = ''.join([random.choice("ab") for i in range(n2)])
            res = LLHelpers.ll_find(llstr(s1), llstr(s2), 0, n1)
            assert res == s1.find(s2)
            res = LLHelpers.ll_rfind(llstr(s1), llstr(s2), 0, n1)
            assert res == s1.rfind(s2)
示例#38
0
 def test_dict_iteration(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     ll_dictnext = rordereddict._ll_dictnext
     num = ll_dictnext(ll_iter)
     assert hlstr(ll_d.entries[num].key) == "k"
     num = ll_dictnext(ll_iter)
     assert hlstr(ll_d.entries[num].key) == "j"
     py.test.raises(StopIteration, ll_dictnext, ll_iter)
示例#39
0
 def test_dict_iteration(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     ll_iterkeys = rordereddict.ll_dictnext_group['keys']
     next = ll_iterkeys(lltype.Signed, ll_iter)
     assert hlstr(next) == "k"
     next = ll_iterkeys(lltype.Signed, ll_iter)
     assert hlstr(next) == "j"
     py.test.raises(StopIteration, ll_iterkeys, lltype.Signed, ll_iter)
示例#40
0
 def test_dict_iteration(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     ll_dictnext = rordereddict._ll_dictnext
     num = ll_dictnext(ll_iter)
     assert hlstr(ll_d.entries[num].key) == "k"
     num = ll_dictnext(ll_iter)
     assert hlstr(ll_d.entries[num].key) == "j"
     py.test.raises(StopIteration, ll_dictnext, ll_iter)
示例#41
0
 def test_popitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     TUP = lltype.Ptr(lltype.GcStruct('x', ('item0', lltype.Ptr(rstr.STR)),
                                           ('item1', lltype.Signed)))
     ll_elem = rordereddict.ll_dict_popitem(TUP, ll_d)
     assert hlstr(ll_elem.item0) == "j"
     assert ll_elem.item1 == 2
     ll_elem = rordereddict.ll_dict_popitem(TUP, ll_d)
     assert hlstr(ll_elem.item0) == "k"
     assert ll_elem.item1 == 1
     py.test.raises(KeyError, rordereddict.ll_dict_popitem, TUP, ll_d)
 def test_popitem(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     TUP = lltype.Ptr(lltype.GcStruct('x', ('item0', lltype.Ptr(rstr.STR)),
                                           ('item1', lltype.Signed)))
     ll_elem = rordereddict.ll_dict_popitem(TUP, ll_d)
     assert hlstr(ll_elem.item0) == "j"
     assert ll_elem.item1 == 2
     ll_elem = rordereddict.ll_dict_popitem(TUP, ll_d)
     assert hlstr(ll_elem.item0) == "k"
     assert ll_elem.item1 == 1
     py.test.raises(KeyError, rordereddict.ll_dict_popitem, TUP, ll_d)
示例#43
0
 def test_simple(self):
     sb = StringBuilderRepr.ll_new(3)
     assert StringBuilderRepr.ll_getlength(sb) == 0
     StringBuilderRepr.ll_append_char(sb, 'x')
     assert StringBuilderRepr.ll_getlength(sb) == 1
     StringBuilderRepr.ll_append(sb, llstr("abc"))
     assert StringBuilderRepr.ll_getlength(sb) == 4
     StringBuilderRepr.ll_append_slice(sb, llstr("foobar"), 2, 5)
     assert StringBuilderRepr.ll_getlength(sb) == 7
     StringBuilderRepr.ll_append_multiple_char(sb, 'y', 3)
     assert StringBuilderRepr.ll_getlength(sb) == 10
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "xabcobayyy"
     assert StringBuilderRepr.ll_getlength(sb) == 10
示例#44
0
 def test_simple(self):
     sb = StringBuilderRepr.ll_new(3)
     assert StringBuilderRepr.ll_getlength(sb) == 0
     StringBuilderRepr.ll_append_char(sb, 'x')
     assert StringBuilderRepr.ll_getlength(sb) == 1
     StringBuilderRepr.ll_append(sb, llstr("abc"))
     assert StringBuilderRepr.ll_getlength(sb) == 4
     StringBuilderRepr.ll_append_slice(sb, llstr("foobar"), 2, 5)
     assert StringBuilderRepr.ll_getlength(sb) == 7
     StringBuilderRepr.ll_append_multiple_char(sb, 'y', 3)
     assert StringBuilderRepr.ll_getlength(sb) == 10
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "xabcobayyy"
     assert StringBuilderRepr.ll_getlength(sb) == 10
示例#45
0
 def test_popitem_first(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     rordereddict.ll_dict_setitem(ll_d, llstr("m"), 3)
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     for expected in ["k", "j", "m"]:
         ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
         num = rordereddict._ll_dictnext(ll_iter)
         ll_key = ll_d.entries[num].key
         assert hlstr(ll_key) == expected
         rordereddict.ll_dict_delitem(ll_d, ll_key)
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     py.test.raises(StopIteration, rordereddict._ll_dictnext, ll_iter)
示例#46
0
 def test_popitem_first_bug(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 1)
     rordereddict.ll_dict_delitem(ll_d, llstr("k"))
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     num = rordereddict._ll_dictnext(ll_iter)
     ll_key = ll_d.entries[num].key
     assert hlstr(ll_key) == "j"
     assert ll_d.lookup_function_no == 4    # 1 free item found at the start
     rordereddict.ll_dict_delitem(ll_d, llstr("j"))
     assert ll_d.num_ever_used_items == 0
     assert ll_d.lookup_function_no == 0    # reset
示例#47
0
 def test_popitem_first_bug(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 1)
     rordereddict.ll_dict_delitem(ll_d, llstr("k"))
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     num = rordereddict._ll_dictnext(ll_iter)
     ll_key = ll_d.entries[num].key
     assert hlstr(ll_key) == "j"
     assert ll_d.lookup_function_no == 4  # 1 free item found at the start
     rordereddict.ll_dict_delitem(ll_d, llstr("j"))
     assert ll_d.num_ever_used_items == 0
     assert ll_d.lookup_function_no == 0  # reset
示例#48
0
 def test_popitem_first(self):
     DICT = self._get_str_dict()
     ll_d = rordereddict.ll_newdict(DICT)
     rordereddict.ll_dict_setitem(ll_d, llstr("k"), 1)
     rordereddict.ll_dict_setitem(ll_d, llstr("j"), 2)
     rordereddict.ll_dict_setitem(ll_d, llstr("m"), 3)
     ITER = rordereddict.get_ll_dictiter(lltype.Ptr(DICT))
     for expected in ["k", "j", "m"]:
         ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
         num = rordereddict._ll_dictnext(ll_iter)
         ll_key = ll_d.entries[num].key
         assert hlstr(ll_key) == expected
         rordereddict.ll_dict_delitem(ll_d, ll_key)
     ll_iter = rordereddict.ll_dictiter(ITER, ll_d)
     py.test.raises(StopIteration, rordereddict._ll_dictnext, ll_iter)
示例#49
0
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)
    
    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
示例#50
0
 def test_nooveralloc(self):
     sb = StringBuilderRepr.ll_new(33)
     StringBuilderRepr.ll_append(sb, llstr("abc" * 11))
     assert StringBuilderRepr.ll_getlength(sb) == 33
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "abc" * 11
     assert StringBuilderRepr.ll_getlength(sb) == 33
示例#51
0
文件: ctypeptr.py 项目: bukzor/pypy
 def convert_array_from_object(self, cdata, w_ob):
     space = self.space
     if (space.isinstance_w(w_ob, space.w_list) or
         space.isinstance_w(w_ob, space.w_tuple)):
         self._convert_array_from_listview(cdata, w_ob)
     elif (self.can_cast_anything or
           (self.ctitem.is_primitive_integer and
            self.ctitem.size == rffi.sizeof(lltype.Char))):
         if not space.isinstance_w(w_ob, space.w_str):
             raise self._convert_error("str or list or tuple", w_ob)
         s = space.str_w(w_ob)
         n = len(s)
         if self.length >= 0 and n > self.length:
             raise oefmt(space.w_IndexError,
                         "initializer string is too long for '%s' (got %d "
                         "characters)", self.name, n)
         copy_string_to_raw(llstr(s), cdata, 0, n)
         if n != self.length:
             cdata[n] = '\x00'
     elif isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveUniChar):
         if not space.isinstance_w(w_ob, space.w_unicode):
             raise self._convert_error("unicode or list or tuple", w_ob)
         s = space.unicode_w(w_ob)
         n = len(s)
         if self.length >= 0 and n > self.length:
             raise oefmt(space.w_IndexError,
                         "initializer unicode string is too long for '%s' "
                         "(got %d characters)", self.name, n)
         unichardata = rffi.cast(rffi.CWCHARP, cdata)
         copy_unicode_to_raw(llunicode(s), unichardata, 0, n)
         if n != self.length:
             unichardata[n] = u'\x00'
     else:
         raise self._convert_error("list or tuple", w_ob)
示例#52
0
 def test_shrinking(self):
     sb = StringBuilderRepr.ll_new(100)
     StringBuilderRepr.ll_append(sb, llstr("abc" * 11))
     assert StringBuilderRepr.ll_getlength(sb) == 33
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "abc" * 11
     assert StringBuilderRepr.ll_getlength(sb) == 33
示例#53
0
 def test_shrinking(self):
     sb = StringBuilderRepr.ll_new(100)
     StringBuilderRepr.ll_append(sb, llstr("abc" * 11))
     assert StringBuilderRepr.ll_getlength(sb) == 33
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "abc" * 11
     assert StringBuilderRepr.ll_getlength(sb) == 33
示例#54
0
 def f():
     a = llstr("xyz")
     b = (llmemory.cast_ptr_to_adr(a) +
          llmemory.offsetof(STR, 'chars') +
          llmemory.itemoffsetof(STR.chars, 0))
     buf = rffi.cast(rffi.VOIDP, b)
     return buf[2]
示例#55
0
def test_streq_slice_char():
    p1 = llstr("hello world")
    func = LLtypeHelpers._ll_4_str_eq_slice_char.im_func
    assert func(p1, 6, 3, "w") == False
    assert func(p1, 6, 0, "w") == False
    assert func(p1, 6, 1, "w") == True
    assert func(p1, 6, 1, "x") == False
示例#56
0
def test_deflate_set_dictionary():
    text = 'abcabc'
    zdict = 'abc'
    stream = rzlib.deflateInit()
    rzlib.deflateSetDictionary(stream, zdict)
    bytes = rzlib.compress(stream, text, rzlib.Z_FINISH)
    rzlib.deflateEnd(stream)

    stream2 = rzlib.inflateInit()

    from rpython.rtyper.lltypesystem import lltype, rffi, rstr
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rlib.rstring import StringBuilder
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(bytes)) as inbuf:
        rstr.copy_string_to_raw(llstr(bytes), inbuf, 0, len(bytes))
        stream2.c_next_in = rffi.cast(rzlib.Bytefp, inbuf)
        rffi.setintfield(stream2, 'c_avail_in', len(bytes))
        with lltype.scoped_alloc(rffi.CCHARP.TO, 100) as outbuf:
            stream2.c_next_out = rffi.cast(rzlib.Bytefp, outbuf)
            bufsize = 100
            rffi.setintfield(stream2, 'c_avail_out', bufsize)
            err = rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            assert err == rzlib.Z_NEED_DICT
            rzlib.inflateSetDictionary(stream2, zdict)
            rzlib._inflate(stream2, rzlib.Z_SYNC_FLUSH)
            avail_out = rffi.cast(lltype.Signed, stream2.c_avail_out)
            result = StringBuilder()
            result.append_charpsize(outbuf, bufsize - avail_out)

    rzlib.inflateEnd(stream2)
    assert result.build() == text
示例#57
0
 def test_nooveralloc(self):
     sb = StringBuilderRepr.ll_new(33)
     StringBuilderRepr.ll_append(sb, llstr("abc" * 11))
     assert StringBuilderRepr.ll_getlength(sb) == 33
     s = StringBuilderRepr.ll_build(sb)
     assert hlstr(s) == "abc" * 11
     assert StringBuilderRepr.ll_getlength(sb) == 33
示例#58
0
def _get_buffer_from_str(data):
    # Dangerous!  The resulting pointer is only valid as long as there
    # is no GC!
    lldata = llstr(data)
    data_start = llmemory.cast_ptr_to_adr(lldata) + \
      rffi.offsetof(rstr.STR, 'chars') + rffi.itemoffsetof(rstr.STR.chars, 0)
    return rffi.cast(rffi.CCHARP, data_start)
示例#59
0
文件: func.py 项目: sota/pypy
def get_raw_address_of_string(space, w_x):
    """Special case for ffi.from_buffer(string).  Returns a 'char *' that
    is valid as long as the string object is alive.  Two calls to
    ffi.from_buffer(same_string) are guaranteed to return the same pointer.
    """
    from rpython.rtyper.annlowlevel import llstr
    from rpython.rtyper.lltypesystem.rstr import STR
    from rpython.rtyper.lltypesystem import llmemory
    from rpython.rlib import rgc

    cache = space.fromcache(RawBytesCache)
    rawbytes = cache.wdict.get(w_x)
    if rawbytes is None:
        data = space.bytes_w(w_x)
        if (we_are_translated() and not rgc.can_move(data)
                and not rgc.must_split_gc_address_space()):
            lldata = llstr(data)
            data_start = (llmemory.cast_ptr_to_adr(lldata) +
                          rffi.offsetof(STR, 'chars') +
                          llmemory.itemoffsetof(STR.chars, 0))
            data_start = rffi.cast(rffi.CCHARP, data_start)
            data_start[len(data)] = '\x00'  # write the final extra null
            return data_start
        rawbytes = RawBytes(data)
        cache.wdict.set(w_x, rawbytes)
    return rawbytes.ptr
示例#60
0
def replace_count_str_chr_chr(input, c1, c2, maxcount):
    from rpython.rtyper.annlowlevel import llstr, hlstr
    s = llstr(input)
    length = len(s.chars)
    start = find(input, c1, 0, len(input))
    if start < 0:
        return input, 0
    newstr = s.malloc(length)
    src = s.chars
    dst = newstr.chars
    s.copy_contents(s, newstr, 0, 0, len(input))
    dst[start] = c2
    count = 1
    start += 1
    maxcount -= 1
    while maxcount != 0:
        next = find(input, c1, start, len(input))
        if next < 0:
            break
        dst[next] = c2
        start = next + 1
        maxcount -= 1
        count += 1

    return hlstr(newstr), count