[pypy-svn] r73805 - pypy/branch/cpython-extension/pypy/module/cpyext

afa at codespeak.net afa at codespeak.net
Fri Apr 16 11:03:53 CEST 2010


Author: afa
Date: Fri Apr 16 11:03:51 2010
New Revision: 73805

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py
Log:
from_ref() is now completely generic. Progress!


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py	Fri Apr 16 11:03:51 2010
@@ -7,7 +7,6 @@
      Py_TPFLAGS_HEAPTYPE, PyTypeObjectPtr
 from pypy.module.cpyext.state import State
 from pypy.objspace.std.typeobject import W_TypeObject
-from pypy.objspace.std.objectobject import W_ObjectObject
 from pypy.rlib.objectmodel import specialize, we_are_translated
 from pypy.rpython.annlowlevel import llhelper
 
@@ -77,7 +76,9 @@
                 return tp_realize(space, ref)
         else:
             def realize(self, space, ref):
-                raise TypeError("cannot realize")
+                # For most types, a reference cannot exist without
+                # a real interpreter object
+                raise InvalidPointerException(str(ref))
     if typedef:
         CpyTypedescr.__name__ = "CpyTypedescr_%s" % (typedef.name,)
 
@@ -193,31 +194,24 @@
 
 
 def from_ref(space, ref, recurse=False):
-    from pypy.module.cpyext.typeobject import PyPyType_Ready
     assert lltype.typeOf(ref) == PyObject
     if not ref:
         return None
     state = space.fromcache(State)
     ptr = rffi.cast(ADDR, ref)
+
     try:
-        w_obj = state.py_objects_r2w[ptr]
+        return state.py_objects_r2w[ptr]
     except KeyError:
         if recurse:
             raise InvalidPointerException(str(ref))
-        ref_type = rffi.cast(PyObject, ref.c_ob_type)
-        if ref != ref_type:
-            w_type = from_ref(space, ref_type, True)
-            assert isinstance(w_type, W_TypeObject)
-            if space.is_w(w_type, space.w_str):
-                return get_typedescr(w_type.instancetypedef).realize(space, ref)
-            elif space.is_w(w_type, space.w_type):
-                PyPyType_Ready(space, rffi.cast(PyTypeObjectPtr, ref), None)
-                return from_ref(space, ref, True)
-            else:
-                raise InvalidPointerException(str(ref))
-        else:
-            raise InvalidPointerException("This should never happen")
-    return w_obj
+
+    # This reference is not yet a real interpreter object.
+    # Realize it.
+    ref_type = rffi.cast(PyObject, ref.c_ob_type)
+    w_type = from_ref(space, ref_type, True)
+    assert isinstance(w_type, W_TypeObject)
+    return get_typedescr(w_type.instancetypedef).realize(space, ref)
 
 
 # XXX Optimize these functions and put them into macro definitions

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py	Fri Apr 16 11:03:51 2010
@@ -281,6 +281,7 @@
     make_typedescr(space.w_type.instancetypedef,
                    basestruct=PyTypeObject,
                    attach=type_attach,
+                   realize=type_realize,
                    dealloc=type_dealloc)
 
 def c_type_descr__call__(space, w_type, __args__):
@@ -449,6 +450,10 @@
     finally:
         Py_DecRef(space, base_pyo)
 
+def type_realize(space, ref):
+    PyPyType_Ready(space, rffi.cast(PyTypeObjectPtr, ref), None)
+    return from_ref(space, ref, True)
+
 def PyPyType_Ready(space, pto, w_obj):
     try:
         pto.c_tp_dict = lltype.nullptr(PyObject.TO) # not supported



More information about the Pypy-commit mailing list