pypy /pypy/tool/error.py

Language Python Lines 215
MD5 Hash e3d96a2f41f9254c9fb89e20cb3dfda0 Estimated Cost $4,620 (why?)
Repository https://bitbucket.org/uiappstore/pypy View Raw File View Project SPDX
  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
""" error handling features, just a way of displaying errors
"""

from pypy.tool.ansi_print import ansi_log, raise_nicer_exception
from pypy.objspace.flow.model import Constant, Variable
import sys

import py
log = py.log.Producer("error")
py.log.setconsumer("error", ansi_log)

SHOW_TRACEBACK = False
SHOW_ANNOTATIONS = True
SHOW_DEFAULT_LINES_OF_CODE = 0

from pypy.interpreter.pytraceback import offset2lineno
import traceback

def source_lines1(graph, block, operindex=None, offset=None, long=False, \
    show_lines_of_code=SHOW_DEFAULT_LINES_OF_CODE):
    if block is not None:
        if block is graph.returnblock:
            return ['<return block>']
    try:
        source = graph.source
    except AttributeError:
        return ['no source!']
    else:
        graph_lines = source.split("\n")
        if offset is not None:
            linestart = offset2lineno(graph.func.func_code, offset)
            linerange = (linestart, linestart)
            here = None
        else:
            if block is None or not block.operations:
                return []
            def toline(operindex):
                return offset2lineno(graph.func.func_code, block.operations[operindex].offset)
            if operindex is None:
                linerange =  (toline(0), toline(-1))
                if not long:
                    return ['?']
                here = None
            else:
                operline = toline(operindex)
                if long:
                    linerange =  (toline(0), toline(-1))
                    here = operline
                else:
                    linerange = (operline, operline)
                    here = None
        lines = ["Happened at file %s line %d" % (graph.filename, here or linerange[0]), ""]
        for n in range(max(0, linerange[0]-show_lines_of_code), \
            min(linerange[1]+1+show_lines_of_code, len(graph_lines)+graph.startline)):
            if n == here:
                prefix = '==> '
            else:
                prefix = '    '
            lines.append(prefix + graph_lines[n-graph.startline])
        lines.append("")
        return lines

def source_lines(graph, *args, **kwds):
    lines = source_lines1(graph, *args, **kwds)
    return ['In %r:' % (graph,)] + lines

class FlowingError(Exception):
    pass

class AnnotatorError(Exception):
    pass

class NoSuchAttrError(Exception):
    pass

def gather_error(annotator, block, graph):
    msg = [""]
    msg.append('-+' * 30)
    from pypy.annotation import model
    msg.append("Blocked block -- operation cannot succeed")
    if model.DEBUG:
        _, _, operindex = annotator.why_not_annotated[block][1].break_at
    else:
        # guess the blocked operation by the fact that its return value is
        # not annotated
        for operindex in range(len(block.operations)):
            if block.operations[operindex].result not in annotator.bindings:
                break
        else:
            operindex = None

    if operindex is not None:
        oper = block.operations[operindex]
        msg.append(" " + str(oper))
    else:
        oper = None
        msg.append(" (inconsistency - the block is fully annotated??)")
    msg += source_lines(graph, block, operindex, long=True)
    if oper is not None:
        if SHOW_ANNOTATIONS:
            msg.append("Known variable annotations:")
            for arg in oper.args + [oper.result]:
                if isinstance(arg, Variable):
                    try:
                        msg.append(" " + str(arg) + " = " + str(annotator.binding(arg)))
                    except KeyError:
                        pass
        if model.DEBUG and SHOW_TRACEBACK:
            msg.extend(traceback.format_exception(*annotator.why_not_annotated[block]))
    return "\n".join(msg)

def format_blocked_annotation_error(annotator, blocked_blocks):
    text = []
    for block, graph in blocked_blocks.items():
        text.append(gather_error(annotator, block, graph))
    return '\n'.join(text)

def format_simple_call(annotator, oper, what, msg):
    msg.append("Simple call of incompatible family:")
    try:
        descs = annotator.bindings[oper.args[0]].descriptions
    except (KeyError, AttributeError), e:
        msg.append("      (%s getting at the binding!)" % (
            e.__class__.__name__,))
        return
    for desc in list(descs):
        func = desc.pyobj
        if func is None:
            r = repr(desc)
        else:
            try:
                if isinstance(func, type):
                    func_name = "%s.__init__" % func.__name__
                    func = func.__init__.im_func
                else:
                    func_name = func.func_name
                r = "function %s <%s, line %s>" % (func_name,
                       func.func_code.co_filename, func.func_code.co_firstlineno)
            except (AttributeError, TypeError):
                r = repr(desc)
        msg.append("  %s returning" % (r,))
        if hasattr(desc, 'getuniquegraph'):
            graph = desc.getuniquegraph()
            r = annotator.binding(graph.returnblock.inputargs[0],
                                  "(no annotation)")
        else:
            r = '?'
        msg.append("      %s" % (r,))
        msg.append("")

def format_someobject_error(annotator, position_key, what, s_value, called_from_graph, binding=""):
    #block = getattr(annotator, 'flowin_block', None) or block
    msg = ["annotation of %r degenerated to SomeObject()" % (what,)]
    if position_key is not None:
        graph, block, operindex = position_key
        if operindex is not None:
            oper = block.operations[operindex]
            if oper.opname == 'simple_call':
                format_simple_call(annotator, oper, what, msg)
            else:
                msg.append(str(oper))
        else:
            msg.append("at the start of the block with input arguments:")
            for v in block.inputargs:
                s_v = annotator.binding(v, "(no annotation)")
                msg.append("%8s: %s" % (v, s_v))
        msg.append('')
        msg += source_lines(graph, block, operindex, long=True)

    if called_from_graph is not None:
        msg.append(".. called from %r" % (called_from_graph,))
    if s_value.origin is not None:
        msg.append(".. SomeObject() origin: %s" % (
            annotator.whereami(s_value.origin),))
    msg.append("Previous annotation:")
    msg.append("  " + str(binding))
    return "\n".join(msg)

def format_global_error(graph, offset, message):
    msg = []
    msg.append('-+' * 30)
    msg.append(message)
    msg += source_lines(graph, None, offset=offset)
    return "\n".join(msg)

def debug(drv, use_pdb=True):
    # XXX unify some code with pypy.translator.goal.translate
    from pypy.translator.tool.pdbplus import PdbPlusShow
    from pypy.translator.driver import log
    t = drv.translator
    class options:
        huge = 100

    tb = None
    import traceback
    errmsg = ["Error:\n"]
    exc, val, tb = sys.exc_info()

    errmsg.extend([" %s" % line for line in traceback.format_exception(exc, val, [])])
    block = getattr(val, '__annotator_block', None)
    if block:
        class FileLike:
            def write(self, s):
                errmsg.append(" %s" % s)
        errmsg.append("Processing block:\n")
        t.about(block, FileLike())
    log.ERROR(''.join(errmsg))

    log.event("start debugger...")

    if use_pdb:
        pdb_plus_show = PdbPlusShow(t)
        pdb_plus_show.start(tb)
Back to Top