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
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
def test_lookup_graphs_abstract(): from pypy.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
def wrap_stackless_function(self, fn): def entry_point(argv): os.write(1, str(fn())+"\n") return 0 from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = self.gcpolicy config.translation.stackless = True if self.stacklessgc: config.translation.gcrootfinder = "stackless" t = TranslationContext(config=config) self.t = t t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() if self.backendopt: backend_optimizations(t) from pypy.translator.transform import insert_ll_stackcheck insert_ll_stackcheck(t) cbuilder = CStandaloneBuilder(t, entry_point, config=config) cbuilder.stackless = True cbuilder.generate_source() cbuilder.compile() res = cbuilder.cmdexec('') return int(res.strip())
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
def _build_gen(func, annotation, graph=None, backendopt=True): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator() inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator() ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir return GenCli(tmpdir, t, TestEntryPoint(main_graph, True))
def test_annotate_attr(self): def f(): a = numpy.empty((3, 4, 5)) return a.ndim t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeInteger) def f(): a = numpy.empty((3, 4, 5)) return a.shape t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeTuple) def f(): a = numpy.empty((3, 4, 5)) return a.dtype t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert isinstance(s, SomeChar)
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
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"
def test_del_basic(): for gcpolicy in ["ref"]: #, "framework"]: 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, gcpolicy=gcpolicy) res = fn(123) assert res == 124
def getcompiled(self, func): t = TranslationContext() # builds starting-types from func_defs argstypelist = [] if func.func_defaults: for spec in func.func_defaults: if isinstance(spec, tuple): spec = spec[0] # use the first type only for the tests argstypelist.append(spec) t.buildannotator().build_types(func, argstypelist) name = func.func_name blobs = [] for graph in t.graphs: g = GenPyrex(graph) g.by_the_way_the_function_was = graph.func # XXX g.setannotator(t.annotator) blobs.append(g.emitcode()) code = g.globaldeclarations() # any 'g' is fine here... if code: blobs.insert(0, code) pyxcode = '\n\n#_________________\n\n'.join(blobs) mod = make_module_from_pyxstring(name, udir, pyxcode) return getattr(mod, name)
def test_counters(self): from pypy.rpython.lltypesystem import lltype from pypy.rpython.lltypesystem.lloperation import llop def entry_point(argv): llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 1) llop.instrument_count(lltype.Void, 'test', 2) llop.instrument_count(lltype.Void, 'test', 1) return 0 t = TranslationContext(self.config) t.config.translation.instrument = True t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, config=t.config) # xxx cbuilder.generate_source() cbuilder.compile() counters_fname = udir.join("_counters_") os.putenv('_INSTRUMENT_COUNTERS', str(counters_fname)) try: data = cbuilder.cmdexec() finally: os.unsetenv('_INSTRUMENT_COUNTERS') f = counters_fname.open('rb') counters_data = f.read() f.close() import struct counters = struct.unpack("LLL", counters_data) assert counters == (0,3,2)
def check(self, fn, signature, args, expected_result, must_be_removed=True, inline=None): remover = self.MallocRemover() t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, fn) if inline is not None: from pypy.translator.backendopt.inline import auto_inline_graphs auto_inline_graphs(t, t.graphs, inline) if option.view: t.view() # to detect missing keepalives and broken intermediate graphs, # we do the loop ourselves instead of calling remove_simple_mallocs() while True: progress = remover.remove_mallocs_once(graph) simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks())) if progress and option.view: t.view() if expected_result is not Ellipsis: interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, args) assert res == expected_result if not progress: break if must_be_removed: self.check_malloc_removed(graph) return graph
def translate(func, argtypes, backend_optimize=True): t = TranslationContext() t.buildannotator().build_types(func, argtypes) t.buildrtyper().specialize() if backend_optimize: backend_optimizations(t) return graphof(t, func), t
def test_runtime_type_info(): S = GcStruct('s', ('is_actually_s1', Bool), rtti=True) S1 = GcStruct('s1', ('sub', S), rtti=True) def rtti_S(p): if p.is_actually_s1: return getRuntimeTypeInfo(S1) else: return getRuntimeTypeInfo(S) def rtti_S1(p): return getRuntimeTypeInfo(S1) def does_stuff(): p = malloc(S) p.is_actually_s1 = False p1 = malloc(S1) p1.sub.is_actually_s1 = True # and no crash when p and p1 are decref'ed return None t = TranslationContext() t.buildannotator().build_types(does_stuff, []) rtyper = t.buildrtyper() rtyper.attachRuntimeTypeInfoFunc(S, rtti_S) rtyper.attachRuntimeTypeInfoFunc(S1, rtti_S1) rtyper.specialize() #t.view() from pypy.translator.c import genc t.config.translation.countmallocs = True builder = genc.CExtModuleBuilder(t, does_stuff, config=t.config) builder.generate_source() builder.compile() f1 = builder.get_entry_point() f1() mallocs, frees = builder.get_malloc_counters()() assert mallocs == frees
def getcompiled(func, view=conftest.option.view, use_boehm=False): from pypy.translator.translator import TranslationContext from pypy.translator.backendopt.all import backend_optimizations from pypy.translator.c import gc from pypy.translator.c.genc import CExtModuleBuilder global t # allow us to view later t = TranslationContext() t.buildannotator().build_types(func, get_annotation(func)) t.buildrtyper().specialize() t.checkgraphs() gcpolicy = None if use_boehm: gcpolicy = gc.BoehmGcPolicy cbuilder = CExtModuleBuilder(t, func, t.config, gcpolicy=gcpolicy) cbuilder.generate_source() cbuilder.compile() backend_optimizations(t) if view: t.viewcg() return getattr(cbuilder.import_module(), func.__name__)
def _build_gen(func, annotation, graph=None, backendopt=True, exctrans=False, annotatorpolicy=None, nowrap=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator(policy=annotatorpolicy) inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator(policy=annotatorpolicy) ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() return _build_gen_from_graph(main_graph, t, exctrans, nowrap)
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
def translate(self, func, sig): t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper(type_system=self.type_system).specialize() if option.view: t.view() return t, RaiseAnalyzer(t)
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
def rtype(fn, argtypes=[]): t = TranslationContext() t.buildannotator().build_types(fn, argtypes) typer = t.buildrtyper() typer.specialize() #t.view() t.checkgraphs() return t
def get_graph(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if conftest.option.view: t.view() return graph, t
def compile(self, entry_point): t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper(type_system='ootype').specialize() cbuilder = CliStandaloneBuilder(t, entry_point, t.config) cbuilder.compile() return t, cbuilder
def rtype(fn, signature): t = TranslationContext() t.buildannotator().build_types(fn, signature) t.buildrtyper().specialize() graph = graphof(t, fn) if option.view: t.view() return t, graph
def rtype(func, inputtypes, specialize=True): t = TranslationContext() t.buildannotator().build_types(func, inputtypes) if specialize: t.buildrtyper().specialize() if conftest.option.view: t.view() return t
def build_adi(function, types): t = TranslationContext() t.buildannotator().build_types(function, types) t.buildrtyper().specialize() adi = AbstractDataFlowInterpreter(t) graph = graphof(t, function) adi.schedule_function(graph) adi.complete() return t, adi, graph
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
def test_preserve_can_raise(self): def f(x): raise ValueError t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper(type_system=self.type_system).specialize() g = graphof(t, f) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) assert etrafo.raise_analyzer.analyze_direct_call(g)
def translateopt(self, func, sig, **optflags): t = TranslationContext() t.buildannotator().build_types(func, sig) t.buildrtyper(type_system=self.type_system).specialize() if conftest.option.view: t.view() backend_optimizations(t, **optflags) if conftest.option.view: t.view() return t
def gengraph(f, args=[], viewBefore=False, viewAfter=False, mangle=True): t = TranslationContext() t.config.translation.ootype.mangle = mangle t.buildannotator().build_types(f, args) if viewBefore or conftest.option.view: t.view() t.buildrtyper(type_system="ootype").specialize() if viewAfter or conftest.option.view: t.view() return graphof(t, f)
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: if op.opname == "malloc": assert op.args[1].value['flavor'] == 'stack' res = interp.eval_graph(graph, args) assert res == expected_result return t
def test_inserting_zeroing_op(self): from pypy.rpython.lltypesystem import lltype S = lltype.GcStruct("S", ('x', lltype.Signed)) def f(x): s = lltype.malloc(S) s.x = 0 return s.x t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper(type_system=self.type_system).specialize() g = graphof(t, f) etrafo = exceptiontransform.ExceptionTransformer(t) etrafo.create_exception_handling(g) ops = dict.fromkeys([o.opname for b, o in g.iterblockops()]) assert 'zero_gc_pointers_inside' in ops
def rgen(self, ll_function, argtypes, rettype=int): #XXX get rettype from annotation t = TranslationContext() t.buildannotator().build_types(ll_function, argtypes) t.buildrtyper().specialize() graph = graphof(t, ll_function) if conftest.option.view: graph.show() rgenop = self.RGenOp() self.rgenop = rgenop # keep this alive! gv_generated = graph2rgenop.compile_graph(rgenop, graph) ctypestypes = [OperationTests._to_ctypes(t) for t in argtypes] fp = cast(c_void_p(gv_generated.value), CFUNCTYPE(OperationTests._to_ctypes(rettype), *ctypestypes)) return fp
def test_secondary_backendopt(self): # checks an issue with a newly added graph that calls an # already-exception-transformed graph. This can occur e.g. # from a late-seen destructor added by the GC transformer # which ends up calling existing code. def common(n): if n > 5: raise ValueError def main(n): common(n) def later(n): try: common(n) return 0 except ValueError: return 1 t = TranslationContext() t.buildannotator().build_types(main, [int]) t.buildrtyper(type_system='lltype').specialize() exctransformer = t.getexceptiontransformer() exctransformer.create_exception_handling(graphof(t, common)) from pypy.annotation import model as annmodel from pypy.rpython.annlowlevel import MixLevelHelperAnnotator annhelper = MixLevelHelperAnnotator(t.rtyper) later_graph = annhelper.getgraph(later, [annmodel.SomeInteger()], annmodel.SomeInteger()) annhelper.finish() annhelper.backend_optimize() # ^^^ as the inliner can't handle exception-transformed graphs, # this should *not* inline common() into later(). if conftest.option.view: later_graph.show() common_graph = graphof(t, common) found = False for block in later_graph.iterblocks(): for op in block.operations: if (op.opname == 'direct_call' and op.args[0].value._obj.graph is common_graph): found = True assert found, "cannot find the call (buggily inlined?)" from pypy.rpython.llinterp import LLInterpreter llinterp = LLInterpreter(t.rtyper) res = llinterp.eval_graph(later_graph, [10]) assert res == 1
def test_exceptiontransformed_add_ovf(): from pypy.translator import exceptiontransform def f(x, y): try: return ovfcheck(x + y) except OverflowError: return -42 t = TranslationContext() t.buildannotator().build_types(f, [int, int]) t.buildrtyper().specialize() etrafo = exceptiontransform.ExceptionTransformer(t) graph = graphof(t, f) etrafo.create_exception_handling(graph) interp = LLInterpreter(t.rtyper) res = interp.eval_graph(graph, [1, -64]) assert res == -63 res = interp.eval_graph(graph, [1, sys.maxint]) assert res == -42
def test_oswrite(): def f(): import os os.write(1,"o") t = TranslationContext() s = t.buildannotator().build_types(f, []) rtyper = t.buildrtyper(type_system="lltype") rtyper.specialize()
def test_annotate_array_dtype(self): def f(): a1 = numpy.array([1, 2], dtype='d') return a1 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'd'
def test_simple(): def f(x): return x*2 t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() t.config.translation.countmallocs = True builder = genc.CExtModuleBuilder(t, f, config=t.config) builder.generate_source() builder.compile() f1 = builder.get_entry_point() assert f1(5) == 10 assert f1(-123) == -246 assert builder.get_malloc_counters()() == (0, 0) py.test.raises(Exception, f1, "world") # check that it's really typed
def test_annotate_array_add_scalar_coerce(self): def f(): a = numpy.array([1, 2]) return a + 3. t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'd'
def test_frexp(self): import math def entry_point(argv): m, e = math.frexp(0) x, y = math.frexp(0) print m, x return 0 t = TranslationContext(self.config) t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, t.config) cbuilder.generate_source() cbuilder.compile() data = cbuilder.cmdexec('hi there') assert map(float, data.split()) == [0.0, 0.0]
def rtype(func, inputtypes, specialize=True, gcname='ref', stacklessgc=False, backendopt=False, **extraconfigopts): from pypy.translator.translator import TranslationContext t = TranslationContext() # XXX XXX XXX mess t.config.translation.gc = gcname if stacklessgc: t.config.translation.gcrootfinder = "stackless" t.config.set(**extraconfigopts) t.buildannotator().build_types(func, inputtypes) if specialize: t.buildrtyper().specialize() if backendopt: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) if conftest.option.view: t.viewcg() return t
def test_annotate_array_attr(self): def fget(): a1 = numpy.array([1, 2]) return a1.shape t = TranslationContext() a = t.buildannotator() s = a.build_types(fget, []) assert type(s) == SomeTuple
def test_hello_world(): def entry_point(argv): os.write(1, "hello world\n") argv = argv[1:] os.write(1, "argument count: " + str(len(argv)) + "\n") for s in argv: os.write(1, " '" + str(s) + "'\n") return 0 t = TranslationContext() t.buildannotator().build_types(entry_point, [s_list_of_strings]) t.buildrtyper().specialize() cbuilder = CStandaloneBuilder(t, entry_point, t.config) cbuilder.generate_source() cbuilder.compile() data = cbuilder.cmdexec('hi there') assert data.startswith('''hello world\nargument count: 2\n 'hi'\n 'there'\n''')
def _build_gen(func, annotation, graph=None, backendopt=True, exctrans=False, annotatorpolicy=None, nowrap=False): try: func = func.im_func except AttributeError: pass t = TranslationContext() if graph is not None: graph.func = func ann = t.buildannotator(policy=annotatorpolicy) inputcells = [ann.typeannotation(a) for a in annotation] ann.build_graph_types(graph, inputcells) t.graphs.insert(0, graph) else: ann = t.buildannotator(policy=annotatorpolicy) ann.build_types(func, annotation) if getoption('view'): t.view() t.buildrtyper(type_system="ootype").specialize() if backendopt: check_virtual_methods(ootype.ROOT) backend_optimizations(t) main_graph = t.graphs[0] if getoption('view'): t.view() if getoption('wd'): tmpdir = py.path.local('.') else: tmpdir = udir return GenCli(tmpdir, t, TestEntryPoint(main_graph, not nowrap), exctrans=exctrans)
def test_py_capi_exc(): def f(x): if x: l = None else: l = [2] x = x*2 return l[0] t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() builder = genc.CExtModuleBuilder(t, f, config=t.config) builder.generate_source() builder.compile() f1 = builder.get_entry_point(isolated=True) x = py.test.raises(Exception, f1, "world") assert not isinstance(x.value, EOFError) # EOFError === segfault
def test_annotate_empty(self): def f(): a = numpy.empty((3, 4, 5)) return a t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'd' assert s.ndim == 3
def test_remove_unaryops(): # We really want to use remove_unaryops for things like ooupcast and # oodowncast in dynamically typed languages, but it's easier to test # it with operations on ints here. def f(x): i = llop.int_invert(lltype.Signed, x) i = llop.int_add(lltype.Signed, x, 1) return llop.int_neg(lltype.Signed, i) t = TranslationContext() t.buildannotator().build_types(f, [int]) t.buildrtyper().specialize() f_graph = graphof(t, f) remove_unaryops(f_graph, ["int_neg", "int_invert"]) t.checkgraphs() interp = LLInterpreter(t.rtyper) result = interp.eval_graph(f_graph, [-2]) assert result == -1
def test_del_inheritance(self): class State: pass s = State() s.a_dels = 0 s.b_dels = 0 class A(object): def __del__(self): s.a_dels += 1 class B(A): def __del__(self): s.b_dels += 1 class C(A): pass def f(): A() B() C() A() B() C() return s.a_dels * 10 + s.b_dels res = f() assert res == 42 t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, f) TYPEA = graph.startblock.operations[0].args[0].value TYPEB = graph.startblock.operations[1].args[0].value TYPEC = graph.startblock.operations[2].args[0].value _, destra = TYPEA._lookup("o__del__") _, destrb = TYPEB._lookup("o__del__") _, destrc = TYPEC._lookup("o__del__") assert destra == destrc assert destrb is not None assert destra is not None
def test_annotate_array_add_coerce(self): def f(): a1 = numpy.array([1, 2]) a2 = numpy.array([6., 9.]) return a1 + a2 t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'd'
def test_annotate_r_dict(): t = TranslationContext() a = t.buildannotator() a.build_types(test_r_dict, []) #t.view() graph = graphof(t, strange_key_eq) assert a.binding(graph.getargs()[0]).knowntype == str assert a.binding(graph.getargs()[1]).knowntype == str graph = graphof(t, strange_key_hash) assert a.binding(graph.getargs()[0]).knowntype == str
def test__del__(self): class A(object): def __init__(self): self.a = 2 def __del__(self): self.a = 3 def f(): a = A() return a.a t = TranslationContext() t.buildannotator().build_types(f, []) t.buildrtyper(type_system=self.type_system).specialize() graph = graphof(t, f) TYPE = graph.startblock.operations[0].args[0].value _, meth = TYPE._lookup("o__del__") assert meth.finalizer
def rtype(self, fn, argtypes, resulttype): t = TranslationContext() a = t.buildannotator() s = a.build_types(fn, argtypes) assert s == a.typeannotation(resulttype) typer = t.buildrtyper() typer.specialize() #t.view() t.checkgraphs() return t
def test_annotate_array_inplace_mul_coerce(self): def f(): a = numpy.array([1, 2, 3, 4]) a *= 0.5 return a t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'l'
def specialize_view(self, f, args=[], opt=False): t = TranslationContext() a = t.buildannotator() a = a.build_types(f, args) r = t.buildrtyper() r.specialize() if opt: from pypy.translator.backendopt.all import backend_optimizations backend_optimizations(t) t.view()
def test_annotate_array_inplace_add_list(self): def f(): a = numpy.array([1, 2, 3, 4]) a += [4, 3, 2, 1] return a t = TranslationContext() a = t.buildannotator() s = a.build_types(f, []) assert s.typecode == 'l'
def _makefunc_str_int(cls, func): def main(argv): arg0 = argv[1] arg1 = int(argv[2]) try: res = func(arg0, arg1) except MemoryError: print 'Result: MemoryError' else: print 'Result: "%s"' % (res,) return 0 from pypy.config.pypyoption import get_pypy_config config = get_pypy_config(translating=True) config.translation.gc = cls.gcpolicy config.translation.gcrootfinder = "asmgcc" if sys.platform == 'win32': config.translation.cc = 'mingw32' t = TranslationContext(config=config) a = t.buildannotator() a.build_types(main, [s_list_of_strings]) t.buildrtyper().specialize() t.checkgraphs() cbuilder = CStandaloneBuilder(t, main, config=config) c_source_filename = cbuilder.generate_source( defines = cbuilder.DEBUG_DEFINES) cls._patch_makefile(cbuilder.targetdir) if conftest.option.view: t.view() exe_name = cbuilder.compile() def run(arg0, arg1): lines = [] print >> sys.stderr, 'RUN: starting', exe_name if sys.platform == 'win32': redirect = ' 2> NUL' else: redirect = '' g = os.popen('"%s" %s %d%s' % (exe_name, arg0, arg1, redirect), 'r') for line in g: print >> sys.stderr, 'RUN:', line.rstrip() lines.append(line) g.close() if not lines: py.test.fail("no output from subprocess") if not lines[-1].startswith('Result:'): py.test.fail("unexpected output from subprocess") result = lines[-1][len('Result:'):].strip() if result == 'MemoryError': raise MemoryError("subprocess got an RPython MemoryError") if result.startswith('"') and result.endswith('"'): return result[1:-1] else: return int(result) return run
def test_annotate_indexing(self): def f(): a = numpy.empty((4, 3), dtype='i') c = a[:, 0] return c t = TranslationContext() a = t.buildannotator() s_array = a.build_types(f, []) assert type(s_array) == SomeArray assert s_array.ndim == 1
def check_malloc_to_coalloc(function, types, args, expected_result, must_remove=-1): 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 num = malloc_to_coalloc(t) if must_remove == -1: for block in graph.iterblocks(): for op in block.operations: assert op.opname != "malloc" else: assert num == must_remove if conftest.option.view: t.view() res = interp.eval_graph(graph, args) assert res == expected_result return t
def test_missing_gvflavor_bug(): class MyClass: def set_x(self): self.x = create_tuple() def create_tuple(): return MyClass(), 42 def fn(): obj = MyClass() obj.set_x() create_tuple() t = TranslationContext() t.buildannotator().build_types(fn, []) t.buildrtyper(type_system='ootype').specialize() #t.view() t.checkgraphs() graph = graphof(t, fn) assert graph.getreturnvar().concretetype == Void
def test_annotate_array_access_bytype(self): def access_array_bytype(dummy): my_array = numpy.array([1], 'd') return my_array[0] t = TranslationContext() a = t.buildannotator() s = a.build_types(access_array_bytype, [int]) assert s.knowntype == float if conftest.option.view: t.view()