[pypy-svn] r20526 - in pypy/branch/somepbc-refactoring/pypy/translator/pyrex: . test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 1 19:46:46 CET 2005


Author: arigo
Date: Thu Dec  1 19:46:44 2005
New Revision: 20526

Modified:
   pypy/branch/somepbc-refactoring/pypy/translator/pyrex/genpyrex.py
   pypy/branch/somepbc-refactoring/pypy/translator/pyrex/test/test_pyrextrans.py
Log:
hack hack hack until the genpyrex tests pass again.


Modified: pypy/branch/somepbc-refactoring/pypy/translator/pyrex/genpyrex.py
==============================================================================
--- pypy/branch/somepbc-refactoring/pypy/translator/pyrex/genpyrex.py	(original)
+++ pypy/branch/somepbc-refactoring/pypy/translator/pyrex/genpyrex.py	Thu Dec  1 19:46:44 2005
@@ -8,7 +8,8 @@
 from pypy.objspace.flow.model import mkentrymap, last_exception
 from pypy.translator.annrpython import RPythonAnnotator
 from pypy.annotation.model import SomePBC
-from pypy.annotation.classdef import isclassdef
+from pypy.annotation.description import MethodDesc
+from pypy.annotation.classdef import ClassDef
 from pypy.tool.uid import uid
 import inspect
 
@@ -213,7 +214,7 @@
             # make the function visible from the outside
             # under its original name
             args = ', '.join([var.name for var in fun.getargs()])
-            self.putline("def %s(%s):" % (fun.name, args))
+            self.putline("def %s(%s):" % (fun.name.split('.')[-1], args))
             self.indent += 1
             self.putline("return %s(%s)" % (
                 self.getfunctionname(function_object), args))
@@ -247,6 +248,10 @@
 
     def get_type(self, var):
         if isinstance(var, Constant):
+            tp = var.value.__class__
+            if self.annotator and tp in self.annotator.bookkeeper.descs:
+                classdesc = self.annotator.bookkeeper.descs[tp]
+                return classdesc.getuniqueclassdef()
             return type(var.value)
         elif self.annotator:
             return self.annotator.gettype(var)
@@ -257,7 +262,7 @@
         vartype = self.get_type(var)
         if vartype in (int, bool):
             prefix = "i_"
-        elif self.annotator and vartype in self.annotator.getuserclasses():
+        elif isinstance(vartype, ClassDef):
             prefix = "p_"
         else:
             prefix = ""
@@ -271,8 +276,7 @@
     def _gettypename(self, vartype):
         if vartype in (int, bool):
             ctype = "int"
-        elif (self.annotator and vartype in self.annotator.getuserclasses()
-              and vartype.__module__ != '__builtin__'):
+        elif isinstance(vartype, ClassDef):
             ctype = self.getclassname(vartype)
         else:
             ctype = "object"
@@ -286,9 +290,9 @@
                 return ""
 
     def getclassname(self,cls):
-        assert inspect.isclass(cls)
-        name = cls.__name__
-        if issubclass(cls,Exception):
+        assert isinstance(cls, ClassDef)
+        name = cls.shortname
+        if cls.issubclass(self.annotator.bookkeeper.getuniqueclassdef(Exception)):
             return name
         return '%s__%x' % (name, uid(cls))#self._hackname(cls)
     
@@ -316,7 +320,8 @@
         elif isinstance(obj, Constant):
             import types
             if isinstance(obj.value,(types.ClassType,type)):
-                fff=self.getclassname(obj.value)
+                bk = self.annotator.bookkeeper
+                fff=self.getclassname(bk.getuniqueclassdef(obj.value))
             elif isinstance(obj.value,(types.FunctionType,
                                        types.MethodType,
                                        type)):
@@ -423,42 +428,41 @@
             self.lines = []
             self.indent = 0
             delay_methods={}
-            for cls in self.annotator.getuserclassdefinitions():
+            for cls in self.annotator.bookkeeper.classdefs:
                 if cls.basedef:
-                    bdef="(%s)" % (self.getclassname(cls.basedef.cls))
+                    bdef="(%s)" % (self.getclassname(cls.basedef))
                 else:
                     bdef=""
-                self.putline("cdef class %s%s:" % (self.getclassname(cls.cls),bdef))
+                self.putline("cdef class %s%s:" % (self.getclassname(cls),bdef))
                 self.indent += 1
                 empty = True
                 for attr, attrdef in cls.attrs.items():
                     s_value = attrdef.s_value
                     if isinstance(s_value, SomePBC):
-                        for py_fun,fun_class in s_value.prebuiltinstances.items():
-                            assert isclassdef(fun_class), ("don't support "
-                                "prebuilt constants like %r" % py_fun)
-                            delay_methods.setdefault(fun_class,[]).append(py_fun)
+                        assert s_value.getKind() is MethodDesc, ("don't support "
+                                "prebuilt constants like %r" % (s_value,))
+                        for methdesc in s_value.descriptions:
+                            meth_class = methdesc.originclassdef
+                            delay_methods.setdefault(meth_class,[]).append(methdesc)
                     else:
                         vartype=self._gettypename(s_value.knowntype)
                         self.putline("cdef public %s %s" % (vartype, attr))
                         empty = False
                 list_methods=delay_methods.get(cls,[])
-                for py_fun in list_methods:
+                for methdesc in list_methods:
                     # XXX!
-                    try:
-                        fun = self.annotator.translator.flowgraphs[py_fun]
-                    except KeyError:
-                        continue  # method present in class but never called
-                    hackedargs = ', '.join([var.name for var in fun.getargs()])
-                    self.putline("def %s(%s):" % (py_fun.__name__, hackedargs))
+                    graph = methdesc.funcdesc.cachedgraph(None)
+                    hackedargs = ', '.join([var.name for var in graph.getargs()])
+                    name = graph.name.split('.')[-1]
+                    self.putline("def %s(%s):" % (name, hackedargs))
                     self.indent += 1
                     # XXX special case hack: cannot use 'return' in __init__
-                    if py_fun.__name__ == "__init__":
+                    if name == "__init__":
                         statement = ""
                     else:
                         statement = "return "
                     self.putline("%s%s(%s)" % (statement,
-                                               self.getfunctionname(py_fun),
+                                               self.getfunctionname(graph.func),
                                                hackedargs))
                     self.indent -= 1
                     empty = False

Modified: pypy/branch/somepbc-refactoring/pypy/translator/pyrex/test/test_pyrextrans.py
==============================================================================
--- pypy/branch/somepbc-refactoring/pypy/translator/pyrex/test/test_pyrextrans.py	(original)
+++ pypy/branch/somepbc-refactoring/pypy/translator/pyrex/test/test_pyrextrans.py	Thu Dec  1 19:46:44 2005
@@ -4,8 +4,9 @@
 from pypy.translator.pyrex.genpyrex import GenPyrex
 from pypy.objspace.flow.model import *
 from pypy.translator.tool.cbuild import build_cfunc
+from pypy.translator.tool.cbuild import make_module_from_pyxstring
 from pypy.translator.tool.cbuild import skip_missing_compiler
-from pypy.translator.translator import Translator
+from pypy.translator.translator import TranslationContext
 from pypy.objspace.flow import FlowObjSpace
 
 from pypy import conftest 
@@ -113,7 +114,7 @@
 class TestTypedTestCase:
 
     def getcompiled(self, func):
-        t = Translator(func, simplifying=True) 
+        t = TranslationContext() 
         # builds starting-types from func_defs 
         argstypelist = []
         if func.func_defaults:
@@ -121,8 +122,23 @@
                 if isinstance(spec, tuple):
                     spec = spec[0] # use the first type only for the tests
                 argstypelist.append(spec)
-        t.annotate(argstypelist) 
-        return skip_missing_compiler(t.pyrexcompile)
+        t.buildannotator().build_types(func, argstypelist) 
+        name = func.func_name
+
+        blobs = []
+        for graph in t.graphs:
+            g = GenPyrex(graph)
+            g.by_the_way_the_function_was = graph.func   # XXX
+            g.setannotator(t.annotator)
+            blobs.append(g.emitcode())
+        code = g.globaldeclarations()  # any 'g' is fine here...
+        if code:
+            blobs.insert(0, code)
+        pyxcode = '\n\n#_________________\n\n'.join(blobs)
+
+        mod = skip_missing_compiler(
+            make_module_from_pyxstring, name, udir, pyxcode)
+        return getattr(mod, name)
 
     def test_set_attr(self):
         set_attr = self.getcompiled(snippet.set_attr)



More information about the Pypy-commit mailing list