[pypy-svn] r48696 - in pypy/dist/pypy/lib: app_test ctypes
fijal at codespeak.net
fijal at codespeak.net
Thu Nov 15 00:03:52 CET 2007
Author: fijal
Date: Thu Nov 15 00:03:51 2007
New Revision: 48696
Added:
pypy/dist/pypy/lib/app_test/test_ctypes.py (contents, props changed)
pypy/dist/pypy/lib/ctypes/
pypy/dist/pypy/lib/ctypes/__init__.py (contents, props changed)
Log:
Add some play around app-level ctypes
Added: pypy/dist/pypy/lib/app_test/test_ctypes.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/app_test/test_ctypes.py Thu Nov 15 00:03:51 2007
@@ -0,0 +1,24 @@
+
+from pypy.conftest import gettestobjspace
+import sys, py
+
+def setup_module(mod):
+ if sys.platform != 'linux2':
+ py.test.skip("Linux only tests by now")
+
+class AppTestCtypes:
+ def setup_class(cls):
+ space = gettestobjspace(usemodules=('_ffi','struct'))
+
+ def test_rand(self):
+ import ctypes
+ libc = ctypes.CDLL('libc.so.6')
+ rand = libc.rand
+ first = rand()
+ counter = 0
+ for i in range(100):
+ next = rand()
+ if next == first:
+ counter += 1
+ assert counter < 100
+
Added: pypy/dist/pypy/lib/ctypes/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/ctypes/__init__.py Thu Nov 15 00:03:51 2007
@@ -0,0 +1,68 @@
+
+import _ffi
+import sys
+
+DEFAULT_MODE = None # XXX, mode support in _ffi
+
+class _CFuncPtr(object):
+ def __init__(self, (name, lib)):
+ self.name = name
+ self.lib = lib
+
+ def __call__(self, *args):
+ if not hasattr(self, '_handle'):
+ self._update_handle()
+ # XXX eventually cast types here
+ return self._handle(*args)
+
+ def _update_handle(self):
+ llargs = [i._lltype for i in self.argtypes]
+ # XXX first approximation
+ self._handle = self.lib._handle.ptr(self.name, llargs,
+ self.restype._lltype)
+
+class c_int(object):
+ _lltype = 'i'
+
+class CDLL(object):
+ """An instance of this class represents a loaded dll/shared
+ library, exporting functions using the standard C calling
+ convention (named 'cdecl' on Windows).
+
+ The exported functions can be accessed as attributes, or by
+ indexing with the function name. Examples:
+
+ <obj>.qsort -> callable object
+ <obj>['qsort'] -> callable object
+
+ Calling the functions releases the Python GIL during the call and
+ reaquires it afterwards.
+ """
+ class _FuncPtr(_CFuncPtr):
+ #_flags_ = _FUNCFLAG_CDECL
+ restype = c_int # default, can be overridden in instances
+ argtypes = []
+
+ def __init__(self, name, mode=DEFAULT_MODE, handle=None):
+ self._name = name
+ if handle is None:
+ self._handle = _ffi.CDLL(self._name)
+ else:
+ self._handle = handle
+
+ def __repr__(self):
+ return "<%s '%s', handle>" % \
+ (self.__class__.__name__, self._name)
+
+ def __getattr__(self, name):
+ if name.startswith('__') and name.endswith('__'):
+ raise AttributeError, name
+ func = self.__getitem__(name)
+ setattr(self, name, func)
+ return func
+
+ def __getitem__(self, name_or_ordinal):
+ func = self._FuncPtr((name_or_ordinal, self))
+ if not isinstance(name_or_ordinal, (int, long)):
+ func.__name__ = name_or_ordinal
+ return func
More information about the Pypy-commit
mailing list