本文整理汇总了Python中pypy.rpython.lltypesystem.lltype.functionptr函数的典型用法代码示例。如果您正苦于以下问题:Python functionptr函数的具体用法?Python functionptr怎么用?Python functionptr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了functionptr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: g
def g(vobj):
vobj = lltype.normalizeptr(vobj)
LLV = lltype.typeOf(vobj).TO
ACCESS = LLV.ACCESS
access = lltype.malloc(ACCESS, immortal=True)
fp = lltype.functionptr(ACCESS.parent.get_inst_v0.TO, 'getv0',
_callable=getv0)
access.parent.get_inst_v0= fp
access.get_inst_v1 = lltype.functionptr(ACCESS.get_inst_v1.TO,
'getv1', _callable=getv1)
vobj.super.vable_access = access.parent
示例2: test_caching_dynamic_deallocator
def test_caching_dynamic_deallocator():
S = lltype.GcStruct("S", ('x', lltype.Signed))
S1 = lltype.GcStruct("S1", ('s', S), ('y', lltype.Signed))
T = lltype.GcStruct("T", ('x', lltype.Signed))
def f_S(s):
s.x = 1
def f_S1(s1):
s1.s.x = 1
s1.y = 2
def f_T(s):
s.x = 1
def type_info_S(p):
return lltype.getRuntimeTypeInfo(S)
def type_info_T(p):
return lltype.getRuntimeTypeInfo(T)
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Ptr(lltype.RuntimeTypeInfo)),
"type_info_S",
_callable=type_info_S)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Void),
"destructor_funcptr",
_callable=f_S)
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Void),
"destructor_funcptr",
_callable=f_S1)
pinf = lltype.attachRuntimeTypeInfo(S1, qp, destrptr=dp)
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)],
lltype.Ptr(lltype.RuntimeTypeInfo)),
"type_info_S",
_callable=type_info_T)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(T)],
lltype.Void),
"destructor_funcptr",
_callable=f_T)
pinf = lltype.attachRuntimeTypeInfo(T, qp, destrptr=dp)
def f():
pass
t = TranslationContext()
t.buildannotator().build_types(f, [])
t.buildrtyper().specialize()
transformer = RefcountingGCTransformer(t)
p_S = transformer.dynamic_deallocation_funcptr_for_type(S)
p_S1 = transformer.dynamic_deallocation_funcptr_for_type(S1)
p_T = transformer.dynamic_deallocation_funcptr_for_type(T)
assert p_S is not p_T
assert p_S is p_S1
示例3: newgraph
def newgraph(gv_FUNCTYPE, name):
FUNCTYPE = from_opaque_object(gv_FUNCTYPE).value
# 'name' is just a way to track things
if not isinstance(name, str):
name = LLSupport.from_rstr(name)
inputargs = []
erasedinputargs = []
for ARG in FUNCTYPE.ARGS:
v = flowmodel.Variable()
v.concretetype = ARG
inputargs.append(v)
v = flowmodel.Variable()
v.concretetype = lltype.erasedType(ARG)
erasedinputargs.append(v)
startblock = flowmodel.Block(inputargs)
# insert an exploding operation here which is removed by
# builder.end() to ensure that builder.end() is actually called.
startblock.operations.append(
flowmodel.SpaceOperation("debug_assert",
[flowmodel.Constant(False, lltype.Bool),
flowmodel.Constant("you didn't call builder.end()?",
lltype.Void)],
varoftype(lltype.Void)))
return_var = flowmodel.Variable()
return_var.concretetype = FUNCTYPE.RESULT
graph = flowmodel.FunctionGraph(name, startblock, return_var)
v1 = flowmodel.Variable()
v1.concretetype = lltype.erasedType(FUNCTYPE.RESULT)
graph.prereturnblock = flowmodel.Block([v1])
casting_link(graph.prereturnblock, [v1], graph.returnblock)
substartblock = flowmodel.Block(erasedinputargs)
casting_link(graph.startblock, inputargs, substartblock)
fptr = lltype.functionptr(FUNCTYPE, name,
graph=graph)
return genconst(fptr)
示例4: test_assemble_cast_consts
def test_assemble_cast_consts():
ssarepr = SSARepr("test")
S = lltype.GcStruct('S')
s = lltype.malloc(S)
F = lltype.FuncType([], lltype.Signed)
f = lltype.functionptr(F, 'f')
ssarepr.insns = [
('int_return', Constant('X', lltype.Char)),
('int_return', Constant(unichr(0x1234), lltype.UniChar)),
('int_return', Constant(f, lltype.Ptr(F))),
('ref_return', Constant(s, lltype.Ptr(S))),
]
assembler = Assembler()
jitcode = assembler.assemble(ssarepr)
assert jitcode.code == ("\x00\x58"
"\x01\xFF"
"\x01\xFE"
"\x02\xFF")
assert assembler.insns == {'int_return/c': 0,
'int_return/i': 1,
'ref_return/r': 2}
f_int = heaptracker.adr2int(llmemory.cast_ptr_to_adr(f))
assert jitcode.constants_i == [0x1234, f_int]
s_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, s)
assert jitcode.constants_r == [s_gcref]
示例5: 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
示例6: test_deallocator_with_destructor
def test_deallocator_with_destructor():
S = lltype.GcStruct("S", ('x', lltype.Signed))
def f(s):
s.x = 1
def type_info_S(p):
return lltype.getRuntimeTypeInfo(S)
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Ptr(lltype.RuntimeTypeInfo)),
"type_info_S",
_callable=type_info_S)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Void),
"destructor_funcptr",
_callable=f)
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp)
graph, t = make_deallocator(S)
示例7: test_get_accessor
def test_get_accessor():
G = lltype.FuncType([rclass.OBJECTPTR], lltype.Void)
witness = []
def getv(vinst):
value = vinst.inst_v
witness.append(value)
return value
def g(vobj):
vobj = lltype.normalizeptr(vobj)
LLV = lltype.typeOf(vobj).TO
ACCESS = LLV.vable_access.TO
access = lltype.malloc(ACCESS, immortal=True)
access.get_inst_v = lltype.functionptr(ACCESS.get_inst_v.TO,
'getv', _callable=getv)
vobj.vable_access = access
gptr = lltype.functionptr(G, 'g', _callable=g)
def f(v):
vinst = V(v)
vobj = cast_instance_to_base_ptr(vinst)
gptr(vobj)
x = vinst.v
return x
res = interpret(f, [42])
assert res == 42
assert witness == [42]
示例8: test_set_accessor
def test_set_accessor():
G = lltype.FuncType([rclass.OBJECTPTR], lltype.Void)
witness = []
def setv(vinst, val):
witness.append(val)
vinst.inst_v = val
def g(vobj):
vobj = lltype.normalizeptr(vobj)
LLV = lltype.typeOf(vobj).TO
ACCESS = LLV.vable_access.TO
access = lltype.malloc(ACCESS, immortal=True)
access.set_inst_v = lltype.functionptr(ACCESS.set_inst_v.TO,
'setv', _callable=setv)
vobj.vable_access = access
gptr = lltype.functionptr(G, 'g', _callable=g)
def f(v):
vinst = V(v)
vobj = cast_instance_to_base_ptr(vinst)
gptr(vobj)
vinst.v = 33
res = interpret(f, [42])
assert witness == [33]
示例9: test_boehm_finalizer_nomix___del___and_pyobj
def test_boehm_finalizer_nomix___del___and_pyobj():
S = lltype.GcStruct("S", ('x', lltype.Signed), ('y', lltype.Ptr(lltype.PyObject)))
def f(s):
s.x = 1
def type_info_S(p):
return lltype.getRuntimeTypeInfo(S)
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Ptr(lltype.RuntimeTypeInfo)),
"type_info_S",
_callable=type_info_S)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Void),
"destructor_funcptr",
_callable=f)
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp)
py.test.raises(Exception, "make_boehm_finalizer(S)")
示例10: test_decode_builtin_call_method
def test_decode_builtin_call_method():
A = lltype.GcArray(lltype.Signed)
def myfoobar(a, i, marker, c):
assert marker == 'mymarker'
return a[i] * ord(c)
myfoobar.oopspec = 'spam.foobar(a, 2, c, i)'
TYPE = lltype.FuncType([lltype.Ptr(A), lltype.Signed,
lltype.Void, lltype.Char],
lltype.Signed)
fnobj = lltype.functionptr(TYPE, 'foobar', _callable=myfoobar)
vi = Variable('i')
vi.concretetype = lltype.Signed
vc = Variable('c')
vc.concretetype = lltype.Char
v_result = Variable('result')
v_result.concretetype = lltype.Signed
myarray = lltype.malloc(A, 10)
myarray[5] = 42
op = SpaceOperation('direct_call', [newconst(fnobj),
newconst(myarray),
vi,
voidconst('mymarker'),
vc],
v_result)
oopspec, opargs = decode_builtin_call(op)
assert oopspec == 'spam.foobar'
assert opargs == [newconst(myarray), newconst(2), vc, vi]
示例11: specialize_call
def specialize_call(self, hop):
FUNCTYPE = lltype.FuncType([r.lowleveltype for r in hop.args_r],
hop.r_result.lowleveltype)
args_v = hop.inputargs(*hop.args_r)
funcptr = lltype.functionptr(FUNCTYPE, func.__name__,
_callable=func, _debugexc=True)
cfunc = hop.inputconst(lltype.Ptr(FUNCTYPE), funcptr)
return hop.genop('direct_call', [cfunc] + args_v, hop.r_result)
示例12: genexternalcall
def genexternalcall(self, fnname, args_v, resulttype=None, **flags):
if isinstance(resulttype, Repr):
resulttype = resulttype.lowleveltype
argtypes = [v.concretetype for v in args_v]
FUNCTYPE = FuncType(argtypes, resulttype or Void)
f = functionptr(FUNCTYPE, fnname, **flags)
cf = inputconst(typeOf(f), f)
return self.genop('direct_call', [cf]+list(args_v), resulttype)
示例13: get_direct_call_op
def get_direct_call_op(argtypes, restype):
FUNC = lltype.FuncType(argtypes, restype)
fnptr = lltype.functionptr(FUNC, "g") # no graph
c_fnptr = const(fnptr)
vars = [varoftype(TYPE) for TYPE in argtypes]
v_result = varoftype(restype)
op = SpaceOperation('direct_call', [c_fnptr] + vars, v_result)
return op
示例14: test_boehm_finalizer___del__
def test_boehm_finalizer___del__():
S = lltype.GcStruct("S", ('x', lltype.Signed))
def f(s):
s.x = 1
def type_info_S(p):
return lltype.getRuntimeTypeInfo(S)
qp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Ptr(lltype.RuntimeTypeInfo)),
"type_info_S",
_callable=type_info_S)
dp = lltype.functionptr(lltype.FuncType([lltype.Ptr(S)],
lltype.Void),
"destructor_funcptr",
_callable=f)
pinf = lltype.attachRuntimeTypeInfo(S, qp, destrptr=dp)
f, t = make_boehm_finalizer(S)
assert f is not None
示例15: test_half_exceptiontransformed_graphs
def test_half_exceptiontransformed_graphs():
from pypy.translator import exceptiontransform
def f1(x):
if x < 0:
raise ValueError
return 754
def g1(x):
try:
return f1(x)
except ValueError:
return 5
def f2(x):
if x < 0:
raise ValueError
return 21
def g2(x):
try:
return f2(x)
except ValueError:
return 6
f3 = lltype.functionptr(lltype.FuncType([lltype.Signed], lltype.Signed), "f3", _callable=f1)
def g3(x):
try:
return f3(x)
except ValueError:
return 7
def f(flag, x):
if flag == 1:
return g1(x)
elif flag == 2:
return g2(x)
else:
return g3(x)
t = TranslationContext()
t.buildannotator().build_types(f, [int, int])
t.buildrtyper().specialize()
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.create_exception_handling(graphof(t, f1))
etrafo.create_exception_handling(graphof(t, g2))
etrafo.create_exception_handling(graphof(t, g3))
graph = graphof(t, f)
interp = LLInterpreter(t.rtyper)
res = interp.eval_graph(graph, [1, -64])
assert res == 5
res = interp.eval_graph(graph, [2, -897])
assert res == 6
res = interp.eval_graph(graph, [3, -9831])
assert res == 7