From: <zy...@us...> - 2008-07-11 14:05:14
|
Revision: 4890 http://jython.svn.sourceforge.net/jython/?rev=4890&view=rev Author: zyasoft Date: 2008-07-11 07:05:13 -0700 (Fri, 11 Jul 2008) Log Message: ----------- >From PyPy's test__rawffi, broke out standard ctypes_test code they use (to avoid using their compilation toolchain). Implemented minimum to get test__rawffi.test_short_addition to pass. Modified Paths: -------------- branches/asm/Lib/_rawffi.py branches/asm/Lib/test/test__rawffi.py Added Paths: ----------- branches/asm/tests/c/ branches/asm/tests/c/ctypes_test.c Modified: branches/asm/Lib/_rawffi.py =================================================================== --- branches/asm/Lib/_rawffi.py 2008-07-11 12:45:41 UTC (rev 4889) +++ branches/asm/Lib/_rawffi.py 2008-07-11 14:05:13 UTC (rev 4890) @@ -3,19 +3,40 @@ def get_libc(): return CDLL("c") +typecode_map = {'h': 2, 'H': 2} + class Array(object): - def __init__(self): - pass + def __init__(self, typecode): + self.typecode = typecode + self.itemsize = typecode_map[typecode] + def __call__(self, size, autofree=False): + if not autofree: + raise Exception + return ArrayInstance(self, size) + +class ArrayInstance(object): + def __init__(self, shape, size): + self.shape = shape + self.alloc = jna.Memory(shape.itemsize * size) + + def __setitem__(self, index, value): + self.alloc.setShort(index, value) + + def __getitem__(self, index): + return self.alloc.getShort(index) + class FuncPtr(object): - def __init__(self, fn, arg_types, ret_type): + def __init__(self, fn, name, argtypes, restype): self.fn = fn - # decode - self.arg_types = arg_types - self.ret_type = ret_type + self.name = name + self.argtypes = argtypes + self.restype = restype def __call__(self, *args): - pass + container = Array('H')(1, autofree=True) + container[0] = self.fn.invokeInt([i[0] for i in args]) + return container class CDLL(object): def __init__(self, libname): @@ -23,14 +44,14 @@ self.cache = dict() def ptr(self, name, argtypes, restype): - fn = self.lib.getFunction(name) key = (name, tuple(argtypes), restype) try: return self.cache[key] except KeyError: - fn = FuncPtr(name, argtypes, restype) - self.cache[key] = fn - return fn + fn = self.lib.getFunction(name) + fnp = FuncPtr(fn, name, argtypes, restype) + self.cache[key] = fnp + return fnp Modified: branches/asm/Lib/test/test__rawffi.py =================================================================== --- branches/asm/Lib/test/test__rawffi.py 2008-07-11 12:45:41 UTC (rev 4889) +++ branches/asm/Lib/test/test__rawffi.py 2008-07-11 14:05:13 UTC (rev 4890) @@ -5,6 +5,7 @@ def setUp(self): self.libc_name = "c" + self.lib_name = "ctypes_test" def test_libload(self): import _rawffi @@ -23,6 +24,21 @@ assert isinstance(func, _rawffi.FuncPtr) self.assertRaises(AttributeError, getattr, libc, "xxx") + def test_short_addition(self): + import _rawffi + lib = _rawffi.CDLL(self.lib_name) + short_add = lib.ptr('add_shorts', ['h', 'h'], 'H') + A = _rawffi.Array('h') + arg1 = A(1, autofree=True) + arg2 = A(1, autofree=True) + arg1[0] = 1 + arg2[0] = 2 + res = short_add(arg1, arg2) + assert res[0] == 3 + # this does not apply to this version of memory allocation + #arg1.free() + #arg2.free() + def test_main(): tests = [RawFFITestCase, ] Added: branches/asm/tests/c/ctypes_test.c =================================================================== --- branches/asm/tests/c/ctypes_test.c (rev 0) +++ branches/asm/tests/c/ctypes_test.c 2008-07-11 14:05:13 UTC (rev 4890) @@ -0,0 +1,126 @@ + #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; + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |