Debian Codegraph @

rpython/flowspace/objspace.py

raw symbols SCIP
1 """Implements the main interface for flow graph creation: build_flow().
2 """
3
4 from inspect import CO_NEWLOCALS, isgeneratorfunction
5
6 from rpython.flowspace.model import checkgraph
7 from rpython.flowspace.bytecode import HostCode
8 from rpython.flowspace.flowcontext import (FlowContext, fixeggblocks)
9 from rpython.flowspace.generator import (tweak_generator_graph,
10 make_generator_entry_graph)
11 from rpython.flowspace.pygraph import PyGraph
12
13
14 def _assert_rpythonic(func):
15 """Raise ValueError if ``func`` is obviously not RPython"""
16 try:
17 func.__code__.co_cellvars
18 except AttributeError:
19 raise ValueError("%r is not RPython: it is likely an unexpected "
20 "built-in function or type" % (func,))
21 if getattr(func, "_not_rpython_", False):
22 raise ValueError("%r is tagged as @not_rpython" % (func,))
23 if func.__doc__ and func.__doc__.lstrip().startswith('NOT_RPYTHON'):
24 raise ValueError("%r is tagged as NOT_RPYTHON" % (func,))
25 if func.__code__.co_cellvars:
26 raise ValueError(
27 """RPython functions cannot create closures
28 Possible causes:
29 Function is inner function
30 Function uses generator expressions
31 Lambda expressions
32 in %r""" % (func,))
33 if not (func.__code__.co_flags & CO_NEWLOCALS):
34 raise ValueError("The code object for a RPython function should have "
35 "the flag CO_NEWLOCALS set.")
36
37
38 def build_flow(func):
39 """
40 Create the flow graph (in SSA form) for the function.
41 """
42 _assert_rpythonic(func)
43 if (isgeneratorfunction(func) and
44 not hasattr(func, '_generator_next_method_of_')):
45 return make_generator_entry_graph(func)
46 code = HostCode._from_code(func.__code__)
47 graph = PyGraph(func, code)
48 ctx = FlowContext(graph, code)
49 ctx.build_flow()
50 fixeggblocks(graph)
51 if code.is_generator:
52 tweak_generator_graph(graph)
53 return graph