def buildflowgraph(self, func, mute_dot=False): """Get the flow graph for a function.""" if not isinstance(func, types.FunctionType): raise TypeError("buildflowgraph() expects a function, " "got %r" % (func, )) if func in self._prebuilt_graphs: graph = self._prebuilt_graphs.pop(func) else: if self.config.translation.verbose: log.start(nice_repr_for_func(func)) from pypy.objspace.flow.objspace import FlowObjSpace space = FlowObjSpace(self.flowconfig) if self.annotator: # ZZZ self.annotator.policy._adjust_space_config(space) elif hasattr(self, 'no_annotator_but_do_imports_immediately'): space.do_imports_immediately = ( self.no_annotator_but_do_imports_immediately) graph = space.build_flow(func) if self.config.translation.simplifying: simplify.simplify_graph(graph) if self.config.translation.list_comprehension_operations: simplify.detect_list_comprehension(graph) if self.config.translation.verbose: log.done(func.__name__) elif not mute_dot: log.dot() self.graphs.append(graph) # store the graph in our list return graph
def test_replace_graph_with_bootstrap(self): def func(n, x, y, z): yield n yield n # space = FlowObjSpace() graph = space.build_flow(func, tweak_for_generator=False) assert graph.startblock.operations[0].opname == 'generator_mark' GeneratorIterator = make_generatoriterator_class(graph) replace_graph_with_bootstrap(GeneratorIterator, graph) 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 buildflowgraph(self, func, mute_dot=False): """Get the flow graph for a function.""" if not isinstance(func, types.FunctionType): raise TypeError("buildflowgraph() expects a function, " "got %r" % (func,)) if func in self._prebuilt_graphs: graph = self._prebuilt_graphs.pop(func) else: if self.config.translation.verbose: log.start(nice_repr_for_func(func)) space = FlowObjSpace(self.flowconfig) if self.annotator: # ZZZ self.annotator.policy._adjust_space_config(space) elif hasattr(self, 'no_annotator_but_do_imports_immediately'): space.do_imports_immediately = ( self.no_annotator_but_do_imports_immediately) graph = space.build_flow(func) if self.config.translation.simplifying: simplify.simplify_graph(graph) if self.config.translation.list_comprehension_operations: simplify.detect_list_comprehension(graph) if self.config.translation.verbose: log.done(func.__name__) elif not mute_dot: log.dot() self.graphs.append(graph) # store the graph in our list return graph
def test_tweak_generator_graph(self): def f(n, x, y, z): z *= 10 yield n + 1 z -= 10 # space = FlowObjSpace() graph = space.build_flow(f, tweak_for_generator=False) GeneratorIterator = make_generatoriterator_class(graph) replace_graph_with_bootstrap(GeneratorIterator, graph) func1 = attach_next_method(GeneratorIterator, graph) if option.view: graph.show() # assert func1._generator_next_method_of_ is GeneratorIterator assert hasattr(GeneratorIterator, 'next') # graph_next = space.build_flow(GeneratorIterator.next.im_func) join_blocks(graph_next) if option.view: graph_next.show() # graph1 = space.build_flow(func1, tweak_for_generator=False) tweak_generator_body_graph(GeneratorIterator.Entry, graph1) if option.view: graph1.show()
def test_automatic(self): def f(n, x, y, z): z *= 10 yield n + 1 z -= 10 # space = FlowObjSpace() graph = space.build_flow(f) # tweak_for_generator=True if option.view: graph.show() block = graph.startblock assert len(block.exits) == 1 assert block.exits[0].target is graph.returnblock
def test_tweak_generator_body_graph(self): def f(n, x, y, z=3): z *= 10 yield n + 1 z -= 10 # space = FlowObjSpace() graph = space.build_flow(f, tweak_for_generator=False) class Entry: varnames = ['g_n', 'g_x', 'g_y', 'g_z'] tweak_generator_body_graph(Entry, graph) if option.view: graph.show() # XXX how to test directly that the graph is correct? :-( assert len(graph.startblock.inputargs) == 1 assert graph.signature == Signature(['entry']) assert graph.defaults == ()
def setup_class(cls): cls.space = FlowObjSpace()
result[chr(i)] = '_%02X' % i for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789': result[c] = c result['_'] = '__' return result CHAR_MAP = _makecharmap() del _makecharmap def safename(name): # turn a random string into something that is a valid dot identifier, # avoiding invalid characters and prepending '_' to make sure it is # not a keyword name = ''.join([CHAR_MAP[c] for c in name]) return '_' + name if __name__ == '__main__': def f(x): i = 0 while i < x: i += 1 return i space = Space() graph = space.build_flow(f) make_dot('f', graph)
def setup_class(cls): cls.space = FlowObjSpace() cls.space.do_imports_immediately = False
def setup_class(cls): cls.space = FlowObjSpace() cls.space.config.translation.builtins_can_raise_exceptions = True
result[chr(i)] = "_%02X" % i for c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789": result[c] = c result["_"] = "__" return result CHAR_MAP = _makecharmap() del _makecharmap def safename(name): # turn a random string into something that is a valid dot identifier, # avoiding invalid characters and prepending '_' to make sure it is # not a keyword name = "".join([CHAR_MAP[c] for c in name]) return "_" + name if __name__ == "__main__": def f(x): i = 0 while i < x: i += 1 return i space = Space() graph = space.build_flow(f) make_dot("f", graph)