本文整理汇总了Python中rpython.translator.translator.TranslationContext类的典型用法代码示例。如果您正苦于以下问题:Python TranslationContext类的具体用法?Python TranslationContext怎么用?Python TranslationContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TranslationContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_type_erase_var_size
def test_type_erase_var_size(self):
class A(object):
pass
class B(object):
pass
def f():
la = [A()]
lb = [B()]
la.append(None)
lb.append(None)
return la, lb
t = TranslationContext()
s = t.buildannotator().build_types(f, [])
rtyper = t.buildrtyper()
rtyper.specialize()
s_A_list = s.items[0]
s_B_list = s.items[1]
r_A_list = rtyper.getrepr(s_A_list)
assert isinstance(r_A_list, self.rlist.ListRepr)
r_B_list = rtyper.getrepr(s_B_list)
assert isinstance(r_B_list, self.rlist.ListRepr)
assert r_A_list.lowleveltype == r_B_list.lowleveltype
示例2: compile
def compile(f, gc, **kwds):
from rpython.annotator.listdef import s_list_of_strings
from rpython.translator.translator import TranslationContext
from rpython.jit.metainterp.warmspot import apply_jit
from rpython.translator.c import genc
#
config = get_combined_translation_config(translating=True)
config.translation.gc = gc
if gc != 'boehm':
config.translation.gcremovetypeptr = True
for name, value in kwds.items():
setattr(config.translation, name, value)
t = TranslationContext(config)
ann = t.buildannotator()
ann.build_types(f, [s_list_of_strings], main_entry_point=True)
t.buildrtyper().specialize()
if kwds['jit']:
patch = get_functions_to_patch()
old_value = {}
try:
for (obj, attr), value in patch.items():
old_value[obj, attr] = getattr(obj, attr)
setattr(obj, attr, value)
#
apply_jit(t)
#
finally:
for (obj, attr), oldvalue in old_value.items():
setattr(obj, attr, oldvalue)
cbuilder = genc.CStandaloneBuilder(t, f, t.config)
cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
cbuilder.compile()
return cbuilder
示例3: translate
def translate(func, argtypes, backendopt=False):
t = TranslationContext()
t.buildannotator().build_types(func, argtypes)
t.buildrtyper(type_system='ootype').specialize()
if backendopt: backend_optimizations(t, merge_if_blocks=True)
return t
示例4: test_pseudohighlevelcallable
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
示例5: test_dont_remove_with__del__
def test_dont_remove_with__del__(self):
import os
delcalls = [0]
class A(object):
nextid = 0
def __init__(self):
self.id = self.nextid
self.nextid += 1
def __del__(self):
delcalls[0] += 1
#os.write(1, "__del__\n")
def f(x=int):
a = A()
i = 0
while i < x:
a = A()
os.write(1, str(delcalls[0]) + "\n")
i += 1
return 1
t = TranslationContext()
t.buildannotator().build_types(f, [int])
t.buildrtyper().specialize()
graph = graphof(t, f)
backend_optimizations(t)
op = graph.startblock.exits[0].target.exits[1].target.operations[0]
assert op.opname == "malloc"
示例6: test_find_loop_blocks3
def test_find_loop_blocks3():
import os
def ps(loops):
return 42.0, 42.1
def f(loops):
benchtime, stones = ps(abs(loops))
s = "" # annotator happiness
if loops >= 0:
s = (
"RPystone(%s) time for %d passes = %f" % (23, loops, benchtime)
+ "\n"
+ ("This machine benchmarks at %f pystones/second" % stones)
)
os.write(1, s)
if loops == 12345:
f(loops - 1)
t = TranslationContext()
t.buildannotator().build_types(f, [int])
t.buildrtyper().specialize()
graph = graphof(t, f)
backedges = find_backedges(graph)
assert backedges == []
loop_blocks = find_loop_blocks(graph)
assert len(loop_blocks) == 0
示例7: test_del_basic
def test_del_basic():
py.test.skip("xxx fix or kill")
S = lltype.GcStruct('S', ('x', lltype.Signed), rtti=True)
TRASH = lltype.GcStruct('TRASH', ('x', lltype.Signed))
GLOBAL = lltype.Struct('GLOBAL', ('x', lltype.Signed))
glob = lltype.malloc(GLOBAL, immortal=True)
def destructor(s):
glob.x = s.x + 1
def type_info_S(s):
return lltype.getRuntimeTypeInfo(S)
def g(n):
s = lltype.malloc(S)
s.x = n
# now 's' should go away
def entrypoint(n):
g(n)
# llop.gc__collect(lltype.Void)
return glob.x
t = TranslationContext()
t.buildannotator().build_types(entrypoint, [int])
rtyper = t.buildrtyper()
destrptr = rtyper.annotate_helper_fn(destructor, [lltype.Ptr(S)])
rtyper.attachRuntimeTypeInfoFunc(S, type_info_S, destrptr=destrptr)
rtyper.specialize()
fn = compile_func(entrypoint, None, t)
res = fn(123)
assert res == 124
示例8: makegraph
def makegraph(func, argtypes):
t = TranslationContext()
t.buildannotator().build_types(func, [int])
t.buildrtyper().specialize()
bk = t.annotator.bookkeeper
graph = bk.getdesc(func).getuniquegraph()
return t, graph
示例9: test_lookup_graphs_abstract
def test_lookup_graphs_abstract():
from rpython.translator.translator import TranslationContext, graphof
class A:
pass
class B(A):
def foo(self):
pass
class C(A):
def foo(self):
pass
def fn(flag):
obj = flag and B() or C()
obj.foo()
return obj
t = TranslationContext()
t.buildannotator().build_types(fn, [int])
t.buildrtyper(type_system='ootype').specialize()
graph = graphof(t, fn)
TYPE_A = graph.getreturnvar().concretetype
TYPE_B = TYPE_A._subclasses[0]
TYPE_C = TYPE_A._subclasses[1]
assert len(TYPE_A._lookup_graphs('ofoo')) == 2
assert len(TYPE_B._lookup_graphs('ofoo')) == 1
assert len(TYPE_C._lookup_graphs('ofoo')) == 1
示例10: __init__
def __init__(self, translator=None, policy=None, bookkeeper=None):
import rpython.rtyper.extfuncregistry # has side effects
if translator is None:
# interface for tests
from rpython.translator.translator import TranslationContext
translator = TranslationContext()
translator.annotator = self
self.translator = translator
self.pendingblocks = {} # map {block: graph-containing-it}
self.annotated = {} # set of blocks already seen
self.added_blocks = None # see processblock() below
self.links_followed = {} # set of links that have ever been followed
self.notify = {} # {block: {positions-to-reflow-from-when-done}}
self.fixed_graphs = {} # set of graphs not to annotate again
self.blocked_blocks = {} # set of {blocked_block: (graph, index)}
# --- the following information is recorded for debugging ---
self.blocked_graphs = {} # set of graphs that have blocked blocks
# --- end of debugging information ---
self.frozen = False
if policy is None:
from rpython.annotator.policy import AnnotatorPolicy
self.policy = AnnotatorPolicy()
else:
self.policy = policy
if bookkeeper is None:
bookkeeper = Bookkeeper(self)
self.bookkeeper = bookkeeper
示例11: rtype
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 rpython.rtyper 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
示例12: test_funny_links
def test_funny_links():
from rpython.flowspace.model import Block, FunctionGraph, \
Variable, Constant, Link
from rpython.flowspace.operation import op
for i in range(2):
v_i = Variable("i")
block = Block([v_i])
g = FunctionGraph("is_one", block)
op1 = op.eq(v_i, Constant(1))
block.operations.append(op1)
block.exitswitch = op1.result
tlink = Link([Constant(1)], g.returnblock, True)
flink = Link([Constant(0)], g.returnblock, False)
links = [tlink, flink]
if i:
links.reverse()
block.closeblock(*links)
t = TranslationContext()
a = t.buildannotator()
a.build_graph_types(g, [annmodel.SomeInteger()])
rtyper = t.buildrtyper()
rtyper.specialize()
interp = LLInterpreter(rtyper)
assert interp.eval_graph(g, [1]) == 1
assert interp.eval_graph(g, [0]) == 0
示例13: test_merge_several
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
示例14: test_replace_exitswitch_by_constant_bug
def test_replace_exitswitch_by_constant_bug():
class X:
pass
def constant9():
x = X()
x.n = 3
x.n = 9
return x.n
def fn():
n = constant9()
if n == 1: return 5
elif n == 2: return 6
elif n == 3: return 8
elif n == 4: return -123
elif n == 5: return 12973
else: return n
t = TranslationContext()
a = t.buildannotator()
a.build_types(fn, [])
rtyper = t.buildrtyper()
rtyper.specialize()
graph = t.graphs[0]
remove_same_as(graph)
merge_if_blocks_once(graph)
from rpython.translator.backendopt import malloc, inline
inline.auto_inlining(t, 20)
malloc.remove_mallocs(t, t.graphs)
from rpython.translator import simplify
simplify.join_blocks(graph)
示例15: test_type_erase
def test_type_erase(self):
class A(object):
pass
class B(object):
pass
def f():
d = {}
d[A()] = B()
d2 = {}
d2[B()] = A()
return d, d2
t = TranslationContext()
s = t.buildannotator().build_types(f, [])
rtyper = t.buildrtyper()
rtyper.specialize()
s_AB_dic = s.items[0]
s_BA_dic = s.items[1]
r_AB_dic = rtyper.getrepr(s_AB_dic)
r_BA_dic = rtyper.getrepr(s_AB_dic)
assert r_AB_dic.lowleveltype == r_BA_dic.lowleveltype