[pypy-svn] r4958 - pypy/trunk/src/pypy/translator/test

hpk at codespeak.net hpk at codespeak.net
Sat Jun 5 14:34:41 CEST 2004


Author: hpk
Date: Sat Jun  5 14:34:41 2004
New Revision: 4958

Added:
   pypy/trunk/src/pypy/translator/test/run_snippet.py
      - copied, changed from r4942, pypy/trunk/src/pypy/translator/test/some_snippets_test.py
Removed:
   pypy/trunk/src/pypy/translator/test/some_snippets_test.py
Log:
a new run_snippet tests which scans through all
snippet functions and tries to flow/annotate and
compile them, printing a nice report and taking
the starting types out of the func_defaults of the
snippets. 



Copied: pypy/trunk/src/pypy/translator/test/run_snippet.py (from r4942, pypy/trunk/src/pypy/translator/test/some_snippets_test.py)
==============================================================================
--- pypy/trunk/src/pypy/translator/test/some_snippets_test.py	(original)
+++ pypy/trunk/src/pypy/translator/test/run_snippet.py	Sat Jun  5 14:34:41 2004
@@ -5,96 +5,84 @@
 """
 import autopath
 import traceback
+import sys
 from pypy.tool import testit
 from pypy.translator.translator import Translator
 
 from pypy.translator.test import snippet
 
-import inspect
-
-def compile(func,argtypes=[]):
-    
-    t = Translator(func)
-    t.simplify()
-    t.annotate(argtypes)#.simplify()
-    #t.view()
-    compiled_function = t.compile()
-    return compiled_function
-
-def compile_by_inspecting(module):
-    bad=0
-    good=0
-    funcs={}
-    all=module.__dict__.items()
-    for fu in all:
-        if isinstance(fu[1],type(compile)):
-            func_info={}
-            func_versions={}
+class Result:
+    def __init__(self, func, argtypes):
+        self.func = func 
+        self.argtypes = argtypes
+        self.r_flow = self.r_annotate = self.r_compile = None 
+        for name in 'flow', 'annotate', 'compile':
+            method = getattr(self, name)
+            resname = 'r_' + name 
             try:
-                args=inspect.getargspec(fu[1])
-                func_info['arg_names']=args[0]
-                func_info['name']=fu[0]
-                func_versions['python']=fu[1]
-                func_versions['pyrex']=compile(fu[1],[int]*len(args[0]))
+                method()
+            except (KeyboardInterrupt, SystemExit):
+                raise
             except:
-                print fu[0]
-                bad+=1
+                self.excinfo = sys.exc_info()
+                setattr(self, resname, False) 
             else:
-                good+=1
-                funcs[fu[0]]=(func_info,func_versions)
-    print "Good: %i, Bad : %i, All: %i"%(good,bad,good+bad) 
-    return funcs
-def random_arg(arg):
-    if arg is int:
-        return 5
-    if arg is list:
-        return [1,2,3,4,5,6] # [1,'ere','fggf']Doesn't work for snippet.yast
-    if arg is str:
-        return 'python'
-    else:
-        return 'object'
-    
-def compile_by_function_info(module,info):
-    result={}
-    for func_name in info.keys():
-        func=module.__dict__[func_name]
-        arg_types=info[func_name]['arg_types']
-        try:
-            pyrex_func=compile(func,arg_types)
-        except:
-            traceback.print_exc()
-            print "Pyrex Compilation exception",func_name
-        args=tuple([random_arg(atype) for atype in arg_types])
-        try:
-            pyresult=func(*args)
-            try:
-                pyrexresult=pyrex_func(*args)
-            except:
-                print "pyrex function not runnable",func_name
-                raise
-        except:
-            print "Python Function not runnable",func_name
-            print traceback.print_exc()
-        else:
-            result[func_name]=(pyresult, pyrexresult) #or (pyresult,pyrexresult)
-        
-    return  result
-  
-def get_funcs_from_module(module):
-    info=module.__dict__.get('function_info',None)
-    if info:
-        funcs=compile_by_function_info(module,info)
-    else:    
-        funcs=compile_by_inspecting(module)
-    return funcs
+                setattr(self, resname, True) 
+                
+    def flow(self):
+        self.translator = Translator(func)
+        self.translator.simplify()
+
+    def annotate(self):
+        self.translator.annotate(self.argtypes)
+
+    def compile(self):
+        compiled_function = self.translator.compile()
+        return compiled_function
     
+def collect_functions(module):
+    l = []
+    for name, value in vars(module).items():
+        if name[0] != '_' and hasattr(value, 'func_code'):
+            l.append(value) 
+    return l
+   
+def get_arg_types(func):
+    # func_defaults e.g.:  ([int, float], [str, int], int) 
+    if func.func_defaults:
+        argstypelist = []
+        for spec in func.func_defaults:
+            if isinstance(spec, tuple):
+                spec = spec[0] # use the first type only for the tests
+            argstypelist.append(spec)
+        yield argstypelist 
+    else:
+        yield []
+
+# format string for result-lines 
+format_str = "%-30s %10s %10s %10s" 
+
+def repr_result(res):
+    name = res.func.func_name 
+    argtypes = res.argtypes 
+    funccall = "%s(%s)" % (name, ", ".join([x.__name__ for x in argtypes]))
+    flow = res.r_flow and 'ok' or 'fail' 
+    ann = res.r_annotate and 'ok' or 'fail'
+    comp = res.r_compile and 'ok' or 'fail'
+    return format_str %(funccall, flow, ann, comp)
+     
 if __name__=='__main__':
-    funcs=get_funcs_from_module(snippet)
-    import pprint
-    print len(funcs)
-    for f in funcs.keys():
-        assert funcs[f][0]==funcs[f][1],"%s!=%s"%(funcs[f][0],funcs[f][1])
-        print f,
-        pprint.pprint(funcs[f])
-    
+    funcs = collect_functions(snippet)
+    funcs.insert(0, snippet._attrs) 
+    results = []
+    print format_str %("functioncall", "flowed", "annotated", "compiled")
+    for func in funcs:
+        for argtypeslist in get_arg_types(func):
+            result = Result(func, argtypeslist) 
+            results.append(result) 
+            print repr_result(result) 
     
+    for res in results:
+        print repr_result(res) 
+   
+     

Deleted: /pypy/trunk/src/pypy/translator/test/some_snippets_test.py
==============================================================================
--- /pypy/trunk/src/pypy/translator/test/some_snippets_test.py	Sat Jun  5 14:34:41 2004
+++ (empty file)
@@ -1,100 +0,0 @@
-""" 
-
-    Use all functions in snippet to test translation to pyrex
-    
-"""
-import autopath
-import traceback
-from pypy.tool import testit
-from pypy.translator.translator import Translator
-
-from pypy.translator.test import snippet
-
-import inspect
-
-def compile(func,argtypes=[]):
-    
-    t = Translator(func)
-    t.simplify()
-    t.annotate(argtypes)#.simplify()
-    #t.view()
-    compiled_function = t.compile()
-    return compiled_function
-
-def compile_by_inspecting(module):
-    bad=0
-    good=0
-    funcs={}
-    all=module.__dict__.items()
-    for fu in all:
-        if isinstance(fu[1],type(compile)):
-            func_info={}
-            func_versions={}
-            try:
-                args=inspect.getargspec(fu[1])
-                func_info['arg_names']=args[0]
-                func_info['name']=fu[0]
-                func_versions['python']=fu[1]
-                func_versions['pyrex']=compile(fu[1],[int]*len(args[0]))
-            except:
-                print fu[0]
-                bad+=1
-            else:
-                good+=1
-                funcs[fu[0]]=(func_info,func_versions)
-    print "Good: %i, Bad : %i, All: %i"%(good,bad,good+bad) 
-    return funcs
-def random_arg(arg):
-    if arg is int:
-        return 5
-    if arg is list:
-        return [1,2,3,4,5,6] # [1,'ere','fggf']Doesn't work for snippet.yast
-    if arg is str:
-        return 'python'
-    else:
-        return 'object'
-    
-def compile_by_function_info(module,info):
-    result={}
-    for func_name in info.keys():
-        func=module.__dict__[func_name]
-        arg_types=info[func_name]['arg_types']
-        try:
-            pyrex_func=compile(func,arg_types)
-        except:
-            traceback.print_exc()
-            print "Pyrex Compilation exception",func_name
-        args=tuple([random_arg(atype) for atype in arg_types])
-        try:
-            pyresult=func(*args)
-            try:
-                pyrexresult=pyrex_func(*args)
-            except:
-                print "pyrex function not runnable",func_name
-                raise
-        except:
-            print "Python Function not runnable",func_name
-            print traceback.print_exc()
-        else:
-            result[func_name]=(pyresult, pyrexresult) #or (pyresult,pyrexresult)
-        
-    return  result
-  
-def get_funcs_from_module(module):
-    info=module.__dict__.get('function_info',None)
-    if info:
-        funcs=compile_by_function_info(module,info)
-    else:    
-        funcs=compile_by_inspecting(module)
-    return funcs
-    
-if __name__=='__main__':
-    funcs=get_funcs_from_module(snippet)
-    import pprint
-    print len(funcs)
-    for f in funcs.keys():
-        assert funcs[f][0]==funcs[f][1],"%s!=%s"%(funcs[f][0],funcs[f][1])
-        print f,
-        pprint.pprint(funcs[f])
-    
-    



More information about the Pypy-commit mailing list