[pypy-svn] r74848 - in pypy/trunk/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Fri May 28 15:33:22 CEST 2010


Author: afa
Date: Fri May 28 15:33:20 2010
New Revision: 74848

Modified:
   pypy/trunk/pypy/module/cpyext/object.py
   pypy/trunk/pypy/module/cpyext/pyobject.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
   pypy/trunk/pypy/module/cpyext/typeobject.py
Log:
Now there is one and only one "make_ref" function.
Move lifeline_dict to pyobject, 


Modified: pypy/trunk/pypy/module/cpyext/object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/object.py	Fri May 28 15:33:20 2010
@@ -5,7 +5,7 @@
     Py_GE, CONST_STRING, FILEP, fwrite)
 from pypy.module.cpyext.pyobject import (
     PyObject, PyObjectP, create_ref, from_ref, Py_IncRef, Py_DecRef,
-    track_reference, get_typedescr)
+    track_reference, get_typedescr, lifeline_dict, PyOLifeline)
 from pypy.module.cpyext.typeobject import PyTypeObjectPtr
 from pypy.module.cpyext.pyerrors import PyErr_NoMemory, PyErr_BadInternalCall
 from pypy.objspace.std.objectobject import W_ObjectObject
@@ -199,7 +199,6 @@
     if w_type.is_cpytype():
         w_obj = space.allocate_instance(W_ObjectObject, w_type)
         track_reference(space, py_obj, w_obj)
-        from pypy.module.cpyext.typeobject import lifeline_dict, PyOLifeline
         lifeline_dict.set(w_obj, PyOLifeline(space, py_obj))
     else:
         assert False, "Please add more cases in PyObject_Init"

Modified: pypy/trunk/pypy/module/cpyext/pyobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/pyobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/pyobject.py	Fri May 28 15:33:20 2010
@@ -8,6 +8,7 @@
 from pypy.module.cpyext.state import State
 from pypy.objspace.std.typeobject import W_TypeObject
 from pypy.rlib.objectmodel import specialize, we_are_translated
+from pypy.rlib.rweakref import RWeakKeyDictionary
 from pypy.rpython.annlowlevel import llhelper
 
 #________________________________________________________
@@ -20,8 +21,6 @@
         raise NotImplementedError
     def allocate(self, space, w_type, itemcount=0):
         raise NotImplementedError
-    def make_ref(self, space, w_type, w_obj, itemcount=0):
-        raise NotImplementedError
     def attach(self, space, pyobj, w_obj):
         raise NotImplementedError
     def realize(self, space, ref):
@@ -40,7 +39,6 @@
     """
 
     tp_basestruct = kw.pop('basestruct', PyObject.TO)
-    tp_make_ref   = kw.pop('make_ref', None)
     tp_attach     = kw.pop('attach', None)
     tp_realize    = kw.pop('realize', None)
     tp_dealloc    = kw.pop('dealloc', None)
@@ -87,18 +85,6 @@
             pyobj.c_ob_type = pytype
             return pyobj
 
-        # Specialized by meta-type
-        if tp_make_ref:
-            def make_ref(self, space, w_type, w_obj, itemcount=0):
-                return tp_make_ref(space, w_type, w_obj, itemcount=itemcount)
-        else:
-            def make_ref(self, space, w_type, w_obj, itemcount=0):
-                typedescr = get_typedescr(w_obj.typedef)
-                w_type = space.type(w_obj)
-                py_obj = typedescr.allocate(space, w_type, itemcount=itemcount)
-                typedescr.attach(space, py_obj, w_obj)
-                return py_obj
-
         if tp_attach:
             def attach(self, space, pyobj, w_obj):
                 tp_attach(space, pyobj, w_obj)
@@ -257,14 +243,26 @@
         print >>sys.stderr, arg,
     print >>sys.stderr
 
-def create_ref(space, w_obj, items=0):
+def create_ref(space, w_obj, itemcount=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)
+    if w_type.is_cpytype():
+        lifeline = lifeline_dict.get(w_obj)
+        if lifeline is not None: # make old PyObject ready for use in C code
+            py_obj = lifeline.pyo
+            assert py_obj.c_ob_refcnt == 0
+            Py_IncRef(space, py_obj)
+            return py_obj
+
+    typedescr = get_typedescr(w_obj.typedef)
+    py_obj = typedescr.allocate(space, w_type, itemcount=itemcount)
+    if w_type.is_cpytype():
+        lifeline_dict.set(w_obj, PyOLifeline(space, py_obj))
+    typedescr.attach(space, py_obj, w_obj)
+    return py_obj
 
 def track_reference(space, py_obj, w_obj, replace=False):
     """
@@ -381,6 +379,26 @@
     generic_cpy_call_dont_decref(space, pto.c_tp_dealloc, obj)
 
 #___________________________________________________________
+# Support for "lifelines"
+#
+# Object structure must stay alive even when not referenced
+# by any C code.
+
+class PyOLifeline(object):
+    def __init__(self, space, pyo):
+        self.pyo = pyo
+        self.space = space
+
+    def __del__(self):
+        if self.pyo:
+            assert self.pyo.c_ob_refcnt == 0
+            _Py_Dealloc(self.space, self.pyo)
+            self.pyo = lltype.nullptr(PyObject.TO)
+        # XXX handle borrowed objects here
+
+lifeline_dict = RWeakKeyDictionary(W_Root, PyOLifeline)
+
+#___________________________________________________________
 # Support for borrowed references
 
 def make_borrowed_ref(space, w_container, w_borrowed):

Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	Fri May 28 15:33:20 2010
@@ -10,9 +10,9 @@
 from pypy.translator import platform
 from pypy.translator.gensupp import uniquemodulename
 from pypy.tool.udir import udir
-from pypy.module.cpyext import api, typeobject
+from pypy.module.cpyext import api
 from pypy.module.cpyext.state import State
-from pypy.module.cpyext.pyobject import RefcountState
+from pypy.module.cpyext.pyobject import RefcountState, lifeline_dict
 from pypy.module.cpyext.pyobject import Py_DecRef, InvalidPointerException
 from pypy.translator.goal import autopath
 from pypy.lib.identity_dict import identity_dict
@@ -92,9 +92,9 @@
         lost_objects_w.update((key, None) for key in self.frozen_refcounts.keys())
 
         # Clear all lifelines, objects won't resurrect
-        for w_obj, obj in typeobject.lifeline_dict._dict.items():
+        for w_obj, obj in lifeline_dict._dict.items():
             if w_obj not in state.py_objects_w2r:
-                typeobject.lifeline_dict.set(w_obj, None)
+                lifeline_dict.set(w_obj, None)
             del obj
         gc.collect()
 
@@ -107,7 +107,7 @@
             if delta != 0:
                 leaking = True
                 print >>sys.stderr, "Leaking %r: %i references" % (w_obj, delta)
-                lifeline = typeobject.lifeline_dict.get(w_obj)
+                lifeline = lifeline_dict.get(w_obj)
                 if lifeline is not None:
                     refcnt = lifeline.pyo.c_ob_refcnt
                     if refcnt > 0:

Modified: pypy/trunk/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/typeobject.py	Fri May 28 15:33:20 2010
@@ -3,8 +3,6 @@
 
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rpython.annlowlevel import llhelper
-from pypy.rlib.rweakref import RWeakKeyDictionary
-from pypy.interpreter.gateway import W_Root
 from pypy.interpreter.baseobjspace import DescrMismatch
 from pypy.objspace.std.typeobject import W_TypeObject, _CPYTYPE
 from pypy.interpreter.typedef import GetSetProperty
@@ -212,20 +210,6 @@
             pto.c_tp_new = base_pto.c_tp_new
     Py_DecRef(space, base_object_pyo)
 
-class PyOLifeline(object):
-    def __init__(self, space, pyo):
-        self.pyo = pyo
-        self.space = space
-
-    def __del__(self):
-        if self.pyo:
-            assert self.pyo.c_ob_refcnt == 0
-            _Py_Dealloc(self.space, self.pyo)
-            self.pyo = lltype.nullptr(PyObject.TO)
-        # XXX handle borrowed objects here
-
-lifeline_dict = RWeakKeyDictionary(W_Root, PyOLifeline)
-
 def check_descr(space, w_self, w_type):
     if not space.is_true(space.isinstance(w_self, w_type)):
         raise DescrMismatch()
@@ -289,7 +273,6 @@
     make_typedescr(space.w_type.instancetypedef,
                    basestruct=PyTypeObject,
                    attach=type_attach,
-                   make_ref=type_make_ref,
                    realize=type_realize,
                    dealloc=type_dealloc)
 
@@ -350,22 +333,6 @@
     # hopefully this does not clash with the memory model assumed in
     # extension modules
 
-def type_make_ref(space, w_type, w_obj, itemcount=0):
-    if w_type.is_cpytype():
-        lifeline = lifeline_dict.get(w_obj)
-        if lifeline is not None: # make old PyObject ready for use in C code
-            py_obj = lifeline.pyo
-            assert py_obj.c_ob_refcnt == 0
-            Py_IncRef(space, py_obj)
-            return py_obj
-
-    typedescr = get_typedescr(w_obj.typedef)
-    py_obj = typedescr.allocate(space, w_type, itemcount=itemcount)
-    if w_type.is_cpytype():
-        lifeline_dict.set(w_obj, PyOLifeline(space, py_obj))
-    typedescr.attach(space, py_obj, w_obj)
-    return py_obj
-
 @cpython_api([PyObject, rffi.INTP], lltype.Signed, external=False,
              error=CANNOT_FAIL)
 def str_segcount(space, w_obj, ref):



More information about the Pypy-commit mailing list