本文整理汇总了Python中pypy.rlib.libffi.CDLL类的典型用法代码示例。如果您正苦于以下问题:Python CDLL类的具体用法?Python CDLL怎么用?Python CDLL使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CDLL类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_convert_pointer_args
def test_convert_pointer_args(self):
"""
extern int dummy; // defined in test_void_result
DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args
DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto
"""
from _ffi import CDLL, types
class MyPointerWrapper(object):
def __init__(self, value):
self.value = value
def _as_ffi_pointer_(self, ffitype):
assert ffitype is types.void_p
return self.value
libfoo = CDLL(self.libfoo_name)
get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p)
set_val_to_ptr = libfoo.getfunc('set_val_to_ptr',
[types.void_p, types.sint],
types.void)
assert get_dummy() == 0
ptr = get_dummy_ptr()
assert type(ptr) in (int, long)
ptr2 = MyPointerWrapper(ptr)
set_val_to_ptr(ptr2, 123)
assert get_dummy() == 123
set_val_to_ptr(ptr2, 0)
示例2: test_convert_unicode_to_unichar_p
def test_convert_unicode_to_unichar_p(self):
"""
#include <wchar.h>
DLLEXPORT
long mystrlen_u(wchar_t* s)
{
long len = 0;
while(*s++)
len++;
return len;
}
"""
from _ffi import CDLL, types
import _rawffi
libfoo = CDLL(self.libfoo_name)
mystrlen = libfoo.getfunc('mystrlen_u', [types.unichar_p], types.slong)
#
# first, try automatic conversion from strings and unicode
assert mystrlen('foobar') == 6
assert mystrlen(u'foobar') == 6
assert mystrlen(u'ab\u2070') == 3
# then, try to pass an explicit pointer
UniCharArray = _rawffi.Array('u')
mystr = UniCharArray(7, u'foobar')
assert mystrlen(mystr.buffer) == 6
mystr.free()
mystrlen.free_temp_buffers()
示例3: test_func_fromaddr
def test_func_fromaddr(self):
from _ffi import CDLL, types, FuncPtr
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
pow = FuncPtr.fromaddr(pow_addr, 'pow', [types.double, types.double],
types.double)
assert pow(2, 3) == 8
示例4: before
def before(n, x):
libc = CDLL(libc_name)
c_strchr = libc.getpointer('strchr', [types.pointer, types.sint],
types.pointer)
glob.c_strchr = c_strchr
return (n, None, None, None, None, None,
None, None, None, None, None, None)
示例5: test_libload_None
def test_libload_None(self):
if self.iswin32:
skip("unix specific")
from _ffi import CDLL, types
# this should return *all* loaded libs, dlopen(NULL)
dll = CDLL(None)
# Assume CPython, or PyPy compiled with cpyext
res = dll.getfunc('Py_IsInitialized', [], types.slong)()
assert res == 1
示例6: test_getaddressindll
def test_getaddressindll(self):
import sys
from _ffi import CDLL
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
fff = sys.maxint*2-1
if sys.platform == 'win32':
fff = sys.maxint*2+1
assert pow_addr == self.pow_addr & fff
示例7: test_calling_convention3
def test_calling_convention3(self):
if not self.iswin32:
skip("windows specific")
from _ffi import CDLL, types
wrong_kernel = CDLL('Kernel32.dll')
wrong_sleep = wrong_kernel.getfunc('Sleep', [types.uint], types.void)
try:
wrong_sleep(10)
except ValueError, e:
assert e.message.startswith('Procedure called with')
示例8: test_huge_pointer_args
def test_huge_pointer_args(self):
"""
#include <stdlib.h>
DLLEXPORT long is_null_ptr(void* ptr) { return ptr == NULL; }
"""
import sys
from _ffi import CDLL, types
libfoo = CDLL(self.libfoo_name)
is_null_ptr = libfoo.getfunc('is_null_ptr', [types.void_p], types.ulong)
assert not is_null_ptr(sys.maxint+1)
示例9: test_int_args
def test_int_args(self):
"""
DLLEXPORT int sum_xy(int x, int y)
{
return x+y;
}
"""
from _ffi import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
assert sum_xy(30, 12) == 42
示例10: test_char_args
def test_char_args(self):
"""
DLLEXPORT char my_toupper(char x)
{
return x - ('a'-'A');
}
"""
from _ffi import CDLL, types
libfoo = CDLL(self.libfoo_name)
my_toupper = libfoo.getfunc('my_toupper', [types.char],
types.char)
assert my_toupper('c') == 'C'
示例11: setup_class
def setup_class(cls):
space = gettestobjspace(usemodules=('_ffi', '_rawffi'))
cls.space = space
cls.w_iswin32 = space.wrap(sys.platform == 'win32')
cls.w_libfoo_name = space.wrap(cls.prepare_c_example())
cls.w_libc_name = space.wrap(get_libc_name())
libm_name = get_libm_name(sys.platform)
cls.w_libm_name = space.wrap(libm_name)
libm = CDLL(libm_name)
pow = libm.getpointer('pow', [], types.void)
pow_addr = rffi.cast(rffi.LONG, pow.funcsym)
cls.w_pow_addr = space.wrap(pow_addr)
示例12: libffi_stuff
def libffi_stuff(i, j):
lib = CDLL(libm_name)
func = lib.getpointer('fabs', [types.double], types.double)
res = 0.0
x = float(j)
while i > 0:
jitdriver2.jit_merge_point(i=i, res=res, func=func, x=x)
promote(func)
argchain = ArgChain()
argchain.arg(x)
res = func.call(argchain, rffi.DOUBLE)
i -= 1
return res
示例13: test_unsigned_short_args
def test_unsigned_short_args(self):
"""
DLLEXPORT unsigned short sum_xy_us(unsigned short x, unsigned short y)
{
return x+y;
}
"""
from _ffi import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_us', [types.ushort, types.ushort],
types.ushort)
assert sum_xy(32000, 8000) == 40000
assert sum_xy(60000, 30000) == 90000 % 65536
示例14: test_func_fromaddr2
def test_func_fromaddr2(self):
if not self.iswin32:
skip("windows specific")
from _ffi import CDLL, types, FuncPtr
from _rawffi import FUNCFLAG_STDCALL
libm = CDLL(self.libm_name)
pow_addr = libm.getaddressindll('pow')
wrong_pow = FuncPtr.fromaddr(pow_addr, 'pow',
[types.double, types.double], types.double, FUNCFLAG_STDCALL)
try:
wrong_pow(2, 3) == 8
except ValueError, e:
assert e.message.startswith('Procedure called with')
示例15: test_signed_byte_args
def test_signed_byte_args(self):
"""
DLLEXPORT signed char sum_xy_sb(signed char x, signed char y)
{
return x+y;
}
"""
from _ffi import CDLL, types
libfoo = CDLL(self.libfoo_name)
sum_xy = libfoo.getfunc('sum_xy_sb', [types.sbyte, types.sbyte],
types.sbyte)
assert sum_xy(10, 20) == 30
assert sum_xy(100, 28) == -128