def test_counters(self):
        from pypy.rpython.lltypesystem import lltype
        from pypy.rpython.lltypesystem.lloperation import llop
        def entry_point(argv):
            llop.instrument_count(lltype.Void, 'test', 2)
            llop.instrument_count(lltype.Void, 'test', 1)
            llop.instrument_count(lltype.Void, 'test', 1)
            llop.instrument_count(lltype.Void, 'test', 2)
            llop.instrument_count(lltype.Void, 'test', 1)        
            return 0
        t = TranslationContext(self.config)
        t.config.translation.instrument = True
        t.buildannotator().build_types(entry_point, [s_list_of_strings])
        t.buildrtyper().specialize()

        cbuilder = CStandaloneBuilder(t, entry_point, config=t.config) # xxx
        cbuilder.generate_source()
        cbuilder.compile()

        counters_fname = udir.join("_counters_")
        os.putenv('_INSTRUMENT_COUNTERS', str(counters_fname))
        try:
            data = cbuilder.cmdexec()
        finally:
            os.unsetenv('_INSTRUMENT_COUNTERS')

        f = counters_fname.open('rb')
        counters_data = f.read()
        f.close()

        import struct
        counters = struct.unpack("LLL", counters_data)

        assert counters == (0,3,2)
Esempio n. 2
0
def test_counters():
    from pypy.rpython.lltypesystem import lltype
    from pypy.rpython.lltypesystem.lloperation import llop
    def entry_point(argv):
        llop.instrument_count(lltype.Void, 'test', 2)
        llop.instrument_count(lltype.Void, 'test', 1)
        llop.instrument_count(lltype.Void, 'test', 1)
        llop.instrument_count(lltype.Void, 'test', 2)
        llop.instrument_count(lltype.Void, 'test', 1)        
        return 0
    t = TranslationContext()
    t.config.translation.instrument = True
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, config=t.config) # xxx
    cbuilder.generate_source()
    cbuilder.compile()

    counters_fname = udir.join("_counters_")
    os.putenv('_INSTRUMENT_COUNTERS', str(counters_fname))
    try:
        data = cbuilder.cmdexec()
    finally:
        os.unsetenv('_INSTRUMENT_COUNTERS')

    f = counters_fname.open('rb')
    counters_data = f.read()
    f.close()

    import struct
    counters = struct.unpack("LLL", counters_data)

    assert counters == (0,3,2)
Esempio n. 3
0
    def wrap_stackless_function(self, fn):
        def entry_point(argv):
            os.write(1, str(fn())+"\n")
            return 0

        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True)
        config.translation.gc = self.gcpolicy
        config.translation.stackless = True
        if self.stacklessgc:
            config.translation.gcrootfinder = "stackless"
        t = TranslationContext(config=config)
        self.t = t
        t.buildannotator().build_types(entry_point, [s_list_of_strings])
        t.buildrtyper().specialize()
        if self.backendopt:
            backend_optimizations(t)

        from pypy.translator.transform import insert_ll_stackcheck
        insert_ll_stackcheck(t)

        cbuilder = CStandaloneBuilder(t, entry_point, config=config)
        cbuilder.stackless = True
        cbuilder.generate_source()
        cbuilder.compile()
        res = cbuilder.cmdexec('')
        return int(res.strip())
Esempio n. 4
0
    def wrap_stackless_function(self, fn):
        def entry_point(argv):
            os.write(1, str(fn()) + "\n")
            return 0

        from pypy.config.pypyoption import get_pypy_config
        config = get_pypy_config(translating=True)
        config.translation.gc = self.gcpolicy
        config.translation.stackless = True
        if self.stacklessgc:
            config.translation.gcrootfinder = "stackless"
        t = TranslationContext(config=config)
        self.t = t
        t.buildannotator().build_types(entry_point, [s_list_of_strings])
        t.buildrtyper().specialize()
        if self.backendopt:
            backend_optimizations(t)

        from pypy.translator.transform import insert_ll_stackcheck
        insert_ll_stackcheck(t)

        cbuilder = CStandaloneBuilder(t, entry_point, config=config)
        cbuilder.stackless = True
        cbuilder.generate_source()
        cbuilder.compile()
        res = cbuilder.cmdexec('')
        return int(res.strip())
Esempio n. 5
0
    def test_standalone_large_files(self):
        from pypy.module.posix.test.test_posix2 import need_sparse_files
        need_sparse_files()
        filename = str(udir.join('test_standalone_largefile'))
        r4800000000 = r_longlong(4800000000L)

        def entry_point(argv):
            fd = os.open(filename, os.O_RDWR | os.O_CREAT, 0644)
            os.lseek(fd, r4800000000, 0)
            os.write(fd, "$")
            newpos = os.lseek(fd, 0, 1)
            if newpos == r4800000000 + 1:
                print "OK"
            else:
                print "BAD POS"
            os.close(fd)
            return 0

        t = TranslationContext(self.config)
        t.buildannotator().build_types(entry_point, [s_list_of_strings])
        t.buildrtyper().specialize()
        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
        cbuilder.generate_source()
        cbuilder.compile()
        data = cbuilder.cmdexec('hi there')
        assert data.strip() == "OK"
Esempio n. 6
0
 def _compile_and_run(self, t, entry_point, entry_point_graph, args):
     from pypy.translator.c.genc import CStandaloneBuilder as CBuilder
     # XXX patch exceptions
     cbuilder = CBuilder(t, entry_point, config=t.config)
     cbuilder.generate_source()
     exe_name = cbuilder.compile()
     log('---------- Test starting ----------')
     stdout = cbuilder.cmdexec(" ".join([str(arg) for arg in args]))
     res = int(stdout)
     log('---------- Test done (%d) ----------' % (res, ))
     return res
Esempio n. 7
0
    def _compile_and_run(self, t, entry_point, entry_point_graph, args):
        from pypy.translator.c.genc import CStandaloneBuilder as CBuilder

        # XXX patch exceptions
        cbuilder = CBuilder(t, entry_point, config=t.config)
        cbuilder.generate_source()
        exe_name = cbuilder.compile()
        log("---------- Test starting ----------")
        stdout = cbuilder.cmdexec(" ".join([str(arg) for arg in args]))
        res = int(stdout)
        log("---------- Test done (%d) ----------" % (res,))
        return res
Esempio n. 8
0
def test_build():
    def entry_point(argv):
        res = interp_pyexpat.XML_ErrorString(3)
        os.write(1, rffi.charp2str(res))
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    builder = CStandaloneBuilder(t, entry_point, t.config)
    builder.generate_source()
    builder.compile()
    data = builder.cmdexec()
    assert data == pyexpat.ErrorString(3)
Esempio n. 9
0
def test_build():
    def entry_point(argv):
        res = interp_pyexpat.XML_ErrorString(3)
        os.write(1, rffi.charp2str(res))
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    builder = CStandaloneBuilder(t, entry_point, t.config)
    builder.generate_source()
    builder.compile()
    data = builder.cmdexec()
    assert data == pyexpat.ErrorString(3)
Esempio n. 10
0
def run_stackless_function(fn):
    def entry_point(argv):
        r = fn()
        os.write(1, str(r) + "\n")
        return 0

    t = rtype_stackless_function(entry_point)

    cbuilder = CStandaloneBuilder(t, entry_point, config=t.config, gcpolicy=gc.BoehmGcPolicy)
    cbuilder.generate_source()
    if conftest.option.view:
        t.view()
    cbuilder.compile()
    res = cbuilder.cmdexec("")
    return int(res.strip())
Esempio n. 11
0
def test_frexp():
    import math
    def entry_point(argv):
        m, e = math.frexp(0)
        x, y = math.frexp(0)
        print m, x
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert map(float, data.split()) == [0.0, 0.0]
Esempio n. 12
0
def test_frexp():
    import math
    def entry_point(argv):
        m, e = math.frexp(0)
        x, y = math.frexp(0)
        print m, x
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert map(float, data.split()) == [0.0, 0.0]
Esempio n. 13
0
def run_stackless_function(fn):
    def entry_point(argv):
        r = fn()
        os.write(1, str(r) + '\n')
        return 0

    t = rtype_stackless_function(entry_point)

    cbuilder = CStandaloneBuilder(t,
                                  entry_point,
                                  config=t.config,
                                  gcpolicy=gc.BoehmGcPolicy)
    cbuilder.generate_source()
    if conftest.option.view:
        t.view()
    cbuilder.compile()
    res = cbuilder.cmdexec('')
    return int(res.strip())
Esempio n. 14
0
def test_hello_world():
    def entry_point(argv):
        os.write(1, "hello world\n")
        argv = argv[1:]
        os.write(1, "argument count: " + str(len(argv)) + "\n")
        for s in argv:
            os.write(1, "   '" + str(s) + "'\n")
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert data.startswith('''hello world\nargument count: 2\n   'hi'\n   'there'\n''')
Esempio n. 15
0
def test_hello_world():
    def entry_point(argv):
        os.write(1, "hello world\n")
        argv = argv[1:]
        os.write(1, "argument count: " + str(len(argv)) + "\n")
        for s in argv:
            os.write(1, "   '" + str(s) + "'\n")
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert data.startswith('''hello world\nargument count: 2\n   'hi'\n   'there'\n''')
Esempio n. 16
0
def test_print():
    def entry_point(argv):
        print "hello simpler world"
        argv = argv[1:]
        print "argument count:", len(argv)
        print "arguments:", argv
        print "argument lengths:",
        print [len(s) for s in argv]
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert data.startswith('''hello simpler world\n'''
                           '''argument count: 2\n'''
                           '''arguments: [hi, there]\n'''
                           '''argument lengths: [2, 5]\n''')
Esempio n. 17
0
def test_standalone_large_files():
    from pypy.module.posix.test.test_posix2 import need_sparse_files
    need_sparse_files()
    filename = str(udir.join('test_standalone_largefile'))
    r4800000000 = r_longlong(4800000000L)
    def entry_point(argv):
        fd = os.open(filename, os.O_RDWR | os.O_CREAT, 0644)
        os.lseek(fd, r4800000000, 0)
        os.write(fd, "$")
        newpos = os.lseek(fd, 0, 1)
        if newpos == r4800000000 + 1:
            print "OK"
        else:
            print "BAD POS"
        os.close(fd)
        return 0
    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()
    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert data.strip() == "OK"
Esempio n. 18
0
        print "hello simpler world"
        argv = argv[1:]
        print "argument count:", len(argv)
        print "arguments:", argv
        print "argument lengths:",
        print [len(s) for s in argv]
        return 0

    t = TranslationContext()
    t.buildannotator().build_types(entry_point, [s_list_of_strings])
    t.buildrtyper().specialize()

    cbuilder = CStandaloneBuilder(t, entry_point, t.config)
    cbuilder.generate_source()
    cbuilder.compile()
    data = cbuilder.cmdexec('hi there')
    assert data.startswith('''hello simpler world\n'''
                           '''argument count: 2\n'''
                           '''arguments: [hi, there]\n'''
                           '''argument lengths: [2, 5]\n''')
    # NB. RPython has only str, not repr, so str() on a list of strings
    # gives the strings unquoted in the list

def test_counters():
    if sys.platform == 'win32':
        py.test.skip("instrument counters support is unix only for now")
    from pypy.rpython.lltypesystem import lltype
    from pypy.rpython.lltypesystem.lloperation import llop
    def entry_point(argv):
        llop.instrument_count(lltype.Void, 'test', 2)
        llop.instrument_count(lltype.Void, 'test', 1)
    def test_stack_size(self):
        import time
        from pypy.module.thread import ll_thread

        class State:
            pass
        state = State()

        def recurse(n):
            if n > 0:
                return recurse(n-1)+1
            else:
                state.ll_lock.release()
                time.sleep(0.2)
                state.ll_lock.acquire(True)
                return 0

        def bootstrap():
            # recurse a lot, like 2500 times
            state.ll_lock.acquire(True)
            recurse(2500)
            state.count += 1
            state.ll_lock.release()

        def entry_point(argv):
            os.write(1, "hello world\n")
            error = ll_thread.set_stacksize(int(argv[1]))
            assert error == 0
            # malloc a bit
            s1 = State(); s2 = State(); s3 = State()
            s1.x = 0x11111111; s2.x = 0x22222222; s3.x = 0x33333333
            # start 3 new threads
            state.ll_lock = ll_thread.Lock(ll_thread.allocate_ll_lock())
            state.count = 0
            ident1 = ll_thread.start_new_thread(bootstrap, ())
            ident2 = ll_thread.start_new_thread(bootstrap, ())
            ident3 = ll_thread.start_new_thread(bootstrap, ())
            # wait for the 3 threads to finish
            while True:
                state.ll_lock.acquire(True)
                if state.count == 3:
                    break
                state.ll_lock.release()
                time.sleep(0.1)
            # check that the malloced structures were not overwritten
            assert s1.x == 0x11111111
            assert s2.x == 0x22222222
            assert s3.x == 0x33333333
            os.write(1, "done\n")
            return 0

        t = TranslationContext()
        t.buildannotator().build_types(entry_point, [s_list_of_strings])
        t.buildrtyper().specialize()

        cbuilder = CStandaloneBuilder(t, entry_point, t.config)
        cbuilder.generate_source()
        cbuilder.compile()

        # recursing should crash with only 32 KB of stack,
        # and it should eventually work with more stack
        for test_kb in [32, 128, 512, 1024, 2048, 4096, 8192, 16384]:
            print >> sys.stderr, 'Trying with %d KB of stack...' % (test_kb,),
            try:
                data = cbuilder.cmdexec(str(test_kb * 1024))
            except Exception, e:
                if e.__class__ is not Exception:
                    raise
                print >> sys.stderr, 'segfault'
                # got a segfault! try with the next stack size...
            else:
                # it worked
                print >> sys.stderr, 'ok'
                assert data == 'hello world\ndone\n'
                assert test_kb > 32   # it cannot work with just 32 KB of stack
                break    # finish