def test_replace_graph_with_bootstrap(self): def func(n, x, y, z): yield n yield n # graph = make_generator_entry_graph(func) if option.view: graph.show() block = graph.startblock ops = block.operations assert ops[0].opname == 'simple_call' # e = Entry1() assert ops[1].opname == 'setattr' # e.g_n = n assert ops[1].args[1].value == 'g_n' assert ops[2].opname == 'setattr' # e.g_x = x assert ops[2].args[1].value == 'g_x' assert ops[3].opname == 'setattr' # e.g_y = y assert ops[3].args[1].value == 'g_y' assert ops[4].opname == 'setattr' # e.g_z = z assert ops[4].args[1].value == 'g_z' assert ops[5].opname == 'simple_call' # g = GeneratorIterator(e) assert ops[5].args[1] == ops[0].result assert len(ops) == 6 assert len(block.exits) == 1 assert block.exits[0].target is graph.returnblock
def build_flow(func): """ Create the flow graph (in SSA form) for the function. """ _assert_rpythonic(func) if (isgeneratorfunction(func) and not hasattr(func, '_generator_next_method_of_')): return make_generator_entry_graph(func) code = HostCode._from_code(func.func_code) graph = PyGraph(func, code) ctx = FlowContext(graph, code) ctx.build_flow() fixeggblocks(graph) if code.is_generator: tweak_generator_graph(graph) return graph
def test_tweak_generator_graph(self): def f(n, x, y, z): z *= 10 yield n + 1 z -= 10 # graph = make_generator_entry_graph(f) func1 = graph._tweaked_func if option.view: graph.show() GeneratorIterator = graph._tweaked_func._generator_next_method_of_ assert hasattr(GeneratorIterator, 'next') # graph_next = build_flow(GeneratorIterator.next.im_func) join_blocks(graph_next) if option.view: graph_next.show() # graph1 = build_flow(func1) if option.view: graph1.show()