[pypy-svn] r28237 - in pypy/dist/pypy: objspace/cpy objspace/cpy/test translator/c

arigo at codespeak.net arigo at codespeak.net
Sun Jun 4 12:14:10 CEST 2006


Author: arigo
Date: Sun Jun  4 12:14:08 2006
New Revision: 28237

Added:
   pypy/dist/pypy/objspace/cpy/function.py
      - copied, changed from r28087, pypy/dist/pypy/objspace/cpy/wrappable.py
   pypy/dist/pypy/objspace/cpy/test/test_function.py
      - copied unchanged from r28087, pypy/dist/pypy/objspace/cpy/test/test_wrappable.py
   pypy/dist/pypy/objspace/cpy/test/test_typedef.py   (contents, props changed)
   pypy/dist/pypy/objspace/cpy/typedef.py   (contents, props changed)
Removed:
   pypy/dist/pypy/objspace/cpy/test/test_wrappable.py
   pypy/dist/pypy/objspace/cpy/wrappable.py
Modified:
   pypy/dist/pypy/objspace/cpy/ann_policy.py
   pypy/dist/pypy/objspace/cpy/capi.py
   pypy/dist/pypy/objspace/cpy/objspace.py
   pypy/dist/pypy/translator/c/pyobj.py
Log:
Start the work on TypeDef support in the CPyObjSpace.  From a TypeDef,
it generates a real CPython type object that is then translated by
pyobj.py in genc.  Not the cleanest solution, but a quick hack building
on top of the existing pyobj, itself a hack.



Modified: pypy/dist/pypy/objspace/cpy/ann_policy.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/ann_policy.py	(original)
+++ pypy/dist/pypy/objspace/cpy/ann_policy.py	Sun Jun  4 12:14:08 2006
@@ -1,5 +1,4 @@
 from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
-from pypy.annotation.pairtype import pair
 from pypy.annotation import model as annmodel
 from pypy.interpreter.error import OperationError
 from pypy.objspace.cpy.ctypes_base import W_Object, rctypes_pyerrchecker
@@ -23,9 +22,8 @@
             pending.update(space.wrap_cache)
             if len(pending) == nb_done:
                 break
-            for obj, w_obj in pending.items():
-                pair(space, obj).follow_annotations(annotator.bookkeeper,
-                                                    w_obj)
+            for w_obj, obj, follow in pending.values():
+                follow(annotator.bookkeeper, w_obj)
             # restart this loop: for all we know follow_annotations()
             # could have found new objects
 

Modified: pypy/dist/pypy/objspace/cpy/capi.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/capi.py	(original)
+++ pypy/dist/pypy/objspace/cpy/capi.py	Sun Jun  4 12:14:08 2006
@@ -43,6 +43,17 @@
 ##                                          ('ml_doc', c_char_p)])
 ##    METH_VARARGS = ctypes_platform.ConstantInteger('METH_VARARGS')
 
+##    # NB. all integers fields can be specified as c_int,
+##    #     which is replaced by the more precise type automatically.
+##    PyObject_HEAD = [('ob_refcnt', c_int), ('ob_type', 
+##    PyTypeObject = ctypes_platform.Struct('PyTypeObject', [
+##        ('tp_name', c_char_p),
+##        ('tp_basicsize', c_int),
+##        ('tp_flags', c_int),
+##        ('tp_doc', c_char_p),
+##        ])
+##    Py_TPFLAGS_DEFAULT = ctypes_platform.ConstantInteger('Py_TPFLAGS_DEFAULT')
+
 globals().update(ctypes_platform.configure(CConfig))
 del CConfig
 

Modified: pypy/dist/pypy/objspace/cpy/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/objspace.py	(original)
+++ pypy/dist/pypy/objspace/cpy/objspace.py	Sun Jun  4 12:14:08 2006
@@ -1,8 +1,8 @@
 from pypy.objspace.cpy.capi import *
 from pypy.objspace.cpy.refcount import Py_Incref
-from pypy.annotation.pairtype import pair
 from pypy.interpreter import baseobjspace
 from pypy.interpreter.error import OperationError
+from pypy.interpreter.function import Function
 
 
 class CPyObjSpace(baseobjspace.ObjSpace):
@@ -22,7 +22,6 @@
         self.w_TypeError     = W_Object(TypeError)
         self.w_KeyError      = W_Object(KeyError)
         self.wrap_cache = {}
-        self.rev_wrap_cache = {}
 
     def _freeze_(self):
         return True
@@ -33,15 +32,17 @@
     def wrap(self, x):
         if isinstance(x, baseobjspace.Wrappable):
             x = x.__spacebind__(self)
-            if isinstance(x, baseobjspace.Wrappable):
-                try:
-                    return self.wrap_cache[x]
-                except KeyError:
-                    import pypy.objspace.cpy.wrappable
-                    result = pair(self, x).wrap()
-                    self.wrap_cache[x] = result
-                    self.rev_wrap_cache[id(result)] = result, x
-                    return result
+            # special cases
+            if isinstance(x, Function):
+                from pypy.objspace.cpy.function import FunctionCache
+                return self.fromcache(FunctionCache).getorbuild(x)
+            # normal case
+            from pypy.objspace.cpy.typedef import TypeDefCache
+            w_x = x.__cpy_wrapper__
+            if w_x is None:
+                w_type = self.fromcache(TypeDefCache).getorbuild(x.typedef)
+                w_x = x.__cpy_wrapper__ = self.call_function(w_type)
+            return w_x
         if x is None:
             return self.w_None
         if isinstance(x, int):
@@ -57,9 +58,11 @@
 
     def interpclass_w(self, w_obj):
         try:
-            return self.rev_wrap_cache[id(w_obj)][1]
+            w_obj, obj, follow = self.wrap_cache[id(w_obj)]
         except KeyError:
             return None
+        else:
+            return obj
 
     # __________ operations with a direct CPython equivalent __________
 

Added: pypy/dist/pypy/objspace/cpy/test/test_typedef.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cpy/test/test_typedef.py	Sun Jun  4 12:14:08 2006
@@ -0,0 +1,25 @@
+from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app, ObjSpace, W_Root
+from pypy.interpreter.function import BuiltinFunction
+from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
+from pypy.objspace.cpy.objspace import CPyObjSpace
+from pypy.translator.c.test.test_genc import compile
+
+
+class W_MyType(Wrappable):
+    def __init__(self, space):
+        self.space = space
+
+
+def test_simple():
+    W_MyType.typedef = TypeDef("MyType")
+    space = CPyObjSpace()
+
+    def make_mytype():
+        return space.wrap(W_MyType(space))
+    fn = compile(make_mytype, [],
+                 annotatorpolicy = CPyAnnotatorPolicy(space))
+
+    res = fn()
+    assert type(res).__name__ == 'MyType'

Added: pypy/dist/pypy/objspace/cpy/typedef.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cpy/typedef.py	Sun Jun  4 12:14:08 2006
@@ -0,0 +1,23 @@
+"""
+Generic support to turn interpreter objects (subclasses of Wrappable)
+into CPython objects (subclasses of W_Object) based on their typedef.
+"""
+
+from pypy.objspace.cpy.capi import *
+from pypy.interpreter.baseobjspace import Wrappable, SpaceCache
+
+class TypeDefCache(SpaceCache):
+    def build(cache, typedef):
+        space = cache.space
+        newtype = type(typedef.name, (), {})
+        w_result = W_Object(newtype)
+        space.wrap_cache[id(w_result)] = w_result, typedef, follow_annotations
+        return w_result
+
+
+def follow_annotations(bookkeeper, w_type):
+    pass
+
+
+# hack!
+Wrappable.__cpy_wrapper__ = None

Modified: pypy/dist/pypy/translator/c/pyobj.py
==============================================================================
--- pypy/dist/pypy/translator/c/pyobj.py	(original)
+++ pypy/dist/pypy/translator/c/pyobj.py	Sun Jun  4 12:14:08 2006
@@ -400,8 +400,9 @@
         baseargs = ", ".join(basenames)
         if baseargs:
             baseargs = '(%s)' % baseargs
-        self.initcode.append('class %s%s:' % (name, baseargs))
+        self.initcode.append('class %s%s:' % (cls.__name__, baseargs))
         self.initcode.append('  __metaclass__ = %s' % metaclass)
+        self.initcode.append('%s = %s' % (name, cls.__name__))
         self.later(initclassobj())
         return name
 



More information about the Pypy-commit mailing list