| 1 |
|
| 2 |
""" error handling features, just a way of displaying errors |
| 3 |
""" |
| 4 |
|
| 5 |
import sys |
| 6 |
|
| 7 |
import py |
| 8 |
|
| 9 |
from rpython.flowspace.model import Variable |
| 10 |
from rpython.rlib import jit |
| 11 |
|
| 12 |
|
| 13 |
SHOW_TRACEBACK = False |
| 14 |
SHOW_ANNOTATIONS = True |
| 15 |
SHOW_DEFAULT_LINES_OF_CODE = 0 |
| 16 |
|
| 17 |
|
| 18 |
def source_lines1(graph, block, operindex=None, offset=None, long=False, |
| 19 |
show_lines_of_code=SHOW_DEFAULT_LINES_OF_CODE): |
| 20 |
if block is not None: |
| 21 |
if block is graph.returnblock: |
| 22 |
return ['<return block>'] |
| 23 |
try: |
| 24 |
source = graph.source |
| 25 |
except AttributeError: |
| 26 |
return ['no source!'] |
| 27 |
else: |
| 28 |
graph_lines = source.split("\n") |
| 29 |
if offset is not None: |
| 30 |
linestart = offset2lineno(graph.func.__code__, offset) |
| 31 |
linerange = (linestart, linestart) |
| 32 |
here = None |
| 33 |
else: |
| 34 |
if block is None or not block.operations: |
| 35 |
return [] |
| 36 |
|
| 37 |
def toline(operindex): |
| 38 |
return offset2lineno(graph.func.__code__, block.operations[operindex].offset) |
| 39 |
if operindex is None: |
| 40 |
linerange = (toline(0), toline(-1)) |
| 41 |
if not long: |
| 42 |
return ['?'] |
| 43 |
here = None |
| 44 |
else: |
| 45 |
operline = toline(operindex) |
| 46 |
if long: |
| 47 |
linerange = (toline(0), toline(-1)) |
| 48 |
here = operline |
| 49 |
else: |
| 50 |
linerange = (operline, operline) |
| 51 |
here = None |
| 52 |
lines = ["Happened at file %s line %d" % (graph.filename, here or linerange[0]), ""] |
| 53 |
for n in range(max(0, linerange[0]-show_lines_of_code), |
| 54 |
min(linerange[1]+1+show_lines_of_code, len(graph_lines)+graph.startline)): |
| 55 |
if n == here: |
| 56 |
prefix = '==> ' |
| 57 |
else: |
| 58 |
prefix = ' ' |
| 59 |
lines.append(prefix + graph_lines[n-graph.startline]) |
| 60 |
lines.append("") |
| 61 |
return lines |
| 62 |
|
| 63 |
def source_lines(graph, *args, **kwds): |
| 64 |
lines = source_lines1(graph, *args, **kwds) |
| 65 |
return ['In %r:' % (graph,)] + lines |
| 66 |
|
| 67 |
def gather_error(annotator, graph, block, operindex): |
| 68 |
msg = [""] |
| 69 |
|
| 70 |
if operindex is not None: |
| 71 |
oper = block.operations[operindex] |
| 72 |
if oper.opname == 'simple_call': |
| 73 |
format_simple_call(annotator, oper, msg) |
| 74 |
else: |
| 75 |
oper = None |
| 76 |
msg.append(" %s\n" % str(oper)) |
| 77 |
msg += source_lines(graph, block, operindex, long=True) |
| 78 |
if oper is not None: |
| 79 |
if SHOW_ANNOTATIONS: |
| 80 |
msg += format_annotations(annotator, oper) |
| 81 |
msg += [''] |
| 82 |
return "\n".join(msg) |
| 83 |
|
| 84 |
def format_annotations(annotator, oper): |
| 85 |
msg = [] |
| 86 |
msg.append("Known variable annotations:") |
| 87 |
for arg in oper.args + [oper.result]: |
| 88 |
if isinstance(arg, Variable): |
| 89 |
try: |
| 90 |
msg.append(" " + str(arg) + " = " + str(annotator.binding(arg))) |
| 91 |
except KeyError: |
| 92 |
pass |
| 93 |
return msg |
| 94 |
|
| 95 |
def format_blocked_annotation_error(annotator, blocked_blocks): |
| 96 |
text = [] |
| 97 |
for block, (graph, index) in blocked_blocks.items(): |
| 98 |
text.append("Blocked block -- operation cannot succeed") |
| 99 |
text.append(gather_error(annotator, graph, block, index)) |
| 100 |
return '\n'.join(text) |
| 101 |
|
| 102 |
def format_simple_call(annotator, oper, msg): |
| 103 |
msg.append("Occurred processing the following simple_call:") |
| 104 |
try: |
| 105 |
descs = annotator.binding(oper.args[0]).descriptions |
| 106 |
except (KeyError, AttributeError) as e: |
| 107 |
msg.append(" (%s getting at the binding!)" % ( |
| 108 |
e.__class__.__name__,)) |
| 109 |
return |
| 110 |
for desc in list(descs): |
| 111 |
func = desc.pyobj |
| 112 |
if func is None: |
| 113 |
r = repr(desc) |
| 114 |
else: |
| 115 |
try: |
| 116 |
if isinstance(func, type): |
| 117 |
func_name = "%s.__init__" % func.__name__ |
| 118 |
func = func.__init__.im_func |
| 119 |
else: |
| 120 |
func_name = func.__name__ |
| 121 |
r = "function %s <%s, line %s>" % (func_name, |
| 122 |
func.__code__.co_filename, func.__code__.co_firstlineno) |
| 123 |
except (AttributeError, TypeError): |
| 124 |
r = repr(desc) |
| 125 |
msg.append(" %s returning" % (r,)) |
| 126 |
msg.append("") |
| 127 |
|
| 128 |
def debug(drv, use_pdb=True): |
| 129 |
|
| 130 |
from rpython.translator.tool.pdbplus import PdbPlusShow |
| 131 |
from rpython.translator.driver import log |
| 132 |
t = drv.translator |
| 133 |
|
| 134 |
class options: |
| 135 |
huge = 100 |
| 136 |
|
| 137 |
tb = None |
| 138 |
import traceback |
| 139 |
errmsg = ["Error:\n"] |
| 140 |
exc, val, tb = sys.exc_info() |
| 141 |
|
| 142 |
errmsg.extend([" %s" % line for line in traceback.format_exception(exc, val, [])]) |
| 143 |
block = getattr(val, '__annotator_block', None) |
| 144 |
if block: |
| 145 |
class FileLike: |
| 146 |
def write(self, s): |
| 147 |
errmsg.append(" %s" % s) |
| 148 |
errmsg.append("Processing block:\n") |
| 149 |
t.about(block, FileLike()) |
| 150 |
log.ERROR(''.join(errmsg)) |
| 151 |
|
| 152 |
log.event("start debugger...") |
| 153 |
|
| 154 |
if use_pdb: |
| 155 |
pdb_plus_show = PdbPlusShow(t) |
| 156 |
pdb_plus_show.start(tb) |
| 157 |
|
| 158 |
|
| 159 |
@jit.elidable |
| 160 |
def offset2lineno(c, stopat): |
| 161 |
|
| 162 |
|
| 163 |
tab = c.co_lnotab |
| 164 |
line = c.co_firstlineno |
| 165 |
addr = 0 |
| 166 |
for i in range(0, len(tab), 2): |
| 167 |
addr = addr + ord(tab[i]) |
| 168 |
if addr > stopat: |
| 169 |
break |
| 170 |
line = line + ord(tab[i+1]) |
| 171 |
return line |