def f(n): obj1 = ootype.new(A) if n: obj2 = obj1 else: obj2 = ootype.new(A) return obj1 is obj2
def f(n): obj1 = ootype.new(A) obj2 = ootype.new(B) s1 = ootype.oostring(obj1, -1) s2 = ootype.oostring(obj2, -1) ch1 = s1.ll_stritem_nonneg(1) ch2 = s2.ll_stritem_nonneg(1) return ord(ch1) + ord(ch2)
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_join_chars(length_dummy, lst): if typeOf(lst)._ITEMTYPE == Char: 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_char(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 fn_mix_null(flag): a = ootype.new(A) obj = ootype.cast_to_object(a) if flag: return obj else: return ootype.NULL
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 fn_instance(): a = ootype.new(A) obj = ootype.cast_to_object(a) a2 = ootype.cast_from_object(A, obj) a3 = ootype.cast_from_object(ootype.ROOT, obj) assert a is a2 assert a is a3
def fn_is_true(flag): if flag: a = ootype.new(A) else: a = ootype.null(A) obj = ootype.cast_to_object(a) return bool(obj)
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 fn_record(): b = ootype.new(B) b.x = 42 obj = ootype.cast_to_object(b) b2 = ootype.cast_from_object(B, obj) assert b2.x == 42 assert b is b2
def make_pyexcclass2exc(self, rtyper): # ll_pyexcclass2exc(python_exception_class) -> exception_instance table = {} Exception_def = rtyper.annotator.bookkeeper.getuniqueclassdef(Exception) for clsdef in rtyper.class_reprs: if (clsdef and clsdef is not Exception_def and clsdef.issubclass(Exception_def)): if not hasattr(clsdef.classdesc, 'pyobj'): continue cls = clsdef.classdesc.pyobj if cls in self.standardexceptions and cls not in FORCE_ATTRIBUTES_INTO_CLASSES: is_standard = True assert not clsdef.attrs, ( "%r should not have grown attributes" % (cls,)) else: is_standard = (cls.__module__ == 'exceptions' and not clsdef.attrs) if is_standard: example = self.get_standard_ll_exc_instance(rtyper, clsdef) table[cls] = example r_inst = rclass.getinstancerepr(rtyper, None) r_inst.setup() r_class = rclass.getclassrepr(rtyper, None) r_class.setup() default_excinst = ootype.new(self.lltype_of_exception_value) default_excinst.meta = r_class.get_meta_instance() # build the table in order base classes first, subclasses last sortedtable = [] def add_class(cls): if cls in table: for base in cls.__bases__: add_class(base) sortedtable.append((cls, table[cls])) del table[cls] for cls in table.keys(): add_class(cls) assert table == {} initial_value_of_i = len(sortedtable) - 1 def pyexcclass2exc(python_exception_class): python_exception_class = python_exception_class._obj.value i = initial_value_of_i while i >= 0: if issubclass(python_exception_class, sortedtable[i][0]): return sortedtable[i][1] i -= 1 return default_excinst # This function will only be used by the llinterpreter which usually # expects a low-level callable (_meth, _static_meth), so we just # fake it here. FakeCallableType = ootype.OOType() FakeCallableType.ARGS = () class fake_callable(object): def __init__(self, fn): self._TYPE = FakeCallableType self._callable = fn return fake_callable(pyexcclass2exc)
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_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 h(x, y, z): s = ootype.new(S) s.x = x s.y = y fsm = llhelper(F, f) gsm = llhelper(G, g) assert typeOf(fsm) == F return fsm(s, z) + fsm(s, z * 2) + gsm(s)
def h(x, y, z): s = ootype.new(S) s.x = x s.y = y fsm = llhelper(F, f) gsm = llhelper(G, g) assert typeOf(fsm) == F return fsm(s, z)+fsm(s, z*2)+gsm(s)
def make_pyexcclass2exc(self, rtyper): # ll_pyexcclass2exc(python_exception_class) -> exception_instance table = {} Exception_def = rtyper.annotator.bookkeeper.getuniqueclassdef(Exception) for clsdef in rtyper.class_reprs: if (clsdef and clsdef is not Exception_def and clsdef.issubclass(Exception_def)): if not hasattr(clsdef.classdesc, 'pyobj'): continue cls = clsdef.classdesc.pyobj if cls in self.standardexceptions and cls not in FORCE_ATTRIBUTES_INTO_CLASSES: is_standard = True assert not clsdef.attrs, ( "%r should not have grown attributes" % (cls,)) else: is_standard = (cls.__module__ == 'exceptions' and not clsdef.attrs) if is_standard: example = self.get_standard_ll_exc_instance(rtyper, clsdef) table[cls] = example r_inst = rclass.getinstancerepr(rtyper, None) r_inst.setup() r_class = rclass.getclassrepr(rtyper, None) r_class.setup() default_excinst = ootype.new(self.lltype_of_exception_value) # build the table in order base classes first, subclasses last sortedtable = [] def add_class(cls): if cls in table: for base in cls.__bases__: add_class(base) sortedtable.append((cls, table[cls])) del table[cls] for cls in table.keys(): add_class(cls) assert table == {} initial_value_of_i = len(sortedtable) - 1 def pyexcclass2exc(python_exception_class): python_exception_class = python_exception_class._obj.value i = initial_value_of_i while i >= 0: if issubclass(python_exception_class, sortedtable[i][0]): return sortedtable[i][1] i -= 1 return default_excinst # This function will only be used by the llinterpreter which usually # expects a low-level callable (_meth, _static_meth), so we just # fake it here. FakeCallableType = ootype.OOType() FakeCallableType.ARGS = () class fake_callable(object): def __init__(self, fn): self._TYPE = FakeCallableType self._callable = fn return fake_callable(pyexcclass2exc)
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 get_meta_instance(self, cast_to_root_meta=True): 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(CLASSTYPE, meta_instance) return meta_instance
def ll_str(self, value): 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_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_cast_record_pbc(self): T = ootype.Record({'x': ootype.Signed}) record = ootype.new(T) record.x = 42 obj = cast_record_to_object(record) def fn(): record2 = cast_object_to_record(T, obj) return record is record2 res = self.interpret(fn, []) assert res
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 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 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 test_cast_record(self): T = ootype.Record({'x': ootype.Signed}) record = ootype.new(T) def fn(flag): if flag: obj = cast_record_to_object(record) else: obj = System.Object() record2 = cast_object_to_record(T, obj) return record is record2 res = self.interpret(fn, [True]) assert res
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 test_fieldinfo_for_const_pbc(self): A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed}) const = ootype.new(A) fieldinfo = fieldinfo_for_const(const) def fn(): const.xx = 42 obj = fieldinfo.GetValue(None) # get the 'xx' field by using reflection t = obj.GetType() x_info = t.GetField('xx') x_value = x_info.GetValue(obj) return unbox(x_value, ootype.Signed) res = self.interpret(fn, []) assert res == 42
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(iter, func, RETURNTYPE): it = iter.iterator if not it.ll_go_next(): raise StopIteration if func is dum_keys: return it.ll_current_key() elif func is dum_values: return it.ll_current_value() elif func is dum_items: res = ootype.new(RETURNTYPE) res.item0 = it.ll_current_key() res.item1 = it.ll_current_value() return res