[pypy-svn] r29730 - in pypy/dist/pypy: annotation interpreter objspace/flow tool

pedronis at codespeak.net pedronis at codespeak.net
Fri Jul 7 12:54:04 CEST 2006


Author: pedronis
Date: Fri Jul  7 12:54:00 2006
New Revision: 29730

Modified:
   pypy/dist/pypy/annotation/annrpython.py
   pypy/dist/pypy/interpreter/error.py
   pypy/dist/pypy/objspace/flow/model.py
   pypy/dist/pypy/objspace/flow/objspace.py
   pypy/dist/pypy/tool/error.py
   pypy/dist/pypy/tool/sourcetools.py
Log:
(fijal, pedronis)

make the better errors more robust, some one off problems now that fijal will fix next



Modified: pypy/dist/pypy/annotation/annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/annrpython.py	(original)
+++ pypy/dist/pypy/annotation/annrpython.py	Fri Jul  7 12:54:00 2006
@@ -12,7 +12,7 @@
 log = py.log.Producer("annrpython") 
 py.log.setconsumer("annrpython", ansi_log) 
 
-from pypy.tool.error import format_annotation_error, format_someobject_error, AnnotatorError
+from pypy.tool.error import format_blocked_annotation_error, format_someobject_error, AnnotatorError
 
 FAIL = object()
 
@@ -216,7 +216,7 @@
             blocked_blocks = [block for block, done in self.annotated.items()
                                     if done is False]
 
-            text = format_annotation_error(self, blocked_blocks, graph)
+            text = format_blocked_annotation_error(self, blocked_blocks, graph)
             #raise SystemExit()
             raise AnnotatorError(text)
         # make sure that the return variables of all graphs is annotated
@@ -258,8 +258,6 @@
     def ondegenerated(self, what, s_value, where=None, called_from_graph=None):
         if self.policy.allow_someobjects:
             return
-
-    
         # is the function itself tagged with allow_someobjects?
         position_key = where or getattr(self.bookkeeper, 'position_key', None)
         if position_key is not None:
@@ -271,16 +269,10 @@
                 pass
 
         graph = position_key[0]
-        msgstr = format_someobject_error(self, graph, block, what)
+        msgstr = format_someobject_error(self, position_key, what, s_value,
+                                         called_from_graph)
         
-##        if position_key is not None:
-##            msglines.append(".. position: %s" % (self.whereami(position_key),))
-##        if called_from_graph is not None:
-##            msglines.append(".. called from %r" % (called_from_graph,))
-##        if s_value.origin is not None:
-##            msglines.append(".. SomeObject() origin: %s" % (
-##                self.whereami(s_value.origin),))
-        # FIXME: we need that as well as error handler
+
         raise AnnotatorError(msgstr)
 
     def setbinding(self, arg, s_value, called_from_graph=None, where=None):

Modified: pypy/dist/pypy/interpreter/error.py
==============================================================================
--- pypy/dist/pypy/interpreter/error.py	(original)
+++ pypy/dist/pypy/interpreter/error.py	Fri Jul  7 12:54:00 2006
@@ -17,8 +17,8 @@
 
     def __init__(self, w_type, w_value, tb=None):
         if w_type is None:
-            from pypy.tool.error import AnnotatorError
-            raise AnnotatorError(w_value)
+            from pypy.tool.error import FlowingError
+            raise FlowingError(w_value)
         self.w_type = w_type
         self.w_value = w_value
         self.application_traceback = tb

Modified: pypy/dist/pypy/objspace/flow/model.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/model.py	(original)
+++ pypy/dist/pypy/objspace/flow/model.py	Fri Jul  7 12:54:00 2006
@@ -65,6 +65,9 @@
 
     def getsource(self):
         from pypy.tool.sourcetools import getsource
+        func = getattr(self, 'func', None)
+        if func is None:
+            return None
         return getsource(self.func)
     source = roproperty(getsource)
     

Modified: pypy/dist/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/objspace.py	(original)
+++ pypy/dist/pypy/objspace/flow/objspace.py	Fri Jul  7 12:54:00 2006
@@ -259,13 +259,13 @@
         graph.defaults = func.func_defaults or ()
         self.setup_executioncontext(ec)
 
-        from pypy.tool.error import AnnotatorError, format_global_error
+        from pypy.tool.error import FlowingError, format_global_error
 
         try:
             ec.build_flow()
-        except AnnotatorError, a:
+        except FlowingError, a:
             # attach additional source info to AnnotatorError
-            raise AnnotatorError(format_global_error(ec.graph, ec.crnt_offset, str(a)))
+            raise FlowingError(format_global_error(ec.graph, ec.crnt_offset, str(a)))
         checkgraph(graph)
         return graph
 

Modified: pypy/dist/pypy/tool/error.py
==============================================================================
--- pypy/dist/pypy/tool/error.py	(original)
+++ pypy/dist/pypy/tool/error.py	Fri Jul  7 12:54:00 2006
@@ -16,16 +16,56 @@
 from pypy.interpreter.pytraceback import offset2lineno
 import traceback
 
+def source_lines(graph, block, operindex=None, offset=None, long=False):
+    source = graph.source
+    if block is not None:
+        if block is graph.returnblock:
+            return ['<return>']
+    if source is not None:
+        graph_lines = graph.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(linerange[0], linerange[1]+1):
+            lines.append(graph_lines[n-graph.startline-1])
+            if n == here:
+                lines.append('^ HERE')
+        return lines
+    else:
+        return ['no source!']
+
+class FlowingError(Exception):
+    pass
+
 class AnnotatorError(Exception):
     pass
 
 def gather_error(annotator, block, graph):
-    oper = block.operations[annotator.why_not_annotated[block][1].break_at[2]]
-    offset = oper.offset
-    lineno = offset2lineno(graph.func.func_code, offset)
     msg = []
     msg.append('-+' * 30)
     msg.append("Operation cannot succeed")
+    _, _, operindex = annotator.why_not_annotated[block][1].break_at
+    oper = block.operations[operindex]
     msg.append(" " + str(oper))
     if SHOW_ANNOTATIONS:
         msg.append("Known variable annotations:")
@@ -36,57 +76,36 @@
                 except KeyError:
                     pass
         msg.append("")
-    msg.append("Happened at file %s line %d" % (graph.filename, lineno))    
     if SHOW_TRACEBACK:
         msg.extend(traceback.format_exception(*annotator.why_not_annotated[block]))
-    add_graph(msg, graph, lineno)
-    return "\n".join(msg)
-
-def add_graph(msg, graph, lineno):
-    graph_lines = graph.source.split("\n")
-    graph_lineno = lineno - graph.startline
-    msg.append("")
-    str_num = (len(graph_lines[graph_lineno]) - 6)/2
-    for num, line in enumerate(graph_lines + [""]):
-        msg.append(line)
-        if num == graph_lineno:
-            msg.append("^" * str_num + " HERE " + "^" * str_num)
-    msg.append('-+' * 30)
-    
-def format_someobject_error(annotator, graph, block, what):
-    #block = getattr(annotator, 'flowin_block', None) or block
-    offset1 = offset2 = 0
-    if block.operations:
-        offset1 = block.operations[0].offset
-        offset2 = block.operations[-1].offset
-        
-    block_start = offset2lineno(graph.func.func_code, offset1) - graph.startline - 1
-    block_end = offset2lineno(graph.func.func_code, offset2) - graph.startline - 1
-    msg = ["annotation of %r degenerated to SomeObject()" % (what,)]
-    graph_lines = graph.source.split("\n")
-    msg.append("Somewhere here:")
-    for num, line in enumerate(graph_lines + [""]):
-        msg.append(line)
-        if num == block_start:
-            str_num = (len(graph_lines[num + 1]) - 6)/2
-            msg.append("-"*str_num + " BELOW " + "-"*str_num)
-        elif num == block_end:
-            str_num = (len(graph_lines[num]) - 6)/2
-            msg.append("^"*str_num + " ABOVE " + "^"*str_num)
+    msg += source_lines(graph, block, operindex, long=True)
     return "\n".join(msg)
 
-def format_annotation_error(annotator, blocked_blocks, graph):
+def format_blocked_annotation_error(annotator, blocked_blocks, graph):
     text = ""
     for block in blocked_blocks:
         text += gather_error(annotator, block, graph)
     return text
+    
+def format_someobject_error(annotator, position_key, what, s_value, called_from_graph):
+    #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
+        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),))
+    return "\n".join(msg)
 
 def format_global_error(graph, offset, message):
     msg = []
     msg.append('-+' * 30)
-    lineno = offset2lineno(graph.func.func_code, offset)
     msg.append(message)
-    add_graph(msg, graph, lineno)
+    msg += source_lines(graph, None, offset=offset)
     return "\n".join(msg)
 
 def debug(drv):

Modified: pypy/dist/pypy/tool/sourcetools.py
==============================================================================
--- pypy/dist/pypy/tool/sourcetools.py	(original)
+++ pypy/dist/pypy/tool/sourcetools.py	Fri Jul  7 12:54:00 2006
@@ -105,7 +105,10 @@
     if hasattr(name, '__source__'):
         src = str(name.__source__)
     else:
-        src = inspect.getsource(object)
+        try:
+            src = inspect.getsource(object)
+        except IOError:
+            return None
     if hasattr(name, "__sourceargs__"):
         return src % name.__sourceargs__
     return src



More information about the Pypy-commit mailing list