Example #1
0
    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')
Example #2
0
    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()
Example #3
0
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
Example #4
0
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, [])
Example #5
0
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, [])
Example #6
0
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])
Example #7
0
    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()
Example #8
0
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
Example #9
0
    def test_around_extcall(self):
        if sys.platform == "win32":
            py.test.skip('No pipes on windows')
        import os
        from rpython.annotator import model as annmodel
        from rpython.rlib.objectmodel import invoke_around_extcall
        from rpython.rtyper.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)
Example #10
0
def test_key_instance():
    class K(object):
        pass
    keys = [K(), K(), K()]

    def g(d):
        assert d.get(keys[3]) is None
        x1 = X(); x2 = X(); x3 = X()
        d.set(keys[0], x1)
        d.set(keys[1], x2)
        d.set(keys[2], x3)
        assert d.get(keys[0]) is x1
        assert d.get(keys[1]) is x2
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        return x1, x3    # x2 dies
    def f():
        keys.append(K())
        d = RWeakValueDictionary(K, X)
        x1, x3 = g(d)
        rgc.collect(); rgc.collect()
        assert d.get(keys[0]) is x1
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
        d.set(keys[0], None)
        assert d.get(keys[0]) is None
        assert d.get(keys[1]) is None
        assert d.get(keys[2]) is x3
        assert d.get(keys[3]) is None
    f()
    interpret(f, [])
Example #11
0
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 = [FrameLoc(9999, x, 'i') for x,y in CASE]
        dstlocs = [FrameLoc(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 rpython.rtyper.test.test_llinterp import interpret
    interpret(main, [])
Example #12
0
def test_make_sure_not_resized_annorder():
    def f(n):
        if n > 5:
            result = None
        else:
            result = [1,2,3]
        make_sure_not_resized(result)
    interpret(f, [10])
Example #13
0
def test_bogus_makekey():
    class X: pass
    class Y: pass
    def g():
        X(); Y()
        RWeakValueDictionary(str, X).get("foobar")
        RWeakValueDictionary(int, Y).get(42)
    interpret(g, [])
Example #14
0
def test_operation_with_float():
    def f(x):
        assert r_longlong(x) + 0.5 == 43.5
        assert r_longlong(x) - 0.5 == 42.5
        assert r_longlong(x) * 0.5 == 21.5
        assert r_longlong(x) / 0.8 == 53.75
    f(43)
    interpret(f, [43])
Example #15
0
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
Example #16
0
 def test_stat(self):
     def f():
         return rposix_stat.stat(self.path).st_mtime
     if sys.platform == 'win32':
         # double vs. float, be satisfied with sub-millisec resolution
         assert abs(interpret(f, []) - os.stat(self.ufilename).st_mtime) < 1e-4
     else:
         assert interpret(f, []) == os.stat(self.ufilename).st_mtime
Example #17
0
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])
Example #18
0
    def test_putenv(self):
        from rpython.rlib import rposix_environ

        def f():
            rposix.putenv(self.path, self.path)
            rposix.unsetenv(self.path)

        interpret(f, [],     # does not crash
                  malloc_check=rposix_environ.REAL_UNSETENV)
Example #19
0
    def test_fchmodat(self):
        def f(dirfd):
            return rposix.fchmodat('test_open_ascii', 0777, dirfd)

        dirfd = os.open(os.path.dirname(self.ufilename), os.O_RDONLY)
        try:
            interpret(f, [dirfd])  # does not crash
        finally:
            os.close(dirfd)
Example #20
0
def test_cast_gcref_to_int():
    A = lltype.GcArray(Address)
    def f():
        ptr = lltype.malloc(A, 10)
        gcref = lltype.cast_opaque_ptr(GCREF, ptr)
        adr = lltype.cast_ptr_to_int(gcref)
        assert adr == lltype.cast_ptr_to_int(ptr)
    f()
    interpret(f, [])
Example #21
0
def test_contains_with_hash():
    def f(i):
        d = {i + .5: 5}
        assert contains_with_hash(d, i + .5, compute_hash(i + .5))
        assert not contains_with_hash(d, i + .3, compute_hash(i + .3))
        return 0

    f(29)
    interpret(f, [29])
Example #22
0
def test_rpython_merge_RWeakKeyDictionary4():
    def g(x):
        if x:
            d = RWeakKeyDictionary(KX, VX)
        else:
            d = RWeakKeyDictionary(KX, VY)
        d.set(KX(), VX())

    with py.test.raises(UnionError):
        interpret(g, [1])
Example #23
0
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
Example #24
0
    def test_unlinkat(self):
        def f(dirfd):
            return rposix.unlinkat('test_open_ascii', dir_fd=dirfd)

        dirfd = os.open(os.path.dirname(self.ufilename), os.O_RDONLY)
        try:
            interpret(f, [dirfd])
        finally:
            os.close(dirfd)
        assert not os.path.exists(self.ufilename)
Example #25
0
    def test_utimensat(self):
        def f(dirfd):
            return rposix.utimensat('test_open_ascii',
                0, rposix.UTIME_NOW, 0, rposix.UTIME_NOW, dir_fd=dirfd)

        dirfd = os.open(os.path.dirname(self.ufilename), os.O_RDONLY)
        try:
            interpret(f, [dirfd])  # does not crash
        finally:
            os.close(dirfd)
Example #26
0
def test_rpython_merge_RWeakValueDictionary3():
    def g(x):
        if x:
            d = RWeakValueDictionary(str, X)
        else:
            d = RWeakValueDictionary(str, Y)
        d.set("x", X())

    with py.test.raises(UnionError):
        interpret(g, [1])
Example #27
0
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')
Example #28
0
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
Example #29
0
def test_select_timeout():
    if os.name == 'nt':
        py.test.skip('cannot select on file handles on windows')
    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, [])
Example #30
0
def test_hash_equal_whatever_lltype():
    s1 = rstr.mallocstr(2)
    s2 = rstr.mallocstr(2)
    s1.chars[0] = 'x'; s1.chars[1] = 'y'
    s2.chars[0] = 'x'; s2.chars[1] = 'y'
    def fn(x):
        assert hash_whatever(lltype.typeOf(x), x) == 42
        assert (hash_whatever(lltype.typeOf(s1), s1) ==
                hash_whatever(lltype.typeOf(s2), s2))
        assert equal_whatever(lltype.typeOf(s1), s1, s2)
    fn(42)
    interpret(fn, [42], type_system='lltype')
Example #31
0
def test_get_untagged_value():
    def fn1(n):
        return C(n).get_untagged_value()

    res = interpret(fn1, [42], taggedpointers=True)
    assert res == 42
Example #32
0
def test_ptradd_interpret():
    interpret(test_ptradd, [])
Example #33
0
def test_rpython_RWeakValueDictionary():
    interpret(make_test(loop=12), [])
Example #34
0
 def inner(arg0=-1, arg1=-1):
     return interpret(f, [i, arg0, arg1])
Example #35
0
def test_simple():
    def dummyfn(x):
        return x + 1

    res = interpret(dummyfn, [7])
    assert res == 8
Example #36
0
    def test_page_size(self):
        def f():
            assert mmap.PAGESIZE > 0
            assert isinstance(mmap.PAGESIZE, int)

        interpret(f, [])
Example #37
0
 def run(self, func, args):
     return interpret(func, args)
Example #38
0
def test_rpython_RWeakValueDictionary_int():
    interpret(make_test(loop=12, keyclass=int), [])
Example #39
0
    def test_unlink(self):
        def f():
            return rposix.unlink(self.path)

        interpret(f, [])
        assert not os.path.exists(self.ufilename)
Example #40
0
def test_annotation():
    diff = interpret(timer, [])
    assert diff > 1000
Example #41
0
    def test_putenv(self):
        def f():
            rposix.putenv(self.path, self.path)
            rposix.unsetenv(self.path)

        interpret(f, [])  # does not crash
Example #42
0
def test_byteswap_interpret():
    interpret(test_byteswap, [])
Example #43
0
def test_attribute():
    def fn1(n):
        return C(n).smallint

    res = interpret(fn1, [42], taggedpointers=True)
    assert res == 42
Example #44
0
    def test_access(self):
        def f():
            return rposix.access(self.path, os.R_OK)

        assert interpret(f, []) == 1
Example #45
0
 def interpret(self, f, args):
     return interpret(f, args, type_system='lltype')
Example #46
0
    def test_chmod(self):
        def f():
            return rposix.chmod(self.path, 0777)

        interpret(f, [])  # does not crash
Example #47
0
    def test_utime(self):
        def f():
            return rposix.utime(self.path, None)

        interpret(f, [])  # does not crash