def test_convert_from_llvalue(self): def func(): x = c_ushort(5) pointer(x)[0] += 1 assert x.value == 6 x = c_char('A') pointer(x)[0] = chr(ord(pointer(x)[0]) + 1) assert x.value == 'B' x = c_longlong(5) pointer(x)[0] += 1 assert x.value == r_longlong(6) x = c_ulonglong(5) pointer(x)[0] += 1 assert x.value == r_ulonglong(6) # x = c_float(2.5) # pointer(x)[0] += 0.25 # assert x.value == 2.75 # pointer(x)[0] -= 1 # assert x.value == 1.75 x = c_double(2.5) pointer(x)[0] += 0.25 assert x.value == 2.75 pointer(x)[0] -= 1 assert x.value == 1.75 # x = c_wchar(u'A') # pointer(x)[0] = unichr(ord(pointer(x)[0]) + 1) # assert x.value == u'B' interpret(func, [])
def test_some_generic_function_call(self): def h(x): return int(x) def c(x): return int(x) + 1 def default(x): return int(x) + 3 def g(a, x): if x == -1: a = None if x > 0: if x == 1: a = h else: a = c x = x + 0.01 return a(x) def f(x): return g(default, x) g._annenforceargs_ = policy.Sig( annmodel.SomeGenericCallable(args=[annmodel.SomeFloat()], result=annmodel.SomeInteger()), float) assert interpret(f, [1.]) == 1 assert interpret(f, [10.]) == 11 assert interpret(f, [-3.]) == 0
def test_specialize_array_sub_1_0(self): def f(): a1 = numpy.array(range(4, 10)) a2 = numpy.array([3]) return a1 - a2 data = [i - 3 for i in range(4, 10)] res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i] def f(): a = numpy.array(range(4, 10)) return a - 3 res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i] def f(): a = numpy.array(range(4, 10)) return 3 - a data = [3 - i for i in range(4, 10)] res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i]
def test_isdir(): if sys.platform != 'win32': py.test.skip("XXX cannot run os.stat() on the llinterp yet") s = str(udir.join('test_isdir')) def f(): return os.path.isdir(s) res = interpret(f, []) assert res == os.path.isdir(s) os.mkdir(s) res = interpret(f, []) assert res is True # On Windows, the libc stat() is flawed: # stat('c:/temp') works # but stat('c:/temp/') does not find the directory... # This test passes with our own stat() implementation. s += os.path.sep def f(): return os.path.isdir(s) res = interpret(f, []) assert res is True
def test_some_generic_function_call(self): def h(x): return int(x) def c(x): return int(x) + 1 def default(x): return int(x) + 3 def g(a, x): if x == -1: a = None if x > 0: if x == 1: a = h else: a = c x = x + 0.01 return a(x) def f(x): return g(default, x) g._annenforceargs_ = policy.Sig(annmodel.SomeGenericCallable( args=[annmodel.SomeFloat()], result=annmodel.SomeInteger()), float) assert interpret(f, [1.]) == 1 assert interpret(f, [10.]) == 11 assert interpret(f, [-3.]) == 0
def test_specialize_array_add_1_0(self): def f(): a1 = numpy.array(range(4, 10)) a2 = numpy.array([3]) return a1 + a2 data = [i + 3 for i in range(4, 10)] res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i] def f(): a = numpy.array(range(4, 10)) return a + 3 res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i] def f(): a = numpy.array(range(4, 10)) return 3 + a res = interpret(f, []) for i in range(len(data)): assert res.dataptr[i] == data[i]
def test_cast_opaque_ptr(): O = GcOpaqueType('O') Q = GcOpaqueType('Q') S = GcStruct('S', ('x', Signed)) def fn(): s = malloc(S) o = cast_opaque_ptr(Ptr(O), s) q = cast_opaque_ptr(Ptr(Q), o) p = cast_opaque_ptr(Ptr(S), q) return p == s res = interpret(fn, []) assert res is True O1 = OpaqueType('O') S1 = Struct('S1', ('x', Signed)) s1 = malloc(S1, immortal=True) def fn1(): o1 = cast_opaque_ptr(Ptr(O1), s1) p1 = cast_opaque_ptr(Ptr(S1), o1) return p1 == s1 res = interpret(fn1, []) assert res is True
def test_overflow_bug(): CASE = [ (-144, -248), # \ cycle (-248, -144), # / (-488, -416), # \ two usages of -488 (-488, -480), # / (-488, -488), # - one self-application of -488 ] class FakeAssembler: def regalloc_mov(self, src, dst): print "mov", src, dst def regalloc_push(self, x): print "push", x def regalloc_pop(self, x): print "pop", x def regalloc_immedmem2mem(self, x, y): print "?????????????????????????" def main(): srclocs = [StackLoc(9999, x, "i") for x, y in CASE] dstlocs = [StackLoc(9999, y, "i") for x, y in CASE] remap_frame_layout(FakeAssembler(), srclocs, dstlocs, eax) # it works when run directly main() # but it used to crash when translated, # because of a -sys.maxint-2 overflowing to sys.maxint from pypy.rpython.test.test_llinterp import interpret interpret(main, [])
def test_isinstance(): class A: _alloc_flavor_ = "raw" class B(A): pass class C(B): pass def f(i): if i == 0: o = None elif i == 1: o = A() elif i == 2: o = B() else: o = C() return 100*isinstance(o, A)+10*isinstance(o, B)+1*isinstance(o ,C) a = RPythonAnnotator() #does not raise: s = a.build_types(f, [int]) assert s.knowntype == int rtyper = RPythonTyper(a) rtyper.specialize() res = interpret(f, [1]) assert res == 100 res = interpret(f, [2]) assert res == 110 res = interpret(f, [3]) assert res == 111 res = interpret(f, [0]) assert res == 0
def test_convert_pointers(self): strlen = CFUNCTYPE(c_int, c_void_p)() strlen.__name__ = 'strlen' def ll_strlen_from_void_p(adr): i = 0 while adr.char[i] != '\x00': i += 1 return i strlen.llinterp_friendly_version = ll_strlen_from_void_p PTR = c_char_p("hello") BUF = create_string_buffer(10) BUF.value = "hello" ARR = (c_byte * 10)(65, 66, 67) def func(n): # constant arguments XXX in-progress ## assert strlen("hello") == 5 ## assert strlen(PTR) == 5 ## assert strlen(BUF) == 5 ## assert strlen(ARR) == 3 # variable arguments s = chr(n) + 'bc' assert strlen(s) == 3 assert strlen(c_char_p(s)) == 3 assert strlen((c_char * 6)('a', 'b')) == 2 # XXX Bytes are not chars in llinterp. # assert strlen((c_byte * 6)(104,101,108,108,111)) == 5 buf = create_string_buffer(10) buf.value = "hello" assert strlen(buf) == 5 interpret(func, [65])
def test_rpython_merge_RWeakValueDictionary2(): class A(object): def __init__(self): self.d = RWeakValueDictionary(str, A) def f(self, key): a = A() self.d.set(key, a) return a empty = A() def f(x): a = A() if x: a = empty a2 = a.f("a") assert a.d.get("a") is a2 f(0) interpret(f, [0]) f(1) interpret(f, [1]) def g(x): if x: d = RWeakValueDictionary(str, X) else: d = RWeakValueDictionary(str, Y) d.set("x", X()) py.test.raises(Exception, interpret, g, [1])
def test_overflow_bug(): CASE = [ (-144, -248), # \ cycle (-248, -144), # / (-488, -416), # \ two usages of -488 (-488, -480), # / (-488, -488), # - one self-application of -488 ] class FakeAssembler: def regalloc_mov(self, src, dst): print "mov", src, dst def regalloc_push(self, x): print "push", x def regalloc_pop(self, x): print "pop", x def regalloc_immedmem2mem(self, x, y): print "?????????????????????????" def main(): srclocs = [StackLoc(9999, x, 'i') for x, y in CASE] dstlocs = [StackLoc(9999, y, 'i') for x, y in CASE] remap_frame_layout(FakeAssembler(), srclocs, dstlocs, eax) # it works when run directly main() # but it used to crash when translated, # because of a -sys.maxint-2 overflowing to sys.maxint from pypy.rpython.test.test_llinterp import interpret interpret(main, [])
def test_callback_field_bound_method(): py.test.skip("needs an rtyping hack to help the js backend " "or generalized bound methods support in many places") class A: def x(self, i): return float(i) aa = A() def callback(x): return 8.3 + x def callback_field(i): a = CD() if i: a.callback_field = aa.x else: a.callback_field = callback return a.callback_field(i+1) res = interpret(callback_field, [1], type_system="ootype") assert res == 2.0 assert isinstance(res, float) res = interpret(callback_field, [0], type_system="ootype") assert res == 8.3
def test_mix_class_record_instance(): I = Instance("test", ROOT, {"a": Signed}) R = Record({"x": Signed}) L = List(Signed) c1 = runtimeClass(I) c2 = runtimeClass(R) c3 = runtimeClass(L) c4 = runtimeClass(Class) def fn(flag): if flag == 0: return c1 elif flag == 1: return c2 elif flag == 2: return c3 else: return c4 res = interpret(fn, [0], type_system='ootype') assert res is c1 res = interpret(fn, [1], type_system='ootype') assert res is c2 res = interpret(fn, [2], type_system='ootype') assert res is c3 res = interpret(fn, [3], type_system='ootype') assert res is c4
def test_find(self): f = open(self.tmpname + "g", "w+") f.write("foobarfoobar\0") f.flush() def func(no): m = mmap.mmap(no, 12) assert m.find("\0", 0, 13) == -1 # no searching past the stop assert m.find("\0", 0, 13, True) == -1 m.close() # m = mmap.mmap(no, 13) assert m.find("b", 0, 7) == 3 assert m.find("z", 0, 7) == -1 assert m.find("o", 11, 13) == -1 assert m.find("ob", 0, 7) == 2 assert m.find("\0", 0, 13) == 12 assert m.find("o", 1, 4) == 1 assert m.find("o", 2, 4) == 2 assert m.find("o", 2, -4) == 2 assert m.find("o", 8, -5) == -1 m.close() func(f.fileno()) interpret(func, [f.fileno()]) f.close()
def test_convert_pointers(self): py.test.skip("in-progress") from pypy.rpython.rctypes.rchar_p import ll_strlen strlen = CFUNCTYPE(c_int, c_char_p)() # not directly executable! strlen.__name__ = 'strlen' strlen.llinterp_friendly_version = ll_strlen PTR = c_char_p("hello") BUF = create_string_buffer(10) BUF.value = "hello" def func(n): # constant arguments assert strlen("hello") == 5 assert strlen(PTR) == 5 assert strlen(BUF) == 5 # variable arguments s = chr(n) + 'bc' assert strlen(s) == 3 assert strlen(c_char_p(s)) == 3 assert strlen((c_char * 6)('a', 'b')) == 2 buf = create_string_buffer(10) buf.value = "hello" assert strlen(buf) == 5 interpret(func, [65])
def test_ntpath(): import ntpath def f(): assert ntpath.join("\\foo", "bar") == "\\foo\\bar" assert ntpath.join("c:\\foo", "spam\\egg") == "c:\\foo\\spam\\egg" assert ntpath.join("c:\\foo", "d:\\bar") == "d:\\bar" interpret(f, [])
def test_set_item(self): f = open(self.tmpname + "s", "w+") f.write("foobar") f.flush() def func(no): m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE) # def f(m): m[1:3] = u'xx' # py.test.raises(IndexError, f, m) # def f(m): m[1:4] = "zz" # py.test.raises(IndexError, f, m) # def f(m): m[1:6] = "z" * 6 # py.test.raises(IndexError, f, m) # def f(m): m[:2] = "z" * 5 # m[1:3] = 'xx' # assert m.read(6) == "fxxbar" # m.seek(0) m.setitem(0, 'x') assert m.getitem(0) == 'x' m.setitem(-6, 'y') data = m.read(6) assert data == "yoobar" # yxxbar with slice's stuff m.close() interpret(func, [f.fileno()]) f.close()
def test_specialize_broadcast(self): def f(): a = numpy.empty((4, 3), dtype='i') b = numpy.array([33]) a[:, :] = b return a res = interpret(f, []) for i in range(4 * 3): assert res.dataptr[i] == 33 def f(): a = numpy.empty((4, 3), dtype='i') b = numpy.array([33]) a[:, ] = b return a res = interpret(f, []) for i in range(4 * 3): assert res.dataptr[i] == 33 def f(): a = numpy.empty((4, 3, 2), dtype='i') a[:] = numpy.array([33]) a[0, :] = numpy.array([22]) return a res = interpret(f, []) data = [22] * 6 + [33] * 18 for i in range(3 * 4 * 2): assert res.dataptr[i] == data[i]
def test_around_extcall(self): if sys.platform == "win32": py.test.skip('No pipes on windows') import os from pypy.annotation import model as annmodel from pypy.rlib.objectmodel import invoke_around_extcall from pypy.rpython.extfuncregistry import register_external read_fd, write_fd = os.pipe() try: # we need an external function that is not going to get wrapped around # before()/after() calls, in order to call it from before()/after()... def mywrite(s): os.write(write_fd, s) def llimpl(s): s = ''.join(s.chars) os.write(write_fd, s) register_external(mywrite, [str], annmodel.s_None, 'll_mywrite', llfakeimpl=llimpl, sandboxsafe=True) def before(): mywrite("B") def after(): mywrite("A") def f(): os.write(write_fd, "-") invoke_around_extcall(before, after) os.write(write_fd, "E") interpret(f, []) data = os.read(read_fd, 99) assert data == "-BEA" finally: os.close(write_fd) os.close(read_fd)
def test_rename(self): def f(): return rposix.rename(self.path, self.path2) interpret(f, []) assert not os.path.exists(self.ufilename) assert os.path.exists(self.ufilename + '.new')
def test_cast_adr_to_int(): S = Struct('S') p = malloc(S, immortal=True) def fn(n): a = llmemory.cast_ptr_to_adr(p) if n == 2: return llmemory.cast_adr_to_int(a, "emulated") elif n == 4: return llmemory.cast_adr_to_int(a, "symbolic") else: return llmemory.cast_adr_to_int(a, "forced") res = interpret(fn, [2]) assert is_valid_int(res) assert res == cast_ptr_to_int(p) # res = interpret(fn, [4]) assert isinstance(res, llmemory.AddressAsInt) assert llmemory.cast_int_to_adr(res) == llmemory.cast_ptr_to_adr(p) # res = interpret(fn, [6]) assert is_valid_int(res) from pypy.rpython.lltypesystem import rffi assert res == rffi.cast(Signed, p)
def test_specialize_null_pointer(self): def fn(): p = POINTER(c_int)() assert not p p.contents = c_int(12) assert p interpret(fn, [])
def test_rpython_merge_RWeakValueDictionary2(): class A(object): def __init__(self): self.d = RWeakValueDictionary(A) def f(self, key): a = A() self.d.set(key, a) return a empty = A() def f(x): a = A() if x: a = empty a2 = a.f("a") assert a.d.get("a") is a2 f(0) interpret(f, [0]) f(1) interpret(f, [1]) def g(x): if x: d = RWeakValueDictionary(X) else: d = RWeakValueDictionary(Y) d.set("x", X()) return 41 py.test.raises(Exception, interpret, g, [1])
def test_callback_field_bound_method(): py.test.skip("needs an rtyping hack to help the js backend " "or generalized bound methods support in many places") class A: def x(self, i): return float(i) aa = A() def callback(x): return 8.3 + x def callback_field(i): a = CD() if i: a.callback_field = aa.x else: a.callback_field = callback return a.callback_field(i + 1) res = interpret(callback_field, [1], type_system="ootype") assert res == 2.0 assert isinstance(res, float) res = interpret(callback_field, [0], type_system="ootype") assert res == 8.3
def test_value_for_various_types(self): def func(): x = c_ushort(5) x.value += 1 assert x.value == 6 x = c_char('A') x.value = chr(ord(x.value) + 1) assert x.value == 'B' x = c_longlong(5) x.value += 1 assert x.value == r_longlong(6) x = c_ulonglong(5) x.value += 1 assert x.value == r_ulonglong(6) # x = c_float(2.5) # x.value += 0.25 # assert x.value == 2.75 # x.value -= 1 # assert x.value == 1.75 x = c_double(2.5) x.value += 0.25 assert x.value == 2.75 x.value -= 1 assert x.value == 1.75 # x = c_wchar(u'A') # x.value = unichr(ord(x.value) + 1) # assert x.value == u'B' interpret(func, [])
def test_prebuilt(): c = C(111) b = B(939393) def makeint(n): if n < 0: x = c elif n > 0: x = C(n) else: x = b return x def fn(n): x = makeint(n) if isinstance(x, B): return 'B', x.normalint elif isinstance(x, C): return 'C', x.smallint else: return 'A', 0 res = interpret(fn, [12], taggedpointers=True) assert res.item0 == 'C' assert res.item1 == 12 res = interpret(fn, [-1], taggedpointers=True) assert res.item0 == 'C' assert res.item1 == 111 res = interpret(fn, [0], taggedpointers=True) assert res.item0 == 'B' assert res.item1 == 939393
def test_posixpath(): import posixpath def f(): assert posixpath.join("/foo", "bar") == "/foo/bar" assert posixpath.join("/foo", "spam/egg") == "/foo/spam/egg" assert posixpath.join("/foo", "/bar") == "/bar" interpret(f, [])
def test_specialize_broadcast(self): def f(): a = numpy.empty((4, 3), dtype="i") b = numpy.array([33]) a[:, :] = b return a res = interpret(f, []) for i in range(4 * 3): assert res.dataptr[i] == 33 def f(): a = numpy.empty((4, 3), dtype="i") b = numpy.array([33]) a[:,] = b return a res = interpret(f, []) for i in range(4 * 3): assert res.dataptr[i] == 33 def f(): a = numpy.empty((4, 3, 2), dtype="i") a[:] = numpy.array([33]) a[0, :] = numpy.array([22]) return a res = interpret(f, []) data = [22] * 6 + [33] * 18 for i in range(3 * 4 * 2): assert res.dataptr[i] == data[i]
def test_llinterpreted(): from pypy.rpython.test.test_llinterp import interpret def f(): test_pack() test_unpack() interpret(f, [])
def test_dfa_compiledummy(): def main(gets): a = getautomaton() dfatable, final_states = convertdfa(a) s = ["aaaaaaaaaab", "aaaa"][gets] return recognizetable(dfatable, s, final_states) assert interpret(main, [0]) assert not interpret(main, [1])
def test_dfa_compiledummy2(): def main(gets): a = getautomaton() alltrans, final_states = convertagain(a) s = ["aaaaaaaaaab", "aaaa"][gets] return recognizeparts(alltrans, final_states, s) assert interpret(main, [0]) assert not interpret(main, [1])
def test_string_annotation(): def oof(lst): return lst.ll_strlen() s = new(String) assert interpret(oof, [s], type_system='ootype') == 0 s = make_string('foo') assert interpret(oof, [s], type_system='ootype') == 3
def test_specialize_ass_value(self): # reading .value is not allowed, as it can't be annotated! def func(x): o = py_object() o.value = x interpret(func, [9]) interpret(func, [9.2])
def test_hash_equal_whatever_ootype(): def fn(c): s1 = ootype.oostring("xy", -1) s2 = ootype.oostring("x" + chr(c), -1) assert (hash_whatever(ootype.typeOf(s1), s1) == hash_whatever(ootype.typeOf(s2), s2)) assert equal_whatever(ootype.typeOf(s1), s1, s2) fn(ord('y')) interpret(fn, [ord('y')], type_system='ootype')
def test_specialize_multi(self): def f(ii, jj): a = numpy.empty((4, 5), dtype='i') for i in range(4): for j in range(5): a[i, j] = i*j return a[ii, jj] assert interpret(f, [0, 0]) == 0 assert interpret(f, [3, 4]) == 12
def test_check_nonneg(): def f(x): assert x >= 5 check_nonneg(x) interpret(f, [9]) def g(x): check_nonneg(x-1) py.test.raises(IntegerCanBeNegative, interpret, g, [9])
def test_args_from_rarith_int(self): from pypy.rpython.test.test_llinterp import interpret def fn(): return (rbigint.fromrarith_int(0), rbigint.fromrarith_int(17), rbigint.fromrarith_int(-17), rbigint.fromrarith_int(r_uint(0)), rbigint.fromrarith_int(r_uint(17))) interpret(fn, [])
def test_specialize_len(self): def func(n): buf = create_string_buffer(n) return len(buf) res = interpret(func, [17]) assert res == 17 res = interpret(func, [0]) assert res == 0
def test_select_timeout(): from time import time def f(): # once there was a bug where the sleeping time was doubled a = time() iwtd, owtd, ewtd = select([], [], [], 5.0) diff = time() - a assert 4.8 < diff < 9.0 interpret(f, [])
def test_specialize_multi(self): def f(ii, jj): a = numpy.empty((4, 5), dtype='i') for i in range(4): for j in range(5): a[i, j] = i * j return a[ii, jj] assert interpret(f, [0, 0]) == 0 assert interpret(f, [3, 4]) == 12
def test_interp_c(): def f(): return stack_frames_depth() def g(): return f() res_f = interpret(f, []) res_g = interpret(g, []) assert res_f == 2 assert res_g == 3
def test_obj_list(): def f(i, c): lis = [1, 2, 3, 4] lis[i] = c lis.append(i) return len(lis) res = interpret(f, [2, 'c'], someobjects=True) assert res == 5 res = interpret(f, [3, 'c'], someobjects=True) assert res == 5
def test_has_weakref_support(): assert has_weakref_support() res = interpret(lambda: has_weakref_support(), [], **{'translation.rweakref': True}) assert res == True res = interpret(lambda: has_weakref_support(), [], **{'translation.rweakref': False}) assert res == False
def test_method(): def fn(n): if n > 0: x = B(n) else: x = C(n) return x.meth(100) res = interpret(fn, [1000], taggedpointers=True) assert res == 1102 res = interpret(fn, [-1000], taggedpointers=True) assert res == -897
def test_format(): def fn(n): if n > 0: x = B(n) else: x = C(n) return '%r' % (x,) res = interpret(fn, [-832], taggedpointers=True) assert ''.join(res.chars) == '<unboxed -832>' res = interpret(fn, [1], taggedpointers=True) assert ''.join(res.chars).startswith('<B object')