Exemple #1
0
    def test_pack_float(self):
        def pack(x, size):
            result = []
            ieee.pack_float(result, x, size, False)
            l = []
            for x in result:
                for c in x:
                    l.append(str(ord(c)))
            return ','.join(l)
        c_pack = compile(pack, [float, int])

        def unpack(s):
            l = s.split(',')
            s = ''.join([chr(int(x)) for x in l])
            return ieee.unpack_float(s, False)
        c_unpack = compile(unpack, [str])

        def check_roundtrip(x, size):
            s = c_pack(x, size)
            if not isnan(x):
                # pack uses copysign which is ambiguous for NAN
                assert s == pack(x, size)
                assert unpack(s) == x
                assert c_unpack(s) == x
            else:
                assert isnan(unpack(s))
                assert isnan(c_unpack(s))

        for size in [2, 4, 8]:
            check_roundtrip(123.4375, size)
            check_roundtrip(-123.4375, size)
            check_roundtrip(INFINITY, size)
            check_roundtrip(NAN, size)
Exemple #2
0
    def test_translated(self):
        from rpython.translator.c.test.test_genc import compile

        def func(no):
            m = mmap.mmap(no, 1)
            r = m.read_byte()
            m.close()
            return r

        compile(func, [int], gcpolicy='boehm')
Exemple #3
0
    def test_utime_negative_fraction(self):
        def f(fname, t1):
            os.utime(fname, (t1, t1))

        fname = udir.join('test_utime_negative_fraction.txt')
        fname.ensure()
        t1 = -123.75
        compile(f, (str, float))(str(fname), t1)
        got = os.stat(str(fname)).st_mtime
        assert got == -123 or got == -123.75
Exemple #4
0
def test_os_path_isdir():
    directory = "./."
    def fn():
        return os.path.isdir(directory)
    f = compile(fn, [])
    assert f() == True
    directory = "some/random/name"
    def fn():
        return os.path.isdir(directory)
    f = compile(fn, [])
    assert f() == False
Exemple #5
0
def test_utimes():
    if os.name != 'nt':
        py.test.skip('Windows specific feature')
    # Windows support centiseconds
    def f(fname, t1):
        os.utime(fname, (t1, t1))

    fname = udir.join('test_utimes.txt')
    fname.ensure()
    t1 = 1159195039.25
    compile(f, (str, float))(str(fname), t1)
    assert t1 == os.stat(str(fname)).st_mtime
Exemple #6
0
    def test_utimes(self):
        # Windows support centiseconds
        def f(fname, t1):
            os.utime(fname, (t1, t1))

        fname = udir.join('test_utimes.txt')
        fname.ensure()
        t1 = 1159195039.25
        compile(f, (str, float))(str(fname), t1)
        assert t1 == os.stat(str(fname)).st_mtime
        t1 = 5000000000.0
        compile(f, (str, float))(str(fname), t1)
        assert t1 == os.stat(str(fname)).st_mtime
Exemple #7
0
def test_translate_pypackrat():
    from rpython.rlib.parsing.pypackrat import PackratParser
    class parser(PackratParser):
        """
        expr:
            additive;
        additive:
            a = additive
            '-'
            b = multitive
            return {'(%s - %s)' % (a, b)}
          | multitive;
        multitive:
            a = multitive
            '*'
            b = simple
            return {'(%s * %s)' % (a, b)}
          | simple;
        simple:
            ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
        """
    print parser._code
    def parse(s):
        p = parser(s)
        return p.expr()
    res = parse("5-5-5")
    assert res == '((5 - 5) - 5)'
    func = compile(parse, [str])
    res = func("5-5-5")
    assert res == '((5 - 5) - 5)'
Exemple #8
0
def test_translate_netdb_lock_thread():
    def f():
        rsocket_startup()
        gethostbyaddr("localhost")
        return 0
    fc = compile(f, [], thread=True)
    assert fc() == 0
Exemple #9
0
class TestLLTermios(object):
    def run(self, arg, expected):
        import pexpect
        child = pexpect.spawn(str(arg.builder.executable_name))
        child.expect(re.escape(expected))
        assert child.status is None

    def test_tcgetattr(self):
        from rpython.translator.c.test.test_genc import compile
        from rpython.rlib import rtermios

        def runs_tcgetattr():
            tpl = list(rtermios.tcgetattr(2)[:-1])
            return str(tpl)

        fn = compile(runs_tcgetattr, [], backendopt=False)
        self.run(fn, TCGETATTR)

    def test_tcgetattr2(self):
        from rpython.translator.c.test.test_genc import compile
        from rpython.rlib import rtermios
        import os, errno

        def runs_tcgetattr():
            fd = os.open('.', 0, 0777)
            try:
                rtermios.tcgetattr(fd)
            except OSError, e:
                assert e.errno == errno.ENOTTY
                print "ok"

        fn = compile(runs_tcgetattr, [], backendopt=False)
        self.run(fn, "ok")
Exemple #10
0
def test_getcwd():
    def does_stuff():
        return os.getcwd()

    f1 = compile(does_stuff, [])
    res = f1()
    assert res == os.getcwd()
Exemple #11
0
def test_translate_netdb_lock_thread():
    def f():
        rsocket_startup()
        gethostbyaddr("localhost")
        return 0
    fc = compile(f, [], thread=True)
    assert fc() == 0
Exemple #12
0
def test_high_precision_stat_time():
    def f():
        st = os.stat('.')
        # should be supported on all platforms, but give a result whose
        # precision might be lower than full nanosecond
        highprec = rposix_stat.get_stat_ns_as_bigint(st, "ctime")
        return '%s;%s' % (st.st_ctime, highprec.str())
    fc = compile(f, [])
    as_string = fc()
    asfloat, highprec = as_string.split(';')
    asfloat = float(asfloat)
    highprec = int(highprec)
    st = os.stat('.')
    assert abs(asfloat - st.st_ctime) < 500e-9
    assert abs(highprec - int(st.st_ctime * 1e9)) < 500
    assert abs(rposix_stat.get_stat_ns_as_bigint(st, "ctime").tolong()
               - st.st_ctime * 1e9) < 3
    if rposix_stat.TIMESPEC is not None:
        with lltype.scoped_alloc(rposix_stat.STAT_STRUCT.TO) as stresult:
            rposix_stat.c_stat(".", stresult)
            if sys.platform == "darwin":
                assert 0 <= stresult.c_st_ctimespec.c_tv_nsec <= 999999999
                assert highprec == (int(stresult.c_st_ctimespec.c_tv_sec) * 1000000000
                                    + int(stresult.c_st_ctimespec.c_tv_nsec))
            else:
                assert 0 <= stresult.c_st_ctim.c_tv_nsec <= 999999999
                assert highprec == (int(stresult.c_st_ctim.c_tv_sec) * 1000000000
                                    + int(stresult.c_st_ctim.c_tv_nsec))
Exemple #13
0
def test_math():
    f = compile(fn, [])
    res = f()
    if res >= 0:
        py.test.fail(repr(MathTests.TESTCASES[res]))
    else:
        assert res == -42
Exemple #14
0
    def test_os_setpgrp():
        def does_stuff():
            return os.setpgrp()

        f1 = compile(does_stuff, [])
        res = f1()
        assert res == os.setpgrp()
Exemple #15
0
def test_dictlike_environ_delitem():
    def fn(s1, s2, s3, s4, s5):
        for n in range(10):
            os.environ[s1] = "t1"
            os.environ[s2] = "t2"
            os.environ[s3] = "t3"
            os.environ[s4] = "t4"
            os.environ[s5] = "t5"
            del os.environ[s3]
            del os.environ[s1]
            del os.environ[s2]
            del os.environ[s4]
            try:
                del os.environ[s2]
            except KeyError:
                pass
            else:
                raise Exception("should have raised!")
            # os.environ[s5] stays

    func = compile(fn, [str] * 5)
    func(
        "PYPY_TEST_DICTLIKE_ENVDEL1",
        "PYPY_TEST_DICTLIKE_ENVDEL_X",
        "PYPY_TEST_DICTLIKE_ENVDELFOO",
        "PYPY_TEST_DICTLIKE_ENVDELBAR",
        "PYPY_TEST_DICTLIKE_ENVDEL5",
    )
Exemple #16
0
    def test_os_getpid():
        def does_stuff():
            return os.getpid()

        f1 = compile(does_stuff, [])
        res = f1()
        assert res != os.getpid()
Exemple #17
0
def test_float_to_str():
    def fn(f):
        return str(f)

    f = compile(fn, [float])
    res = f(1.5)
    assert eval(res) == 1.5
Exemple #18
0
def test_system():
    def does_stuff(cmd):
        return os.system(cmd)

    f1 = compile(does_stuff, [str])
    res = f1("echo hello")
    assert res == 0
Exemple #19
0
 def test_translate_simple(self):
     digits = RangeExpression("0", "9")
     lower = RangeExpression("a", "z")
     upper = RangeExpression("A", "Z")
     keywords = StringExpression("if") | StringExpression("else") | StringExpression("def") | StringExpression("class")
     underscore = StringExpression("_")
     atoms = lower + (upper | lower | digits | underscore).kleene()
     vars = underscore | (upper + (upper | lower | underscore | digits).kleene())
     integers = StringExpression("0") | (RangeExpression("1", "9") + digits.kleene())
     white = StringExpression(" ")
     l1 = self.get_lexer([keywords, atoms, vars, integers, white], ["KEYWORD", "ATOM", "VAR", "INT", "WHITE"])
     l2 = self.get_lexer([keywords, atoms, vars, integers, white], ["KEYWORD", "ATOM", "VAR", "INT", "WHITE"], ["WHITE"])
     def lex(s, ignore=False):
         if ignore:
             tokens = l2.tokenize(s)
         else:
             tokens = l1.tokenize(s)
         return "-%-".join([t.name for t in tokens])
     res = lex("if A a 12341 0 else").split("-%-")
     assert res == ("KEYWORD WHITE VAR WHITE ATOM WHITE INT WHITE "
                    "INT WHITE KEYWORD").split()
     res = lex("if A a 12341 0 else", True).split("-%-")
     assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
     func = compile(lex, [str, bool])
     res = lex("if A a 12341 0 else", False).split("-%-")
     assert res == ("KEYWORD WHITE VAR WHITE ATOM WHITE INT WHITE "
                    "INT WHITE KEYWORD").split()
     res = lex("if A a 12341 0 else", True).split("-%-")
     assert res == "KEYWORD VAR ATOM INT INT KEYWORD".split()
Exemple #20
0
def test_unset_error():
    import sys

    def foo(x):
        if x:
            os.environ['TEST'] = 'STRING'
            assert os.environ['TEST'] == 'STRING'
            del os.environ['TEST']
            try:
                del os.environ['key=']
            except (KeyError, OSError):
                return 1
            return 2
        else:
            return 0

    f = compile(foo, [int], backendopt=False)
    if sys.platform.startswith('win'):
        # Do not open error dialog box
        import ctypes
        SEM_NOGPFAULTERRORBOX = 0x0002  # From MSDN
        old_err_mode = ctypes.windll.kernel32.GetErrorMode()
        new_err_mode = old_err_mode | SEM_NOGPFAULTERRORBOX
        ctypes.windll.kernel32.SetErrorMode(new_err_mode)
    assert f(1) == 1
    if sys.platform.startswith('win'):
        ctypes.windll.kernel32.SetErrorMode(old_err_mode)
Exemple #21
0
def test_math():
    f = compile(fn, [])
    res = f()
    if res >= 0:
        py.test.fail(repr(MathTests.TESTCASES[res]))
    else:
        assert res == -42
Exemple #22
0
def test_os_stat():
    filename = str(py.path.local(__file__))
    has_blksize = hasattr(os.stat_result, "st_blksize")
    has_blocks = hasattr(os.stat_result, "st_blocks")

    def call_stat():
        st = os.stat(filename)
        res = (st[0], st.st_ino, st.st_ctime)
        if has_blksize:
            res += (st.st_blksize,)
        if has_blocks:
            res += (st.st_blocks,)
        return str(res)

    f = compile(call_stat, [])
    res = eval(f())
    assert res[0] == os.stat(filename).st_mode
    assert res[1] == os.stat(filename).st_ino
    st_ctime = res[2]
    if isinstance(st_ctime, float):
        assert (st_ctime - os.stat(filename).st_ctime) < 0.1
    else:
        assert st_ctime == int(os.stat(filename).st_ctime)
    if has_blksize:
        assert res[3] == os.stat(filename).st_blksize
        if has_blocks:
            assert res[4] == os.stat(filename).st_blocks
Exemple #23
0
def test_gcref():
    if sys.platform == "darwin":
        py.test.skip("'boehm' may crash")
    S = lltype.GcStruct("S", ("x", lltype.Signed))
    s = lltype.malloc(S)
    s.x = 123
    g1 = lltype.cast_opaque_ptr(GCREF, s)
    g2 = lltype.cast_opaque_ptr(GCREF, lltype.nullptr(S))

    def f2(x):
        if x > 0:
            return g1
        else:
            return g2

    def f(x):
        gref = f2(x)
        g = lltype.cast_opaque_ptr(lltype.Ptr(S), gref)
        if g:
            return g.x
        else:
            return -42

    fn = compile(f, [int], gcpolicy="boehm")
    assert fn(3) == 123
    assert fn(-3) == -42
Exemple #24
0
def test_vmprof_execute_code_2():

    class MyCode:
        pass
    try:
        rvmprof.register_code_object_class(MyCode, lambda code: 'some code')
    except rvmprof.VMProfPlatformUnsupported:
        pass

    class A:
        pass

    @rvmprof.vmprof_execute_code("xcode2", lambda num, code: code,
                                 result_class=A)
    def main(num, code):
        print num
        return A()

    def f():
        a = main(7, MyCode())
        assert isinstance(a, A)
        return 0

    assert f() == 0
    fn = compile(f, [])
    assert fn() == 0
Exemple #25
0
def test_os_stat():
    filename = str(py.path.local(__file__))
    has_blksize = hasattr(os.stat_result, 'st_blksize')
    has_blocks = hasattr(os.stat_result, 'st_blocks')

    def call_stat():
        st = os.stat(filename)
        res = (st[0], st.st_ino, st.st_ctime)
        if has_blksize: res += (st.st_blksize, )
        if has_blocks: res += (st.st_blocks, )
        return str(res)

    f = compile(call_stat, [])
    res = eval(f())
    assert res[0] == os.stat(filename).st_mode
    # windows zeros out st_ino in the app-level os.stat
    if sys.platform != 'win32':
        assert res[1] == os.stat(filename).st_ino
    st_ctime = res[2]
    if isinstance(st_ctime, float):
        assert (st_ctime - os.stat(filename).st_ctime) < 0.1
    else:
        assert st_ctime == int(os.stat(filename).st_ctime)
    if has_blksize:
        assert res[3] == os.stat(filename).st_blksize
        if has_blocks:
            assert res[4] == os.stat(filename).st_blocks
Exemple #26
0
def test_translate_netdb_lock():
    def f():
        gethostbyaddr("localhost")
        return 0

    fc = compile(f, [])
    assert fc() == 0
Exemple #27
0
def test_strerror():
    def does_stuff(n):
        return os.strerror(n)
    f1 = compile(does_stuff, [int])
    for i in range(4):
        res = f1(i)
        assert res == os.strerror(i)
Exemple #28
0
def test_pipe_dup_dup2():
    def does_stuff():
        a, b = os.pipe()
        c = os.dup(a)
        d = os.dup(b)
        assert a != b
        assert a != c
        assert a != d
        assert b != c
        assert b != d
        assert c != d
        os.close(c)
        os.dup2(d, c)
        e, f = os.pipe()
        assert e != a
        assert e != b
        assert e != c
        assert e != d
        assert f != a
        assert f != b
        assert f != c
        assert f != d
        assert f != e
        os.close(a)
        os.close(b)
        os.close(c)
        os.close(d)
        os.close(e)
        os.close(f)
        return 42
    f1 = compile(does_stuff, [])
    res = f1()
    assert res == 42
def test_debug_print_traceback():
    from rpython.translator.c.test.test_genc import compile
    from rpython.rtyper.lltypesystem import lltype
    from rpython.rtyper.lltypesystem.lloperation import llop

    def ggg(n):
        if n < 10:
            ggg(n + 1)
        else:
            raise ValueError

    def recovery():
        llop.debug_print_traceback(lltype.Void)

    recovery._dont_inline_ = True

    def fff():
        try:
            ggg(0)
        except:
            recovery()

    fn = compile(fff, [], return_stderr=True)
    stderr = fn()
    assert 'RPython traceback:\n' in stderr
    assert stderr.count('entry_point') == 1
    assert stderr.count('ggg') == 11
    assert stderr.count('recovery') == 0
Exemple #30
0
def test_chdir():
    def does_stuff(path):
        os.chdir(path)
        return os.getcwd()
    f1 = compile(does_stuff, [str])
    # different on windows please
    assert f1('/tmp') == os.path.realpath('/tmp')
Exemple #31
0
def test_os_access():
    filename = str(py.path.local(__file__))
    def call_access(path, mode):
        return os.access(path, mode)
    f = compile(call_access, [str, int])
    for mode in os.R_OK, os.W_OK, os.X_OK, (os.R_OK | os.W_OK | os.X_OK):
        assert f(filename, mode) == os.access(filename, mode)
Exemple #32
0
def test_os_isatty():
    def call_isatty(fd):
        return os.isatty(fd)
    f = compile(call_isatty, [int])
    assert f(0) == os.isatty(0)
    assert f(1) == os.isatty(1)
    assert f(2) == os.isatty(2)
Exemple #33
0
def test_unset_error():
    import sys
    def foo(x):
        if x:
            os.environ['TEST'] = 'STRING'
            assert os.environ['TEST'] == 'STRING'
            del os.environ['TEST']
            try:
                del os.environ['key=']
            except OSError:
                return 1
            return 2
        else:
            return 0

    f = compile(foo, [int], backendopt=False)
    if sys.platform.startswith('win'):
        # Do not open error dialog box
        import ctypes
        SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN
        old_err_mode = ctypes.windll.kernel32.GetErrorMode()
        new_err_mode = old_err_mode | SEM_NOGPFAULTERRORBOX
        ctypes.windll.kernel32.SetErrorMode(new_err_mode)
    assert f(1) == 1
    if sys.platform.startswith('win'):
        ctypes.windll.kernel32.SetErrorMode(old_err_mode)
Exemple #34
0
def test_largefile():
    if not hasattr(os, 'ftruncate'):
        py.test.skip("this os has no ftruncate :-(")
    need_sparse_files()
    filename = str(udir.join('test_largefile'))
    r4800000000  = r_longlong(4800000000L)
    r4900000000  = r_longlong(4900000000L)
    r5000000000  = r_longlong(5000000000L)
    r5200000000  = r_longlong(5200000000L)
    r9900000000  = r_longlong(9900000000L)
    r10000000000 = r_longlong(10000000000L)
    def does_stuff():
        fd = os.open(filename, os.O_RDWR | os.O_CREAT, 0666)
        os.ftruncate(fd, r10000000000)
        res = os.lseek(fd, r9900000000, 0)
        assert res == r9900000000
        res = os.lseek(fd, -r5000000000, 1)
        assert res == r4900000000
        res = os.lseek(fd, -r5200000000, 2)
        assert res == r4800000000
        os.close(fd)
        try:
            os.lseek(fd, 0, 0)
        except OSError:
            pass
        else:
            print "DID NOT RAISE"
            raise AssertionError
        st = os.stat(filename)
        assert st.st_size == r10000000000
    does_stuff()
    os.unlink(filename)
    f1 = compile(does_stuff, [])
    f1()
    os.unlink(filename)
Exemple #35
0
    def get_interp(self):
        def f(testfile):
            interp = Interpreter({'no-exception-jseval': True})

            shell_src = load_file(str(shellpath))
            interp.run_src(shell_src)
            test_src = load_file(testfile)
            interp.run_src(test_src)

            global_object = interp.global_object
            testcases = global_object.get(u'testcases')

            testcount = testcases.get(u'length').ToInt32()

            run_test_func = global_object.get(u'run_test')

            test_results = []

            for number in xrange(testcount):
                w_test_number = _w(number)
                result_obj = run_test_func.Call(args=[w_test_number])
                result_passed = result_obj.get(u'passed').to_boolean()
                result_reason = str(result_obj.get(u'reason').to_string())
                test_results.append({'number': number, 'passed': result_passed, 'reason': result_reason})

            return test_results

        if self.do_compile:
            if self.compiled_interpreter is None:
                from rpython.translator.c.test.test_genc import compile
                self.compiled_interpreter = compile(f, [str])
            return self.compiled_interpreter
        else:
            return f
Exemple #36
0
def test_stat_result():
    import os
    from rpython.translator.c.test.test_genc import compile
    from rpython.rtyper.module.ll_os_stat import s_StatResult
    marshal_stat_result = get_marshaller(s_StatResult)
    unmarshal_stat_result = get_unmarshaller(s_StatResult)

    def f(path):
        st = os.stat(path)
        buf = []
        marshal_stat_result(buf, st)
        buf = ''.join(buf)
        st2 = unmarshal_stat_result(buf)
        assert st2.st_mode == st.st_mode
        assert st2[9] == st[9]
        return buf

    fn = compile(f, [str])
    res = fn('.')
    st = os.stat('.')
    sttuple = marshal.loads(res)
    assert sttuple[0] == st[0]
    assert sttuple[1] == st[1]
    assert sttuple[2] == st[2]
    assert sttuple[3] == st[3]
    assert sttuple[4] == st[4]
    assert sttuple[5] == st[5]
    assert len(sttuple) == 10
Exemple #37
0
def test_translate_compiled_parser():
    r0 = Rule("expression", [["additive", "EOF"]])
    r1 = Rule("additive", [["multitive", "+", "additive"], ["multitive"]])
    r2 = Rule("multitive", [["primary", "*", "multitive"], ["primary"]])
    r3 = Rule("primary", [["(", "additive", ")"], ["decimal"]])
    r4 = Rule("decimal", [[symb] for symb in "0123456789"])
    p = PackratParser([r0, r1, r2, r3, r4], "expression")
    compiler = ParserCompiler(p)
    kls = compiler.compile()
    p = kls()
    tree = p.parse([
        Token(c, i, SourcePos(i, 0, i))
        for i, c in enumerate(list("2*(3+4)") + ["EOF"])
    ])
    data = [
        Token(c, i, SourcePos(i, 0, i))
        for i, c in enumerate(list("2*(3+4)") + ["EOF"])
    ]
    print tree
    p = kls()

    def parse(choose):
        tree = p.parse(data)
        return tree.symbol + " " + "-%-".join(
            [c.symbol for c in tree.children])

    func = compile(parse, [bool])
    res1 = parse(True)
    res2 = func(True)
    assert res1 == res2
Exemple #38
0
 def test_links():
     import stat
     tmpfile1 = str(udir.join('test_links_1.txt'))
     tmpfile2 = str(udir.join('test_links_2.txt'))
     tmpfile3 = str(udir.join('test_links_3.txt'))
     f = open(tmpfile1, 'w')
     f.close()
     def does_stuff():
         os.symlink(tmpfile1, tmpfile2)
         os.link(tmpfile1, tmpfile3)
         assert os.readlink(tmpfile2) == tmpfile1
         flag= 0
         st = os.lstat(tmpfile1)
         flag = flag*10 + stat.S_ISREG(st[0])
         flag = flag*10 + stat.S_ISLNK(st[0])
         st = os.lstat(tmpfile2)
         flag = flag*10 + stat.S_ISREG(st[0])
         flag = flag*10 + stat.S_ISLNK(st[0])
         st = os.lstat(tmpfile3)
         flag = flag*10 + stat.S_ISREG(st[0])
         flag = flag*10 + stat.S_ISLNK(st[0])
         return flag
     f1 = compile(does_stuff, [])
     res = f1()
     assert res == 100110
     assert os.path.islink(tmpfile2)
     assert not os.path.islink(tmpfile3)
Exemple #39
0
def test_dictlike_environ_delitem():
    def fn(s1, s2, s3, s4, s5):
        for n in range(10):
            os.environ[s1] = 't1'
            os.environ[s2] = 't2'
            os.environ[s3] = 't3'
            os.environ[s4] = 't4'
            os.environ[s5] = 't5'
            del os.environ[s3]
            del os.environ[s1]
            del os.environ[s2]
            del os.environ[s4]
            try:
                del os.environ[s2]
            except KeyError:
                pass
            else:
                raise Exception("should have raised!")
            # os.environ[s5] stays
    func = compile(fn, [str] * 5)
    func('PYPY_TEST_DICTLIKE_ENVDEL1',
         'PYPY_TEST_DICTLIKE_ENVDEL_X',
         'PYPY_TEST_DICTLIKE_ENVDELFOO',
         'PYPY_TEST_DICTLIKE_ENVDELBAR',
         'PYPY_TEST_DICTLIKE_ENVDEL5')
Exemple #40
0
def test_vmprof_execute_code_2():

    class MyCode:
        pass
    try:
        rvmprof.register_code_object_class(MyCode, lambda code: 'some code')
    except rvmprof.VMProfPlatformUnsupported:
        pass

    class A:
        pass

    @rvmprof.vmprof_execute_code("xcode2", lambda num, code: code,
                                 result_class=A)
    def main(num, code):
        print num
        return A()

    def f():
        a = main(7, MyCode())
        assert isinstance(a, A)
        return 0

    assert f() == 0
    fn = compile(f, [])
    assert fn() == 0
Exemple #41
0
def test_getcwd():
    def does_stuff():
        return os.getcwd()

    f1 = compile(does_stuff, [])
    res = f1()
    assert res == os.getcwd()
Exemple #42
0
def test_gcref():
    if sys.platform == 'darwin':
        py.test.skip("'boehm' may crash")
    S = lltype.GcStruct("S", ("x", lltype.Signed))
    s = lltype.malloc(S)
    s.x = 123
    g1 = lltype.cast_opaque_ptr(GCREF, s)
    g2 = lltype.cast_opaque_ptr(GCREF, lltype.nullptr(S))

    def f2(x):
        if x > 0:
            return g1
        else:
            return g2

    def f(x):
        gref = f2(x)
        g = lltype.cast_opaque_ptr(lltype.Ptr(S), gref)
        if g:
            return g.x
        else:
            return -42

    fn = compile(f, [int], gcpolicy='boehm')
    assert fn(3) == 123
    assert fn(-3) == -42
Exemple #43
0
def test_debug_print_traceback():
    from rpython.translator.c.test.test_genc import compile
    from rpython.rtyper.lltypesystem import lltype
    from rpython.rtyper.lltypesystem.lloperation import llop

    def ggg(n):
        if n < 10:
            ggg(n + 1)
        else:
            raise ValueError
    def recovery():
        llop.debug_print_traceback(lltype.Void)
    recovery._dont_inline_ = True
    def fff():
        try:
            ggg(0)
        except:
            recovery()

    fn = compile(fff, [], return_stderr=True)
    stderr = fn()
    assert 'RPython traceback:\n' in stderr
    assert stderr.count('entry_point') == 1
    assert stderr.count('ggg') == 11
    assert stderr.count('recovery') == 0
Exemple #44
0
    def test_os_setpgrp():
        def does_stuff():
            return os.setpgrp()

        f1 = compile(does_stuff, [])
        res = f1()
        assert res == os.setpgrp()
Exemple #45
0
    def test_links():
        import stat
        tmpfile1 = str(udir.join('test_links_1.txt'))
        tmpfile2 = str(udir.join('test_links_2.txt'))
        tmpfile3 = str(udir.join('test_links_3.txt'))
        f = open(tmpfile1, 'w')
        f.close()

        def does_stuff():
            os.symlink(tmpfile1, tmpfile2)
            os.link(tmpfile1, tmpfile3)
            assert os.readlink(tmpfile2) == tmpfile1
            flag = 0
            st = os.lstat(tmpfile1)
            flag = flag * 10 + stat.S_ISREG(st[0])
            flag = flag * 10 + stat.S_ISLNK(st[0])
            st = os.lstat(tmpfile2)
            flag = flag * 10 + stat.S_ISREG(st[0])
            flag = flag * 10 + stat.S_ISLNK(st[0])
            st = os.lstat(tmpfile3)
            flag = flag * 10 + stat.S_ISREG(st[0])
            flag = flag * 10 + stat.S_ISLNK(st[0])
            return flag

        f1 = compile(does_stuff, [])
        res = f1()
        assert res == 100110
        assert os.path.islink(tmpfile2)
        assert not os.path.islink(tmpfile3)
Exemple #46
0
def test_pipe_dup_dup2():
    def does_stuff():
        a, b = os.pipe()
        c = os.dup(a)
        d = os.dup(b)
        assert a != b
        assert a != c
        assert a != d
        assert b != c
        assert b != d
        assert c != d
        os.close(c)
        os.dup2(d, c)
        e, f = os.pipe()
        assert e != a
        assert e != b
        assert e != c
        assert e != d
        assert f != a
        assert f != b
        assert f != c
        assert f != d
        assert f != e
        os.close(a)
        os.close(b)
        os.close(c)
        os.close(d)
        os.close(e)
        os.close(f)
        return 42

    f1 = compile(does_stuff, [])
    res = f1()
    assert res == 42
Exemple #47
0
    def test_os_getpid():
        def does_stuff():
            return os.getpid()

        f1 = compile(does_stuff, [])
        res = f1()
        assert res != os.getpid()
Exemple #48
0
def test_float_to_str():
    def fn(f):
        return str(f)

    f = compile(fn, [float])
    res = f(1.5)
    assert eval(res) == 1.5
Exemple #49
0
def test_system():
    def does_stuff(cmd):
        return os.system(cmd)

    f1 = compile(does_stuff, [str])
    res = f1("echo hello")
    assert res == 0
Exemple #50
0
def test_translate_pypackrat():
    from rpython.rlib.parsing.pypackrat import PackratParser

    class parser(PackratParser):
        """
        expr:
            additive;
        additive:
            a = additive
            '-'
            b = multitive
            return {'(%s - %s)' % (a, b)}
          | multitive;
        multitive:
            a = multitive
            '*'
            b = simple
            return {'(%s * %s)' % (a, b)}
          | simple;
        simple:
            ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
        """

    print parser._code

    def parse(s):
        p = parser(s)
        return p.expr()

    res = parse("5-5-5")
    assert res == '((5 - 5) - 5)'
    func = compile(parse, [str])
    res = func("5-5-5")
    assert res == '((5 - 5) - 5)'
Exemple #51
0
def test_stat_result():
    import os
    from rpython.translator.c.test.test_genc import compile
    from rpython.rlib.rposix_stat import s_StatResult
    marshal_stat_result = get_marshaller(s_StatResult)
    unmarshal_stat_result = get_unmarshaller(s_StatResult)
    def f(path):
        st = os.stat(path)
        buf = []
        marshal_stat_result(buf, st)
        buf = ''.join(buf)
        st2 = unmarshal_stat_result(buf)
        assert st2.st_mode == st.st_mode
        assert st2[9] == st[9]
        return buf
    fn = compile(f, [str])
    res = fn('.')
    st = os.stat('.')
    sttuple = marshal.loads(res)
    assert sttuple[0] == st[0]
    assert sttuple[1] == st[1]
    assert sttuple[2] == st[2]
    assert sttuple[3] == st[3]
    assert sttuple[4] == st[4]
    assert sttuple[5] == st[5]
    assert len(sttuple) == 10
Exemple #52
0
def test_strerror():
    def does_stuff(n):
        return os.strerror(n)

    f1 = compile(does_stuff, [int])
    for i in range(4):
        res = f1(i)
        assert res == os.strerror(i)
Exemple #53
0
def test_os_isatty():
    def call_isatty(fd):
        return os.isatty(fd)

    f = compile(call_isatty, [int])
    assert f(0) == os.isatty(0)
    assert f(1) == os.isatty(1)
    assert f(2) == os.isatty(2)
Exemple #54
0
def test_utimes():
    if os.name != 'nt':
        py.test.skip('Windows specific feature')
    # Windows support centiseconds
    def f(fname, t1):
        os.utime(fname, (t1, t1))

    fname = udir.join('test_utimes.txt')
    fname.ensure()
    t1 = 1159195039.25
    compile(f, (str, float))(str(fname), t1)
    assert t1 == os.stat(str(fname)).st_mtime
    if sys.version_info < (2, 7):
        py.test.skip('requires Python 2.7')
    t1 = 5000000000.0
    compile(f, (str, float))(str(fname), t1)
    assert t1 == os.stat(str(fname)).st_mtime
Exemple #55
0
 def test_compile_c(self):
     function = compile(self.get_unit, [])
     unit = function()
     if platform.processor() in ('x86', 'x86_64'):
         assert unit == rtimer.UNIT_TSC
     else:
         assert unit in (rtimer.UNIT_TSC, rtimer.UNIT_NS,
                         rtimer.UNIT_QUERY_PERFORMANCE_COUNTER)
Exemple #56
0
    def test_csetupterm(self):
        from rpython.translator.c.test.test_genc import compile
        from pypy.module._minimal_curses import interp_curses
        def runs_setupterm():
            interp_curses._curses_setupterm_null(1)

        fn = compile(runs_setupterm, [])
        fn()
Exemple #57
0
def test_translate_and_large_input():
    from rpython.translator.c.test.test_genc import compile

    def f(i, check):
        bytes = "s" * i
        if check == 1:
            for j in range(3):
                stream = rminiz_oxide.deflateInit()
                bytes = rminiz_oxide.compress(stream, bytes,
                                              rminiz_oxide.Z_FINISH)
                rminiz_oxide.deflateEnd(stream)
            return bytes
        if check == 2:
            return str(rminiz_oxide.adler32(bytes))
        if check == 3:
            return str(rminiz_oxide.crc32(bytes))
        return '?'

    fc = compile(f, [int, int])

    test_list = [
        1, 2, 3, 5, 8, 87, 876, 8765, 87654, 876543, 8765432, 127329129
    ]  # up to ~128MB
    if sys.maxint > 2**32:
        test_list.append(4305704715)  # 4.01GB
        # XXX should we have a way to say "I don't have enough RAM,
        # don't run this"?

    for a in test_list:
        print 'Testing compression of "s" * %d' % a
        z = zlib.compressobj()
        count = a
        pieces = []
        while count > 1024 * 1024:
            pieces.append(z.compress("s" * (1024 * 1024)))
            count -= 1024 * 1024
        pieces.append(z.compress("s" * count))
        pieces.append(z.flush(zlib.Z_FINISH))
        expected = ''.join(pieces)
        del pieces
        expected = zlib.compress(expected)
        expected = zlib.compress(expected)
        assert fc(a, 1) == expected

        print 'Testing adler32 and crc32 of "s" * %d' % a

        def compute(function, start):
            count = a
            while count > 0:
                count1 = min(count, 1024 * 1024)
                start = function("s" * count1, start)
                count -= count1
            return start

        expected_adler32 = compute(zlib.adler32, 1) & (2**32 - 1)
        expected_crc32 = compute(zlib.crc32, 0) & (2**32 - 1)
        assert fc(a, 2) == str(expected_adler32)
        assert fc(a, 3) == str(expected_crc32)
Exemple #58
0
    def test_large_dict(self):
        d = {}
        d_small = {1: 2}
        fixlist = [x for x in range(100)]
        dynlist = [x for x in range(100)]
        test_dict = dict(map(lambda x: (x, hex(x)), range(256, 4096)))
        reverse_dict = dict(map(lambda (x, y): (y, x), test_dict.items()))

        class wrap:
            pass

        for x in xrange(100):
            i = wrap()
            i.x = x
            d[x] = i

        def f(x):
            if x > 42:
                dynlist.append(x)
            return d[x].x + fixlist[x] + d_small[x] + reverse_dict[
                test_dict[x]]

        func = compile(f, [int])
        db = func.builder.db
        gcontainers = list(db.globalcontainers())
        t = db.translator
        rtyper = t.rtyper
        get_container = lambda x: rtyper.getrepr(
            t.annotator.bookkeeper.immutablevalue(x)).convert_const(x)._obj
        dictvalnode = db.getcontainernode(get_container(d))
        dictvalnode2 = db.getcontainernode(get_container(d_small))
        fixarrayvalnode = db.getcontainernode(get_container(fixlist))
        dynarrayvalnode = db.getcontainernode(get_container(dynlist))
        test_dictnode = db.getcontainernode(get_container(test_dict))
        reverse_dictnode = db.getcontainernode(get_container(reverse_dict))

        S = rffi.sizeof(lltype.Signed)
        P = rffi.sizeof(rffi.VOIDP)
        B = 1  # bool
        assert guess_size(func.builder.db, dictvalnode, set()) > 100
        assert guess_size(func.builder.db, dictvalnode2, set()) == (
            (4 * S + 2 * P) +  # struct dicttable
            # (S + 16) +          # indexes, length 16, but is absent here
            (S + S + S))  # entries, length 1
        r_set = set()
        dictnode_size = guess_size(db, test_dictnode, r_set)
        assert dictnode_size == (
            (4 * S + 2 * P) +  # struct dicttable
            # (S + 2 * 8192) +     # indexes, length 8192, rffi.USHORT,
            # but is absent here during translation
            (S + (S + S) * 3840) +  # entries, length 3840
            (S + S + 6) * 3840)  # 3840 strings with 5 chars each (+1 final)
        assert guess_size(func.builder.db, fixarrayvalnode, set(
        )) == 100 * rffi.sizeof(lltype.Signed) + 1 * rffi.sizeof(lltype.Signed)
        assert guess_size(
            func.builder.db, dynarrayvalnode,
            set()) == 100 * rffi.sizeof(lltype.Signed) + 2 * rffi.sizeof(
                lltype.Signed) + 1 * rffi.sizeof(rffi.VOIDP)
Exemple #59
0
def test_code_to_unichr():
    def f(c):
        return ord(code_to_unichr(c)[0])
    f1 = compile(f, [int])
    got = f1(0x12346)
    if MAXUNICODE == 65535:
        assert got == 0xd808    # first char of a pair
    else:
        assert got == 0x12346
Exemple #60
0
def test_compiled_encode_nan():
    fn2 = compile(fn_encode_nan, [float, int])
    ints = [int(-2**31), int(2**31 - 1), 42]
    for x in enum_floats():
        y = ints.pop()
        ints.insert(0, y)
        fn_encode_nan(x, y)
        res = fn2(x, y)
        assert res == 42