Exemplo n.º 1
0
 def getcompiled(self, func, argstypelist = [],
                 annotatorpolicy=None):
     from pypy.config.pypyoption import get_pypy_config
     config = get_pypy_config(translating=True)
     config.translation.gc = self.gcpolicy
     config.translation.thread = self.use_threads
     if self.stacklessgc:
         config.translation.gcrootfinder = "stackless"
     config.translation.simplifying = True
     t = TranslationContext(config=config)
     self.t = t
     a = t.buildannotator(policy=annotatorpolicy)
     a.build_types(func, argstypelist)
     t.buildrtyper().specialize()
     t.checkgraphs()
     def compile():
         cbuilder = CExtModuleBuilder(t, func, config=config)
         c_source_filename = cbuilder.generate_source(
             defines = cbuilder.DEBUG_DEFINES)
         if conftest.option.view:
             t.view()
         cbuilder.compile()
         self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test
         return cbuilder.get_entry_point(isolated=True)
     return compile()
def test_remove_same_as():
    def nothing(x):
        return x
    def f():
        nothing(False)
        if nothing(True):
            return 42
        else:
            return 666
    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    # now we make the 'if True' appear
    f_graph = graphof(t, f)
    simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Exemplo n.º 3
0
def test_remove_same_as_nonconst():
    from pypy.rlib.nonconst import NonConstant
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rpython.lltypesystem import lltype

    def f():
        if NonConstant(False):
            x = llop.same_as(lltype.Signed, 666)
        return 42

    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    f_graph = graphof(t, f)
    #simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    for block in t.annotator.annotated:
        assert None not in block.operations

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Exemplo n.º 4
0
def getcompiled(func, view=conftest.option.view, use_boehm=False):
    from pypy.translator.translator import TranslationContext
    from pypy.translator.backendopt.all import backend_optimizations

    from pypy.translator.c import gc
    from pypy.translator.c.genc import CExtModuleBuilder

    global t # allow us to view later
    t = TranslationContext()
    t.buildannotator().build_types(func, get_annotation(func))
    t.buildrtyper().specialize()
    t.checkgraphs()

    gcpolicy = None
    if use_boehm:
        gcpolicy = gc.BoehmGcPolicy

    cbuilder = CExtModuleBuilder(t, func, t.config, gcpolicy=gcpolicy)
    cbuilder.generate_source()
    cbuilder.compile()

    backend_optimizations(t)
    if view:
        t.viewcg()
    return getattr(cbuilder.import_module(), func.__name__)
Exemplo n.º 5
0
def test_remove_same_as_nonconst():
    from pypy.rlib.nonconst import NonConstant
    from pypy.rpython.lltypesystem.lloperation import llop
    from pypy.rpython.lltypesystem import lltype

    def f():
        if NonConstant(False):
            x = llop.same_as(lltype.Signed, 666)
        return 42

    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    f_graph = graphof(t, f)
    #simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    for block in t.annotator.annotated:
        assert None not in block.operations

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Exemplo n.º 6
0
 def translates(self, func=None, argtypes=None, **kwds):
     config = make_config(None, **kwds)
     if func is not None:
         if argtypes is None:
             nb_args = func.func_code.co_argcount
             argtypes = [W_Root] * nb_args
     #
     t = TranslationContext(config=config)
     self.t = t  # for debugging
     ann = t.buildannotator()
     ann.policy.allow_someobjects = False
     if func is not None:
         ann.build_types(func, argtypes, complete_now=False)
     #
     # annotate all _seen_extras, knowing that annotating some may
     # grow the list
     done = 0
     while done < len(self._seen_extras):
         #print self._seen_extras
         ann.build_types(self._seen_extras[done], [], complete_now=False)
         done += 1
     ann.complete()
     #t.viewcg()
     t.buildrtyper().specialize()
     t.checkgraphs()
Exemplo n.º 7
0
 def check(self, fn, signature, args, expected_result,
           expected_mallocs=0, expected_calls=0):
     t = TranslationContext()
     self.translator = t
     t.buildannotator().build_types(fn, signature)
     t.buildrtyper(type_system=self.type_system).specialize()
     graph = graphof(t, fn)
     if option.view:
         t.view()
     self.original_graph_count = len(t.graphs)
     # to detect broken intermediate graphs,
     # we do the loop ourselves instead of calling remove_simple_mallocs()
     maxiter = 100
     mallocv = MallocVirtualizer(t.graphs, t.rtyper, verbose=True)
     while True:
         progress = mallocv.remove_mallocs_once()
         #simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks()))
         if progress and option.view:
             t.view()
         t.checkgraphs()
         if expected_result is not DONT_CHECK_RESULT:
             interp = LLInterpreter(t.rtyper)
             if not isinstance(expected_result, CHECK_RAISES):
                 res = interp.eval_graph(graph, args)
                 assert res == expected_result
             else:
                 excinfo = py.test.raises(LLException,
                                          interp.eval_graph, graph, args)
                 assert expected_result.excname in str(excinfo.value)
         if not progress:
             break
         maxiter -= 1
         assert maxiter > 0, "infinite loop?"
     self.check_malloc_removed(graph, expected_mallocs, expected_calls)
     return graph
Exemplo n.º 8
0
    def rtype(self, fn, argtypes, resulttype, checkfunction=None):
        t = TranslationContext()
        a = t.buildannotator()
        a.build_types(prefn, [int])
        typer = t.buildrtyper()
        typer.specialize()
        #t.view()

        s_result = a.typeannotation(resulttype)

        from pypy.rpython import annlowlevel
        # annotate, normalize and rtype fn after the fact
        annhelper = annlowlevel.MixLevelHelperAnnotator(typer)               
        graph = annhelper.getgraph(fn, [a.typeannotation(argtype) for argtype in argtypes],
                                   s_result)
        annhelper.finish()
        t.checkgraphs()

        if checkfunction is not None:
            checkfunction(t)

        # sanity check prefn
        llinterp = LLInterpreter(typer)
        res = llinterp.eval_graph(graphof(t, prefn), [1])
        assert res == 100
        res = llinterp.eval_graph(graphof(t, prefn), [2])
        assert res == 201
        
        return t
Exemplo n.º 9
0
 def translates(self, func=None, argtypes=None, **kwds):
     config = make_config(None, **kwds)
     if func is not None:
         if argtypes is None:
             nb_args = func.func_code.co_argcount
             argtypes = [W_Root] * nb_args
     #
     t = TranslationContext(config=config)
     self.t = t     # for debugging
     ann = t.buildannotator()
     ann.policy.allow_someobjects = False
     if func is not None:
         ann.build_types(func, argtypes, complete_now=False)
     #
     # annotate all _seen_extras, knowing that annotating some may
     # grow the list
     done = 0
     while done < len(self._seen_extras):
         #print self._seen_extras
         ann.build_types(self._seen_extras[done], [],
                         complete_now=False)
         done += 1
     ann.complete()
     #t.viewcg()
     t.buildrtyper().specialize()
     t.checkgraphs()
Exemplo n.º 10
0
    def rtype(self, fn, argtypes, resulttype, checkfunction=None):
        t = TranslationContext()
        a = t.buildannotator()
        a.build_types(prefn, [int])
        typer = t.buildrtyper()
        typer.specialize()
        #t.view()

        s_result = a.typeannotation(resulttype)

        from pypy.rpython import annlowlevel
        # annotate, normalize and rtype fn after the fact
        annhelper = annlowlevel.MixLevelHelperAnnotator(typer)
        graph = annhelper.getgraph(
            fn, [a.typeannotation(argtype) for argtype in argtypes], s_result)
        annhelper.finish()
        t.checkgraphs()

        if checkfunction is not None:
            checkfunction(t)

        # sanity check prefn
        llinterp = LLInterpreter(typer)
        res = llinterp.eval_graph(graphof(t, prefn), [1])
        assert res == 100
        res = llinterp.eval_graph(graphof(t, prefn), [2])
        assert res == 201

        return t
Exemplo n.º 11
0
def test_remove_same_as():
    def nothing(x):
        return x

    def f():
        nothing(False)
        if nothing(True):
            return 42
        else:
            return 666

    t = TranslationContext()
    t.buildannotator().build_types(f, [])
    t.buildrtyper().specialize()
    # now we make the 'if True' appear
    f_graph = graphof(t, f)
    simple_inline_function(t, nothing, f_graph)
    # here, the graph looks like  v21=same_as(True);  exitswitch: v21
    remove_same_as(f_graph)
    t.checkgraphs()
    # only one path should be left
    for block in f_graph.iterblocks():
        assert len(block.exits) <= 1

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [])
    assert result == 42
Exemplo n.º 12
0
    def getcompiled(self, func, argstypelist=[], annotatorpolicy=None):
        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True)
        config.translation.gc = self.gcpolicy
        config.translation.thread = self.use_threads
        config.translation.simplifying = True
        t = TranslationContext(config=config)
        self.t = t
        a = t.buildannotator(policy=annotatorpolicy)
        a.build_types(func, argstypelist)
        t.buildrtyper().specialize()
        t.checkgraphs()

        def compile():
            cbuilder = CExtModuleBuilder(t, func, config=config)
            c_source_filename = cbuilder.generate_source(
                defines=cbuilder.DEBUG_DEFINES)
            if conftest.option.view:
                t.view()
            cbuilder.compile()
            self._cleanups.append(
                cbuilder.cleanup)  # schedule cleanup after test
            return cbuilder.get_entry_point(isolated=True)

        return compile()
Exemplo n.º 13
0
 def check(self, fn, signature, args, expected_result,
           expected_mallocs=0, expected_calls=0):
     t = TranslationContext()
     self.translator = t
     t.buildannotator().build_types(fn, signature)
     t.buildrtyper(type_system=self.type_system).specialize()
     graph = graphof(t, fn)
     if option.view:
         t.view()
     self.original_graph_count = len(t.graphs)
     # to detect missing keepalives and broken intermediate graphs,
     # we do the loop ourselves instead of calling remove_simple_mallocs()
     maxiter = 100
     mallocv = MallocVirtualizer(t.graphs, t.rtyper, verbose=True)
     while True:
         progress = mallocv.remove_mallocs_once()
         #simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks()))
         if progress and option.view:
             t.view()
         t.checkgraphs()
         if expected_result is not DONT_CHECK_RESULT:
             interp = LLInterpreter(t.rtyper)
             if not isinstance(expected_result, CHECK_RAISES):
                 res = interp.eval_graph(graph, args)
                 assert res == expected_result
             else:
                 excinfo = py.test.raises(LLException,
                                          interp.eval_graph, graph, args)
                 assert expected_result.excname in str(excinfo.value)
         if not progress:
             break
         maxiter -= 1
         assert maxiter > 0, "infinite loop?"
     self.check_malloc_removed(graph, expected_mallocs, expected_calls)
     return graph
Exemplo n.º 14
0
def rtype(fn, argtypes=[]):
    t = TranslationContext()
    t.buildannotator().build_types(fn, argtypes)
    typer = t.buildrtyper()
    typer.specialize()
    #t.view()
    t.checkgraphs()
    return t
Exemplo n.º 15
0
def rtype(fn, argtypes=[]):
    t = TranslationContext()
    t.buildannotator().build_types(fn, argtypes)
    typer = t.buildrtyper()
    typer.specialize()
    #t.view()
    t.checkgraphs()
    return t
Exemplo n.º 16
0
 def rtype(self, fn, argtypes, resulttype):
     t = TranslationContext()
     a = t.buildannotator()
     s = a.build_types(fn, argtypes)
     assert s == a.typeannotation(resulttype)
     typer = t.buildrtyper()
     typer.specialize()
     #t.view()
     t.checkgraphs()
     return t
Exemplo n.º 17
0
 def rtype(self, fn, argtypes, resulttype):
     t = TranslationContext()
     a = t.buildannotator()
     s = a.build_types(fn, argtypes)
     assert s == a.typeannotation(resulttype)
     typer = t.buildrtyper()
     typer.specialize()
     #t.view()
     t.checkgraphs()
     return t
Exemplo n.º 18
0
    def _makefunc_str_int(cls, func):
        def main(argv):
            arg0 = argv[1]
            arg1 = int(argv[2])
            try:
                res = func(arg0, arg1)
            except MemoryError:
                print 'Result: MemoryError'
            else:
                print 'Result: "%s"' % (res,)
            return 0
        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True)
        config.translation.gc = cls.gcpolicy
        config.translation.gcrootfinder = "asmgcc"
        if sys.platform == 'win32':
            config.translation.cc = 'mingw32'
        t = TranslationContext(config=config)
        a = t.buildannotator()
        a.build_types(main, [s_list_of_strings])
        t.buildrtyper().specialize()
        t.checkgraphs()

        cbuilder = CStandaloneBuilder(t, main, config=config)
        c_source_filename = cbuilder.generate_source(
            defines = cbuilder.DEBUG_DEFINES)
        cls._patch_makefile(cbuilder.targetdir)
        if conftest.option.view:
            t.view()
        exe_name = cbuilder.compile()

        def run(arg0, arg1):
            lines = []
            print >> sys.stderr, 'RUN: starting', exe_name
            if sys.platform == 'win32':
                redirect = ' 2> NUL'
            else:
                redirect = ''
            g = os.popen('"%s" %s %d%s' % (exe_name, arg0, arg1, redirect), 'r')
            for line in g:
                print >> sys.stderr, 'RUN:', line.rstrip()
                lines.append(line)
            g.close()
            if not lines:
                py.test.fail("no output from subprocess")
            if not lines[-1].startswith('Result:'):
                py.test.fail("unexpected output from subprocess")
            result = lines[-1][len('Result:'):].strip()
            if result == 'MemoryError':
                raise MemoryError("subprocess got an RPython MemoryError")
            if result.startswith('"') and result.endswith('"'):
                return result[1:-1]
            else:
                return int(result)
        return run
Exemplo n.º 19
0
def test_retval_None():
    def f(x):
        pass
    t = TranslationContext()
    t.buildannotator().build_types(f, [int])
    t.buildrtyper().specialize()
    #t.view()
    t.checkgraphs()
    graph = graphof(t, f)
    assert graph.getreturnvar().concretetype == Void
    assert graph.startblock.exits[0].args[0].concretetype == Void
Exemplo n.º 20
0
def test_retval_None():
    def f(x):
        pass

    t = TranslationContext()
    t.buildannotator().build_types(f, [int])
    t.buildrtyper().specialize()
    #t.view()
    t.checkgraphs()
    graph = graphof(t, f)
    assert graph.getreturnvar().concretetype == Void
    assert graph.startblock.exits[0].args[0].concretetype == Void
Exemplo n.º 21
0
    def getcompiled(self, func):
        def main(argv):
            try:
                res = func()
            except MemoryError:
                print 'Result: MemoryError'
            else:
                if isinstance(res, int):
                    print 'Result:', res
                else:
                    print 'Result: "%s"' % (res,)
            return 0
        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True)
        config.translation.gc = self.gcpolicy
        config.translation.gcrootfinder = "asmgcc"
        t = TranslationContext(config=config)
        self.t = t
        a = t.buildannotator()
        a.build_types(main, [s_list_of_strings])
        t.buildrtyper().specialize()
        t.checkgraphs()

        cbuilder = CStandaloneBuilder(t, main, config=config)
        c_source_filename = cbuilder.generate_source(
            defines = cbuilder.DEBUG_DEFINES)
        self.patch_makefile(cbuilder.targetdir)
        if conftest.option.view:
            t.view()
        exe_name = cbuilder.compile()

        def run():
            lines = []
            print >> sys.stderr, 'RUN: starting', exe_name
            g = os.popen("'%s'" % (exe_name,), 'r')
            for line in g:
                print >> sys.stderr, 'RUN:', line.rstrip()
                lines.append(line)
            g.close()
            if not lines:
                py.test.fail("no output from subprocess")
            if not lines[-1].startswith('Result:'):
                py.test.fail("unexpected output from subprocess")
            result = lines[-1][len('Result:'):].strip()
            if result == 'MemoryError':
                raise MemoryError("subprocess got an RPython MemoryError")
            if result.startswith('"') and result.endswith('"'):
                return result[1:-1]
            else:
                return int(result)
        return run
Exemplo n.º 22
0
def test_stackless():
    t = TranslationContext()
    a = t.buildannotator()
    a.build_types(g, [int])
    a.simplify()
    t.buildrtyper().specialize()
    backend_optimizations(t)
    t.checkgraphs()
    n = insert_ll_stackcheck(t)
    t.checkgraphs()
    assert n == 1

    t.config.translation.stackless = True
    stacklesstransf = StacklessTransformer(t, g)

    f_graph = graphof(t, f)
    stacklesstransf.transform_graph(f_graph)
    if conftest.option.view:
        f_graph.show()

    exctransf = t.getexceptiontransformer()
    exctransf.create_exception_handling(f_graph)
    if conftest.option.view:
        f_graph.show()
    check(f_graph, 'f', 'fetch_retval_void')

    class GCTransform(framework.FrameworkGCTransformer):
        from pypy.rpython.memory.gc.generation import GenerationGC as \
                                                          GCClass
        GC_PARAMS = {}

    gctransf = GCTransform(t)
    gctransf.transform_graph(f_graph)
    if conftest.option.view:
        f_graph.show()
    relevant = check(f_graph, 'f', 'fetch_retval_void')
    for p in relevant:
        in_between = False
        reload = 0
        for spaceop in p:
            if spaceop.opname == 'direct_call':
                target = direct_target(spaceop)
                if target == 'f':
                    in_between = False
                elif target == 'stack_check___':
                    in_between = True
            if in_between and spaceop.opname == 'gc_reload_possibly_moved':
                reload += 1

        assert reload == 1
Exemplo n.º 23
0
def test_simple():
    t = TranslationContext()
    a = t.buildannotator()
    a.build_types(g, [int])
    a.simplify()
    t.buildrtyper().specialize()        
    backend_optimizations(t)
    t.checkgraphs()
    n = insert_ll_stackcheck(t)
    t.checkgraphs()
    assert n == 1
    if conftest.option.view:
        t.view()
    check(graphof(t, f), 'f')
Exemplo n.º 24
0
def test_stackless():
    t = TranslationContext()
    a = t.buildannotator()
    a.build_types(g, [int])
    a.simplify()
    t.buildrtyper().specialize()        
    backend_optimizations(t)
    t.checkgraphs()
    n = insert_ll_stackcheck(t)
    t.checkgraphs()
    assert n == 1

    t.config.translation.stackless = True
    stacklesstransf = StacklessTransformer(t, g)
        
    f_graph = graphof(t, f)
    stacklesstransf.transform_graph(f_graph)
    if conftest.option.view:
        f_graph.show()

    exctransf = t.getexceptiontransformer()
    exctransf.create_exception_handling(f_graph)
    if conftest.option.view:
        f_graph.show()
    check(f_graph, 'f', 'fetch_retval_void')    

    class GCTransform(framework.FrameworkGCTransformer):
        from pypy.rpython.memory.gc.generation import GenerationGC as \
                                                          GCClass
        GC_PARAMS = {}

    gctransf = GCTransform(t)
    gctransf.transform_graph(f_graph)
    if conftest.option.view:
        f_graph.show()
    relevant = check(f_graph, 'f', 'fetch_retval_void')
    for p in relevant:
        in_between = False
        reload = 0
        for spaceop in p:
            if spaceop.opname == 'direct_call':
                target = direct_target(spaceop)
                if target == 'f':
                    in_between = False
                elif target == 'stack_check___':
                    in_between = True
            if in_between and spaceop.opname == 'gc_reload_possibly_moved':
                reload += 1
                
        assert reload == 1
Exemplo n.º 25
0
def test_simple():
    t = TranslationContext()
    a = t.buildannotator()
    a.build_types(g, [int])
    a.simplify()
    t.buildrtyper().specialize()        
    backend_optimizations(t)
    t.checkgraphs()
    n = insert_ll_stackcheck(t)
    t.checkgraphs()
    assert n == 1
    if conftest.option.view:
        t.view()
    check(graphof(t, f), 'f')
Exemplo n.º 26
0
def test_missing_gvflavor_bug():
    class MyClass:
        def set_x(self):
            self.x = create_tuple()
    def create_tuple():
        return MyClass(), 42
    def fn():
        obj = MyClass()
        obj.set_x()
        create_tuple()
    t = TranslationContext()
    t.buildannotator().build_types(fn, [])
    t.buildrtyper(type_system='ootype').specialize()
    #t.view()
    t.checkgraphs()
    graph = graphof(t, fn)
    assert graph.getreturnvar().concretetype == Void
Exemplo n.º 27
0
def test_remove_unaryops():
    # We really want to use remove_unaryops for things like ooupcast and
    # oodowncast in dynamically typed languages, but it's easier to test
    # it with operations on ints here.
    def f(x):
        i = llop.int_invert(lltype.Signed, x)
        i = llop.int_add(lltype.Signed, x, 1)
        return llop.int_neg(lltype.Signed, i)
    t = TranslationContext()
    t.buildannotator().build_types(f, [int])
    t.buildrtyper().specialize()
    f_graph = graphof(t, f)
    remove_unaryops(f_graph, ["int_neg", "int_invert"])
    t.checkgraphs()

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [-2])
    assert result == -1
Exemplo n.º 28
0
def test_remove_unaryops():
    # We really want to use remove_unaryops for things like ooupcast and
    # oodowncast in dynamically typed languages, but it's easier to test
    # it with operations on ints here.
    def f(x):
        i = llop.int_invert(lltype.Signed, x)
        i = llop.int_add(lltype.Signed, x, 1)
        return llop.int_neg(lltype.Signed, i)

    t = TranslationContext()
    t.buildannotator().build_types(f, [int])
    t.buildrtyper().specialize()
    f_graph = graphof(t, f)
    remove_unaryops(f_graph, ["int_neg", "int_invert"])
    t.checkgraphs()

    interp = LLInterpreter(t.rtyper)
    result = interp.eval_graph(f_graph, [-2])
    assert result == -1
Exemplo n.º 29
0
def test_missing_gvflavor_bug():
    class MyClass:
        def set_x(self):
            self.x = create_tuple()

    def create_tuple():
        return MyClass(), 42

    def fn():
        obj = MyClass()
        obj.set_x()
        create_tuple()

    t = TranslationContext()
    t.buildannotator().build_types(fn, [])
    t.buildrtyper(type_system='ootype').specialize()
    #t.view()
    t.checkgraphs()
    graph = graphof(t, fn)
    assert graph.getreturnvar().concretetype == Void
Exemplo n.º 30
0
    def _makefunc_str_int(cls, func):
        def main(argv):
            arg0 = argv[1]
            arg1 = int(argv[2])
            try:
                res = func(arg0, arg1)
            except MemoryError:
                print 'Result: MemoryError'
            else:
                print 'Result: "%s"' % (res, )
            return 0

        config = cls.make_config()
        t = TranslationContext(config=config)
        a = t.buildannotator()
        sec_ep = getattr(cls, 'secondary_entrypoints', [])
        for f, inputtypes in sec_ep:
            a.build_types(f, inputtypes, False)
        a.build_types(main, [s_list_of_strings])
        t.buildrtyper().specialize()
        t.checkgraphs()

        cbuilder = CStandaloneBuilder(t,
                                      main,
                                      config=config,
                                      secondary_entrypoints=sec_ep)
        c_source_filename = cbuilder.generate_source(
            defines=cbuilder.DEBUG_DEFINES)
        cls._patch_makefile(cbuilder.targetdir)
        if conftest.option.view:
            t.view()
        exe_name = cbuilder.compile()

        def run(arg0, arg1):
            lines = []
            print >> sys.stderr, 'RUN: starting', exe_name, arg0, arg1
            if sys.platform == 'win32':
                redirect = ' 2> NUL'
            else:
                redirect = ''
            if config.translation.shared and os.name == 'posix':
                env = 'LD_LIBRARY_PATH="%s" ' % (exe_name.dirpath(), )
            else:
                env = ''
            cwd = os.getcwd()
            try:
                os.chdir(str(exe_name.dirpath()))
                g = os.popen(
                    '%s"%s" %s %d%s' % (env, exe_name, arg0, arg1, redirect),
                    'r')
            finally:
                os.chdir(cwd)
            for line in g:
                print >> sys.stderr, 'RUN:', line.rstrip()
                lines.append(line)
            g.close()
            if not lines:
                py.test.fail("no output from subprocess")
            if not lines[-1].startswith('Result:'):
                py.test.fail("unexpected output from subprocess")
            result = lines[-1][len('Result:'):].strip()
            if result == 'MemoryError':
                raise MemoryError("subprocess got an RPython MemoryError")
            if result.startswith('"') and result.endswith('"'):
                return result[1:-1]
            else:
                return int(result)

        return run
Exemplo n.º 31
0
 def _test(self, func, types):
     t = TranslationContext()
     t.buildannotator().build_types(func, types)
     t.buildrtyper().specialize()
     t.checkgraphs()
Exemplo n.º 32
0
 def _test(self, func, types):
     t = TranslationContext()
     t.buildannotator().build_types(func, types)
     t.buildrtyper().specialize()
     t.checkgraphs()    
Exemplo n.º 33
0
    def _makefunc_str_int(cls, func):
        def main(argv):
            arg0 = argv[1]
            arg1 = int(argv[2])
            try:
                res = func(arg0, arg1)
            except MemoryError:
                print 'Result: MemoryError'
            else:
                print 'Result: "%s"' % (res,)
            return 0
        config = cls.make_config()
        t = TranslationContext(config=config)
        a = t.buildannotator()
        sec_ep = getattr(cls, 'secondary_entrypoints', [])
        for f, inputtypes in sec_ep:
            a.build_types(f, inputtypes, False)
        a.build_types(main, [s_list_of_strings])
        t.buildrtyper().specialize()
        t.checkgraphs()

        cbuilder = CStandaloneBuilder(t, main, config=config,
                secondary_entrypoints=sec_ep)
        c_source_filename = cbuilder.generate_source(
            defines = cbuilder.DEBUG_DEFINES)
        cls._patch_makefile(cbuilder.targetdir)
        if conftest.option.view:
            t.view()
        exe_name = cbuilder.compile()

        def run(arg0, arg1):
            lines = []
            print >> sys.stderr, 'RUN: starting', exe_name, arg0, arg1
            if sys.platform == 'win32':
                redirect = ' 2> NUL'
            else:
                redirect = ''
            if config.translation.shared and os.name == 'posix':
                env = 'LD_LIBRARY_PATH="%s" ' % (exe_name.dirpath(),)
            else:
                env = ''
            cwd = os.getcwd()
            try:
                os.chdir(str(exe_name.dirpath()))
                g = os.popen(
                    '%s"%s" %s %d%s' % (env, exe_name, arg0, arg1, redirect), 'r')
            finally:
                os.chdir(cwd)
            for line in g:
                print >> sys.stderr, 'RUN:', line.rstrip()
                lines.append(line)
            g.close()
            if not lines:
                py.test.fail("no output from subprocess")
            if not lines[-1].startswith('Result:'):
                py.test.fail("unexpected output from subprocess")
            result = lines[-1][len('Result:'):].strip()
            if result == 'MemoryError':
                raise MemoryError("subprocess got an RPython MemoryError")
            if result.startswith('"') and result.endswith('"'):
                return result[1:-1]
            else:
                return int(result)
        return run