[pypy-svn] r5388 - in pypy/trunk/src: goal/translate_pypy pypy/interpreter pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Tue Jun 29 14:37:07 CEST 2004


Author: arigo
Date: Tue Jun 29 14:37:07 2004
New Revision: 5388

Modified:
   pypy/trunk/src/goal/translate_pypy/index.html
   pypy/trunk/src/goal/translate_pypy/translate_pypy.py
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/interpreter/typedef.py
   pypy/trunk/src/pypy/objspace/std/cpythonobject.py
   pypy/trunk/src/pypy/objspace/std/objspace.py
Log:
More on translate_pypy.py.  It no longer crashes -- though it doesn't 
translate PyPy yet: it only analyzes a small bit of it.

Got rid of the placeholder class UserSubclass which was giving trouble as 
expected.  Moved the equivalent functionality into a root class "W_Root", base 
of all wrapped objects in the StdObjSpace (both Wrappables and W_Objects).

Debugging helper function: about(x) in translate_pypy.py.



Modified: pypy/trunk/src/goal/translate_pypy/index.html
==============================================================================
--- pypy/trunk/src/goal/translate_pypy/index.html	(original)
+++ pypy/trunk/src/goal/translate_pypy/index.html	Tue Jun 29 14:37:07 2004
@@ -7,9 +7,10 @@
     print "<tr><td>&nbsp;"
     print "%s:%d" % (htmlquote(func.func_globals.get('__name__', '?')),
                      func.func_code.co_firstlineno)
-    print "&nbsp;</td><td>&nbsp;<code><a href='func?i=%d'>" % i
-    print htmlquote(func.__name__)
-    print "</a></code>&nbsp;</td></tr>"
+    print "&nbsp;</td><td>&nbsp;"
+    print "<code><a href='func?i=%d'>%s</a></code>" % (i,
+        htmlquote(func.__name__))
+    print "&nbsp;</td></tr>"
 )s
 </table>
 </body>

Modified: pypy/trunk/src/goal/translate_pypy/translate_pypy.py
==============================================================================
--- pypy/trunk/src/goal/translate_pypy/translate_pypy.py	(original)
+++ pypy/trunk/src/goal/translate_pypy/translate_pypy.py	Tue Jun 29 14:37:07 2004
@@ -21,19 +21,45 @@
 # __________  Main  __________
 
 if __name__ == '__main__':
+
+    def about(x):
+        """ interactive debugging helper """
+        from pypy.objspace.flow.model import Block, flatten
+        if isinstance(x, Block):
+            for func, graph in t.flowgraphs.items():
+                if x in flatten(graph):
+                    print x
+                    print 'is a block in the graph of'
+                    print func
+                    print 'at %s:%d' % (func.func_globals.get('__name__', '?'),
+                                        func.func_code.co_firstlineno)
+                    break
+            else:
+                print x
+                print 'is a block at some unknown location'
+            print 'containing the following operations:'
+            for op in x.operations:
+                print op
+            print '--end--'
+            return
+        print "don't know about", x
+
+    import graphserver
+    def run_server():
+        graphserver.Server(t).serve()
+
     t = Translator(entry_point, verbose=True, simplifying=True)
     try:
         a = t.annotate([])
-        #a.simplify()
+        a.simplify()
     except:
         import sys, traceback, thread
         exc, val, tb = sys.exc_info()
         print >> sys.stderr
         traceback.print_exception(exc, val, tb)
         print >> sys.stderr
-        import graphserver
-        def bkgnd_server():
-            graphserver.Server(t).serve()
-        thread.start_new_thread(bkgnd_server, ())
+        thread.start_new_thread(run_server, ())
         import pdb
         pdb.post_mortem(tb)
+    else:
+        run_server()

Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Tue Jun 29 14:37:07 2004
@@ -3,14 +3,21 @@
 from pypy.interpreter.miscutils import getthreadlocals
 from pypy.interpreter.argument import Arguments
 
-__all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'BaseWrappable']
+__all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'BaseWrappable',
+           'W_Root']
 
 
-class BaseWrappable:
-    """A subclass of BaseWrappable is an internal, interpreter-level class
-    that can nevertheless be exposed at application-level by space.wrap()."""
+class W_Root:
+    """This is the abstract root class of all wrapped objects that live
+    in a 'normal' object space like StdObjSpace."""
     def getdict(self):
         return None
+    def getclass(self, space):
+        return space.gettypeobject(self.typedef)
+
+class BaseWrappable(W_Root):
+    """A subclass of BaseWrappable is an internal, interpreter-level class
+    that can nevertheless be exposed at application-level by space.wrap()."""
     def __spacebind__(self, space):
         return self
 

Modified: pypy/trunk/src/pypy/interpreter/typedef.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/typedef.py	(original)
+++ pypy/trunk/src/pypy/interpreter/typedef.py	Tue Jun 29 14:37:07 2004
@@ -25,7 +25,7 @@
 
         class User_InsertNameHere(object):
             
-            def getclass(self):
+            def getclass(self, space):
                 return self.w__class__
             
             def setclass(self, w_subtype):
@@ -54,13 +54,10 @@
                     self.w__dict__ = space.newdict([])
 
         body = dict(User_InsertNameHere.__dict__.items())
-        subcls = type(name, (cls, UserSubclass), body)
+        subcls = type(name, (cls,), body)
         unique_interplevel_subclass_cache[cls] = subcls
         return subcls
 
-class UserSubclass(object):
-    pass   # XXX this should probably not exist
-
 def instantiate(cls):
     "Create an empty instance of 'cls'."
     if isinstance(cls, type):

Modified: pypy/trunk/src/pypy/objspace/std/cpythonobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/cpythonobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/cpythonobject.py	Tue Jun 29 14:37:07 2004
@@ -41,6 +41,27 @@
         """ representation for debugging purposes """
         return "cpyobj(%r)" % (w_self.cpyobj,)
 
+    def getclass(w_self, space):
+        return space.wrap(w_self.cpyobj.__class__)
+
+    def lookup(w_self, name):
+        # hack for wrapped CPython types
+        cls = w_self.cpyobj
+        if isinstance(cls, type):
+            for base in cls.__mro__:
+                if name in base.__dict__:
+                    return w_self.space.wrap(base.__dict__[name])
+            return None
+        elif isinstance(cls, types.ClassType):
+            while True:
+                if name in cls.__dict__:
+                    return w_self.space.wrap(base.__dict__[name])
+                if not cls.__bases__:
+                    return None
+                cls, = cls.__bases__  # no old-style multiple inheritance
+        else:
+            raise TypeError, '%r is not a class' % (cls,)
+
 registerimplementation(W_CPythonObject)
 
 

Modified: pypy/trunk/src/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/objspace.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/objspace.py	Tue Jun 29 14:37:07 2004
@@ -1,14 +1,14 @@
 from pypy.objspace.std.register_all import register_all
 from pypy.interpreter.baseobjspace import *
 from pypy.interpreter.typedef import get_unique_interplevel_subclass
-from pypy.interpreter.typedef import instantiate, UserSubclass
+from pypy.interpreter.typedef import instantiate
 from pypy.objspace.std.multimethod import *
 from pypy.objspace.descroperation import DescrOperation
 from pypy.objspace.std import stdtypedef
 import types
 
 
-class W_Object(object):
+class W_Object(W_Root, object):
     "Parent base class for wrapped objects."
     typedef = None
 
@@ -25,9 +25,6 @@
             s += ' instance of %s' % self.w__class__
         return '<%s>' % s
 
-    def getdict(self):
-        return None
-
 # delegation priorities
 PRIORITY_SAME_TYPE    = 2  # converting between several impls of the same type
 PRIORITY_PARENT_TYPE  = 1  # converting to a base type (e.g. bool -> int)
@@ -290,26 +287,11 @@
         return W_SeqIterObject(self, w_obj)
 
     def type(self, w_obj):
-        if isinstance(w_obj, UserSubclass):
-            return w_obj.getclass()
-        elif isinstance(w_obj, W_CPythonObject):
-                #raise TypeError, str(w_obj.cpyobj)
-                return self.wrap(type(w_obj.cpyobj))
-        else:
-            assert w_obj.typedef, w_obj
-            return self.gettypeobject(w_obj.typedef)
+        return w_obj.getclass(self)
 
     def lookup(self, w_obj, name):
-        if not isinstance(w_obj, W_CPythonObject):
-            # usual case
-            w_type = self.type(w_obj)
-            return w_type.lookup(name)
-        else:
-            # hack
-            for cls in type(w_obj.cpyobj).__mro__:
-                if name in cls.__dict__:
-                    return self.wrap(cls.__dict__[name])
-            return None
+        w_type = w_obj.getclass(self)
+        return w_type.lookup(name)
 
     def allocate_instance(self, cls, w_subtype):
         """Allocate the memory needed for an instance of an internal or



More information about the Pypy-commit mailing list