本文整理汇总了Python中pypy.rpython.llinterp.LLInterpreter.eval_graph方法的典型用法代码示例。如果您正苦于以下问题:Python LLInterpreter.eval_graph方法的具体用法?Python LLInterpreter.eval_graph怎么用?Python LLInterpreter.eval_graph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pypy.rpython.llinterp.LLInterpreter
的用法示例。
在下文中一共展示了LLInterpreter.eval_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_merge_several
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_merge_several():
def merge(n, m):
r = -1
if n == 0:
if m == 0:
r = 0
elif m == 1:
r = 1
else:
r = 2
elif n == 1:
r = 4
else:
r = 6
return r
t = TranslationContext()
a = t.buildannotator()
a.build_types(merge, [int, int])
rtyper = t.buildrtyper()
rtyper.specialize()
graph = tgraphof(t, merge)
remove_same_as(graph)
merge_if_blocks(graph)
assert len(graph.startblock.exits) == 3
assert len(list(graph.iterblocks())) == 3
interp = LLInterpreter(rtyper)
for m in range(3):
res = interp.eval_graph(graph, [0, m])
assert res == m
res = interp.eval_graph(graph, [1, 0])
assert res == 4
res = interp.eval_graph(graph, [2, 0])
assert res == 6
示例2: test_normalize_abstract_method
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_normalize_abstract_method(self):
class Base:
def fn(self):
raise NotImplementedError
class Sub1(Base):
def fn(self):
return 1
class Sub2(Base):
def fn(self):
return -2
def dummyfn(n):
if n == 1:
x = Sub1()
else:
x = Sub2()
return x.fn()
translator = self.rtype(dummyfn, [int], int)
base_graph = graphof(translator, Base.fn.im_func)
sub1_graph = graphof(translator, Sub1.fn.im_func)
sub2_graph = graphof(translator, Sub2.fn.im_func)
assert base_graph.getreturnvar().concretetype == lltype.Signed
assert sub1_graph.getreturnvar().concretetype == lltype.Signed
assert sub2_graph.getreturnvar().concretetype == lltype.Signed
llinterp = LLInterpreter(translator.rtyper)
res = llinterp.eval_graph(graphof(translator, dummyfn), [1])
assert res == 1
res = llinterp.eval_graph(graphof(translator, dummyfn), [2])
assert res == -2
示例3: rtype
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def rtype(self, fn, argtypes, resulttype, checkfunction=None):
t = TranslationContext()
a = t.buildannotator()
a.build_types(prefn, [int])
typer = t.buildrtyper()
typer.specialize()
#t.view()
s_result = a.typeannotation(resulttype)
from pypy.rpython import annlowlevel
# annotate, normalize and rtype fn after the fact
annhelper = annlowlevel.MixLevelHelperAnnotator(typer)
graph = annhelper.getgraph(fn, [a.typeannotation(argtype) for argtype in argtypes],
s_result)
annhelper.finish()
t.checkgraphs()
if checkfunction is not None:
checkfunction(t)
# sanity check prefn
llinterp = LLInterpreter(typer)
res = llinterp.eval_graph(graphof(t, prefn), [1])
assert res == 100
res = llinterp.eval_graph(graphof(t, prefn), [2])
assert res == 201
return t
示例4: test_framework_simple
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_framework_simple():
def g(x):
return x + 1
class A(object):
pass
def entrypoint(argv):
a = A()
a.b = g(1)
return str(a.b)
from pypy.rpython.llinterp import LLInterpreter
from pypy.translator.c.genc import CStandaloneBuilder
from pypy.translator.c import gc
from pypy.annotation.listdef import s_list_of_strings
t = rtype(entrypoint, [s_list_of_strings])
cbuild = CStandaloneBuilder(t, entrypoint, t.config,
gcpolicy=FrameworkGcPolicy2)
db = cbuild.generate_graphs_for_llinterp()
entrypointptr = cbuild.getentrypointptr()
entrygraph = entrypointptr._obj.graph
r_list_of_strings = t.rtyper.getrepr(s_list_of_strings)
ll_argv = r_list_of_strings.convert_const([])
llinterp = LLInterpreter(t.rtyper)
# FIIIIISH
setupgraph = db.gctransformer.frameworkgc_setup_ptr.value._obj.graph
llinterp.eval_graph(setupgraph, [])
res = llinterp.eval_graph(entrygraph, [ll_argv])
assert ''.join(res.chars) == "2"
示例5: test_premature_death
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_premature_death(self):
import os
from pypy.annotation.listdef import s_list_of_strings
inputtypes = [s_list_of_strings]
def debug(msg):
os.write(2, "debug: " + msg + '\n')
def entry_point(argv):
#debug("entry point starting")
for arg in argv:
#debug(" argv -> " + arg)
r = arg.replace('_', '-')
#debug(' replaced -> ' + r)
a = r.lower()
#debug(" lowered -> " + a)
return 0
t = self.translateopt(entry_point, inputtypes, mallocs=True)
entry_point_graph = graphof(t, entry_point)
argv = t.rtyper.getrepr(inputtypes[0]).convert_const(['./pypy-c'])
interp = LLInterpreter(t.rtyper)
interp.eval_graph(entry_point_graph, [argv])
示例6: test_half_exceptiontransformed_graphs
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
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
示例7: get_residual_graph
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def get_residual_graph(self):
exc_data_ptr = self.hrtyper.exceptiondesc.exc_data_ptr
llinterp = LLInterpreter(self.rtyper, exc_data_ptr=exc_data_ptr)
if self.main_is_portal:
residual_graph = llinterp.eval_graph(self.readportalgraph,
self.main_args)._obj.graph
else:
residual_graphs = llinterp.eval_graph(self.readallportalsgraph, [])
assert residual_graphs.ll_length() == 1
residual_graph = residual_graphs.ll_getitem_fast(0)._obj.graph
return residual_graph
示例8: runner
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def runner(self, f, nbargs=0, statistics=False, transformer=False,
**extraconfigopts):
if nbargs == 2:
def entrypoint(args):
x = args[0]
y = args[1]
r = f(x, y)
return r
elif nbargs == 0:
def entrypoint(args):
return f()
else:
raise NotImplementedError("pure laziness")
from pypy.rpython.llinterp import LLInterpreter
from pypy.translator.c.genc import CStandaloneBuilder
ARGS = lltype.FixedSizeArray(lltype.Signed, nbargs)
s_args = annmodel.SomePtr(lltype.Ptr(ARGS))
t = rtype(entrypoint, [s_args], gcname=self.gcname,
stacklessgc=self.stacklessgc,
**extraconfigopts)
cbuild = CStandaloneBuilder(t, entrypoint, config=t.config,
gcpolicy=self.gcpolicy)
db = cbuild.generate_graphs_for_llinterp()
entrypointptr = cbuild.getentrypointptr()
entrygraph = entrypointptr._obj.graph
if conftest.option.view:
t.viewcg()
llinterp = LLInterpreter(t.rtyper)
# FIIIIISH
setupgraph = db.gctransformer.frameworkgc_setup_ptr.value._obj.graph
llinterp.eval_graph(setupgraph, [])
def run(args):
ll_args = lltype.malloc(ARGS, immortal=True)
for i in range(nbargs):
ll_args[i] = args[i]
res = llinterp.eval_graph(entrygraph, [ll_args])
return res
if statistics:
statisticsgraph = db.gctransformer.statistics_ptr.value._obj.graph
ll_gc = db.gctransformer.c_const_gc.value
def statistics(index):
return llinterp.eval_graph(statisticsgraph, [ll_gc, index])
return run, statistics
elif transformer:
return run, db.gctransformer
else:
return run
示例9: test_split_blocks_conditional
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_split_blocks_conditional():
for i in range(3):
def f(x, y):
if x + 12:
return y + 1
else:
return y + 2
graph, t = translate(f, [int, int])
split_block(t.annotator, graph.startblock, i)
checkgraph(graph)
interp = LLInterpreter(t.rtyper)
result = interp.eval_graph(graph, [-12, 2])
assert result == 4
result = interp.eval_graph(graph, [0, 2])
assert result == 3
示例10: check_graph
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def check_graph(graph, args, expected_result, t):
if conftest.option.view:
t.view()
checkgraph(graph)
interp = LLInterpreter(t.rtyper)
res = interp.eval_graph(graph, args)
assert res == expected_result
示例11: test_remove_same_as_nonconst
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_remove_same_as_nonconst():
from pypy.rlib.nonconst import NonConstant
from pypy.rpython.lltypesystem.lloperation import llop
from pypy.rpython.lltypesystem import lltype
def f():
if NonConstant(False):
x = llop.same_as(lltype.Signed, 666)
return 42
t = TranslationContext()
t.buildannotator().build_types(f, [])
t.buildrtyper().specialize()
f_graph = graphof(t, f)
#simple_inline_function(t, nothing, f_graph)
# here, the graph looks like v21=same_as(True); exitswitch: v21
remove_same_as(f_graph)
t.checkgraphs()
# only one path should be left
for block in f_graph.iterblocks():
assert len(block.exits) <= 1
for block in t.annotator.annotated:
assert None not in block.operations
interp = LLInterpreter(t.rtyper)
result = interp.eval_graph(f_graph, [])
assert result == 42
示例12: test_pseudohighlevelcallable
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_pseudohighlevelcallable():
t = TranslationContext()
t.buildannotator()
rtyper = t.buildrtyper()
rtyper.specialize()
a = MixLevelHelperAnnotator(rtyper)
class A:
value = 5
def double(self):
return self.value * 2
def fn1(a):
a2 = A()
a2.value = a.double()
return a2
s_A, r_A = a.s_r_instanceof(A)
fn1ptr = a.delayedfunction(fn1, [s_A], s_A)
pseudo = PseudoHighLevelCallable(fn1ptr, [s_A], s_A)
def fn2(n):
a = A()
a.value = n
a2 = pseudo(a)
return a2.value
graph = a.getgraph(fn2, [annmodel.SomeInteger()], annmodel.SomeInteger())
a.finish()
llinterp = LLInterpreter(rtyper)
res = llinterp.eval_graph(graph, [21])
assert res == 42
示例13: check
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def check(self, fn, signature, args, expected_result,
expected_mallocs=0, expected_calls=0):
t = TranslationContext()
self.translator = t
t.buildannotator().build_types(fn, signature)
t.buildrtyper(type_system=self.type_system).specialize()
graph = graphof(t, fn)
if option.view:
t.view()
self.original_graph_count = len(t.graphs)
# to detect broken intermediate graphs,
# we do the loop ourselves instead of calling remove_simple_mallocs()
maxiter = 100
mallocv = MallocVirtualizer(t.graphs, t.rtyper, verbose=True)
while True:
progress = mallocv.remove_mallocs_once()
#simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks()))
if progress and option.view:
t.view()
t.checkgraphs()
if expected_result is not DONT_CHECK_RESULT:
interp = LLInterpreter(t.rtyper)
if not isinstance(expected_result, CHECK_RAISES):
res = interp.eval_graph(graph, args)
assert res == expected_result
else:
excinfo = py.test.raises(LLException,
interp.eval_graph, graph, args)
assert expected_result.excname in str(excinfo.value)
if not progress:
break
maxiter -= 1
assert maxiter > 0, "infinite loop?"
self.check_malloc_removed(graph, expected_mallocs, expected_calls)
return graph
示例14: check_malloc_removal
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def check_malloc_removal(function, types, args, expected_result, must_remove=True):
t = TranslationContext()
t.buildannotator().build_types(function, types)
t.buildrtyper().specialize()
interp = LLInterpreter(t.rtyper)
graph = graphof(t, function)
res = interp.eval_graph(graph, args)
assert res == expected_result
malloc_to_stack(t)
if must_remove:
for block in graph.iterblocks():
for op in block.operations:
assert op.opname != "malloc"
res = interp.eval_graph(graph, args)
assert res == expected_result
return t
示例15: test_remove_same_as
# 需要导入模块: from pypy.rpython.llinterp import LLInterpreter [as 别名]
# 或者: from pypy.rpython.llinterp.LLInterpreter import eval_graph [as 别名]
def test_remove_same_as():
def nothing(x):
return x
def f():
nothing(False)
if nothing(True):
return 42
else:
return 666
t = TranslationContext()
t.buildannotator().build_types(f, [])
t.buildrtyper().specialize()
# now we make the 'if True' appear
f_graph = graphof(t, f)
simple_inline_function(t, nothing, f_graph)
# here, the graph looks like v21=same_as(True); exitswitch: v21
remove_same_as(f_graph)
t.checkgraphs()
# only one path should be left
for block in f_graph.iterblocks():
assert len(block.exits) <= 1
interp = LLInterpreter(t.rtyper)
result = interp.eval_graph(f_graph, [])
assert result == 42