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

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


Author: afa
Date: Fri May 28 15:44:26 2010
New Revision: 74849

Modified:
   pypy/trunk/pypy/module/cpyext/object.py
   pypy/trunk/pypy/module/cpyext/pyobject.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
Log:
Move the lifeline_dict instance into the RefcountState class.


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:44:26 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, lifeline_dict, PyOLifeline)
+    track_reference, get_typedescr, RefcountState)
 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,8 @@
     if w_type.is_cpytype():
         w_obj = space.allocate_instance(W_ObjectObject, w_type)
         track_reference(space, py_obj, w_obj)
-        lifeline_dict.set(w_obj, PyOLifeline(space, py_obj))
+        state = space.fromcache(RefcountState)
+        state.set_lifeline(w_obj, py_obj)
     else:
         assert False, "Please add more cases in PyObject_Init"
     return py_obj

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:44:26 2010
@@ -138,6 +138,8 @@
         self.py_objects_w2r = {} # { w_obj -> raw PyObject }
         self.py_objects_r2w = {} # { addr of raw PyObject -> w_obj }
 
+        self.lifeline_dict = RWeakKeyDictionary(W_Root, PyOLifeline)
+
         self.borrow_mapping = {None: {}}
         # { w_container -> { w_containee -> None } }
         # the None entry manages references borrowed during a call to
@@ -165,6 +167,19 @@
         for w_obj, obj in self.py_objects_w2r.items():
             print "%r: %i" % (w_obj, obj.c_ob_refcnt)
 
+    def get_from_lifeline(self, w_obj):
+        lifeline = self.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
+            return py_obj
+        else:
+            lltype.nullptr(PyObject.TO)
+
+    def set_lifeline(self, w_obj, py_obj):
+        self.lifeline_dict.set(w_obj,
+                               PyOLifeline(self.space, py_obj))
+
     def make_borrowed(self, w_container, w_borrowed):
         """
         Create a borrowed reference, which will live as long as the container
@@ -250,17 +265,16 @@
     """
     w_type = space.type(w_obj)
     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
+        state = space.fromcache(RefcountState)
+        py_obj = state.get_from_lifeline(w_obj)
+        if py_obj:
             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))
+        state.set_lifeline(w_obj, py_obj)
     typedescr.attach(space, py_obj, w_obj)
     return py_obj
 
@@ -396,8 +410,6 @@
             self.pyo = lltype.nullptr(PyObject.TO)
         # XXX handle borrowed objects here
 
-lifeline_dict = RWeakKeyDictionary(W_Root, PyOLifeline)
-
 #___________________________________________________________
 # Support for borrowed references
 

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:44:26 2010
@@ -12,7 +12,7 @@
 from pypy.tool.udir import udir
 from pypy.module.cpyext import api
 from pypy.module.cpyext.state import State
-from pypy.module.cpyext.pyobject import RefcountState, lifeline_dict
+from pypy.module.cpyext.pyobject import RefcountState
 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 lifeline_dict._dict.items():
+        for w_obj, obj in state.lifeline_dict._dict.items():
             if w_obj not in state.py_objects_w2r:
-                lifeline_dict.set(w_obj, None)
+                state.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 = lifeline_dict.get(w_obj)
+                lifeline = state.lifeline_dict.get(w_obj)
                 if lifeline is not None:
                     refcnt = lifeline.pyo.c_ob_refcnt
                     if refcnt > 0:



More information about the Pypy-commit mailing list