| 1 |
from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS |
| 2 |
from rpython.tool.ansi_print import AnsiLogger |
| 3 |
from rpython.translator.backendopt import graphanalyze |
| 4 |
|
| 5 |
log = AnsiLogger("canraise") |
| 6 |
|
| 7 |
|
| 8 |
class RaiseAnalyzer(graphanalyze.BoolGraphAnalyzer): |
| 9 |
ignore_exact_class = None |
| 10 |
|
| 11 |
def do_ignore_memory_error(self): |
| 12 |
self.ignore_exact_class = MemoryError |
| 13 |
|
| 14 |
def analyze_simple_operation(self, op, graphinfo): |
| 15 |
try: |
| 16 |
canraise = LL_OPERATIONS[op.opname].canraise |
| 17 |
return bool(canraise) and canraise != (self.ignore_exact_class,) |
| 18 |
except KeyError: |
| 19 |
log.WARNING("Unknown operation: %s" % op.opname) |
| 20 |
return True |
| 21 |
|
| 22 |
def analyze_external_call(self, fnobj, seen=None): |
| 23 |
return getattr(fnobj, 'canraise', True) |
| 24 |
|
| 25 |
analyze_exceptblock = None |
| 26 |
|
| 27 |
def analyze_exceptblock_in_graph(self, graph, block, seen=None): |
| 28 |
if self.ignore_exact_class is not None: |
| 29 |
from rpython.translator.backendopt.ssa import DataFlowFamilyBuilder |
| 30 |
dff = DataFlowFamilyBuilder(graph) |
| 31 |
variable_families = dff.get_variable_families() |
| 32 |
v_exc_instance = variable_families.find_rep(block.inputargs[1]) |
| 33 |
for link1 in graph.iterlinks(): |
| 34 |
v = link1.last_exc_value |
| 35 |
if v is not None: |
| 36 |
if variable_families.find_rep(v) is v_exc_instance: |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
return False |
| 41 |
return True |
| 42 |
|
| 43 |
|
| 44 |
def can_raise(self, op, seen=None): |
| 45 |
return self.analyze(op, seen) |