File: finalizer.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 (64 lines) | stat: -rw-r--r-- 2,708 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from rpython.translator.backendopt import graphanalyze
from rpython.rtyper.lltypesystem import lltype
from rpython.tool.ansi_print import AnsiLogger

log = AnsiLogger("finalizer")


class FinalizerError(Exception):
    """__del__() is used for lightweight RPython destructors,
    but the FinalizerAnalyzer found that it is not lightweight.

    The set of allowed operations is restrictive for a good reason
    - it's better to be safe. Specifically disallowed operations:

    * anything that escapes self
    * anything that can allocate
    """

class FinalizerAnalyzer(graphanalyze.BoolGraphAnalyzer):
    """ Analyzer that determines whether a finalizer is lightweight enough
    so it can be called without all the complicated logic in the garbage
    collector.
    """
    ok_operations = ['ptr_nonzero', 'ptr_eq', 'ptr_ne', 'free', 'same_as',
                     'direct_ptradd', 'force_cast', 'track_alloc_stop',
                     'raw_free', 'adr_eq', 'adr_ne',
                     'debug_print']

    def analyze_light_finalizer(self, graph):
        if getattr(graph.func, '_must_be_light_finalizer_', False):
            self._must_be_light = graph
            result = self.analyze_direct_call(graph)
            del self._must_be_light
            if result is self.top_result():
                msg = '%s\nIn %r' % (FinalizerError.__doc__, graph)
                raise FinalizerError(msg)
        else:
            result = self.analyze_direct_call(graph)
            #if result is self.top_result():
            #    log.red('old-style non-light finalizer: %r' % (graph,))
        return result

    def analyze_simple_operation(self, op, graphinfo):
        if op.opname in self.ok_operations:
            return self.bottom_result()
        if (op.opname.startswith('int_') or op.opname.startswith('float_')
            or op.opname.startswith('uint_') or op.opname.startswith('cast_')):
            return self.bottom_result()
        if op.opname == 'setfield' or op.opname == 'bare_setfield':
            TP = op.args[2].concretetype
            if not isinstance(TP, lltype.Ptr) or TP.TO._gckind == 'raw':
                # primitive type
                return self.bottom_result()
        if op.opname == 'getfield':
            TP = op.result.concretetype
            if not isinstance(TP, lltype.Ptr) or TP.TO._gckind == 'raw':
                # primitive type
                return self.bottom_result()

        if not hasattr(self, '_must_be_light'):
            return self.top_result()
        msg = '%s\nFound this forbidden operation:\n%r\nin %r\nfrom %r' % (
            FinalizerError.__doc__, op, graphinfo, self._must_be_light)
        raise FinalizerError(msg)