[pypy-svn] r74380 - pypy/trunk/pypy/module/cpyext

afa at codespeak.net afa at codespeak.net
Wed May 5 16:56:29 CEST 2010


Author: afa
Date: Wed May  5 16:56:27 2010
New Revision: 74380

Modified:
   pypy/trunk/pypy/module/cpyext/pyobject.py
   pypy/trunk/pypy/module/cpyext/typeobject.py
Log:
More docstrings, some light refactoring.


Modified: pypy/trunk/pypy/module/cpyext/pyobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/pyobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/pyobject.py	Wed May  5 16:56:27 2010
@@ -164,22 +164,37 @@
     print >>sys.stderr
 
 def create_ref(space, w_obj, items=0):
+    """
+    Allocates a PyObject, and fills its fields with info from the given
+    intepreter object.
+    """
     w_type = space.type(w_obj)
     metatypedescr = get_typedescr(w_type.typedef)
     return metatypedescr.make_ref(space, w_type, w_obj, itemcount=items)
 
-def track_reference(space, py_obj, w_obj, borrowed=False):
+def track_reference(space, py_obj, w_obj, borrowed=False, replace=False):
+    """
+    Ties together a PyObject and an interpreter object.
+    """
     # XXX looks like a PyObject_GC_TRACK
     ptr = rffi.cast(ADDR, py_obj)
+    state = space.fromcache(State)
     if DEBUG_REFCOUNT:
         debug_refcount("MAKREF", py_obj, w_obj)
-    state = space.fromcache(State)
+        if not replace:
+            assert w_obj not in state.py_objects_w2r
+        assert ptr not in state.py_objects_r2w
+        assert ptr not in state.borrowed_objects
     state.py_objects_w2r[w_obj] = py_obj
     state.py_objects_r2w[ptr] = w_obj
-    if borrowed and ptr not in state.borrowed_objects:
+    if borrowed:
         state.borrowed_objects[ptr] = None
 
 def make_ref(space, w_obj, borrowed=False, steal=False, items=0):
+    """
+    Returns a reference to an intepreter object.
+    if borrowed=False, the caller owns the reference.
+    """
     if w_obj is None:
         return lltype.nullptr(PyObject.TO)
     assert isinstance(w_obj, W_Root)
@@ -204,6 +219,10 @@
 
 
 def from_ref(space, ref, recurse=False):
+    """
+    Finds the interpreter object corresponding to the given reference.  If the
+    object is not yet realized (see stringobject.py), creates it.
+    """
     assert lltype.typeOf(ref) == PyObject
     if not ref:
         return None

Modified: pypy/trunk/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/typeobject.py	Wed May  5 16:56:27 2010
@@ -6,18 +6,18 @@
 from pypy.rlib.rweakref import RWeakKeyDictionary
 from pypy.interpreter.gateway import ObjSpace, W_Root, Arguments
 from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.baseobjspace import Wrappable, DescrMismatch
+from pypy.interpreter.baseobjspace import DescrMismatch
 from pypy.objspace.std.typeobject import W_TypeObject, _CPYTYPE, call__Type
 from pypy.objspace.std.typetype import _precheck_for_new
-from pypy.objspace.std.objectobject import W_ObjectObject
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.module.cpyext.api import cpython_api, cpython_struct, bootstrap_function, \
-    PyVarObjectFields, Py_ssize_t, Py_TPFLAGS_READYING, generic_cpy_call, \
-    Py_TPFLAGS_READY, Py_TPFLAGS_HEAPTYPE, ADDR, \
-    Py_TPFLAGS_HAVE_CLASS, METH_VARARGS, METH_KEYWORDS, \
-    CANNOT_FAIL, PyBufferProcs, build_type_checkers
-from pypy.module.cpyext.pyobject import PyObject, make_ref, create_ref, from_ref
-from pypy.module.cpyext.pyobject import get_typedescr, make_typedescr, track_reference
+from pypy.module.cpyext.api import (
+    cpython_api, cpython_struct, bootstrap_function, Py_ssize_t,
+    generic_cpy_call, Py_TPFLAGS_READY, Py_TPFLAGS_READYING,
+    Py_TPFLAGS_HEAPTYPE, METH_VARARGS, METH_KEYWORDS, CANNOT_FAIL,
+    PyBufferProcs, build_type_checkers)
+from pypy.module.cpyext.pyobject import (
+    PyObject, make_ref, create_ref, from_ref, get_typedescr, make_typedescr,
+    track_reference)
 from pypy.interpreter.module import Module
 from pypy.interpreter.function import FunctionWithFixedCode, StaticMethod
 from pypy.module.cpyext import structmemberdefs
@@ -367,9 +367,9 @@
     pto_tuple.c_tp_bases.c_ob_type = pto_tuple
 
     # Restore the mapping
-    track_reference(space, py_type, space.w_type)
-    track_reference(space, py_object, space.w_object)
-    track_reference(space, py_tuple, space.w_tuple)
+    track_reference(space, py_type, space.w_type, replace=True)
+    track_reference(space, py_object, space.w_object, replace=True)
+    track_reference(space, py_tuple, space.w_tuple, replace=True)
 
 
 @cpython_api([PyObject], lltype.Void, external=False)
@@ -447,7 +447,9 @@
 
 
 def type_attach(space, py_obj, w_type):
-    """ Allocates a PyTypeObject from a w_type which must be a PyPy type. """
+    """
+    Fills a newly allocated PyTypeObject from an existing type.
+    """
     from pypy.module.cpyext.object import PyObject_Del
 
     assert isinstance(w_type, W_TypeObject)
@@ -547,15 +549,17 @@
     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 type_realize(space, py_obj):
+    """
+    Creates an interpreter type from a PyTypeObject structure.
+    """
+    return PyPyType_Ready(space, rffi.cast(PyTypeObjectPtr, py_obj), None)
 
 def PyPyType_Ready(space, pto, w_obj):
     try:
         pto.c_tp_dict = lltype.nullptr(PyObject.TO) # not supported
         if pto.c_tp_flags & Py_TPFLAGS_READY:
-            return 0
+            return w_obj
         assert pto.c_tp_flags & Py_TPFLAGS_READYING == 0
         pto.c_tp_flags |= Py_TPFLAGS_READYING
         base = pto.c_tp_base
@@ -588,22 +592,21 @@
         # tp_mro, tp_subclasses
     finally:
         pto.c_tp_flags &= ~Py_TPFLAGS_READYING
-    pto.c_tp_flags = (pto.c_tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY
-    return 0
+    pto.c_tp_flags |= Py_TPFLAGS_READY
+    return w_obj
 
 def PyPyType_Register(space, pto):
+    w_obj = space.allocate_instance(
+        W_PyCTypeObject,
+        space.gettypeobject(W_PyCTypeObject.typedef))
     state = space.fromcache(State)
-    ptr = rffi.cast(ADDR, pto)
-    if ptr not in state.py_objects_r2w:
-        w_obj = space.allocate_instance(W_PyCTypeObject,
-                space.gettypeobject(W_PyCTypeObject.typedef))
-        state.non_heaptypes.append(w_obj)
-        pyo = rffi.cast(PyObject, pto)
-        state.py_objects_r2w[ptr] = w_obj
-        state.py_objects_w2r[w_obj] = pyo
-        w_obj.__init__(space, pto)
-        w_obj.ready()
-        return w_obj
+    state.non_heaptypes.append(w_obj)
+    py_obj = rffi.cast(PyObject, pto)
+
+    track_reference(space, py_obj, w_obj)
+    w_obj.__init__(space, pto)
+    w_obj.ready()
+    return w_obj
 
 @cpython_api([PyTypeObjectPtr, PyTypeObjectPtr], rffi.INT_real, error=CANNOT_FAIL)
 def PyType_IsSubtype(space, a, b):



More information about the Pypy-commit mailing list