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() ]
class Utf8StringBuilder(object): @always_inline def __init__(self, size=0): self._s = StringBuilder(size) self._lgt = 0 @always_inline def append(self, s): # for strings self._s.append(s) newlgt = codepoints_in_utf8(s) self._lgt += newlgt @always_inline def append_slice(self, s, start, end): self._s.append_slice(s, start, end) newlgt = codepoints_in_utf8(s, start, end) self._lgt += newlgt @signature(types.self(), char(), returns=none()) @always_inline def append_char(self, s): # for characters, ascii self._s.append(s) self._lgt += 1 @try_inline def append_code(self, code): unichr_as_utf8_append(self._s, code, True) self._lgt += 1 @always_inline def append_utf8(self, utf8, length): self._s.append(utf8) self._lgt += length @always_inline def append_utf8_slice(self, utf8, start, end, slicelength): self._s.append_slice(utf8, start, end) self._lgt += slicelength if not we_are_translated(): assert len(utf8[start:end].decode("utf-8")) == slicelength @always_inline def append_multiple_char(self, utf8, times): self._s.append(utf8 * times) self._lgt += times @always_inline def build(self): return self._s.build() @always_inline def getlength(self): return self._lgt
def test_list(): @signature(types.list(types.int()), returns=types.int()) def f(a): return len(a) argtype = getsig(f)[0] assert isinstance(argtype, model.SomeList) item = argtype.listdef.listitem assert item.s_value == model.SomeInteger() assert item.resized == True @check_annotator_fails def ok_for_body(): f(["a"]) @check_annotator_fails def bad_for_body(): f("a") @signature(returns=types.list(types.char())) def ff(): return ["a"] @check_annotator_fails def mutate_broader(): ff()[0] = "abc" @check_annotator_fails def mutate_unrelated(): ff()[0] = 1 @check_annotator_fails @signature(types.list(types.char()), returns=types.int()) def mutate_in_body(l): l[0] = "abc" return len(l) def can_append(): l = ff() l.append("b") getsig(can_append)
def test_list(): @signature(types.list(types.int()), returns=types.int()) def f(a): return len(a) argtype = getsig(f)[0] assert isinstance(argtype, model.SomeList) item = argtype.listdef.listitem assert item.s_value == model.SomeInteger() assert item.resized == True @check_annotator_fails def ok_for_body(): f(['a']) @check_annotator_fails def bad_for_body(): f('a') @signature(returns=types.list(types.char())) def ff(): return ['a'] @check_annotator_fails def mutate_broader(): ff()[0] = 'abc' @check_annotator_fails def mutate_unrelated(): ff()[0] = 1 @check_annotator_fails @signature(types.list(types.char()), returns=types.int()) def mutate_in_body(l): l[0] = 'abc' return len(l) def can_append(): l = ff() l.append('b') getsig(can_append)
class Utf8StringBuilder(object): @always_inline def __init__(self, size=0): self._s = StringBuilder(size) self._lgt = 0 @always_inline def append(self, s): # for strings self._s.append(s) newlgt = get_utf8_length(s) self._lgt += newlgt @always_inline def append_slice(self, s, start, end): self._s.append_slice(s, start, end) newlgt = get_utf8_length(s, start, end) self._lgt += newlgt @signature(types.self(), char(), returns=none()) @always_inline def append_char(self, s): # for characters, ascii self._s.append(s) self._lgt += 1 @try_inline def append_code(self, code): unichr_as_utf8_append(self._s, code, True) self._lgt += 1 @always_inline def append_utf8(self, utf8, length): self._s.append(utf8) self._lgt += length @always_inline def append_multiple_char(self, utf8, times): self._s.append(utf8 * times) self._lgt += times @always_inline def build(self): return self._s.build() @always_inline def getlength(self): return self._lgt
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()]