def test_dict_to_switch_translate(): from rpython.flowspace.model import summary d = dict_to_switch({1: 17, 2: 19}) r = interpret(d, [1]) assert r == 17 r = interpret(d, [2]) assert r == 19 with pytest.raises(LLException): interpret(d, [10]) d = dict_to_switch({'1': 17, '2': 19}) r = interpret(d, ['1']) assert r == 17 r = interpret(d, ['2']) assert r == 19 with pytest.raises(LLException): interpret(d, ['a']) d = dict_to_switch({'1': 17, '2': 19, '3': 1121, 'a': -12}) def f(i): c = chr(i) if not i: c = 'abc' try: return d(c) except KeyError: return -1 t, typer, g = gengraph(f, [int]) lookupg = t.graphs[1] assert summary(lookupg)['direct_call'] == 1 # should be char based
def test_mark_dict_non_null(): def f(): d = {"ac": "bx"} mark_dict_non_null(d) return d t, typer, graph = gengraph(f, []) assert sorted(graph.returnblock.inputargs[0].concretetype.TO.entries.TO.OF._flds.keys()) == ['key', 'value']
def test_loop_1(): def f(a, b): while a > 0: b += a a -= 1 return b t, rtyper, graph = gengraph(f, [int, int], viewbefore=False) regalloc = perform_register_allocation(graph, is_int) num_renamings = check_valid(graph, regalloc, is_int) assert num_renamings == 0
def test_loop_2(): def f(a, b): while a > 0: b += a if b < 10: a, b = b, a a -= 1 return b t, rtyper, graph = gengraph(f, [int, int], viewbefore=False) regalloc = perform_register_allocation(graph, is_int) check_valid(graph, regalloc, is_int)
def gengraph(func, argtypes=[], viewbefore='auto', policy=None, backendopt=False, config=None): return gengraph(func, argtypes, viewbefore, policy, backendopt=backendopt, config=config)
def test_collect_step(): def f(): return rgc.collect_step() assert f() t, typer, graph = gengraph(f, []) blockops = list(graph.iterblockops()) opnames = [ op.opname for block, op in blockops if op.opname.startswith('gc__') ] assert opnames == ['gc__collect_step'] res = interpret(f, []) assert res
def test_register_custom_trace_hook(): TP = lltype.GcStruct('X') def trace_func(): xxx # should not be annotated here lambda_trace_func = lambda: trace_func def f(): rgc.register_custom_trace_hook(TP, lambda_trace_func) t, typer, graph = gengraph(f, []) assert typer.custom_trace_funcs == [(TP, trace_func)]
def test_collect(): def f(): return gc.collect() t, typer, graph = gengraph(f, []) ops = list(graph.iterblockops()) assert len(ops) == 1 op = ops[0][1] assert op.opname == 'gc__collect' assert len(op.args) == 0 res = interpret(f, []) assert res is None
def test_can_move(): T0 = lltype.GcStruct('T') T1 = lltype.GcArray(lltype.Float) def f(i): if i: return rgc.can_move(lltype.malloc(T0)) else: return rgc.can_move(lltype.malloc(T1, 1)) t, typer, graph = gengraph(f, [int]) ops = list(graph.iterblockops()) res = [op for op in ops if op[1].opname == 'gc_can_move'] assert len(res) == 2 res = interpret(f, [1]) assert res == True
def test_collect_0(): if sys.version_info < (2, 5): py.test.skip("requires Python 2.5 to call gc.collect() with an arg") def f(): return gc.collect(0) t, typer, graph = gengraph(f, []) ops = list(graph.iterblockops()) assert len(ops) == 1 op = ops[0][1] assert op.opname == 'gc__collect' assert len(op.args) == 1 assert op.args[0].value == 0 res = interpret(f, []) assert res is None
def test_enable_disable(): def f(): gc.enable() a = gc.isenabled() gc.disable() b = gc.isenabled() return a and not b t, typer, graph = gengraph(f, []) blockops = list(graph.iterblockops()) opnames = [ op.opname for block, op in blockops if op.opname.startswith('gc__') ] assert opnames == [ 'gc__enable', 'gc__isenabled', 'gc__disable', 'gc__isenabled' ] res = interpret(f, []) assert res
def test_gengraph(): t, typer, graph = gengraph(main, [int])
def gengraph(self, func, argtypes=[], viewbefore='auto', policy=None, backendopt=False, config=None): return gengraph(func, argtypes, viewbefore, policy, type_system=self.type_system, backendopt=backendopt, config=config)
def make_graph(f, argtypes): t, rtyper, graph = gengraph(f, argtypes, viewbefore=False) if getattr(option, 'view', False): graph.show() return graph
def gengraph(func, argtypes=[], viewbefore="auto", policy=None, backendopt=False, config=None): return gengraph(func, argtypes, viewbefore, policy, backendopt=backendopt, config=config)