def ll_join_strs(length_dummy, lst): if typeOf(lst).ITEM == ootype.String: buf = ootype.new(ootype.StringBuilder) else: buf = ootype.new(ootype.UnicodeBuilder) length = lst.ll_length() #buf.ll_allocate(length) i = 0 while i < length: buf.ll_append(lst.ll_getitem_fast(i)) i += 1 return buf.ll_build()
def ll_char_mul(ch, times): if times < 0: times = 0 if typeOf(ch) == Char: buf = ootype.new(ootype.StringBuilder) else: buf = ootype.new(ootype.UnicodeBuilder) buf.ll_allocate(times) i = 0 while i < times: buf.ll_append_char(ch) i += 1 return buf.ll_build()
def ll_char_mul(ch, times): if times < 0: times = 0 if typeOf(ch) == Char: buf = ootype.new(ootype.StringBuilder) else: buf = ootype.new(ootype.UnicodeBuilder) buf.ll_allocate(times) i = 0 while i<times: buf.ll_append_char(ch) i+= 1 return buf.ll_build()
def ll_join_chars(length_dummy, lst, RES): if RES is ootype.String: target = Char buf = ootype.new(ootype.StringBuilder) else: target = UniChar buf = ootype.new(ootype.UnicodeBuilder) length = lst.ll_length() buf.ll_allocate(length) i = 0 while i < length: buf.ll_append_char(cast_primitive(target, lst.ll_getitem_fast(i))) i += 1 return buf.ll_build()
def test_unwrap_object(self): A = ootype.Instance("A", ootype.ROOT, {}) a1 = ootype.new(A) a2 = ootype.new(A) obj1 = ootype.cast_to_object(a1) obj2 = ootype.cast_to_object(a2) def fn(flag): if flag: obj = obj1 else: obj = obj2 a3 = ootype.cast_from_object(A, obj) return a3 is a1 res = self.interpret(fn, [True], backendopt=False) assert res is True
def test_invalid_cache(): DT = Dict(Signed, Signed) d = new(DT) py.test.raises(AssertionError, d.ll_get, 0) d.ll_set(42, 1) d.ll_contains(43) py.test.raises(AssertionError, d.ll_get, 42)
def ll_rangeiter(ITER, rng): iter = new(ITER) iter.next = rng.start iter.stop = rng.stop if ITER is RANGESTITER: iter.step = rng.step return iter
def ll_known_maxlength2list(RESLIST, l): res = ootype.new(RESLIST) length = l.length res._ll_resize_ge(length) for i in range(length): item = l.items.ll_getitem_fast(i) res.ll_setitem_fast(i, item) return res
def ll_newrangest(start, stop, step): if step == 0: raise ValueError l = new(RANGEST) l.start = start l.stop = stop l.step = step return l
def ll_decode_latin1(self, value): sb = ootype.new(ootype.UnicodeBuilder) length = value.ll_strlen() sb.ll_allocate(length) for i in range(length): c = value.ll_stritem_nonneg(i) sb.ll_append_char(cast_primitive(UniChar, c)) return sb.ll_build()
def ll_popitem(ELEM, d): it = d.ll_get_items_iterator() if it.ll_go_next(): res = ootype.new(ELEM) key = res.item0 = it.ll_current_key() res.item1 = it.ll_current_value() d.ll_remove(key) return res raise KeyError
def ll_encode_latin1(self, value): sb = ootype.new(ootype.StringBuilder) length = value.ll_strlen() sb.ll_allocate(length) for i in range(length): c = value.ll_stritem_nonneg(i) if ord(c) > 255: raise UnicodeEncodeError("%d > 255, not latin-1" % ord(c)) sb.ll_append_char(cast_primitive(Char, c)) return sb.ll_build()
def fn(): a = ootype.new(A) ahash = ootype.identityhash(a) obj = ootype.cast_to_object(a) native = cast_to_native_object(obj) name = native.GetType().get_Name() obj2 = cast_from_native_object(native) a2 = ootype.cast_from_object(A, obj2) a2hash = ootype.identityhash(a2) return name, ahash == a2hash
def test_iteritems(): DT = Dict(Signed, Float) d = new(DT) d.ll_set(42, 43.0) d.ll_set(52, 53.0) it = d.ll_get_items_iterator() items = [] while it.ll_go_next(): items.append((it.ll_current_key(), it.ll_current_value())) items.sort() assert items == [(42, 43.0), (52, 53.0)]
def get_meta_instance(self, cast_to_root_meta=True): if self.lowleveltype == ootype.Class: raise TyperError("no meta-instance for class %r" % (self.classdef, )) if self.meta_instance is None: self.meta_instance = ootype.new(self.lowleveltype) self.setup_meta_instance(self.meta_instance, self) meta_instance = self.meta_instance if cast_to_root_meta: meta_instance = ootype.ooupcast(META, meta_instance) return meta_instance
def get_meta_instance(self, cast_to_root_meta=True): if self.lowleveltype == ootype.Class: raise TyperError("no meta-instance for class %r" % (self.classdef,)) if self.meta_instance is None: self.meta_instance = ootype.new(self.lowleveltype) self.setup_meta_instance(self.meta_instance, self) meta_instance = self.meta_instance if cast_to_root_meta: meta_instance = ootype.ooupcast(META, meta_instance) return meta_instance
def ll_str_mul(s, times): if times < 0: times = 0 try: size = ovfcheck(s.ll_strlen() * times) except OverflowError: raise MemoryError buf = ootype.new(typeOf(s).builder) buf.ll_allocate(size) for i in xrange(times): buf.ll_append(s) return buf.ll_build()
def ll_str(self, value): if not value: return self.ll.ll_constant('None') sb = ootype.new(ootype.StringBuilder) lgt = value.ll_strlen() sb.ll_allocate(lgt) for i in range(lgt): c = value.ll_stritem_nonneg(i) if ord(c) > 127: raise UnicodeEncodeError("%d > 127, not ascii" % ord(c)) sb.ll_append_char(cast_primitive(Char, c)) return sb.ll_build()
def ll_int2oct(i, addPrefix): if not addPrefix or i==0: return oostring(i, 8) buf = new(StringBuilder) if i<0: if i == SPECIAL_VALUE: return SPECIAL_VALUE_OCT i = -i buf.ll_append_char('-') buf.ll_append_char('0') buf.ll_append(oostring(i, 8)) return buf.ll_build()
def ll_dictnext(RETURNTYPE, iter): # note that RETURNTYPE is None for keys and values it = iter.iterator if not it.ll_go_next(): raise StopIteration if kind == 'keys': return it.ll_current_key() elif kind == 'values': return it.ll_current_value() elif kind == 'items': res = ootype.new(RETURNTYPE) res.item0 = it.ll_current_key() res.item1 = it.ll_current_value() return res
def ll_int2hex(i, addPrefix): if not addPrefix: return oostring(i, 16) buf = new(StringBuilder) if i<0: if i == SPECIAL_VALUE: return SPECIAL_VALUE_HEX i = -i buf.ll_append_char('-') buf.ll_append_char('0') buf.ll_append_char('x') buf.ll_append(oostring(i, 16)) return buf.ll_build()
def ll_dict_kvi(LIST, d): length = d.ll_length() result = LIST.ll_newlist(length) it = d.ll_get_items_iterator() i = 0 while it.ll_go_next(): if kind == 'keys': result.ll_setitem_fast(i, it.ll_current_key()) elif kind == 'values': result.ll_setitem_fast(i, it.ll_current_value()) elif kind == 'items': r = ootype.new(LIST.ITEM) r.item0 = it.ll_current_key() # TODO: do we need casting? r.item1 = it.ll_current_value() result.ll_setitem_fast(i, r) i += 1 assert i == length return result
def ll_join(s, length_dummy, lst): length = lst.ll_length() buf = ootype.new(typeOf(s).builder) # TODO: check if it's worth of preallocating the buffer with # the exact length ## itemslen = 0 ## i = 0 ## while i < length: ## itemslen += lst.ll_getitem_fast(i).ll_strlen() ## i += 1 ## resultlen = itemslen + s.ll_strlen()*(length-1) ## buf.ll_allocate(resultlen) i = 0 while i < length - 1: item = lst.ll_getitem_fast(i) buf.ll_append(item) buf.ll_append(s) i += 1 if length > 0: lastitem = lst.ll_getitem_fast(i) buf.ll_append(lastitem) return buf.ll_build()
def ll_join(s, length_dummy, lst): length = lst.ll_length() buf = ootype.new(typeOf(s).builder) # TODO: check if it's worth of preallocating the buffer with # the exact length ## itemslen = 0 ## i = 0 ## while i < length: ## itemslen += lst.ll_getitem_fast(i).ll_strlen() ## i += 1 ## resultlen = itemslen + s.ll_strlen()*(length-1) ## buf.ll_allocate(resultlen) i = 0 while i < length-1: item = lst.ll_getitem_fast(i) buf.ll_append(item) buf.ll_append(s) i += 1 if length > 0: lastitem = lst.ll_getitem_fast(i) buf.ll_append(lastitem) return buf.ll_build()
def ll_listiter(ITER, lst): iter = ootype.new(ITER) iter.iterable = lst iter.index = 0 return iter
def fn(x, y): a = ootype.new(A) meth = a.m args = (x, y) return meth(*args)
def ll_tupleiter(ITERINST, tuple): iter = ootype.new(ITERINST) iter.iterable = tuple return iter
def ll_build_start(parts_count): return ootype.new(ootype.StringBuilder)
def ll_unicodeiter(string): iter = ootype.new(unicode_repr.string_iterator_repr.lowleveltype) iter.string = string iter.index = 0 return iter
def ll_newlist_maxlength(LIST, ARRAY, length): lst = ootype.new(LIST) lst.items = ootype.oonewarray(ARRAY, length) lst.length = 0 return lst
def ll_newlist(LIST, length): lst = ootype.new(LIST) lst._ll_resize(length) return lst
def create_instance(self): return ootype.new(self.object_type)
def fn3(x): return new(C).bar(x)
def f(): buf = ootype.new(ootype.StringBuilder) buf.ll_append(myconst) return buf.ll_build()
def test_string_builder(): b = ootype.new(ootype.StringBuilder) b.ll_append_char('a') b.ll_append(ootype.make_string('bcd')) res = b.ll_build() assert res._str == 'abcd'
def fn1(): return new(C).foo('a')
def ll_newrange(_RANGE, start, stop): l = new(RANGE) l.start = start l.stop = stop return l
def fn2(): return new(C).foo('aa')
def create(): if isinstance(TYPE, ootype.OOType): return boxresult(TYPE, ootype.new(TYPE)) return None
def fn(flag): a = ootype.new(A) a.xx = 42 b_obj = box(a) a2 = unbox(b_obj, A) return a2.xx