本文整理汇总了Python中pypy.rpython.lltypesystem.rffi.llexternal函数的典型用法代码示例。如果您正苦于以下问题:Python llexternal函数的具体用法?Python llexternal怎么用?Python llexternal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了llexternal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: external
def external(name, args, result):
unsafe = rffi.llexternal(name, args, result,
compilation_info=CConfig._compilation_info_)
safe = rffi.llexternal(name, args, result,
compilation_info=CConfig._compilation_info_,
sandboxsafe=True, threadsafe=False)
return unsafe, safe
示例2: test_cancollect_external
def test_cancollect_external():
fext1 = rffi.llexternal('fext1', [], lltype.Void, threadsafe=False)
def g():
fext1()
t = rtype(g, [])
gg = graphof(t, g)
assert not CollectAnalyzer(t).analyze_direct_call(gg)
fext2 = rffi.llexternal('fext2', [], lltype.Void, threadsafe=True)
def g():
fext2()
t = rtype(g, [])
gg = graphof(t, g)
assert CollectAnalyzer(t).analyze_direct_call(gg)
S = lltype.GcStruct('S', ('x', lltype.Signed))
FUNC = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Void))
fext3 = rffi.llexternal('fext3', [FUNC], lltype.Void, threadsafe=False)
def h(x):
lltype.malloc(S, zero=True)
def g():
fext3(h)
t = rtype(g, [])
gg = graphof(t, g)
assert CollectAnalyzer(t).analyze_direct_call(gg)
示例3: test_different_signatures
def test_different_signatures(self):
fcntl_int = rffi.llexternal('fcntl', [rffi.INT, rffi.INT, rffi.INT],
rffi.INT)
fcntl_str = rffi.llexternal('fcntl', [rffi.INT, rffi.INT, rffi.CCHARP],
rffi.INT)
fcntl_int(12345, 1, 0)
fcntl_str(12345, 3, "xxx")
fcntl_int(12345, 1, 0)
示例4: test_different_signatures
def test_different_signatures(self):
if sys.platform=='win32':
py.test.skip("No fcntl on win32")
fcntl_int = rffi.llexternal('fcntl', [rffi.INT, rffi.INT, rffi.INT],
rffi.INT)
fcntl_str = rffi.llexternal('fcntl', [rffi.INT, rffi.INT, rffi.CCHARP],
rffi.INT)
fcntl_int(12345, 1, 0)
fcntl_str(12345, 3, "xxx")
fcntl_int(12345, 1, 0)
示例5: test_llexternal_macro
def test_llexternal_macro(self):
eci = ExternalCompilationInfo(
post_include_bits = ["#define fn(x) (42 + x)"],
)
fn1 = rffi.llexternal('fn', [rffi.INT], rffi.INT,
compilation_info=eci, macro=True)
fn2 = rffi.llexternal('fn2', [rffi.DOUBLE], rffi.DOUBLE,
compilation_info=eci, macro='fn')
res = fn1(10)
assert res == 52
res = fn2(10.5)
assert res == 52.5
示例6: test_opaque_obj_2
def test_opaque_obj_2(self):
FILEP = rffi.COpaquePtr('FILE')
fopen = rffi.llexternal('fopen', [rffi.CCHARP, rffi.CCHARP], FILEP)
fclose = rffi.llexternal('fclose', [FILEP], rffi.INT)
tmppath = udir.join('test_ll2ctypes.test_opaque_obj_2')
ll_file = fopen(str(tmppath), "w")
assert ll_file
fclose(ll_file)
assert tmppath.check(file=1)
assert not ALLOCATED # detects memory leaks in the test
assert rffi.cast(FILEP, -1) == rffi.cast(FILEP, -1)
示例7: setup_init_functions
def setup_init_functions(eci):
init_buffer = rffi.llexternal('init_bufferobject', [], lltype.Void, compilation_info=eci)
init_pycobject = rffi.llexternal('init_pycobject', [], lltype.Void, compilation_info=eci)
init_capsule = rffi.llexternal('init_capsule', [], lltype.Void, compilation_info=eci)
INIT_FUNCTIONS.extend([
lambda space: init_buffer(),
lambda space: init_pycobject(),
lambda space: init_capsule(),
])
from pypy.module.posix.interp_posix import add_fork_hook
reinit_tls = rffi.llexternal('PyThread_ReInitTLS', [], lltype.Void,
compilation_info=eci)
add_fork_hook('child', reinit_tls)
示例8: test_func_not_in_clib
def test_func_not_in_clib():
foobar = rffi.llexternal('I_really_dont_exist', [], lltype.Signed)
py.test.raises(NotImplementedError, foobar)
foobar = rffi.llexternal('I_really_dont_exist', [], lltype.Signed,
libraries=['m']) # math library
py.test.raises(NotImplementedError, foobar)
foobar = rffi.llexternal('I_really_dont_exist', [], lltype.Signed,
libraries=['m', 'z']) # math and zlib libraries
py.test.raises(NotImplementedError, foobar)
foobar = rffi.llexternal('I_really_dont_exist', [], lltype.Signed,
libraries=['I_really_dont_exist_either'])
py.test.raises(NotImplementedError, foobar)
示例9: install_dll
def install_dll(self, eci):
"""NOT_RPYTHON
Called when the dll has been compiled"""
if sys.platform == 'win32':
self.get_pythonapi_handle = rffi.llexternal(
'pypy_get_pythonapi_handle', [], DLLHANDLE,
compilation_info=eci)
示例10: define_callback_simple
def define_callback_simple(cls):
import gc
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rpython.annlowlevel import llhelper
from pypy.translator.tool.cbuild import ExternalCompilationInfo
c_source = py.code.Source("""
int mystuff(int(*cb)(int, int))
{
return cb(40, 2) + cb(3, 4);
}
""")
eci = ExternalCompilationInfo(separate_module_sources=[c_source])
S = lltype.GcStruct('S', ('x', lltype.Signed))
CALLBACK = lltype.FuncType([lltype.Signed, lltype.Signed],
lltype.Signed)
z = rffi.llexternal('mystuff', [lltype.Ptr(CALLBACK)], lltype.Signed,
compilation_info=eci)
def mycallback(a, b):
gc.collect()
return a + b
def f():
p = lltype.malloc(S)
p.x = 100
result = z(mycallback)
return result * p.x
return f
示例11: test_llexternal
def test_llexternal(self):
from pypy.rpython.lltypesystem.rffi import llexternal
from pypy.rpython.lltypesystem import lltype
z = llexternal('z', [lltype.Signed], lltype.Signed)
def f(x):
return z(x)
t, ra = self.translate(f, [int])
fgraph = graphof(t, f)
backend_optimizations(t)
assert fgraph.startblock.operations[0].opname == 'direct_call'
result = ra.can_raise(fgraph.startblock.operations[0])
assert not result
z = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed),
'foobar')
def g(x):
return z(x)
t, ra = self.translate(g, [int])
ggraph = graphof(t, g)
assert ggraph.startblock.operations[0].opname == 'direct_call'
result = ra.can_raise(ggraph.startblock.operations[0])
assert result
示例12: test_llexternal_source
def test_llexternal_source(self):
eci = ExternalCompilationInfo(
separate_module_sources = ["int fn() { return 42; }"]
)
fn = rffi.llexternal('fn', [], rffi.INT, compilation_info=eci)
res = fn()
assert res == 42
示例13: configure_boehm_once
def configure_boehm_once(cls):
""" Configure boehm only once, since we don't cache failures
"""
if hasattr(cls, 'malloc_fn_ptr'):
return cls.malloc_fn_ptr
from pypy.rpython.tool import rffi_platform
compilation_info = rffi_platform.configure_boehm()
# Versions 6.x of libgc needs to use GC_local_malloc().
# Versions 7.x of libgc removed this function; GC_malloc() has
# the same behavior if libgc was compiled with
# THREAD_LOCAL_ALLOC.
class CConfig:
_compilation_info_ = compilation_info
HAS_LOCAL_MALLOC = rffi_platform.Has("GC_local_malloc")
config = rffi_platform.configure(CConfig)
if config['HAS_LOCAL_MALLOC']:
GC_MALLOC = "GC_local_malloc"
else:
GC_MALLOC = "GC_malloc"
malloc_fn_ptr = rffi.llexternal(GC_MALLOC,
[lltype.Signed], # size_t, but good enough
llmemory.GCREF,
compilation_info=compilation_info,
sandboxsafe=True,
_nowrapper=True)
cls.malloc_fn_ptr = malloc_fn_ptr
cls.compilation_info = compilation_info
return malloc_fn_ptr
示例14: test_qsort
def test_qsort(self):
CMPFUNC = lltype.FuncType([rffi.VOIDP, rffi.VOIDP], rffi.INT)
qsort = rffi.llexternal('qsort', [rffi.VOIDP,
rffi.SIZE_T,
rffi.SIZE_T,
lltype.Ptr(CMPFUNC)],
lltype.Void)
lst = [23, 43, 24, 324, 242, 34, 78, 5, 3, 10]
A = lltype.Array(lltype.Signed, hints={'nolength': True})
a = lltype.malloc(A, 10, flavor='raw')
for i in range(10):
a[i] = lst[i]
SIGNEDPTR = lltype.Ptr(lltype.FixedSizeArray(lltype.Signed, 1))
def my_compar(p1, p2):
p1 = rffi.cast(SIGNEDPTR, p1)
p2 = rffi.cast(SIGNEDPTR, p2)
print 'my_compar:', p1[0], p2[0]
return rffi.cast(rffi.INT, cmp(p1[0], p2[0]))
qsort(rffi.cast(rffi.VOIDP, a),
rffi.cast(rffi.SIZE_T, 10),
rffi.cast(rffi.SIZE_T, llmemory.sizeof(lltype.Signed)),
llhelper(lltype.Ptr(CMPFUNC), my_compar))
for i in range(10):
print a[i],
print
lst.sort()
for i in range(10):
assert a[i] == lst[i]
lltype.free(a, flavor='raw')
assert not ALLOCATED # detects memory leaks in the test
示例15: test_pass_around_t_object
def test_pass_around_t_object(self):
from pypy.rpython.annlowlevel import base_ptr_lltype
T = base_ptr_lltype()
class X(object):
_TYPE = T
x = 10
def callback(x):
return x.x
c_source = py.code.Source("""
int eating_callback(void *arg, int(*call)(int))
{
return call(arg);
}
""")
eci = ExternalCompilationInfo(separate_module_sources=[c_source],
export_symbols=['eating_callback'])
args = [T, rffi.CCallback([T], rffi.INT)]
eating_callback = rffi.llexternal('eating_callback', args, rffi.INT,
compilation_info=eci)
res = eating_callback(X(), callback)
assert res == 10