[pypy-svn] r17923 - in pypy/dist/pypy/translator: goal tool

pedronis at codespeak.net pedronis at codespeak.net
Tue Sep 27 23:29:04 CEST 2005


Author: pedronis
Date: Tue Sep 27 23:29:02 2005
New Revision: 17923

Modified:
   pypy/dist/pypy/translator/goal/query.py
   pypy/dist/pypy/translator/goal/translate_pypy_new.py
   pypy/dist/pypy/translator/tool/util.py
Log:
* in translate_pypy_new
  use polluted logic instead of find_somebojects,
  complete the formet

* move check_exceptblocks logic to query too

* started reorganizing the queries to be used for sanity checks by translate_pypy to be generator based
  with a helper function to write the produced bits of output, in preparatio to use logging with them 



Modified: pypy/dist/pypy/translator/goal/query.py
==============================================================================
--- pypy/dist/pypy/translator/goal/query.py	(original)
+++ pypy/dist/pypy/translator/goal/query.py	Tue Sep 27 23:29:02 2005
@@ -5,48 +5,6 @@
 import pypy.annotation.model as annmodel
 import pypy.objspace.flow.model as flowmodel
 
-#def sources(translator):
-#    annotator = translator.annotator
-#    d = {}
-#    for v, s in annotator.bindings.iteritems():
-#        if s.__class__ == annmodel.SomeObject and s.knowntype != type:
-#            if s.origin:
-#                d[s.origin[0]] = 1
-#    for func in d:
-#        print func.__module__ or '?', func.__name__
-#    print len(d)
-#    return d.keys()
-
-class Found(Exception):
-    pass
-
-def sovars(translator, g):
-    annotator = translator.annotator
-    def visit(block):
-        if isinstance(block, flowmodel.Block):
-            for v in block.getvariables():
-                s = annotator.binding(v, extquery=True)
-                if s and s.__class__ == annmodel.SomeObject and s.knowntype != type:
-                    print v,s
-    flowmodel.traverse(visit, g)
-
-def polluted(translator):
-    """list functions with still real SomeObject variables"""
-    annotator = translator.annotator
-    def visit(block):
-        if isinstance(block, flowmodel.Block):
-            for v in block.getvariables():
-                s = annotator.binding(v, extquery=True)
-                if s and s.__class__ == annmodel.SomeObject and s.knowntype != type:
-                    raise Found
-    c = 0
-    for f,g in translator.flowgraphs.iteritems():
-        try:
-            flowmodel.traverse(visit, g)
-        except Found:
-            print prettycallable((None, f))
-            c += 1
-    return c
 
 class typerep(object):
     
@@ -122,6 +80,14 @@
     classes = [clsdef and clsdef.cls for clsdef, func in bunch]
     return roots(classes), tuple(typereps(callables))
 
+def prettyfunc(func):
+    descr = "(%s:%s)" % (getattr(func, '__module__', None) or '?', func.func_code.co_firstlineno)
+    funcname = getattr(func, '__name__', None) or 'UNKNOWN'
+    cls = getattr(func, 'class_', None)
+    if cls:
+        funcname = "%s.%s" % (cls.__name__, funcname)
+    return descr+funcname
+
 def prettycallable((cls, obj)):
     if cls is None or cls == (True, ()):
         cls = None
@@ -137,7 +103,7 @@
             cls = "_|%s" % cls
 
     if isinstance(obj, types.FunctionType):
-        obj = "(%s)%s" % (getattr(obj, '__module__', None) or '?', getattr(obj, '__name__', None) or 'UNKNOWN')
+        obj = prettyfunc(obj) 
     elif isinstance(obj, tuple):
         obj = "[%s]" % '|'.join([str(x) for x in obj])
     else:
@@ -558,3 +524,73 @@
         t.about(block)
     ansi_print("`----------------------------------------------------------------------------'", 36)
     print
+
+# query used for sanity checks by translate_pypy
+
+def short_binding(annotator, var):
+    try:
+        binding = annotator.binding(var)
+    except KeyError:
+        return "?"
+    if binding.is_constant():
+        return 'const %s' % binding.__class__.__name__
+    else:
+        return binding.__class__.__name__
+
+def graph_sig(t, g):
+    ann = t.annotator
+    hbinding = lambda v: short_binding(ann, v)
+    return "%s -> %s" % (
+        ', '.join(map(hbinding, g.getargs())),
+        hbinding(g.getreturnvar()))
+    
+class Found(Exception):
+    pass
+
+def polluted_qgen(translator):
+    """list functions with still real SomeObject variables"""
+    annotator = translator.annotator
+    def visit(block):
+        if isinstance(block, flowmodel.Block):
+            for v in block.getvariables():
+                s = annotator.binding(v, extquery=True)
+                if s and s.__class__ == annmodel.SomeObject and s.knowntype != type:
+                    raise Found
+    for f,g in translator.flowgraphs.iteritems():
+        try:
+            flowmodel.traverse(visit, g)
+        except Found:
+            line = "%s: %s" % (prettyfunc(f), graph_sig(translator, g))
+            yield line
+
+def check_exceptblocks_qgen(translator):
+    annotator = translator.annotator
+    for graph in translator.flowgraphs.itervalues():
+        et, ev = graph.exceptblock.inputargs
+        s_et = annotator.binding(et, extquery=True)
+        s_ev = annotator.binding(ev, extquery=True)
+        if s_et:
+            if s_et.knowntype == type:
+                if s_et.__class__ == annmodel.SomeObject:
+                    if hasattr(s_et, 'is_type_of') and  s_et.is_type_of == [ev]:
+                        continue
+                else:
+                    if s_et.__class__ == annmodel.SomePBC:
+                        continue
+            yield "%s exceptblock is not completely sane" % graph.name
+
+
+def qoutput(queryg, write=None):
+    if write is None:
+        def write(s):
+            print s
+    c = 0
+    for bit in queryg:
+        write(bit)
+        c += 1
+    return c
+
+def polluted(translator):
+    c = qoutput(polluted_qgen(translator))
+    print c
+    

Modified: pypy/dist/pypy/translator/goal/translate_pypy_new.py
==============================================================================
--- pypy/dist/pypy/translator/goal/translate_pypy_new.py	(original)
+++ pypy/dist/pypy/translator/goal/translate_pypy_new.py	Tue Sep 27 23:29:02 2005
@@ -89,8 +89,7 @@
 # XXX this tries to make compiling faster
 from pypy.translator.tool import cbuild
 cbuild.enable_fast_compilation()
-from pypy.translator.tool.util import find_someobjects 
-from pypy.translator.tool.util import sanity_check_exceptblocks, update_usession_dir
+from pypy.translator.tool.util import update_usession_dir
 from pypy.translator.tool.util import assert_rpython_mostly_not_imported, mkexename
 
 annmodel.DEBUG = False
@@ -99,6 +98,20 @@
 
 # __________  Main  __________
 
+def sanity_check_annotation(t):
+    irreg = query.qoutput(query.check_exceptblocks_qgen(t))
+    if not irreg:
+        print "++ All exceptblocks seem sane"
+
+    lost = query.sanity_check_methods(t)
+    assert not lost, "lost methods, something gone wrong with the annotation of method defs"
+    print "*** No lost method defs."
+
+    so = query.qoutput(query.polluted_qgen(t))
+    tot = len(t.flowgraphs)
+    percent = int(tot and (100.0*so / tot) or 0)
+    print "-- someobjectness %2d (%d of %d functions polluted by SomeObjects)" % (percent, so, tot)
+
 def analyse(t, inputtypes):
 
     standalone = inputtypes is None
@@ -110,11 +123,8 @@
         print 'Annotating...'
         print 'with policy: %s.%s' % (policy.__class__.__module__, policy.__class__.__name__) 
         a = t.annotate(inputtypes, policy=policy)
-        sanity_check_exceptblocks(t)
-        lost = query.sanity_check_methods(t)
-        assert not lost, "lost methods, something gone wrong with the annotation of method defs"
-        print "*** No lost method defs."
-        find_someobjects(t)
+        sanity_check_annotation(t)
+
     if a: #and not options['-no-s']:
         print 'Simplifying...'
         a.simplify()

Modified: pypy/dist/pypy/translator/tool/util.py
==============================================================================
--- pypy/dist/pypy/translator/tool/util.py	(original)
+++ pypy/dist/pypy/translator/tool/util.py	Tue Sep 27 23:29:02 2005
@@ -41,82 +41,5 @@
        raise RuntimeError("cannot fork because improper rtyper code"
                           " has already been imported: %r" %(wrongimports,))
 
-def sanity_check_exceptblocks(translator):
-    annotator = translator.annotator
-    irreg = 0
-    for graph in translator.flowgraphs.itervalues():
-        et, ev = graph.exceptblock.inputargs
-        s_et = annotator.binding(et, extquery=True)
-        s_ev = annotator.binding(ev, extquery=True)
-        if s_et:
-            if s_et.knowntype == type:
-                if s_et.__class__ == SomeObject:
-                    if hasattr(s_et, 'is_type_of') and  s_et.is_type_of == [ev]:
-                        continue
-                else:
-                    if s_et.__class__ == annmodel.SomePBC:
-                        continue
-            print "*****", graph.name, "exceptblock is not completely sane"
-            irreg += 1
-    if irreg == 0:
-        print "*** All exceptblocks seem sane."
-
-def find_someobjects(translator, quiet=False):
-    """Find all functions in that have SomeObject in their signature."""
-    annotator = translator.annotator
-    if not annotator:
-        return # no annotations available
-
-    translator.highlight_functions = {}
-
-    def is_someobject(var):
-        try:
-            return annotator.binding(var).__class__ == SomeObject
-        except KeyError:
-            return False
-
-    def short_binding(var):
-        try:
-            binding = annotator.binding(var)
-        except KeyError:
-            return "?"
-        if binding.is_constant():
-            return 'const %s' % binding.__class__.__name__
-        else:
-            return binding.__class__.__name__
-
-    header = True
-    items = [(graph.name, func, graph)
-             for func, graph in translator.flowgraphs.items()]
-    items.sort()
-    num = someobjnum = 0
-    for graphname, func, graph in items:
-        unknown_input_args = len(filter(is_someobject, graph.getargs()))
-        unknown_return_value = is_someobject(graph.getreturnvar())
-        if unknown_input_args or unknown_return_value:
-            someobjnum += 1
-            translator.highlight_functions[func] = True
-            if not quiet:
-                if header:
-                    header = False
-                    print "=" * 70
-                    print "Functions that have SomeObject in their signature"
-                    print "=" * 70
-                print ("%(name)s(%(args)s) -> %(result)s\n"
-                       "%(filename)s:%(lineno)s\n"
-                       % {'name': graph.name,
-                          'filename': func.func_globals.get('__name__', '?'),
-                          'lineno': func.func_code.co_firstlineno,
-                          'args': ', '.join(map(short_binding,
-                                                graph.getargs())),
-                          'result': short_binding(graph.getreturnvar())})
-        num += 1
-    if not quiet:
-        print "=" * 70
-        percent = int(num and (100.0*someobjnum / num) or 0)
-        print "someobjectness: %2d percent" % (percent)
-        print "(%d out of %d functions get or return SomeObjects" % (
-            someobjnum, num) 
-        print "=" * 70
 
 



More information about the Pypy-commit mailing list