Beispiel #1
0
 def compile(self):
     assert self.c_source_filename 
     assert not self._compiled
     compile_c_module([self.c_source_filename] + self.extrafiles,
                      self.c_source_filename.purebasename, self.eci,
                      tmpdir=self.c_source_filename.dirpath())
     self._compiled = True
Beispiel #2
0
 def compile(self):
     assert self.c_source_filename 
     assert not self._compiled
     extra_includes = self.extraincludes
     compile_c_module([self.c_source_filename] + self.extrafiles,
                      self.c_source_filename.purebasename,
                      include_dirs = [autopath.this_dir] + extra_includes,
                      libraries=self.libraries)
     self._compiled = True
Beispiel #3
0
    def test_struct_by_val(self):
        from pypy.translator.tool.cbuild import compile_c_module, \
             ExternalCompilationInfo
        from pypy.tool.udir import udir

        c_file = udir.ensure("test_libffi", dir=1).join("xlib.c")
        c_file.write(
            py.code.Source('''
        #include <stdlib.h>
        #include <stdio.h>

        struct x_y {
            long x;
            long y;
        };

        long sum_x_y(struct x_y s) {
            return s.x + s.y;
        }

        long sum_x_y_p(struct x_y *p) {
            return p->x + p->y;
        }
        
        '''))
        eci = ExternalCompilationInfo(export_symbols=['sum_x_y'])
        lib_name = compile_c_module([c_file], 'x', eci)

        lib = CDLL(lib_name)

        slong = cast_type_to_ffitype(rffi.LONG)
        size = slong.c_size * 2
        alignment = slong.c_alignment
        tp = make_struct_ffitype(size, alignment)

        sum_x_y = lib.getrawpointer('sum_x_y', [tp], slong)

        buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw')
        buffer[0] = 200
        buffer[1] = 220
        buffer[2] = 666
        sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)],
                     rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2)))
        assert buffer[2] == 420

        lltype.free(buffer, flavor='raw')
        del sum_x_y
        lltype.free(tp, flavor='raw')
        del lib

        assert not ALLOCATED
Beispiel #4
0
    def test_struct_by_val(self):
        from pypy.translator.tool.cbuild import compile_c_module, \
             ExternalCompilationInfo
        from pypy.tool.udir import udir

        c_file = udir.ensure("test_libffi", dir=1).join("xlib.c")
        c_file.write(py.code.Source('''
        #include <stdlib.h>
        #include <stdio.h>

        struct x_y {
            long x;
            long y;
        };

        long sum_x_y(struct x_y s) {
            return s.x + s.y;
        }

        long sum_x_y_p(struct x_y *p) {
            return p->x + p->y;
        }
        
        '''))
        lib_name = compile_c_module([c_file], 'x', ExternalCompilationInfo())

        lib = CDLL(lib_name)

        size = ffi_type_slong.c_size*2
        alignment = ffi_type_slong.c_alignment
        tp = make_struct_ffitype(size, alignment)

        sum_x_y = lib.getrawpointer('sum_x_y', [tp], ffi_type_slong)

        buffer = lltype.malloc(rffi.LONGP.TO, 3, flavor='raw')
        buffer[0] = 200
        buffer[1] = 220
        buffer[2] = 666
        sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)],
                     rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2)))
        assert buffer[2] == 420

        lltype.free(buffer, flavor='raw')
        del sum_x_y
        lltype.free(tp, flavor='raw')
        del lib

        assert not ALLOCATED
Beispiel #5
0
    def test_ret_struct_val(self):
        from pypy.translator.tool.cbuild import compile_c_module, \
             ExternalCompilationInfo
        from pypy.tool.udir import udir

        c_file = udir.ensure("test_libffi", dir=1).join("xlib.c")
        c_file.write(
            py.code.Source('''
        #include <stdlib.h>
        #include <stdio.h>

        struct s2h {
            short x;
            short y;
        };

        struct s2h give(short x, short y) {
            struct s2h out;
            out.x = x;
            out.y = y;
            return out;
        }

        struct s2h perturb(struct s2h inp) {
            inp.x *= 2;
            inp.y *= 3;
            return inp;
        }
        
        '''))
        lib_name = compile_c_module([c_file], 'x', ExternalCompilationInfo())

        lib = CDLL(lib_name)

        size = ffi_type_sshort.c_size * 2
        alignment = ffi_type_sshort.c_alignment
        tp = make_struct_ffitype(size, alignment)

        give = lib.getrawpointer('give', [ffi_type_sshort, ffi_type_sshort],
                                 tp)
        inbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw')
        inbuffer[0] = rffi.cast(rffi.SHORT, 40)
        inbuffer[1] = rffi.cast(rffi.SHORT, 72)

        outbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw')

        give.call([
            rffi.cast(rffi.VOIDP, inbuffer),
            rffi.cast(rffi.VOIDP, rffi.ptradd(inbuffer, 1))
        ], rffi.cast(rffi.VOIDP, outbuffer))

        assert outbuffer[0] == 40
        assert outbuffer[1] == 72

        perturb = lib.getrawpointer('perturb', [tp], tp)

        inbuffer[0] = rffi.cast(rffi.SHORT, 7)
        inbuffer[1] = rffi.cast(rffi.SHORT, 11)

        perturb.call([rffi.cast(rffi.VOIDP, inbuffer)],
                     rffi.cast(rffi.VOIDP, outbuffer))

        assert inbuffer[0] == 7
        assert inbuffer[1] == 11

        assert outbuffer[0] == 14
        assert outbuffer[1] == 33

        lltype.free(outbuffer, flavor='raw')
        lltype.free(inbuffer, flavor='raw')
        del give
        del perturb
        lltype.free(tp, flavor='raw')
        del lib

        assert not ALLOCATED
Beispiel #6
0
    def prepare_c_example():
        from pypy.tool.udir import udir
        c_file = udir.ensure("test__rawffi", dir=1).join("xlib.c")
        c_file.write(
            py.code.Source('''
        #include <stdlib.h>
        #include <stdio.h>

        struct x
        {
           int x1;
           short x2;
           char x3;
           struct x* next;
        };

        void nothing()
        {
        }

        char inner_struct_elem(struct x *x1)
        {
           return x1->next->x3;
        }

        struct x* create_double_struct()
        {
           struct x* x1, *x2;

           x1 = (struct x*)malloc(sizeof(struct x));
           x2 = (struct x*)malloc(sizeof(struct x));
           x1->next = x2;
           x2->x2 = 3;
           return x1;
        }

        void free_double_struct(struct x* x1)
        {
            free(x1->next);
            free(x1);
        }
        
        const char *static_str = "xxxxxx";
        const long static_int = 42;
        const double static_double = 42.42;
        
        unsigned short add_shorts(short one, short two)
        {
           return one + two;
        }

        void* get_raw_pointer()
        {
           return (void*)add_shorts;
        }

        char get_char(char* s, unsigned short num)
        {
           return s[num];
        }

        char *char_check(char x, char y)
        {
           if (y == static_str[0])
              return static_str;
           return NULL;
        }

        int get_array_elem(int* stuff, int num)
        {
           return stuff[num];
        }

        struct x* get_array_elem_s(struct x** array, int num)
        {
           return array[num];
        }

        long long some_huge_value()
        {
           return 1LL<<42;
        }

        unsigned long long some_huge_uvalue()
        {
           return 1LL<<42;
        }

        long long pass_ll(long long x)
        {
           return x;
        }

        static int prebuilt_array1[] = {3};

        int* allocate_array()
        {
            return prebuilt_array1;
        }

        long long runcallback(long long(*callback)())
        {
            return callback();
        }

        struct x_y {
            long x;
            long y;
        };

        long sum_x_y(struct x_y s) {
            return s.x + s.y;
        }

        struct s2h {
            short x;
            short y;
        };

        struct s2h give(short x, short y) {
            struct s2h out;
            out.x = x;
            out.y = y;
            return out;
        }

        struct s2h perturb(struct s2h inp) {
            inp.x *= 2;
            inp.y *= 3;
            return inp;
        }
        
        '''))
        symbols = """get_char char_check get_raw_pointer
                     add_shorts
                     inner_struct_elem create_double_struct free_double_struct
                     get_array_elem get_array_elem_s
                     nothing
                     some_huge_value some_huge_uvalue pass_ll
                     runcallback
                     allocate_array
                     static_int static_double
                     sum_x_y
                     give perturb
                  """.split()
        eci = ExternalCompilationInfo(export_symbols=symbols)
        return compile_c_module([c_file], 'x', eci)
Beispiel #7
0
    def test_ret_struct_val(self):
        from pypy.translator.tool.cbuild import compile_c_module, \
             ExternalCompilationInfo
        from pypy.tool.udir import udir

        c_file = udir.ensure("test_libffi", dir=1).join("xlib.c")
        c_file.write(py.code.Source('''
        #include <stdlib.h>
        #include <stdio.h>

        struct s2h {
            short x;
            short y;
        };

        struct s2h give(short x, short y) {
            struct s2h out;
            out.x = x;
            out.y = y;
            return out;
        }

        struct s2h perturb(struct s2h inp) {
            inp.x *= 2;
            inp.y *= 3;
            return inp;
        }
        
        '''))
        lib_name = compile_c_module([c_file], 'x', ExternalCompilationInfo())

        lib = CDLL(lib_name)

        size = ffi_type_sshort.c_size*2
        alignment = ffi_type_sshort.c_alignment
        tp = make_struct_ffitype(size, alignment)

        give  = lib.getrawpointer('give', [ffi_type_sshort, ffi_type_sshort],
                                  tp)
        inbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw')
        inbuffer[0] = rffi.cast(rffi.SHORT, 40)
        inbuffer[1] = rffi.cast(rffi.SHORT, 72)

        outbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw')

        give.call([rffi.cast(rffi.VOIDP, inbuffer),
                   rffi.cast(rffi.VOIDP, rffi.ptradd(inbuffer, 1))],
                   rffi.cast(rffi.VOIDP, outbuffer))

        assert outbuffer[0] == 40
        assert outbuffer[1] == 72

        perturb  = lib.getrawpointer('perturb', [tp], tp)

        inbuffer[0] = rffi.cast(rffi.SHORT, 7)
        inbuffer[1] = rffi.cast(rffi.SHORT, 11)

        perturb.call([rffi.cast(rffi.VOIDP, inbuffer)],
                     rffi.cast(rffi.VOIDP, outbuffer))

        assert inbuffer[0] == 7
        assert inbuffer[1] == 11

        assert outbuffer[0] == 14
        assert outbuffer[1] == 33

        lltype.free(outbuffer, flavor='raw')
        lltype.free(inbuffer, flavor='raw')
        del give
        del perturb
        lltype.free(tp, flavor='raw')
        del lib

        assert not ALLOCATED
Beispiel #8
0
from ctypes import cdll
from ctypes import POINTER, Structure, c_int, byref, pointer, c_void_p
from ctypes import CFUNCTYPE

# __________ compile and load our local test C file __________

# LoadLibrary is deprecated in ctypes, this should be removed at some point
if "load" in dir(cdll):
    cdll_load = cdll.load
else:
    cdll_load = cdll.LoadLibrary

# XXX the built module and intermediate files should go to /tmp/usession-*,
#     see pypy.tool.udir
c_source = thisdir.join("_rctypes_test.c")
compile_c_module([c_source], "_rctypes_test")
includes = (str(c_source),)   # in the sequel, we #include the whole .c file
del c_source                  # into the generated C sources

if sys.platform == "win32":
    _rctypes_test = cdll_load(str(thisdir.join("_rctypes_test.pyd")))
else:
    _rctypes_test = cdll_load(str(thisdir.join("_rctypes_test.so")))

# struct tagpoint
class tagpoint(Structure):
    _fields_ = [("x", c_int),
                ("y", c_int),
                ("_z", c_int)]
    _external_ = True       # hack to avoid redeclaration of the struct in C