| 1 | |
| 2 | import ctypes |
| 3 | from rpython.rtyper.tool.mkrffi import * |
| 4 | from rpython.translator import cdir |
| 5 | import py |
| 6 | |
| 7 | class random_structure(ctypes.Structure): |
| 8 | _fields_ = [('one', ctypes.c_int), |
| 9 | ('two', ctypes.POINTER(ctypes.c_int))] |
| 10 | |
| 11 | def test_rffisource(): |
| 12 | res = RffiSource({1:2, 3:4}, "ab") + RffiSource(None, "c") |
| 13 | assert res.structs == {1:2, 3:4} |
| 14 | assert str(res.source) == "ab\nc" |
| 15 | res += RffiSource({5:6}) |
| 16 | assert 5 in res.structs.keys() |
| 17 | |
| 18 | def test_proc_tp_simple(): |
| 19 | rffi_source = RffiSource() |
| 20 | assert rffi_source.proc_tp(ctypes.c_int) == 'rffi.INT_real' |
| 21 | assert rffi_source.proc_tp(ctypes.c_void_p) == 'rffi.VOIDP' |
| 22 | assert rffi_source.compiled() |
| 23 | |
| 24 | def test_proc_tp_complicated(): |
| 25 | rffi_source = RffiSource() |
| 26 | assert rffi_source.proc_tp(ctypes.POINTER(ctypes.c_uint)) == \ |
| 27 | "lltype.Ptr(lltype.Array(rffi.UINT_real, hints={'nolength': True}))" |
| 28 | src = rffi_source.proc_tp(random_structure) |
| 29 | _src = py.code.Source(""" |
| 30 | random_structure = lltype.Struct('random_structure', ('one', rffi.INT_real), ('two', lltype.Ptr(lltype.Array(rffi.INT_real, hints={'nolength': True}))), hints={'external':'C'}) |
| 31 | """) |
| 32 | src = rffi_source.source |
| 33 | assert src.strip() == _src.strip(), str(src) + "\n" + str(_src) |
| 34 | assert rffi_source.compiled() |
| 35 | |
| 36 | def test_proc_tp_array(): |
| 37 | rffi_source = RffiSource() |
| 38 | src = rffi_source.proc_tp(ctypes.c_uint * 12) |
| 39 | _src = "lltype.Ptr(lltype.Array(rffi.UINT_real, hints={'nolength': True}))" |
| 40 | assert src == _src |
| 41 | |
| 42 | def test_proc_cyclic_structure(): |
| 43 | rffi_source = RffiSource() |
| 44 | x_ptr = ctypes.POINTER('x') |
| 45 | class x(ctypes.Structure): |
| 46 | _fields_ = [('x', x_ptr)] |
| 47 | x_ptr._type_ = x |
| 48 | rffi_source.proc_tp(x) |
| 49 | _src = py.code.Source(""" |
| 50 | forward_ref0 = lltype.ForwardReference() |
| 51 | x = lltype.Struct('x', ('x', lltype.Ptr(forward_ref0)), hints={'external':'C'}) |
| 52 | |
| 53 | forward_ref0.become(x) |
| 54 | """) |
| 55 | assert rffi_source.source.strip() == _src.strip() |
| 56 | |
| 57 | class TestMkrffi(object): |
| 58 | def setup_class(cls): |
| 59 | import ctypes |
| 60 | from rpython.tool.udir import udir |
| 61 | from rpython.translator.platform import platform |
| 62 | from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 63 | |
| 64 | c_source = """ |
| 65 | #include "src/precommondefs.h" |
| 66 | |
| 67 | RPY_EXPORTED |
| 68 | void *int_to_void_p(int arg) {} |
| 69 | |
| 70 | RPY_EXPORTED |
| 71 | struct random_structure* int_int_to_struct_p(int one, int two) {} |
| 72 | """ |
| 73 | |
| 74 | c_file = udir.join('rffilib.c') |
| 75 | c_file.write(c_source) |
| 76 | eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 77 | libname = platform.compile([c_file], eci, |
| 78 | standalone=False) |
| 79 | cls.lib = ctypes.CDLL(str(libname)) |
| 80 | |
| 81 | def test_single_func(self): |
| 82 | func = self.lib.int_to_void_p |
| 83 | func.argtypes = [ctypes.c_int] |
| 84 | func.restype = ctypes.c_voidp |
| 85 | |
| 86 | src = RffiSource() |
| 87 | src.proc_func(func) |
| 88 | _src = py.code.Source(""" |
| 89 | int_to_void_p = rffi.llexternal('int_to_void_p', [rffi.INT_real], rffi.VOIDP) |
| 90 | """) |
| 91 | |
| 92 | assert src.source == _src, str(src) + "\n" + str(_src) |
| 93 | assert src.compiled() |
| 94 | |
| 95 | def test_struct_return(self): |
| 96 | func = self.lib.int_int_to_struct_p |
| 97 | func.argtypes = [ctypes.c_int, ctypes.c_int] |
| 98 | func.restype = ctypes.POINTER(random_structure) |
| 99 | rffi_source = RffiSource() |
| 100 | rffi_source.proc_func(func) |
| 101 | assert random_structure in rffi_source.structs |
| 102 | _src = py.code.Source(""" |
| 103 | random_structure = lltype.Struct('random_structure', ('one', rffi.INT_real), ('two', lltype.Ptr(lltype.Array(rffi.INT_real, hints={'nolength': True}))), hints={'external':'C'}) |
| 104 | |
| 105 | int_int_to_struct_p = rffi.llexternal('int_int_to_struct_p', [rffi.INT_real, rffi.INT_real], lltype.Ptr(random_structure)) |
| 106 | """) |
| 107 | src = rffi_source.source |
| 108 | assert src.strip() == _src.strip(), str(src) + "\n" + str(_src) |
| 109 | assert rffi_source.compiled() |
| 110 |