File: canraise.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (45 lines) | stat: -rw-r--r-- 1,801 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS
from rpython.tool.ansi_print import AnsiLogger
from rpython.translator.backendopt import graphanalyze

log = AnsiLogger("canraise")


class RaiseAnalyzer(graphanalyze.BoolGraphAnalyzer):
    ignore_exact_class = None

    def do_ignore_memory_error(self):
        self.ignore_exact_class = MemoryError

    def analyze_simple_operation(self, op, graphinfo):
        try:
            canraise = LL_OPERATIONS[op.opname].canraise
            return bool(canraise) and canraise != (self.ignore_exact_class,)
        except KeyError:
            log.WARNING("Unknown operation: %s" % op.opname)
            return True

    def analyze_external_call(self, fnobj, seen=None):
        return getattr(fnobj, 'canraise', True)

    analyze_exceptblock = None    # don't call this

    def analyze_exceptblock_in_graph(self, graph, block, seen=None):
        if self.ignore_exact_class is not None:
            from rpython.translator.backendopt.ssa import DataFlowFamilyBuilder
            dff = DataFlowFamilyBuilder(graph)
            variable_families = dff.get_variable_families()
            v_exc_instance = variable_families.find_rep(block.inputargs[1])
            for link1 in graph.iterlinks():
                v = link1.last_exc_value
                if v is not None:
                    if variable_families.find_rep(v) is v_exc_instance:
                        # this is a case of re-raise the exception caught;
                        # it doesn't count.  We'll see the place that really
                        # raises the exception in the first place.
                        return False
        return True

    # backward compatible interface
    def can_raise(self, op, seen=None):
        return self.analyze(op, seen)