From: <zy...@us...> - 2008-07-11 12:16:31
|
Revision: 4888 http://jython.svn.sourceforge.net/jython/?rev=4888&view=rev Author: zyasoft Date: 2008-07-11 05:16:28 -0700 (Fri, 11 Jul 2008) Log Message: ----------- Begin port of PyPy's _rawffi, which with their pure-Python ctypes wrapper, implements ctypes Added Paths: ----------- branches/asm/Lib/_rawffi.py branches/asm/Lib/test/test__rawffi.py Added: branches/asm/Lib/_rawffi.py =================================================================== --- branches/asm/Lib/_rawffi.py (rev 0) +++ branches/asm/Lib/_rawffi.py 2008-07-11 12:16:28 UTC (rev 4888) @@ -0,0 +1,36 @@ +import com.sun.jna as jna + +def get_libc(): + return CDLL("c") + +class Array(object): + def __init__(self): + pass + +class FuncPtr(object): + def __init__(self, fn, arg_types, ret_type): + self.fn = fn + # decode + self.arg_types = arg_types + self.ret_type = ret_type + + def __call__(self, *args): + pass + +class CDLL(object): + def __init__(self, libname): + self.lib = jna.NativeLibrary.getInstance(libname) + 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 + + + Added: branches/asm/Lib/test/test__rawffi.py =================================================================== --- branches/asm/Lib/test/test__rawffi.py (rev 0) +++ branches/asm/Lib/test/test__rawffi.py 2008-07-11 12:16:28 UTC (rev 4888) @@ -0,0 +1,32 @@ +import unittest +from test import test_support + +class RawFFITestCase(unittest.TestCase): + + def setUp(self): + self.libc_name = "c" + + def test_libload(self): + import _rawffi + _rawffi.CDLL(self.libc_name) + + def test_libc_load(self): + import _rawffi + _rawffi.get_libc() + + def test_getattr(self): + import _rawffi + libc = _rawffi.get_libc() + func = libc.ptr('rand', [], 'i') + assert libc.ptr('rand', [], 'i') is func # caching + assert libc.ptr('rand', [], 'l') is not func + assert isinstance(func, _rawffi.FuncPtr) + self.assertRaises(AttributeError, getattr, libc, "xxx") + +def test_main(): + tests = [RawFFITestCase, + ] + test_support.run_unittest(*tests) + +if __name__ == '__main__': + test_main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |