def test_annotator_folding():
    from pypy.translator.interactive import Translation

    gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
    gcgroup = OptionDescription('gc', '', [gcoption])
    descr = OptionDescription('pypy', '', [gcgroup])
    config = Config(descr)
    
    def f(x):
        if config.gc.name == 'ref':
            return x + 1
        else:
            return 'foo'

    t = Translation(f)
    t.rtype([int])
    
    block = t.context.graphs[0].startblock
    assert len(block.exits[0].target.operations) == 0
    assert len(block.operations) == 1
    assert len(block.exits) == 1
    assert block.operations[0].opname == 'int_add'

    assert config._freeze_()
    # does not raise, since it does not change the attribute
    config.gc.name = "ref"
    py.test.raises(TypeError, 'config.gc.name = "framework"')
示例#2
0
    def test_prof_inline(self):
        if sys.platform == 'win32':
            py.test.skip("instrumentation support is unix only for now")

        def add(a, b):
            return a + b - b + b - b + b - b + b - b + b - b + b - b + b

        def entry_point(argv):
            tot = 0
            x = int(argv[1])
            while x > 0:
                tot = add(tot, x)
                x -= 1
            os.write(1, str(tot))
            return 0

        from pypy.translator.interactive import Translation
        t = Translation(entry_point, backend='c', standalone=True)
        # no counters
        t.backendopt(inline_threshold=100, profile_based_inline="500")
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2

        t = Translation(entry_point, backend='c', standalone=True)
        # counters
        t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST * 0.5,
                     profile_based_inline="500")
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
示例#3
0
def test_lib():
    def entry_point(argv):
        fd = os.open("/tmp/foobar", os.O_RDONLY, 0777)
        assert fd == 77
        res = os.read(fd, 123)
        assert res == "he\x00llo"
        count = os.write(fd, "world\x00!\x00")
        assert count == 42
        for arg in argv:
            count = os.write(fd, arg)
            assert count == 61
        os.close(fd)
        return 0
    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = MySandboxedProc([exe, 'x1', 'y2'], expected = [
        ("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77),
        ("read", (77, 123), "he\x00llo"),
        ("write", (77, "world\x00!\x00"), 42),
        ("write", (77, exe), 61),
        ("write", (77, "x1"), 61),
        ("write", (77, "y2"), 61),
        ("close", (77,), None),
        ])
    proc.handle_forever()
    assert proc.seen == len(proc.expected)
示例#4
0
    def _makefunc_str_int(cls, f):
        def main(argv):
            arg0 = argv[1]
            arg1 = int(argv[2])
            try:
                res = f(arg0, arg1)
            except MemoryError:
                print "MEMORY-ERROR"
            else:
                print res
            return 0
        
        t = Translation(main, standalone=True, gc=cls.gcpolicy,
                        policy=annpolicy.StrictAnnotatorPolicy(),
                        taggedpointers=cls.taggedpointers,
                        gcremovetypeptr=cls.removetypeptr)
        t.disable(['backendopt'])
        t.set_backend_extra_options(c_debug_defines=True)
        t.rtype()
        if conftest.option.view:
            t.viewcg()
        exename = t.compile()

        def run(s, i):
            data = py.process.cmdexec("%s %s %d" % (exename, s, i))
            data = data.strip()
            if data == 'MEMORY-ERROR':
                raise MemoryError
            return data

        return run
示例#5
0
def test_lib():
    def entry_point(argv):
        fd = os.open("/tmp/foobar", os.O_RDONLY, 0777)
        assert fd == 77
        res = os.read(fd, 123)
        assert res == "he\x00llo"
        count = os.write(fd, "world\x00!\x00")
        assert count == 42
        for arg in argv:
            count = os.write(fd, arg)
            assert count == 61
        os.close(fd)
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = MySandboxedProc([exe, 'x1', 'y2'],
                           expected=[
                               ("open", ("/tmp/foobar", os.O_RDONLY, 0777),
                                77),
                               ("read", (77, 123), "he\x00llo"),
                               ("write", (77, "world\x00!\x00"), 42),
                               ("write", (77, exe), 61),
                               ("write", (77, "x1"), 61),
                               ("write", (77, "y2"), 61),
                               ("close", (77, ), None),
                           ])
    proc.handle_forever()
    assert proc.seen == len(proc.expected)
示例#6
0
    def test_profopt(self):
        def add(a, b):
            return a + b - b + b - b + b - b + b - b + b - b + b - b + b

        def entry_point(argv):
            tot = 0
            x = int(argv[1])
            while x > 0:
                tot = add(tot, x)
                x -= 1
            os.write(1, str(tot))
            return 0

        from pypy.translator.interactive import Translation
        # XXX this is mostly a "does not crash option"
        t = Translation(entry_point, backend='c', standalone=True, profopt="")
        # no counters
        t.backendopt()
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
        t = Translation(entry_point,
                        backend='c',
                        standalone=True,
                        profopt="",
                        noprofopt=True)
        # no counters
        t.backendopt()
        exe = t.compile()
        out = py.process.cmdexec("%s 500" % exe)
        assert int(out) == 500 * 501 / 2
示例#7
0
    def test_profopt_mac_osx_bug(self):
        if sys.platform == 'win32':
            py.test.skip("no profopt on win32")

        def entry_point(argv):
            import os
            pid = os.fork()
            if pid:
                os.waitpid(pid, 0)
            else:
                os._exit(0)
            return 0

        from pypy.translator.interactive import Translation
        # XXX this is mostly a "does not crash option"
        t = Translation(entry_point, backend='c', standalone=True, profopt="")
        # no counters
        t.backendopt()
        exe = t.compile()
        #py.process.cmdexec(exe)
        t = Translation(entry_point,
                        backend='c',
                        standalone=True,
                        profopt="",
                        noprofopt=True)
        # no counters
        t.backendopt()
        exe = t.compile()
示例#8
0
def main():
    import_benchmarks()
    benchmarks = []
    for name, thing in globals().iteritems():
        if getattr(thing, 'benchmark', False):
            benchmarks.append((name, thing))
    benchmarks.sort()
    
    def entry_point(argv):
        for name, func in benchmarks:
            print name, ':', func()
        return 0

    t = Translation(entry_point, standalone=True, backend='c')
    c_exe = t.compile()
    t = Translation(entry_point, standalone=True, backend='cli')
    cli_exe = t.compile()

    c_res = run_benchmark(c_exe)
    cli_res = run_benchmark(cli_exe)

    print 'benchmark                              genc     gencli       ratio'
    print
    for name, _ in benchmarks:
        c_time = c_res[name]
        cli_time = cli_res[name]
        if c_time == 0:
            ratio = '%10s' % '---'
        else:
            ratio = '%10.2f' % (cli_time/c_time)
        print '%-32s %10.2f %10.2f %s' % (name, c_time, cli_time, ratio)
示例#9
0
def test_annotator_folding():
    from pypy.translator.interactive import Translation

    gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
    gcgroup = OptionDescription('gc', '', [gcoption])
    descr = OptionDescription('pypy', '', [gcgroup])
    config = Config(descr)
    
    def f(x):
        if config.gc.name == 'ref':
            return x + 1
        else:
            return 'foo'

    t = Translation(f)
    t.rtype([int])
    
    block = t.context.graphs[0].startblock
    assert len(block.exits[0].target.operations) == 0
    assert len(block.operations) == 1
    assert len(block.exits) == 1
    assert block.operations[0].opname == 'int_add'

    assert config._freeze_()
    # does not raise, since it does not change the attribute
    config.gc.name = "ref"
    py.test.raises(TypeError, 'config.gc.name = "framework"')
示例#10
0
 def run(self, entry_point, args, expected):
     t = Translation(entry_point, backend='c', standalone=True,
                     sandbox=True)
     exe = t.compile()
     from pypy.translator.sandbox.sandlib import SimpleIOSandboxedProc
     proc = SimpleIOSandboxedProc([exe] + args)
     output, error = proc.communicate()
     assert error == ''
     assert output == expected
示例#11
0
def test_disable_logic():
    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    t.disable(["backendopt"])
    t.source_c()

    assert "backendopt" not in t.driver.done
示例#12
0
def test_translate():
    from pypy.translator.interactive import Translation
    def f(x, y):
        rnd = Random(x)
        rnd.init_by_array([x, y])
        rnd.jumpahead(intmask(y))
        return rnd.genrand32(), rnd.random()
    t = Translation(f)
    fc = t.compile_c([r_uint, r_uint])
    assert fc(r_uint(1), r_uint(2)) == f(r_uint(1), r_uint(2))
示例#13
0
def test_translate():
    from pypy.translator.interactive import Translation
    def f(x, y):
        rnd = Random(x)
        rnd.init_by_array([x, y])
        rnd.jumpahead(y)
        return rnd.genrand32(), rnd.random()
    t = Translation(f)
    fc = t.compile_c([int, int])
    assert fc(1, 2) == f(1, 2)
示例#14
0
def build_adi(function, types):
    t = Translation(function)
    t.rtype(types)
    if conftest.option.view:
        t.view()
    adi = AbstractDataFlowInterpreter(t.context)
    graph = graphof(t.context, function)
    adi.schedule_function(graph)
    adi.complete()
    return t.context, adi, graph
示例#15
0
def compile(fn,
            argtypes,
            view=False,
            gcpolicy="ref",
            backendopt=True,
            annotatorpolicy=None):
    t = Translation(fn,
                    argtypes,
                    gc=gcpolicy,
                    backend="c",
                    policy=annotatorpolicy)
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.annotate()
    # XXX fish
    t.driver.config.translation.countmallocs = True
    compiled_fn = t.compile_c()
    if getattr(py.test.config.option, 'view', False):
        t.view()
    malloc_counters = t.driver.cbuilder.get_malloc_counters()

    def checking_fn(*args, **kwds):
        if 'expected_extra_mallocs' in kwds:
            expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
        else:
            expected_extra_mallocs = 0
        res = compiled_fn(*args, **kwds)
        mallocs, frees = malloc_counters()
        if isinstance(expected_extra_mallocs, int):
            assert mallocs - frees == expected_extra_mallocs
        else:
            assert mallocs - frees in expected_extra_mallocs
        return res

    return checking_fn
示例#16
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])

    t = Translation(parse)
    t.annotate([bool])
    t.backendopt()
    t.rtype()
    func = t.compile_c()
    res1 = parse(True)
    res2 = func(True)
    assert res1 == res2
示例#17
0
def test_translate_ast_visitor():
    from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
    regexs, rules, ToAST = parse_ebnf("""
DECIMAL: "0|[1-9][0-9]*";
IGNORE: " ";
additive: multitive ["+!"] additive | <multitive>;
multitive: primary ["*!"] multitive | <primary>; #nonsense!
primary: "(" <additive> ")" | <DECIMAL>;
""")
    parse = make_parse_function(regexs, rules)

    def f():
        tree = parse("(0 +! 10) *! (999 +! 10) +! 1")
        tree = ToAST().visit_additive(tree)
        assert len(tree) == 1
        tree = tree[0]
        return tree.symbol + " " + "-&-".join(
            [c.symbol for c in tree.children])

    res1 = f()
    t = Translation(f)
    t.annotate()
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    res2 = func()
    assert res1 == res2
示例#18
0
def test_tagged_boehm():
    t = Translation(entry_point, standalone=True, gc='boehm', taggedpointers=True)
    try:
        exename = str(t.compile_c())
    finally:
        if conftest.option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#19
0
def test_tagged_boehm():
    t = Translation(entry_point, standalone=True, gc='boehm')
    try:
        exename = t.compile_c()
    finally:
        if conftest.option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#20
0
def test_tagged_boehm():
    py.test.skip("broken as test need rffi")
    t = Translation(entry_point, standalone=True, gc='boehm')
    try:
        exename = t.compile_llvm()
    finally:
        if conftest.option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#21
0
def test_tagged_boehm():
    runtest.llvm_test()
    runtest.gcc3_test()
    t = Translation(entry_point, standalone=True, gc='boehm')
    try:
        exename = t.compile_llvm()
    finally:
        if conftest.option.view:
            t.view()
    g = os.popen(exename, 'r')
    data = g.read()
    g.close()
    assert data.rstrip().endswith('ALL OK')
示例#22
0
def test_enforced_args():
    from pypy.annotation.model import s_None
    from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
    from pypy.translator.interactive import Translation
    def f1():
        str2charp("hello")
    def f2():
        str2charp("world")
    t = Translation(f1, [])
    t.rtype()
    mixann = MixLevelHelperAnnotator(t.context.rtyper)
    mixann.getgraph(f2, [], s_None)
    mixann.finish()
示例#23
0
def test_prof_inline():
    if sys.platform == 'win32':
        py.test.skip("instrumentation support is unix only for now")
    def add(a,b):
        return a + b - b + b - b + b - b + b - b + b - b + b - b + b
    def entry_point(argv):
        tot =  0
        x = int(argv[1])
        while x > 0:
            tot = add(tot, x)
            x -= 1
        os.write(1, str(tot))
        return 0
    from pypy.translator.interactive import Translation
    t = Translation(entry_point, backend='c', standalone=True)
    # no counters
    t.backendopt(inline_threshold=100, profile_based_inline="500")
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2

    t = Translation(entry_point, backend='c', standalone=True)
    # counters
    t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST*0.5,
                 profile_based_inline="500")
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
示例#24
0
 def test_profopt(self):
     def add(a,b):
         return a + b - b + b - b + b - b + b - b + b - b + b - b + b
     def entry_point(argv):
         tot =  0
         x = int(argv[1])
         while x > 0:
             tot = add(tot, x)
             x -= 1
         os.write(1, str(tot))
         return 0
     from pypy.translator.interactive import Translation
     # XXX this is mostly a "does not crash option"
     t = Translation(entry_point, backend='c', standalone=True, profopt="100")
     # no counters
     t.backendopt()
     exe = t.compile()
     out = py.process.cmdexec("%s 500" % exe)
     assert int(out) == 500*501/2
     t = Translation(entry_point, backend='c', standalone=True, profopt="100",
                     noprofopt=True)
     # no counters
     t.backendopt()
     exe = t.compile()
     out = py.process.cmdexec("%s 500" % exe)
     assert int(out) == 500*501/2
示例#25
0
文件: test_genc.py 项目: alkorzt/pypy
def compile(fn, argtypes, view=False, gcpolicy="ref", backendopt=True,
            annotatorpolicy=None):
    t = Translation(fn, argtypes, gc=gcpolicy, backend="c",
                    policy=annotatorpolicy)
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.annotate()
    # XXX fish
    t.driver.config.translation.countmallocs = True
    compiled_fn = t.compile_c()
    if getattr(py.test.config.option, 'view', False):
        t.view()
    malloc_counters = t.driver.cbuilder.get_malloc_counters()
    def checking_fn(*args, **kwds):
        if 'expected_extra_mallocs' in kwds:
            expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
        else:
            expected_extra_mallocs = 0
        res = compiled_fn(*args, **kwds)
        mallocs, frees = malloc_counters()
        if isinstance(expected_extra_mallocs, int):
            assert mallocs - frees == expected_extra_mallocs
        else:
            assert mallocs - frees in expected_extra_mallocs
        return res
    return checking_fn
示例#26
0
def compile(fn, argtypes, view=False, gcpolicy="ref", backendopt=True,
            annotatorpolicy=None):
    if argtypes is not None and "__pypy__" in sys.builtin_module_names:
        py.test.skip("requires building cpython extension modules")
    t = Translation(fn, argtypes, gc=gcpolicy, backend="c",
                    policy=annotatorpolicy)
    if not backendopt:
        t.disable(["backendopt_lltype"])
    t.annotate()
    # XXX fish
    t.driver.config.translation.countmallocs = True
    compiled_fn = t.compile_c()
    try:
        if py.test.config.option.view:
            t.view()
    except AttributeError:
        pass
    malloc_counters = t.driver.cbuilder.get_malloc_counters()
    def checking_fn(*args, **kwds):
        if 'expected_extra_mallocs' in kwds:
            expected_extra_mallocs = kwds.pop('expected_extra_mallocs')
        else:
            expected_extra_mallocs = 0
        res = compiled_fn(*args, **kwds)
        mallocs, frees = malloc_counters()
        if isinstance(expected_extra_mallocs, int):
            assert mallocs - frees == expected_extra_mallocs
        else:
            assert mallocs - frees in expected_extra_mallocs
        return res
    return checking_fn
示例#27
0
def test_profopt():
    if sys.platform == 'win32':
        py.test.skip("instrumentation support is unix only for now")
    def add(a,b):
        return a + b - b + b - b + b - b + b - b + b - b + b - b + b
    def entry_point(argv):
        tot =  0
        x = int(argv[1])
        while x > 0:
            tot = add(tot, x)
            x -= 1
        os.write(1, str(tot))
        return 0
    from pypy.translator.interactive import Translation
    # XXX this is mostly a "does not crash option"
    t = Translation(entry_point, backend='c', standalone=True, profopt="")
    # no counters
    t.backendopt()
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
    t = Translation(entry_point, backend='c', standalone=True, profopt="",
                    noprofopt=True)
    # no counters
    t.backendopt()
    exe = t.compile()
    out = py.process.cmdexec("%s 500" % exe)
    assert int(out) == 500*501/2
示例#28
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])
    t = Translation(parse)
    t.annotate([bool])
    t.backendopt()
    t.rtype()
    func = t.compile_c()
    res1 = parse(True)
    res2 = func(True)
    assert res1 == res2
示例#29
0
def test_simple_rtype():
    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    s = t.annotate()
    t.rtype()

    assert 'rtype_lltype' in t.driver.done

    t = Translation(f)
    s = t.annotate([int, int])
    t.rtype()

    assert 'rtype_lltype' in t.driver.done
示例#30
0
def test_dup2_access():
    def entry_point(argv):
        os.dup2(34, 56)
        y = os.access("spam", 77)
        return 1 - y

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()
    g, f = os.popen2(exe, "t", 0)
    expect(f, g, "ll_os.ll_os_dup2",   (34, 56), None)
    expect(f, g, "ll_os.ll_os_access", ("spam", 77), True)
    g.close()
    tail = f.read()
    f.close()
    assert tail == ""
示例#31
0
def test_time():
    def entry_point(argv):
        t = time.time()
        os.dup(int(t*1000))
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()
    g, f = os.popen2(exe, "t", 0)
    expect(f, g, "ll_time.ll_time_time", (), 3.141592)
    expect(f, g, "ll_os.ll_os_dup", (3141,), 3)
    g.close()
    tail = f.read()
    f.close()
    assert tail == ""
示例#32
0
def test_enforced_args():
    from pypy.annotation.model import s_None
    from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
    from pypy.translator.interactive import Translation

    def f1():
        str2charp("hello")

    def f2():
        str2charp("world")

    t = Translation(f1, [])
    t.rtype()
    mixann = MixLevelHelperAnnotator(t.context.rtyper)
    mixann.getgraph(f2, [], s_None)
    mixann.finish()
示例#33
0
def test_foobar():
    py.test.skip("to be updated")
    foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG)
    def entry_point(argv):
        s = rffi.str2charp(argv[1]); n = foobar(s); rffi.free_charp(s)
        s = rffi.str2charp(argv[n]); n = foobar(s); rffi.free_charp(s)
        return n
    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = MySandboxedProc([exe, 'spam', 'egg'], expected = [
        ("foobar", ("spam",), 2),
        ("foobar", ("egg",), 0),
        ])
    proc.handle_forever()
    assert proc.seen == len(proc.expected)
示例#34
0
def test_open_dup():
    def entry_point(argv):
        fd = os.open("/tmp/foobar", os.O_RDONLY, 0777)
        assert fd == 77
        fd2 = os.dup(fd)
        assert fd2 == 78
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()
    g, f = os.popen2(exe, "t", 0)
    expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77)
    expect(f, g, "ll_os.ll_os_dup",  (77,), 78)
    g.close()
    tail = f.read()
    f.close()
    assert tail == ""
示例#35
0
def test_simple_compile_c_isolate():
    from pypy.tool import isolate

    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    t.set_backend_extra_options(c_isolated=True)
    t_f = t.compile()

    assert isinstance(t_f, isolate.IsolateInvoker)

    res = t_f(2, 3)
    assert res == 5

    # cleanup
    t_f.close_isolate()
示例#36
0
def test_socketio():
    class SocketProc(VirtualizedSocketProc, SimpleIOSandboxedProc):
        def build_virtual_root(self):
            pass

    def entry_point(argv):
        fd = os.open("tcp://google.com:80", os.O_RDONLY, 0777)
        os.write(fd, 'GET /\n')
        print os.read(fd, 30)
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = SocketProc([exe])
    output, error = proc.communicate("")
    assert output.startswith('<HTML><HEAD>')
def test_simple_source():
    def f(x, y):
        return x,y

    t = Translation(f, backend='c')
    t.annotate([int, int])
    t.source()
    assert 'source_c' in t.driver.done

    t = Translation(f, [int, int])
    t.source_c()
    assert 'source_c' in t.driver.done

    t = Translation(f, [int, int])
    py.test.raises(Exception, "t.source()")
示例#38
0
文件: test_genc.py 项目: ieure/pypy
def test_inhibit_tail_call():
    from pypy.rpython.lltypesystem import lltype
    def foobar_fn(n):
        return 42
    foobar_fn._dont_inline_ = True
    def main(n):
        return foobar_fn(n)
    #
    t = Translation(main, [int], backend="c")
    t.rtype()
    t.context._graphof(foobar_fn).inhibit_tail_call = True
    t.source_c()
    lines = t.driver.cbuilder.c_source_filename.readlines()
    for i, line in enumerate(lines):
        if '= pypy_g_foobar_fn' in line:
            break
    else:
        assert 0, "the call was not found in the C source"
    assert 'PYPY_INHIBIT_TAIL_CALL();' in lines[i+1]
示例#39
0
def test_compile_recognizer():
    try:
        from pypy.translator.interactive import Translation
    except ImportError:
        py.test.skip("pypy not found on path")
    a = DFA()
    s0 = a.add_state("start")
    s1 = a.add_state()
    s2 = a.add_state(final=True)
    a[s0, "a"] = s0
    a[s0, "c"] = s1
    a[s0, "b"] = s2
    a[s1, "b"] = s2
    recognize = a.make_code()
    t = Translation(recognize)
    t.backendopt([str], backend="c")
    cfn = t.compile_c()
    assert cfn("aaaaaaaaaab")
    assert cfn("b")
    assert cfn("aaaacb")
示例#40
0
def test_simple_backendopt():
    def f(x, y):
        return x, y

    t = Translation(f, [int, int], backend='c')
    t.backendopt()

    assert 'backendopt_lltype' in t.driver.done

    t = Translation(f, [int, int])
    t.backendopt()

    assert 'backendopt_lltype' in t.driver.done
示例#41
0
def test_stat_ftruncate():
    from pypy.rpython.module.ll_os_stat import s_StatResult
    from pypy.rlib.rarithmetic import r_longlong
    r0x12380000007 = r_longlong(0x12380000007)

    def entry_point(argv):
        st = os.stat("somewhere")
        os.ftruncate(st.st_mode, st.st_size)  # nonsense, just to see outside
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()
    g, f = os.popen2(exe, "t", 0)
    st = os.stat_result((55, 0, 0, 0, 0, 0, 0x12380000007, 0, 0, 0))
    expect(f, g, "ll_os.ll_os_stat", ("somewhere",), st,
           resulttype = s_StatResult)
    expect(f, g, "ll_os.ll_os_ftruncate", (55, 0x12380000007), None)
    g.close()
    tail = f.read()
    f.close()
    assert tail == ""
示例#42
0
def test_simpleio():
    def entry_point(argv):
        print "Please enter a number:"
        buf = ""
        while True:
            t = os.read(0, 1)    # 1 character from stdin
            if not t:
                raise EOFError
            if t == '\n':
                break
            buf += t
        num = int(buf)
        print "The double is:", num * 2
        return 0
    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = SimpleIOSandboxedProc([exe, 'x1', 'y2'])
    output, error = proc.communicate("21\n")
    assert output == "Please enter a number:\nThe double is: 42\n"
    assert error == ""
示例#43
0
def test_simple_source_llvm():
    from pypy.translator.llvm.test.runtest import llvm_test
    llvm_test()

    def f(x, y):
        return x + y

    t = Translation(f, [int, int], backend='llvm')
    t.source(gc='boehm')
    assert 'source_llvm' in t.driver.done

    t = Translation(f, [int, int])
    t.source_llvm()
    assert 'source_llvm' in t.driver.done
示例#44
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()
     t = Translation(lex)
     t.annotate([str, bool])
     t.rtype()
     func = t.compile_c()
     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()
示例#45
0
def test_read_write():
    def entry_point(argv):
        fd = os.open("/tmp/foobar", os.O_RDONLY, 0777)
        assert fd == 77
        res = os.read(fd, 123)
        assert res == "he\x00llo"
        count = os.write(fd, "world\x00!\x00")
        assert count == 42
        os.close(fd)
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()
    g, f = os.popen2(exe, "t", 0)
    expect(f, g, "ll_os.ll_os_open",  ("/tmp/foobar", os.O_RDONLY, 0777), 77)
    expect(f, g, "ll_os.ll_os_read",  (77, 123), "he\x00llo")
    expect(f, g, "ll_os.ll_os_write", (77, "world\x00!\x00"), 42)
    expect(f, g, "ll_os.ll_os_close", (77,), None)
    g.close()
    tail = f.read()
    f.close()
    assert tail == ""
示例#46
0
def test_simpleio():
    def entry_point(argv):
        print "Please enter a number:"
        buf = ""
        while True:
            t = os.read(0, 1)  # 1 character from stdin
            if not t:
                raise EOFError
            if t == '\n':
                break
            buf += t
        num = int(buf)
        print "The double is:", num * 2
        return 0

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = SimpleIOSandboxedProc([exe, 'x1', 'y2'])
    output, error = proc.communicate("21\n")
    assert output == "Please enter a number:\nThe double is: 42\n"
    assert error == ""
示例#47
0
def test_simple_rtype():
    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    s = t.annotate()
    t.rtype()

    assert "rtype_lltype" in t.driver.done

    t = Translation(f)
    s = t.annotate([int, int])
    t.rtype()

    assert "rtype_lltype" in t.driver.done
示例#48
0
def test_parser():
    def f(x):
        if x:
            s = "a(X, Y, Z)."
        else:
            s = "f(a, X, _, _, X, f(X, 2.455))."
        term = parsing.parse_file(s)
        assert isinstance(term, parsing.Nonterminal)
        return term.symbol

    assert f(True) == "file"
    assert f(True) == "file"
    t = Translation(f)
    t.annotate([bool])
    t.rtype()
    t.backendopt()
    func = t.compile_c()
    assert func(True) == "file"
    assert func(False) == "file"
示例#49
0
def test_name():
    def f():
        return 3

    f.c_name = 'pypy_xyz_f'

    t = Translation(f, [], backend="c")
    t.annotate()
    compiled_fn = t.compile_c()
    if py.test.config.option.view:
        t.view()
    assert 'pypy_xyz_f' in t.driver.cbuilder.c_source_filename.read()
示例#50
0
def test_simple_compile_c():
    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    t.source(backend='c')
    t_f = t.compile()

    res = t_f(2, 3)
    assert res == 5

    t = Translation(f, [int, int])
    t_f = t.compile_c()

    res = t_f(2, 3)
    assert res == 5
示例#51
0
def test_foobar():
    py.test.skip("to be updated")
    foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG)

    def entry_point(argv):
        s = rffi.str2charp(argv[1])
        n = foobar(s)
        rffi.free_charp(s)
        s = rffi.str2charp(argv[n])
        n = foobar(s)
        rffi.free_charp(s)
        return n

    t = Translation(entry_point, backend='c', standalone=True, sandbox=True)
    exe = t.compile()

    proc = MySandboxedProc([exe, 'spam', 'egg'],
                           expected=[
                               ("foobar", ("spam", ), 2),
                               ("foobar", ("egg", ), 0),
                           ])
    proc.handle_forever()
    assert proc.seen == len(proc.expected)
示例#52
0
def test_disable_logic():
    def f(x, y):
        return x + y

    t = Translation(f, [int, int])
    t.disable(['backendopt'])
    t.source_c()

    assert 'backendopt' not in t.driver.done
示例#53
0
def compile_rex(rex):
    try:
        from pypy.translator.interactive import Translation
    except ImportError:
        py.test.skip("pypy not found")
    fda = rex.make_automaton().make_deterministic()
    fda.optimize()
    fn = fda.make_code()
    t = Translation(fn)
    t.backendopt([str], backend="c")
    if py.test.config.option.view:
        t.view()
    return t.compile_c()