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

afa at codespeak.net afa at codespeak.net
Wed May 5 23:46:18 CEST 2010


Author: afa
Date: Wed May  5 23:46:17 2010
New Revision: 74391

Modified:
   pypy/trunk/pypy/module/cpyext/pyobject.py
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
   pypy/trunk/pypy/module/cpyext/test/test_getargs.py
Log:
- Fix a test that returned a borrowed reference
- Fix cleanup of the kept alive references: remove them from borrowed_objects before DecRef
This allows a simplification in Py_DecRef

The "borrowing" system starts to make sense to me


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 23:46:17 2010
@@ -258,10 +258,6 @@
     if obj.c_ob_refcnt == 0:
         state = space.fromcache(State)
         ptr = rffi.cast(ADDR, obj)
-        try:
-            del state.borrowed_objects[ptr]
-        except KeyError:
-            pass
         if ptr not in state.py_objects_r2w:
             w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
             if space.is_w(w_type, space.w_str) or space.is_w(w_type, space.w_unicode):
@@ -277,11 +273,12 @@
                 _Py_Dealloc(space, obj)
             del state.py_objects_w2r[w_obj]
         if ptr in state.borrow_mapping: # move to lifeline __del__
+            # The object is a borrow container, clear borrowed references
             for containee in state.borrow_mapping[ptr]:
                 w_containee = state.py_objects_r2w.get(containee, None)
                 if w_containee is not None:
                     containee = state.py_objects_w2r[w_containee]
-                    Py_DecRef(space, w_containee)
+                    Py_DecRef(space, containee)
                     containee_ptr = rffi.cast(ADDR, containee)
                     try:
                         del state.borrowed_objects[containee_ptr]
@@ -342,10 +339,8 @@
         raise NullPointerException
     if container_ptr == -1:
         return
-    borrowees = state.borrow_mapping.get(container_ptr, None)
-    if borrowees is None:
-        state.borrow_mapping[container_ptr] = borrowees = {}
     obj_ptr = rffi.cast(ADDR, obj)
+    borrowees = state.borrow_mapping.setdefault(container_ptr, {})
     borrowees[obj_ptr] = None
 
 #___________________________________________________________

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	Wed May  5 23:46:17 2010
@@ -263,10 +263,9 @@
             Py_DecRef(self.space, w_obj)
         state.non_heaptypes[:] = []
         while state.borrowed_objects:
-            addr = state.borrowed_objects.keys()[0]
+            addr, _ = state.borrowed_objects.popitem()
             w_obj = state.py_objects_r2w[addr]
             Py_DecRef(self.space, w_obj)
-        state.borrowed_objects = {}
         state.borrow_mapping = {}
         if self.check_and_print_leaks():
             assert False, "Test leaks or loses object(s)."

Modified: pypy/trunk/pypy/module/cpyext/test/test_getargs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_getargs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_getargs.py	Wed May  5 23:46:17 2010
@@ -47,9 +47,11 @@
              if (!PyArg_ParseTuple(args, "O|O", &a, &b)) {
                  return NULL;
              }
-             if (!b) {
+             if (b)
+                 Py_INCREF(b);
+             else
                  b = PyInt_FromLong(42);
-             }
+             /* return an owned reference */
              return b;
              ''')])
         assert mod.oneargint(1) == 1



More information about the Pypy-commit mailing list