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"]
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
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 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
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)
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
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))
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
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
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
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"
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)
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"))
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
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
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
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
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)
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)
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_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
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)
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
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
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
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)
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
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]
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
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)
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
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