def ll_search(s1, s2, start, end, mode): from rpython.rtyper.annlowlevel import hlstr, hlunicode from rpython.rlib import rstring tp = typeOf(s1) if tp == string_repr.lowleveltype or tp == Char: return rstring._search(hlstr(s1), hlstr(s2), start, end, mode) else: return rstring._search(hlunicode(s1), hlunicode(s2), start, end, mode)
def test_os_confstr(self): def f(i): try: return os.confstr(i) except OSError: return "oooops!!" some_value = os.confstr_names.values()[-1] res = self.interpret(f, [some_value]) assert hlstr(res) == f(some_value) res = self.interpret(f, [94781413]) assert hlstr(res) == "oooops!!"
def test_strip_no_arg(self): strings = [" xyz ", "", "\t\vx"] def f(i): return strings[i].strip() res = self.interpret(f, [0]) assert hlstr(res) == "xyz" res = self.interpret(f, [1]) assert hlstr(res) == "" res = self.interpret(f, [2]) assert hlstr(res) == "x"
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_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_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 ll_matches(MATCHTYPE, MATCH_INIT, MATCH_CONTEXTTYPE, MATCH_CONTEXT_INIT, MATCH_CONTEXT, ll_rule, s, pos): s = hlstr(s) assert pos >= 0 ctx = instantiate(MATCH_CONTEXTTYPE) hlinvoke(MATCH_CONTEXT_INIT, rsre_core.StrMatchContext.__init__, ctx, ll_rule.code, hlstr(s), pos, len(s), 0) matched = hlinvoke(MATCH_CONTEXT, rsre_core.match_context, ctx) if matched: match = instantiate(MATCHTYPE) hlinvoke(MATCH_INIT, Match.__init__, match, ctx.match_start, ctx.match_end) return match else: return None
def _normalize(x): if not isinstance(x, str): TYPE = lltype.typeOf(x) if isinstance(TYPE, lltype.Ptr) and TYPE.TO._name == 'rpy_string': from rpython.rtyper.annlowlevel import hlstr return hlstr(x) return x
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 get_location_str(greenkey): greenargs = unwrap_greenkey(greenkey) fn = support.maybe_on_top_of_llinterp(rtyper, get_location_ptr) llres = fn(*greenargs) if not we_are_translated() and isinstance(llres, str): return llres return hlstr(llres)
def main(): loop(1) op = jit_hooks.resop_new( rop.INT_ADD, [jit_hooks.boxint_new(3), jit_hooks.boxint_new(4)], jit_hooks.boxint_new(1)) assert hlstr(jit_hooks.resop_getopname(op)) == 'int_add' assert jit_hooks.resop_getopnum(op) == rop.INT_ADD box = jit_hooks.resop_getarg(op, 0) assert jit_hooks.box_getint(box) == 3 box2 = jit_hooks.box_clone(box) assert box2 != box assert jit_hooks.box_getint(box2) == 3 assert not jit_hooks.box_isconst(box2) box3 = jit_hooks.box_constbox(box) assert jit_hooks.box_getint(box) == 3 assert jit_hooks.box_isconst(box3) box4 = jit_hooks.box_nonconstbox(box) assert not jit_hooks.box_isconst(box4) box5 = jit_hooks.boxint_new(18) jit_hooks.resop_setarg(op, 0, box5) assert jit_hooks.resop_getarg(op, 0) == box5 box6 = jit_hooks.resop_getresult(op) assert jit_hooks.box_getint(box6) == 1 jit_hooks.resop_setresult(op, box5) assert jit_hooks.resop_getresult(op) == box5
def finish(self): if not self.ll_val: raise ValueError("Cannot call finish() twice") result = hlstr(self.ll_val) self.ll_val = lltype.nullptr(STR) self.readonly = True return result
def ll_splitlines(cls, LIST, ll_str, keep_newlines): from rpython.rtyper.annlowlevel import hlstr s = hlstr(ll_str) strlen = len(s) i = 0 j = 0 # The annotator makes sure this list is resizable. res = LIST.ll_newlist(0) while j < strlen: while i < strlen and s[i] != '\n' and s[i] != '\r': i += 1 eol = i if i < strlen: if s[i] == '\r' and i + 1 < strlen and s[i + 1] == '\n': i += 2 else: i += 1 if keep_newlines: eol = i list_length = res.ll_length() res._ll_resize_ge(list_length + 1) item = cls.ll_stringslice_startstop(ll_str, j, eol) res.ll_setitem_fast(list_length, item) j = i if j < strlen: list_length = res.ll_length() res._ll_resize_ge(list_length + 1) item = cls.ll_stringslice_startstop(ll_str, j, strlen) res.ll_setitem_fast(list_length, item) return res
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_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 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 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
def main(): loop(1) op = jit_hooks.resop_new(rop.INT_ADD, [jit_hooks.boxint_new(3), jit_hooks.boxint_new(4)], jit_hooks.boxint_new(1)) assert hlstr(jit_hooks.resop_getopname(op)) == 'int_add' assert jit_hooks.resop_getopnum(op) == rop.INT_ADD box = jit_hooks.resop_getarg(op, 0) assert jit_hooks.box_getint(box) == 3 box2 = jit_hooks.box_clone(box) assert box2 != box assert jit_hooks.box_getint(box2) == 3 assert not jit_hooks.box_isconst(box2) box3 = jit_hooks.box_constbox(box) assert jit_hooks.box_getint(box) == 3 assert jit_hooks.box_isconst(box3) box4 = jit_hooks.box_nonconstbox(box) assert not jit_hooks.box_isconst(box4) box5 = jit_hooks.boxint_new(18) jit_hooks.resop_setarg(op, 0, box5) assert jit_hooks.resop_getarg(op, 0) == box5 box6 = jit_hooks.resop_getresult(op) assert jit_hooks.box_getint(box6) == 1 jit_hooks.resop_setresult(op, box5) assert jit_hooks.resop_getresult(op) == box5
def _get_str(self): # for debugging only from rpython.rtyper.annlowlevel import hlstr from rpython.rtyper.lltypesystem import rstr try: return hlstr(lltype.cast_opaque_ptr(lltype.Ptr(rstr.STR), self.value)) except lltype.UninitializedMemoryAccess: return '<uninitialized string>'
def f(x, y): y = const(hlstr(y)) if x > 0: l = [const('a'), const('b')] else: l = [const('a')] l += y return const('').join(l)
def ll_matches(MATCHTYPE, MATCH_INIT, MATCH_CONTEXTTYPE, MATCH_CONTEXT_INIT, MATCH_CONTEXT, ll_rule, s, pos): s = hlstr(s) assert pos >= 0 ctx = instantiate(MATCH_CONTEXTTYPE) hlinvoke(MATCH_CONTEXT_INIT, rsre_core.StrMatchContext.__init__, ctx, ll_rule.code, hlstr(s), pos, len(s), 0 ) matched = hlinvoke(MATCH_CONTEXT, rsre_core.match_context, ctx) if matched: match = instantiate(MATCHTYPE) hlinvoke(MATCH_INIT, Match.__init__, match, ctx.match_start, ctx.match_end ) return match else: return None
def f(x, y): y = const(hlstr(y)) if x > 0: l = [const("a"), const("b")] else: l = [const("a")] l += y return const("").join(l)
def test_charp(self): sb = StringBuilderRepr.ll_new(32) with rffi.scoped_str2charp("hello world") as p: StringBuilderRepr.ll_append_charpsize(sb, p, 12) with rffi.scoped_str2charp("0123456789abcdefghijklmn") as p: StringBuilderRepr.ll_append_charpsize(sb, p, 24) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == "hello world\x000123456789abcdefghijklmn"
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 ll_decode_utf8(self, llvalue): from rpython.rtyper.annlowlevel import hlstr value = hlstr(llvalue) assert value is not None # NB. keep the arguments in sync with annotator/unaryop.py u = str_decode_utf8(value) # XXX maybe the whole ''.decode('utf-8') should be not RPython. return self.ll.llunicode(u)
def test_getslice(self): def f(x): b = bytearray(str(x)) b = b[1:3] b[0] += 5 return str(b) ll_res = self.interpret(f, [12345]) assert hlstr(ll_res) == f(12345) == "73"
def test_several_builds(self): sb = StringBuilderRepr.ll_new(32) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == "" assert s == StringBuilderRepr.ll_build(sb) assert s == StringBuilderRepr.ll_build(sb) # sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append(sb, llstr("abcdefgh" * 3)) # not full s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == "abcdefgh" * 3 assert s == StringBuilderRepr.ll_build(sb) assert s == StringBuilderRepr.ll_build(sb) StringBuilderRepr.ll_append(sb, llstr("extra")) # overflow s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == "abcdefgh" * 3 + "extra" assert s == StringBuilderRepr.ll_build(sb) assert s == StringBuilderRepr.ll_build(sb)
def get_location_str(greenkey): if not have_debug_prints_for("jit-"): return missing greenargs = unwrap_greenkey(greenkey) fn = support.maybe_on_top_of_llinterp(rtyper, printable_loc_ptr) llres = fn(*greenargs) if not we_are_translated() and isinstance(llres, str): return llres return hlstr(llres)
def ll_decode_utf8(self, llvalue): from rpython.rtyper.annlowlevel import hlstr value = hlstr(llvalue) assert value is not None univalue, _ = self.rstr_decode_utf_8( value, len(value), 'strict', final=False, errorhandler=self.ll_raise_unicode_exception_decode, allow_surrogates=False) return self.ll.llunicode(univalue)
def entrypoint2(r, string, repeat): r = rsre_core.CompiledPattern(array2list(r), 0) string = hlstr(string) match = None for i in range(repeat): match = rsre_core.search(r, string) if match is None: return -1 else: return match.match_start
def ll_isalnum(s): from rpython.rtyper.annlowlevel import hlstr s = hlstr(s) if not s: return False for ch in s: if not ch.isalnum(): return False return True
def run(): space = getspace() interp = Interpreter(space) bc = unserialize(hlstr(read_code_ptr()), space) interp.setup(False, {}) try: interp.run_main(space, bc) except ExplicitExitException, e: print "EXITED WITH %d" % e.code return
def ll_decode_utf8(self, llvalue): from rpython.rtyper.annlowlevel import hlstr value = hlstr(llvalue) assert value is not None result = UnicodeBuilder(len(value)) self.rstr_decode_utf_8( value, len(value), 'strict', final=True, errorhandler=self.ll_raise_unicode_exception_decode, allow_surrogates=False, result=result) return self.ll.llunicode(result.build())
def entrypoint1(r, string, repeat): r = array2list(r) string = hlstr(string) match = None for i in range(repeat): match = rsre_core.match(r, string) if match is None: return -1 else: return match.match_end
def posix_fakeimpl(arg): if s_arg == traits.str0: arg = hlstr(arg) st = getattr(os, name)(arg) fields = [TYPE for fieldname, TYPE in STATVFS_FIELDS] TP = TUPLE_TYPE(fields) ll_tup = lltype.malloc(TP.TO) for i, (fieldname, TYPE) in enumerate(STATVFS_FIELDS): val = getattr(st, fieldname) rffi.setintfield(ll_tup, 'item%d' % i, int(val)) return ll_tup
def ll_decode_utf8(self, llvalue): from rpython.rtyper.annlowlevel import hlstr from rpython.rlib import runicode value = hlstr(llvalue) assert value is not None errorhandler = runicode.default_unicode_error_decode # NB. keep the arguments in sync with annotator/unaryop.py u, pos = runicode.str_decode_utf_8_elidable(value, len(value), 'strict', True, errorhandler, True) # XXX maybe the whole ''.decode('utf-8') should be not RPython. return self.ll.llunicode(u)
def test_large_build(self): s1 = 'xyz' * 500 s2 = 'XYZ' * 500 # sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append(sb, llstr(s1)) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1 # sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append(sb, llstr(s1)) StringBuilderRepr.ll_append(sb, llstr(s2)) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1 + s2 # sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append(sb, llstr(s1)) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1 StringBuilderRepr.ll_append(sb, llstr(s2)) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1 + s2 # sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append(sb, llstr(s1)) StringBuilderRepr.ll_append_char(sb, '.') s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1 + '.' # for start in [0, 1]: for stop in [len(s1), len(s1) - 1]: sb = StringBuilderRepr.ll_new(32) StringBuilderRepr.ll_append_slice(sb, llstr(s1), start, stop) s = StringBuilderRepr.ll_build(sb) assert hlstr(s) == s1[start:stop]
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 get_location(greenkey): greenargs = unwrap_greenkey(greenkey) fn = support.maybe_on_top_of_llinterp(rtyper, get_location_ptr) value_tuple = fn(*greenargs) values = [] for i, (sem_type,gen_type) in unrolled_types: if gen_type == "s": value = getattr(value_tuple, 'item' + str(i)) values.append(jl.wrap(sem_type,gen_type,hlstr(value))) elif gen_type == "i": value = getattr(value_tuple, 'item' + str(i)) values.append(jl.wrap(sem_type,gen_type,intmask(value))) else: raise NotImplementedError return values
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