[pypy-svn] r9485 - in pypy/dist/pypy: annotation interpreter tool translator

pedronis at codespeak.net pedronis at codespeak.net
Thu Feb 24 17:37:59 CET 2005


Author: pedronis
Date: Thu Feb 24 17:37:59 2005
New Revision: 9485

Added:
   pypy/dist/pypy/tool/ansi_print.py   (contents, props changed)
Modified:
   pypy/dist/pypy/annotation/bookkeeper.py
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/unaryop.py
   pypy/dist/pypy/interpreter/error.py
   pypy/dist/pypy/translator/annrpython.py
Log:
make a print using ansi terminal codes generally available,

use where possible coloring for the annotation WARNINGs

add a WARNING in debug mode for functions inferred to return SomeObject



Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Thu Feb 24 17:37:59 2005
@@ -246,7 +246,7 @@
         try:
             inputcells = args.match_signature(signature, defs_s)
         except ArgErr, e:
-            assert False, 'ABOUT TO IGNORE %r' % e     # hopefully temporary hack
+            assert False, 'ABOUT TO IGNORE %r' % e     # we should take care that we don't end up here anymore
             return SomeImpossibleValue()
 
         return self.annotator.recursivecall(func, self.position_key, inputcells)

Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Thu Feb 24 17:37:59 2005
@@ -3,6 +3,7 @@
 """
 
 import types
+from pypy.tool.ansi_print import ansi_print
 from pypy.annotation.model import SomeInteger, SomeObject, SomeChar, SomeBool
 from pypy.annotation.model import SomeList, SomeString, SomeTuple, SomeSlice
 from pypy.annotation.bookkeeper import getbookkeeper
@@ -91,15 +92,19 @@
 
 def builtin_getattr(s_obj, s_attr, s_default=None):
     if not s_attr.is_constant() or not isinstance(s_attr.const, str):
-        print "UN-RPYTHONIC-WARNING", \
-              'getattr(%r, %r) is not RPythonic enough' % (s_obj, s_attr)
+        ansi_print("UN-RPYTHONIC-WARNING " +
+                   '[%s] getattr(%r, %r) is not RPythonic enough' % (getbookkeeper().whereami(),
+                                                                     s_obj, s_attr),
+                   esc="31") # RED
         return SomeObject()
     return s_obj.getattr(s_attr)
 
 def builtin_hasattr(s_obj, s_attr):
     if not s_attr.is_constant() or not isinstance(s_attr.const, str):
-        print "UN-RPYTHONIC-WARNING", \
-              'hasattr(%r, %r) is not RPythonic enough' % (s_obj, s_attr)
+        ansi_print("UN-RPYTHONIC-WARNING " +
+                   '[%s] hasattr(%r, %r) is not RPythonic enough' % (getbookkeeper().whereami(),
+                                                                     s_obj, s_attr),
+                   esc="31") # RED
     return SomeBool()
 
 def builtin_hash(s_obj):

Modified: pypy/dist/pypy/annotation/unaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/unaryop.py	(original)
+++ pypy/dist/pypy/annotation/unaryop.py	Thu Feb 24 17:37:59 2005
@@ -3,6 +3,7 @@
 """
 
 from types import FunctionType
+from pypy.tool.ansi_print import ansi_print
 from pypy.interpreter.argument import Arguments
 from pypy.annotation.pairtype import pair
 from pypy.annotation.model import SomeObject, SomeInteger, SomeBool
@@ -74,7 +75,8 @@
 
     def call(obj, args):
         #raise Exception, "cannot follow call_args%r" % ((obj, args),)
-        print "*** [%s] cannot follow call(%r, %r)" % (getbookkeeper().whereami(), obj, args)
+        ansi_print("*** WARNING: [%s] cannot follow call(%r, %r)" %
+                   (getbookkeeper().whereami(), obj, args), esc="31") # RED
         return SomeObject()
 
 
@@ -233,7 +235,8 @@
 
     def setattr(pbc, s_attr, s_value):
         #raise Exception, "oops!"
-        print "*** WARNING: setattr not wanted on %r" % pbc 
+        ansi_print("*** WARNING: [%s] setattr not wanted on %r" %
+                   (getbookkeeper().whereami(), pbc), esc="31") # RED
         pass
 
     def call(pbc, args):

Modified: pypy/dist/pypy/interpreter/error.py
==============================================================================
--- pypy/dist/pypy/interpreter/error.py	(original)
+++ pypy/dist/pypy/interpreter/error.py	Thu Feb 24 17:37:59 2005
@@ -1,3 +1,4 @@
+import autopath
 import os, sys
 
 AUTO_DEBUG = os.getenv('PYPY_DEBUG')
@@ -123,16 +124,10 @@
 
 
 # Utilities
+from pypy.tool.ansi_print import ansi_print
 
 def debug_print(text, file=None):
-    if file is None: file = sys.stderr
-    text = text.rstrip()
-    if sys.platform != "win32" and file.isatty():
-        text = ('\x1b[31m' +   # ANSI color code "red"
-                text +
-                '\x1b[0m')     # ANSI color code "reset"
-    file.write(text + '\n')
-
+    ansi_print(text, esc="31", file=file) # ANSI color code "red"
 
 ### installing the excepthook for OperationErrors
 ##def operr_excepthook(exctype, value, traceback):

Added: pypy/dist/pypy/tool/ansi_print.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/tool/ansi_print.py	Thu Feb 24 17:37:59 2005
@@ -0,0 +1,10 @@
+import sys
+
+def ansi_print(text, esc, file=None):
+    if file is None: file = sys.stderr
+    text = text.rstrip()
+    if sys.platform != "win32" and file.isatty():
+        text = ('\x1b[%sm' % esc  +  
+                text +
+                '\x1b[0m')     # ANSI color code "reset"
+    file.write(text + '\n')

Modified: pypy/dist/pypy/translator/annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/annrpython.py	(original)
+++ pypy/dist/pypy/translator/annrpython.py	Thu Feb 24 17:37:59 2005
@@ -1,6 +1,7 @@
 from __future__ import generators
 
 from types import FunctionType, ClassType
+from pypy.tool.ansi_print import ansi_print
 from pypy.annotation import model as annmodel
 from pypy.annotation.model import pair
 from pypy.annotation.factory import ListFactory, DictFactory, BlockedInference
@@ -38,22 +39,35 @@
         self.binding_cause_history = {} # map Variables to lists of positions
                 # history of binding_caused_by, kept in sync with
                 # bindingshistory
+        self.return_bindings = {} # map return Variables to functions
         # --- end of debugging information ---
         self.bookkeeper = Bookkeeper(self)
 
+
+    def _register_returnvar(self, flowgraph, func):
+        if annmodel.DEBUG:
+            self.return_bindings[flowgraph.getreturnvar()] = func
+
     #___ convenience high-level interface __________________
 
+    def getflowgraph(self, func, called_by=None, call_tag=None):        
+        flowgraph = self.translator.getflowgraph(func, called_by=called_by, call_tag=call_tag)
+        self._register_returnvar(flowgraph, func)
+        return flowgraph
+        
+
     def build_types(self, func_or_flowgraph, input_arg_types, func=None):
         """Recursively build annotations about the specific entry point."""
         if isinstance(func_or_flowgraph, FunctionGraph):
             flowgraph = func_or_flowgraph
+            self._register_returnvar(flowgraph, func)
         else:
             func = func_or_flowgraph
             if self.translator is None:
                 from pypy.translator.translator import Translator
                 self.translator = Translator(func, simplifying=True)
                 self.translator.annotator = self
-            flowgraph = self.translator.getflowgraph(func)
+            flowgraph = self.getflowgraph(func)
         # make input arguments and set their type
         input_arg_types = list(input_arg_types)
         nbarg = len(flowgraph.getargs())
@@ -166,6 +180,17 @@
                 cause_history.append(self.binding_caused_by[arg])
         self.bindings[arg] = s_value
         if annmodel.DEBUG:
+            #if arg in self.return_bindings:
+            #    ansi_print("%s -> %s" % (self.whereami((self.return_bindings[arg],
+            #                                             None, None)),
+            #                             s_value),
+            #               esc="1") # bold
+
+            if arg in self.return_bindings and s_value == annmodel.SomeObject():
+                ansi_print("*** WARNING: %s result degenerated to SomeObject" %
+                           self.whereami((self.return_bindings[arg],None, None)),
+                           esc="31") # RED
+                
             self.binding_caused_by[arg] = called_from
 
 
@@ -173,8 +198,7 @@
 
     def recursivecall(self, func, position_key, inputcells):
         parent_fn, parent_block, parent_index = position_key
-        graph = self.translator.getflowgraph(func, parent_fn,
-                                             position_key)
+        graph = self.getflowgraph(func, parent_fn, position_key)
         # self.notify[graph.returnblock] is a dictionary of call
         # points to this func which triggers a reflow whenever the
         # return block of this graph has been analysed.
@@ -297,8 +321,12 @@
         mod = getattr(fn, '__module__', None)
         if mod is None:
             mod = '?'
-        name = fn.__name__
-        firstlineno = fn.func_code.co_firstlineno
+        name = getattr(fn, '__name__', None)
+        if name is not None:
+            firstlineno = fn.func_code.co_firstlineno
+        else:
+            name = 'UNKNOWN'
+            firstlineno = -1
         return "(%s:%d) %s" % (mod, firstlineno, name)
 
     def flowin(self, fn, block):



More information about the Pypy-commit mailing list